Unit testing for two difference implementations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python101
    New Member
    • Sep 2007
    • 90

    Unit testing for two difference implementations

    [code=python]
    class Calculation:
    def __init__(self, A=0, B=1):
    factor = gcd( abs(A), abs(B) )
    if B < 0:
    factor = -factor
    self._numA = A // factor
    self._denoB = B // factor


    def __str__(self):
    return str(self._numA) + '/' + str(self._denoB )

    # now I have 2 different implementations for __int__"
    #either 1:
    def __int__(self):
    return int(float(self) )

    # or 2 is used:
    def __int__(self):
    return self._numA/self._denoB

    [/code]

    My question is how can I a write a unit test (__name__='__ma in__') to test for which these two implementations produce different result?
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    How about commenting out first one, then the other, since you will only have one (the second one defined), anyway.

    Comment

    • python101
      New Member
      • Sep 2007
      • 90

      #3
      I tried to test individual implementation but both gave the same result. Can anyone give a case that two implementations will have two different results?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by python101
        [code=python]
        class Calculation:
        def __init__(self, A=0, B=1):
        factor = gcd( abs(A), abs(B) )
        if B < 0:
        factor = -factor
        self._numA = A // factor
        self._denoB = B // factor


        def __str__(self):
        return str(self._numA) + '/' + str(self._denoB )

        # now I have 2 different implementations for __int__"
        #either 1:
        def __int__(self):
        return int(float(self) )

        # or 2 is used:
        def __int__(self):
        return self._numA/self._denoB

        [/code]

        My question is how can I a write a unit test (__name__='__ma in__') to test for which these two implementations produce different result?
        It looks like they return the same thing.[code=Python]
        def gcd(m,n):
        if n == 0: return m
        return gcd(n,m%n)

        class Calculation:
        def __init__(self, A=0, B=1):
        factor = gcd( abs(A), abs(B) )
        if B < 0:
        factor = -factor
        self._numA = A // factor
        self._denoB = B // factor

        def __str__(self):
        return str(self._numA) + '/' + str(self._denoB )

        def __float__(self) :
        return float(self._num A)/self._denoB

        def __int__(self):
        return self._numA/self._denoB


        class Calc(Calculatio n):
        def __init__(self, A=0, B=1):
        Calculation.__i nit__(self, A, B)

        def __int__(self):
        return int(float(self) )

        if __name__ == '__main__':
        test = (302, 22)

        a = Calculation(*te st)
        print int(a)

        b = Calc(*test)
        print int(b)

        print float(a) [/code]>>> 13
        13
        13.7272727273
        >>>

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Here is a test for a range of numbers:[code=Python]if __name__ == '__main__':

          testList = [(i,j) for i in xrange(9,109) for j in xrange(6,80)]
          strList = []
          for test in testList:
          a = Calculation(*te st)
          b = Calc(*test)
          strList.append( (int(a), int(b), float(a)))
          print '\n'.join(['%d %d %0.6f' % item for item in strList])[/code]This test does not print anything if results are the same:[code=Python]if __name__ == '__main__':
          testList = [(i,j) for i in xrange(9,2000) for j in xrange(6,80)]
          for test in testList:
          a = Calculation(*te st)
          b = Calc(*test)
          if int(a) != int(b):
          print int(a), int(b)[/code]

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            There is a difference when one of the numbers is negative:[code=Python]if __name__ == '__main__':
            testList = [(i,j) for i in xrange(-9,500,2) for j in xrange(-7,80,2)]
            for test in testList:
            a = Calculation(*te st)
            b = Calc(*test)
            if int(a) != int(b):
            print test[0], test[1], int(a), int(b), str(a), str(b)[/code][code=Python]
            ............
            -1 63 -1 0 -1/63 -1/63
            -1 65 -1 0 -1/65 -1/65
            -1 67 -1 0 -1/67 -1/67
            -1 69 -1 0 -1/69 -1/69
            -1 71 -1 0 -1/71 -1/71
            -1 73 -1 0 -1/73 -1/73
            -1 75 -1 0 -1/75 -1/75
            -1 77 -1 0 -1/77 -1/77
            -1 79 -1 0 -1/79 -1/79
            1 -7 -1 0 -1/7 -1/7
            1 -5 -1 0 -1/5 -1/5
            1 -3 -1 0 -1/3 -1/3
            3 -7 -1 0 -3/7 -3/7
            3 -5 -1 0 -3/5 -3/5
            5 -7 -1 0 -5/7 -5/7
            5 -3 -2 -1 -5/3 -5/3
            7 -5 -2 -1 -7/5 -7/5
            7 -3 -3 -2 -7/3 -7/3
            ............... ..........[/code]

            Comment

            • python101
              New Member
              • Sep 2007
              • 90

              #7
              thank you very much.

              Comment

              Working...