473,221 Members | 1,965 Online

# Unit testing for two difference implementations

Expand|Select|Wrap|Line Numbers
1. class Calculation:
2.   def __init__(self, A=0, B=1):
3.       factor = gcd( abs(A), abs(B) )
4.       if B < 0:
5.         factor = -factor
6.       self._numA = A // factor
7.       self._denoB = B // factor
8.
9.
10.   def __str__(self):
11.       return str(self._numA) + '/' + str(self._denoB)
12.
13. # now I have 2 different implementations for __int__"
14. #either 1:
15.   def __int__(self):
16.       return int(float(self))
17.
18. # or 2 is used:
19.   def __int__(self):
20.       return self._numA/self._denoB
21.
22.
My question is how can I a write a unit test (__name__='__main__') to test for which these two implementations produce different result?
Nov 12 '07 #1
6 1128
bartonc
6,596 Expert 4TB
How about commenting out first one, then the other, since you will only have one (the second one defined), anyway.
Nov 13 '07 #2
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?
Nov 13 '07 #3
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
1. class Calculation:
2.   def __init__(self, A=0, B=1):
3.       factor = gcd( abs(A), abs(B) )
4.       if B < 0:
5.         factor = -factor
6.       self._numA = A // factor
7.       self._denoB = B // factor
8.
9.
10.   def __str__(self):
11.       return str(self._numA) + '/' + str(self._denoB)
12.
13. # now I have 2 different implementations for __int__"
14. #either 1:
15.   def __int__(self):
16.       return int(float(self))
17.
18. # or 2 is used:
19.   def __int__(self):
20.       return self._numA/self._denoB
21.
22.
My question is how can I a write a unit test (__name__='__main__') to test for which these two implementations produce different result?
It looks like they return the same thing.
Expand|Select|Wrap|Line Numbers
1. def gcd(m,n):
2.     if n == 0: return m
3.     return gcd(n,m%n)
4.
5. class Calculation:
6.     def __init__(self, A=0, B=1):
7.         factor = gcd( abs(A), abs(B) )
8.         if B < 0:
9.             factor = -factor
10.         self._numA = A // factor
11.         self._denoB = B // factor
12.
13.     def __str__(self):
14.         return str(self._numA) + '/' + str(self._denoB)
15.
16.     def __float__(self):
17.         return float(self._numA)/self._denoB
18.
19.     def __int__(self):
20.         return self._numA/self._denoB
21.
22.
23. class Calc(Calculation):
24.     def __init__(self, A=0, B=1):
25.         Calculation.__init__(self, A, B)
26.
27.     def __int__(self):
28.         return int(float(self))
29.
30. if __name__ == '__main__':
31.     test = (302, 22)
32.
33.     a = Calculation(*test)
34.     print int(a)
35.
36.     b = Calc(*test)
37.     print int(b)
38.
39.     print float(a)
>>> 13
13
13.7272727273
>>>
Nov 13 '07 #4
bvdet
2,851 Expert Mod 2GB
Here is a test for a range of numbers:
Expand|Select|Wrap|Line Numbers
1. if __name__ == '__main__':
2.
3.     testList = [(i,j) for i in xrange(9,109) for j in xrange(6,80)]
4.     strList = []
5.     for test in testList:
6.         a = Calculation(*test)
7.         b = Calc(*test)
8.         strList.append((int(a), int(b), float(a)))
9.     print '\n'.join(['%d %d %0.6f' % item for item in strList])
This test does not print anything if results are the same:
Expand|Select|Wrap|Line Numbers
1. if __name__ == '__main__':
2.     testList = [(i,j) for i in xrange(9,2000) for j in xrange(6,80)]
3.     for test in testList:
4.         a = Calculation(*test)
5.         b = Calc(*test)
6.         if int(a) != int(b):
7.             print int(a), int(b)
Nov 13 '07 #5
bvdet
2,851 Expert Mod 2GB
There is a difference when one of the numbers is negative:
Expand|Select|Wrap|Line Numbers
1. if __name__ == '__main__':
2.     testList = [(i,j) for i in xrange(-9,500,2) for j in xrange(-7,80,2)]
3.     for test in testList:
4.         a = Calculation(*test)
5.         b = Calc(*test)
6.         if int(a) != int(b):
7.             print test[0], test[1], int(a), int(b), str(a), str(b)
Expand|Select|Wrap|Line Numbers
1. ............
2. -1 63 -1 0 -1/63 -1/63
3. -1 65 -1 0 -1/65 -1/65
4. -1 67 -1 0 -1/67 -1/67
5. -1 69 -1 0 -1/69 -1/69
6. -1 71 -1 0 -1/71 -1/71
7. -1 73 -1 0 -1/73 -1/73
8. -1 75 -1 0 -1/75 -1/75
9. -1 77 -1 0 -1/77 -1/77
10. -1 79 -1 0 -1/79 -1/79
11. 1 -7 -1 0 -1/7 -1/7
12. 1 -5 -1 0 -1/5 -1/5
13. 1 -3 -1 0 -1/3 -1/3
14. 3 -7 -1 0 -3/7 -3/7
15. 3 -5 -1 0 -3/5 -3/5
16. 5 -7 -1 0 -5/7 -5/7
17. 5 -3 -2 -1 -5/3 -5/3
18. 7 -5 -2 -1 -7/5 -7/5
19. 7 -3 -3 -2 -7/3 -7/3
20. .........................
Nov 13 '07 #6
thank you very much.
Nov 13 '07 #7