473,221 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Edvard Majakari | last post by:
Hi all ya unit-testing experts there :) Code I'm working on has to parse large and complex files and detect equally complex and large amount of errors before the contents of the file is fed to...
19
by: newbiecpp | last post by:
I am looking for a C++ unit testing program. I googled and found there are quite a few there, such as CppTest, C++Test, and DejaGnu. Can someone give me a recommendation among these programs? I...
3
by: Matt Kruse | last post by:
Does anyone actively use JSUnit < http://www.edwardh.com/jsunit/ > for unit testing their javascript code? For logical functions, I imagine it could be very useful. However, I'm wondering how...
38
by: Christoph Zwerschke | last post by:
In August 2001, there was a thread about the "Art of Unit Testing": http://groups.google.com/group/comp.lang.python/browse_frm/thread/aa2bd17e7f995d05/71a29faf0a0485d5 Paul Moore asked the...
14
by: | last post by:
Hi! I'm looking for unit-testing tools for .NET. Somthing like Java has --> http://www.junit.org regards, gicio
5
by: David K Allen | last post by:
Here's my problem. Have a database interface with several methods. I created a database class to implement the interface, and a set of unit tests to test each method. Now I want to support...
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
27
by: brad | last post by:
Does anyone else feel that unittesting is too much work? Not in general, just the official unittest module for small to medium sized projects? It seems easier to write some quick methods that are...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.