-
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
-
-
My question is how can I a write a unit test (__name__='__main__') to test for which these two implementations produce different result?
6 1132
How about commenting out first one, then the other, since you will only have one (the second one defined), anyway.
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?
bvdet 2,851
Expert Mod 2GB -
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
-
-
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. -
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._numA)/self._denoB
-
-
def __int__(self):
-
return self._numA/self._denoB
-
-
-
class Calc(Calculation):
-
def __init__(self, A=0, B=1):
-
Calculation.__init__(self, A, B)
-
-
def __int__(self):
-
return int(float(self))
-
-
if __name__ == '__main__':
-
test = (302, 22)
-
-
a = Calculation(*test)
-
print int(a)
-
-
b = Calc(*test)
-
print int(b)
-
-
print float(a)
>>> 13
13
13.7272727273
>>>
bvdet 2,851
Expert Mod 2GB
Here is a test for a range of numbers: - 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(*test)
-
b = Calc(*test)
-
strList.append((int(a), int(b), float(a)))
-
print '\n'.join(['%d %d %0.6f' % item for item in strList])
This test does not print anything if results are the same: - if __name__ == '__main__':
-
testList = [(i,j) for i in xrange(9,2000) for j in xrange(6,80)]
-
for test in testList:
-
a = Calculation(*test)
-
b = Calc(*test)
-
if int(a) != int(b):
-
print int(a), int(b)
bvdet 2,851
Expert Mod 2GB
There is a difference when one of the numbers is negative: - 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(*test)
-
b = Calc(*test)
-
if int(a) != int(b):
-
print test[0], test[1], int(a), int(b), str(a), str(b)
-
............
-
-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
-
.........................
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
by: |
last post by:
Hi!
I'm looking for unit-testing tools for .NET.
Somthing like Java has --> http://www.junit.org
regards,
gicio
|
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...
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |