datatypehelpers.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from __future__ import division, absolute_import, print_function, unicode_literals
  2. from awlsim_tstlib import *
  3. initTest(__file__)
  4. from awlsim.common.datatypehelpers import * #+cimport
  5. class Test_DataTypeHelpers(TestCase):
  6. def test_swapEndianWord(self):
  7. self.assertEqual(swapEndianWord(0x1234), 0x3412)
  8. self.assertEqual(swapEndianWord(swapEndianWord(0x1234)), 0x1234)
  9. def test_swapEndianDWord(self):
  10. self.assertEqual(swapEndianDWord(0x12345678), 0x78563412)
  11. self.assertEqual(swapEndianDWord(swapEndianDWord(0x12345678)), 0x12345678)
  12. def test_floatConst(self):
  13. self.assertEqual(pyFloatToDWord(floatConst.minNormPosFloat32), floatConst.minNormPosFloat32DWord)
  14. self.assertEqual(pyFloatToDWord(floatConst.minNormNegFloat32), floatConst.minNormNegFloat32DWord)
  15. self.assertEqual(pyFloatToDWord(floatConst.maxNormNegFloat32), floatConst.maxNormNegFloat32DWord)
  16. self.assertEqual(pyFloatToDWord(floatConst.maxNormPosFloat32), floatConst.maxNormPosFloat32DWord)
  17. self.assertEqual(pyFloatToDWord(floatConst.posInfFloat), floatConst.posInfDWord)
  18. self.assertEqual(pyFloatToDWord(floatConst.negInfFloat), floatConst.negInfDWord)
  19. self.assertEqual(pyFloatToDWord(floatConst.nNaNFloat), floatConst.nNaNDWord)
  20. def test_getMSB(self):
  21. self.assertEqual(getMSB(0), 0)
  22. self.assertEqual(getMSB(0xFFFFFFFF), 0x80000000)
  23. self.assertEqual(getMSB(0x57C31), 0x40000)
  24. self.assertEqual(getMSB(0xA6B204), 0x800000)
  25. mask0 = 0xAAAAAAAA
  26. mask1 = 0x55555555
  27. for i in range(32):
  28. self.assertEqual(getMSB(1 << i), 1 << i)
  29. self.assertEqual(getMSB(mask0), 1 << (31 - i))
  30. self.assertEqual(getMSB(mask1), (1 << (31 - i)) >> 1)
  31. mask0 >>= 1
  32. mask1 >>= 1