473,320 Members | 1,876 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,320 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 1132
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.