improve setup.py description string
[pyion.git] / tests / xn / fixed_test.py
blob8bdeccd5b8e3305bfc7c8a25446be337b7b5b5f3
1 import unittest
2 from ion.n.fixed import *
3 from ion.n.powers import *
4 #intToExponent
6 class KnownValues(unittest.TestCase):
8 knownvalues = (
9 # value, fval, ival
10 ( "65535/256", 255.99609375, 255),
11 ( "65535/16", 4095.93750000, 4095),
12 ( "65535/4", 16383.75000000, 16383),
13 ( "65535/2", 32767.50000000, 32767)
16 def testFloat(self):
17 "convert to float"
18 for value,fval,ival in self.knownvalues:
19 result = float(fixed(value))
20 self.assertAlmostEqual(result, fval, 2)
22 def testInt(self):
23 "convert to integer"
24 for value, fval, ival in self.knownvalues:
25 result = int(fixed(value))
26 self.assertEqual(result, ival, 2)
28 def testUnFloat(self):
29 "convert from float"
30 for value, fval, ival in self.knownvalues:
31 #extract the scale from the value string
32 bits = value.find("/")
33 bits = value[bits+1:]
34 bits = int(bits)
35 print bits,fval
36 result = str(fixed(fval,intToExponent(bits)))
37 self.assertEqual(result, value)
39 def testUnInt(self):
40 "convert from int"
41 for value, fval, ival in self.knownvalues:
42 bits = value.find("/")
43 bits = value[bits+1:]
44 bits = int(bits)
45 result = str(fixed(ival,intToExponent(bits)))
46 self.assertEqual(result, "%s/%s" % (ival,bits) , 2)
49 #class Arithmetic(unittest.TestCase):
50 # KnownValues = (
52 # )
53 # def testMul(self):
54 #testDiv
55 #testExtendedSyntax
56 #test
58 if __name__ == "__main__":
59 unittest.main()