test_transport.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. #
  2. # Copyright (C) 2014-2015 UAVCAN Development Team <uavcan.org>
  3. #
  4. # This software is distributed under the terms of the MIT License.
  5. #
  6. # Author: Ben Dyer <ben_dyer@mac.com>
  7. # Pavel Kirienko <pavel.kirienko@zubax.com>
  8. #
  9. import unittest
  10. from uavcan import transport
  11. from uavcan.dsdl import parser
  12. class TestBitsFromBytes(unittest.TestCase):
  13. def test_empty(self):
  14. bits = transport.bits_from_bytes(bytearray(b""))
  15. self.assertEqual(bits, "")
  16. def test_single_byte(self):
  17. bits = transport.bits_from_bytes(bytearray(b"\x00"))
  18. self.assertEqual(bits, "00000000")
  19. bits = transport.bits_from_bytes(bytearray(b"\xA5"))
  20. self.assertEqual(bits, "10100101")
  21. bits = transport.bits_from_bytes(bytearray(b"\xFF"))
  22. self.assertEqual(bits, "11111111")
  23. bits = transport.bits_from_bytes(bytearray(b"\x0F"))
  24. self.assertEqual(bits, "00001111")
  25. bits = transport.bits_from_bytes(bytearray(b"\xF0"))
  26. self.assertEqual(bits, "11110000")
  27. def test_multiple_bytes(self):
  28. bits = transport.bits_from_bytes(bytearray(b"\x00\xFF"))
  29. self.assertEqual(bits, "0000000011111111")
  30. bits = transport.bits_from_bytes(bytearray(b"\xFF\x00"))
  31. self.assertEqual(bits, "1111111100000000")
  32. bits = transport.bits_from_bytes(bytearray(b"\x00\x00\xAA\x55\xFF\xFF"))
  33. self.assertEqual(
  34. bits, "000000000000000010101010010101011111111111111111")
  35. class TestBytesFromBits(unittest.TestCase):
  36. def test_empty(self):
  37. result_bytes = transport.bytes_from_bits("")
  38. self.assertEqual(result_bytes, b"")
  39. def test_partial_byte(self):
  40. result_bytes = transport.bytes_from_bits("0")
  41. self.assertEqual(result_bytes, b"\x00")
  42. result_bytes = transport.bytes_from_bits("1")
  43. self.assertEqual(result_bytes, b"\x01")
  44. result_bytes = transport.bytes_from_bits("100")
  45. self.assertEqual(result_bytes, b"\x04")
  46. result_bytes = transport.bytes_from_bits("001")
  47. self.assertEqual(result_bytes, b"\x01")
  48. result_bytes = transport.bytes_from_bits("1001")
  49. self.assertEqual(result_bytes, b"\x09")
  50. result_bytes = transport.bytes_from_bits("01001")
  51. self.assertEqual(result_bytes, b"\x09")
  52. result_bytes = transport.bytes_from_bits("0001001")
  53. self.assertEqual(result_bytes, b"\x09")
  54. result_bytes = transport.bytes_from_bits("1001001")
  55. self.assertEqual(result_bytes, b"\x49")
  56. def test_single_byte(self):
  57. result_bytes = transport.bytes_from_bits("10010110")
  58. self.assertEqual(result_bytes, b"\x96")
  59. result_bytes = transport.bytes_from_bits("10100101")
  60. self.assertEqual(result_bytes, b"\xA5")
  61. result_bytes = transport.bytes_from_bits("00000000")
  62. self.assertEqual(result_bytes, b"\x00")
  63. def test_multiple_bytes(self):
  64. result_bytes = transport.bytes_from_bits("1010010110010110")
  65. self.assertEqual(result_bytes, b"\xA5\x96")
  66. result_bytes = transport.bytes_from_bits("11010010110010110")
  67. self.assertEqual(result_bytes, b"\xD2\xCB\x00")
  68. result_bytes = transport.bytes_from_bits("10100101100101101")
  69. self.assertEqual(result_bytes, b"\xA5\x96\x01")
  70. class TestBEFromLEBits(unittest.TestCase):
  71. def test_partial_byte(self):
  72. for bits in ("0", "1", "100", "001", "1001", "01001", "001001",
  73. "1001001"):
  74. out_bits = transport.be_from_le_bits(bits, len(bits))
  75. self.assertEqual(out_bits, bits)
  76. def test_single_byte(self):
  77. for bits in ("10010110", "10100101", "00000000"):
  78. out_bits = transport.be_from_le_bits(bits, 8)
  79. self.assertEqual(out_bits, bits)
  80. def test_multiple_bytes(self):
  81. # llllllllmmmm
  82. out_bits = transport.be_from_le_bits("110110101110", 12)
  83. # mmmmllllllll
  84. self.assertEqual(out_bits, "111011011010")
  85. # llllllllmmmmmmmm
  86. out_bits = transport.be_from_le_bits("1010010110010110", 16)
  87. # mmmmmmmmllllllll
  88. self.assertEqual(out_bits, "1001011010100101")
  89. # llllllll........m
  90. out_bits = transport.be_from_le_bits("11010010110010110", 17)
  91. # m........llllllll
  92. self.assertEqual(out_bits, "01100101111010010")
  93. # llllllll........m
  94. out_bits = transport.be_from_le_bits("10100101100101101", 17)
  95. # m........llllllll
  96. self.assertEqual(out_bits, "11001011010100101")
  97. class TestLEFromBEBits(unittest.TestCase):
  98. def test_partial_byte(self):
  99. for bits in ("0", "1", "100", "001", "1001", "01001", "001001",
  100. "1001001"):
  101. out_bits = transport.le_from_be_bits(bits, len(bits))
  102. self.assertEqual(out_bits, bits)
  103. def test_single_byte(self):
  104. for bits in ("10010110", "10100101", "00000000"):
  105. out_bits = transport.le_from_be_bits(bits, 8)
  106. self.assertEqual(out_bits, bits)
  107. def test_multiple_bytes(self):
  108. # mmmmllllllll
  109. out_bits = transport.le_from_be_bits("111011011010", 12)
  110. # llllllllmmmm
  111. self.assertEqual(out_bits, "110110101110")
  112. # mmmmmmmmllllllll
  113. out_bits = transport.le_from_be_bits("1001011010100101", 16)
  114. # llllllllmmmmmmmm
  115. self.assertEqual(out_bits, "1010010110010110")
  116. # m........llllllll
  117. out_bits = transport.le_from_be_bits("01100101111010010", 17)
  118. # llllllll........m
  119. self.assertEqual(out_bits, "11010010110010110")
  120. # m........llllllll
  121. out_bits = transport.le_from_be_bits("11001011010100101", 17)
  122. # llllllll........m
  123. self.assertEqual(out_bits, "10100101100101101")
  124. class TestCast(unittest.TestCase):
  125. def test_truncated_1bit(self):
  126. dtype = parser.PrimitiveType(
  127. parser.PrimitiveType.KIND_UNSIGNED_INT,
  128. 1,
  129. parser.PrimitiveType.CAST_MODE_TRUNCATED)
  130. value = transport.cast(0, dtype)
  131. self.assertEqual(value, 0)
  132. value = transport.cast(1, dtype)
  133. self.assertEqual(value, 1)
  134. value = transport.cast(2, dtype)
  135. self.assertEqual(value, 0)
  136. value = transport.cast(-1, dtype)
  137. self.assertEqual(value, 1)
  138. value = transport.cast(5, dtype)
  139. self.assertEqual(value, 1)
  140. value = transport.cast(-10, dtype)
  141. self.assertEqual(value, 0)
  142. def test_truncated_4bit(self):
  143. dtype = parser.PrimitiveType(
  144. parser.PrimitiveType.KIND_UNSIGNED_INT,
  145. 4,
  146. parser.PrimitiveType.CAST_MODE_TRUNCATED)
  147. value = transport.cast(0, dtype)
  148. self.assertEqual(value, 0)
  149. value = transport.cast(15, dtype)
  150. self.assertEqual(value, 15)
  151. value = transport.cast(16, dtype)
  152. self.assertEqual(value, 0)
  153. value = transport.cast(-1, dtype)
  154. self.assertEqual(value, 15)
  155. value = transport.cast(30, dtype)
  156. self.assertEqual(value, 14)
  157. def test_truncated_33bit(self):
  158. dtype = parser.PrimitiveType(
  159. parser.PrimitiveType.KIND_UNSIGNED_INT,
  160. 33,
  161. parser.PrimitiveType.CAST_MODE_TRUNCATED)
  162. value = transport.cast(0, dtype)
  163. self.assertEqual(value, 0)
  164. value = transport.cast(8589934591, dtype)
  165. self.assertEqual(value, 8589934591)
  166. value = transport.cast(8589934592, dtype)
  167. self.assertEqual(value, 0)
  168. value = transport.cast(-1, dtype)
  169. self.assertEqual(value, 8589934591)
  170. def test_truncated_64bit(self):
  171. dtype = parser.PrimitiveType(
  172. parser.PrimitiveType.KIND_UNSIGNED_INT,
  173. 64,
  174. parser.PrimitiveType.CAST_MODE_TRUNCATED)
  175. value = transport.cast(0, dtype)
  176. self.assertEqual(value, 0)
  177. value = transport.cast(18446744073709551615, dtype)
  178. self.assertEqual(value, 18446744073709551615)
  179. value = transport.cast(-1, dtype)
  180. self.assertEqual(value, 18446744073709551615)
  181. value = transport.cast(18446744073709551617, dtype)
  182. self.assertEqual(value, 1)
  183. value = transport.cast(-18446744073709551615, dtype)
  184. self.assertEqual(value, 1)
  185. def test_unsigned_saturated_1bit(self):
  186. dtype = parser.PrimitiveType(
  187. parser.PrimitiveType.KIND_UNSIGNED_INT,
  188. 1,
  189. parser.PrimitiveType.CAST_MODE_SATURATED)
  190. value = transport.cast(0, dtype)
  191. self.assertEqual(value, 0)
  192. value = transport.cast(1, dtype)
  193. self.assertEqual(value, 1)
  194. value = transport.cast(2, dtype)
  195. self.assertEqual(value, 1)
  196. value = transport.cast(-1, dtype)
  197. self.assertEqual(value, 0)
  198. value = transport.cast(5, dtype)
  199. self.assertEqual(value, 1)
  200. value = transport.cast(-10, dtype)
  201. self.assertEqual(value, 0)
  202. def test_unsigned_saturated_4bit(self):
  203. dtype = parser.PrimitiveType(
  204. parser.PrimitiveType.KIND_UNSIGNED_INT,
  205. 4,
  206. parser.PrimitiveType.CAST_MODE_SATURATED)
  207. value = transport.cast(0, dtype)
  208. self.assertEqual(value, 0)
  209. value = transport.cast(15, dtype)
  210. self.assertEqual(value, 15)
  211. value = transport.cast(16, dtype)
  212. self.assertEqual(value, 15)
  213. value = transport.cast(-1, dtype)
  214. self.assertEqual(value, 0)
  215. value = transport.cast(30, dtype)
  216. self.assertEqual(value, 15)
  217. def test_unsigned_saturated_33bit(self):
  218. dtype = parser.PrimitiveType(
  219. parser.PrimitiveType.KIND_UNSIGNED_INT,
  220. 33,
  221. parser.PrimitiveType.CAST_MODE_SATURATED)
  222. value = transport.cast(0, dtype)
  223. self.assertEqual(value, 0)
  224. value = transport.cast(8589934591, dtype)
  225. self.assertEqual(value, 8589934591)
  226. value = transport.cast(8589934592, dtype)
  227. self.assertEqual(value, 8589934591)
  228. value = transport.cast(-1, dtype)
  229. self.assertEqual(value, 0)
  230. def test_unsigned_saturated_64bit(self):
  231. dtype = parser.PrimitiveType(
  232. parser.PrimitiveType.KIND_UNSIGNED_INT,
  233. 64,
  234. parser.PrimitiveType.CAST_MODE_SATURATED)
  235. value = transport.cast(0, dtype)
  236. self.assertEqual(value, 0)
  237. value = transport.cast(18446744073709551615, dtype)
  238. self.assertEqual(value, 18446744073709551615)
  239. value = transport.cast(-1, dtype)
  240. self.assertEqual(value, 0)
  241. value = transport.cast(18446744073709551617, dtype)
  242. self.assertEqual(value, 18446744073709551615)
  243. value = transport.cast(-18446744073709551615, dtype)
  244. self.assertEqual(value, 0)
  245. def test_signed_saturated_4bit(self):
  246. dtype = parser.PrimitiveType(
  247. parser.PrimitiveType.KIND_SIGNED_INT,
  248. 4,
  249. parser.PrimitiveType.CAST_MODE_SATURATED)
  250. value = transport.cast(0, dtype)
  251. self.assertEqual(value, 0)
  252. value = transport.cast(15, dtype)
  253. self.assertEqual(value, 7)
  254. value = transport.cast(16, dtype)
  255. self.assertEqual(value, 7)
  256. value = transport.cast(-1, dtype)
  257. self.assertEqual(value, -1)
  258. value = transport.cast(-30, dtype)
  259. self.assertEqual(value, -8)
  260. def test_signed_saturated_33bit(self):
  261. dtype = parser.PrimitiveType(
  262. parser.PrimitiveType.KIND_SIGNED_INT,
  263. 33,
  264. parser.PrimitiveType.CAST_MODE_SATURATED)
  265. value = transport.cast(0, dtype)
  266. self.assertEqual(value, 0)
  267. value = transport.cast(8589934591, dtype)
  268. self.assertEqual(value, 4294967295)
  269. value = transport.cast(-8589934592, dtype)
  270. self.assertEqual(value, -4294967296)
  271. value = transport.cast(-1, dtype)
  272. self.assertEqual(value, -1)
  273. def test_signed_saturated_64bit(self):
  274. dtype = parser.PrimitiveType(
  275. parser.PrimitiveType.KIND_SIGNED_INT,
  276. 64,
  277. parser.PrimitiveType.CAST_MODE_SATURATED)
  278. value = transport.cast(0, dtype)
  279. self.assertEqual(value, 0)
  280. value = transport.cast(18446744073709551615, dtype)
  281. self.assertEqual(value, 9223372036854775808)
  282. value = transport.cast(-1, dtype)
  283. self.assertEqual(value, -1)
  284. value = transport.cast(-18446744073709551617, dtype)
  285. self.assertEqual(value, -9223372036854775809)
  286. value = transport.cast(-9223372036854775808, dtype)
  287. self.assertEqual(value, -9223372036854775808)
  288. def test_float16_truncated(self):
  289. pass
  290. def test_float16_saturated(self):
  291. pass
  292. def test_float32_truncated(self):
  293. pass
  294. def test_float32_saturated(self):
  295. pass
  296. class TestArrayBasic(unittest.TestCase):
  297. def setUp(self):
  298. custom_type = parser.CompoundType(
  299. "CustomType",
  300. parser.CompoundType.KIND_MESSAGE,
  301. "source.uavcan",
  302. 0,
  303. ""
  304. )
  305. custom_type.fields = [
  306. parser.Field(
  307. parser.PrimitiveType(
  308. parser.PrimitiveType.KIND_SIGNED_INT,
  309. 8,
  310. parser.PrimitiveType.CAST_MODE_TRUNCATED
  311. ),
  312. "a"
  313. ),
  314. parser.Field(
  315. parser.PrimitiveType(
  316. parser.PrimitiveType.KIND_FLOAT,
  317. 16,
  318. parser.PrimitiveType.CAST_MODE_SATURATED
  319. ),
  320. "b"
  321. ),
  322. parser.Field(
  323. parser.ArrayType(
  324. parser.PrimitiveType(
  325. parser.PrimitiveType.KIND_UNSIGNED_INT,
  326. 1,
  327. parser.PrimitiveType.CAST_MODE_SATURATED
  328. ),
  329. parser.ArrayType.MODE_DYNAMIC,
  330. 5
  331. ),
  332. "c"
  333. )
  334. ]
  335. def custom_type_factory(*args, **kwargs):
  336. return transport.CompoundValue(custom_type, *args, **kwargs)
  337. custom_type._instantiate = custom_type_factory
  338. self.a1_type = parser.ArrayType(
  339. parser.PrimitiveType(
  340. parser.PrimitiveType.KIND_SIGNED_INT,
  341. 8,
  342. parser.PrimitiveType.CAST_MODE_TRUNCATED
  343. ),
  344. parser.ArrayType.MODE_STATIC,
  345. 4
  346. )
  347. self.a2_type = parser.ArrayType(
  348. parser.PrimitiveType(
  349. parser.PrimitiveType.KIND_FLOAT,
  350. 16,
  351. parser.PrimitiveType.CAST_MODE_SATURATED
  352. ),
  353. parser.ArrayType.MODE_STATIC,
  354. 2
  355. )
  356. self.a3_type = parser.ArrayType(
  357. custom_type,
  358. parser.ArrayType.MODE_STATIC,
  359. 2
  360. )
  361. def test_size(self):
  362. self.assertEqual(self.a3_type.value_type.fields[2].type.value_type.bitlen, 1)
  363. self.assertEqual(self.a1_type.get_max_bitlen(), 8 * 4)
  364. self.assertEqual(self.a2_type.get_max_bitlen(), 16 * 2)
  365. self.assertEqual(self.a3_type.get_max_bitlen(), (8 + 16 + 5 + 3) * 2)
  366. def test_representation(self):
  367. a1 = transport.ArrayValue(self.a1_type)
  368. a2 = transport.ArrayValue(self.a2_type)
  369. a3 = transport.ArrayValue(self.a3_type)
  370. for i in range(4):
  371. a1[i] = i
  372. for i in range(2):
  373. a2[i] = i
  374. for i in range(2):
  375. a3[i].a = i
  376. a3[i].b = i
  377. for i2 in range(5):
  378. a3[i].c.append(i2 & 1)
  379. self.assertEqual(len(a3[i].c), 5)
  380. self.assertEqual(
  381. transport.format_bits(a1._pack(False)),
  382. "00000000 00000001 00000010 00000011"
  383. )
  384. self.assertEqual(
  385. transport.format_bits(a2._pack(False)),
  386. "00000000 00000000 00000000 00111100"
  387. )
  388. self.assertEqual(
  389. transport.format_bits(a3._pack(True)),
  390. "00000000 00000000 00000000 10101010 " +
  391. "00000001 00000000 00111100 10101010"
  392. )
  393. class TestVoid(unittest.TestCase):
  394. def setUp(self):
  395. self.custom_type = parser.CompoundType(
  396. "CustomType",
  397. parser.CompoundType.KIND_MESSAGE,
  398. "source.uavcan",
  399. 0,
  400. ""
  401. )
  402. self.custom_type.fields = [
  403. parser.Field(
  404. parser.PrimitiveType(
  405. parser.PrimitiveType.KIND_FLOAT,
  406. 16,
  407. parser.PrimitiveType.CAST_MODE_SATURATED
  408. ),
  409. "a"
  410. ),
  411. parser.Field(parser.VoidType(3), None),
  412. parser.Field(
  413. parser.PrimitiveType(
  414. parser.PrimitiveType.KIND_UNSIGNED_INT,
  415. 1,
  416. parser.PrimitiveType.CAST_MODE_SATURATED
  417. ),
  418. "b"
  419. )
  420. ]
  421. def custom_type_factory(*args, **kwargs):
  422. return transport.CompoundValue(self.custom_type, *args,
  423. **kwargs)
  424. self.custom_type._instantiate = custom_type_factory
  425. def test_size(self):
  426. self.assertEqual(self.custom_type.fields[1].type.bitlen, 3)
  427. self.assertEqual(self.custom_type.get_max_bitlen(), 20)
  428. def test_representation(self):
  429. c1 = self.custom_type()
  430. self.assertEqual(
  431. transport.format_bits(c1._pack(False)),
  432. "00000000 00000000 0000"
  433. )
  434. c1.a = 1
  435. c1.b = 1
  436. self.assertEqual(
  437. transport.format_bits(c1._pack(False)),
  438. "00000000 00111100 0001"
  439. )
  440. class TestMessageUnion(unittest.TestCase):
  441. def setUp(self):
  442. self.custom_type = parser.CompoundType(
  443. "CustomType",
  444. parser.CompoundType.KIND_MESSAGE,
  445. "source.uavcan",
  446. 0,
  447. ""
  448. )
  449. self.custom_type.union = True
  450. self.custom_type.fields = [
  451. parser.Field(
  452. parser.PrimitiveType(
  453. parser.PrimitiveType.KIND_FLOAT,
  454. 16,
  455. parser.PrimitiveType.CAST_MODE_SATURATED
  456. ),
  457. "a"
  458. ),
  459. parser.Field(
  460. parser.ArrayType(
  461. parser.PrimitiveType(
  462. parser.PrimitiveType.KIND_UNSIGNED_INT,
  463. 8,
  464. parser.PrimitiveType.CAST_MODE_SATURATED
  465. ),
  466. parser.ArrayType.MODE_STATIC,
  467. 2
  468. ),
  469. "b"
  470. )
  471. ]
  472. def custom_type_factory(*args, **kwargs):
  473. return transport.CompoundValue(self.custom_type, *args,
  474. **kwargs)
  475. self.custom_type._instantiate = custom_type_factory
  476. def test_size(self):
  477. self.assertEqual(self.custom_type.fields[0].type.bitlen, 16)
  478. self.assertEqual(self.custom_type.fields[1].type.get_max_bitlen(), 16)
  479. self.assertEqual(self.custom_type.get_max_bitlen(), 17)
  480. def test_representation(self):
  481. c1 = self.custom_type()
  482. self.assertEqual(
  483. transport.format_bits(c1._pack(True)),
  484. "00000000 00000000 0"
  485. )
  486. c2 = self.custom_type()
  487. c2.a = 1
  488. self.assertEqual(transport.get_active_union_field(c2), "a")
  489. self.assertEqual(
  490. transport.format_bits(c2._pack(True)),
  491. "00000000 00011110 0"
  492. )
  493. c3 = self.custom_type()
  494. c3.b[0] = 1
  495. c3.b[1] = 3
  496. self.assertEqual(transport.get_active_union_field(c3), "b")
  497. self.assertEqual(
  498. transport.format_bits(c3._pack(False)),
  499. "10000000 10000001 1"
  500. )
  501. class TestAssignment(unittest.TestCase):
  502. def setUp(self):
  503. import uavcan
  504. self.a = uavcan.protocol.GetNodeInfo.Response()
  505. def test_compound_assignment(self):
  506. import uavcan
  507. orig_status = self.a.status
  508. print(orig_status)
  509. self.assertEqual(orig_status.mode, 0)
  510. self.assertEqual(orig_status.uptime_sec, 0)
  511. self.a.status = uavcan.protocol.NodeStatus(mode=3, uptime_sec=12345)
  512. new_status = self.a.status
  513. print(new_status)
  514. self.assertEqual(new_status.mode, 3)
  515. self.assertEqual(new_status.uptime_sec, 12345)
  516. def test_array_assignment(self):
  517. print(repr(self.a.name))
  518. print(str(self.a.name))
  519. self.assertEqual(self.a.name, [])
  520. self.assertEqual(self.a.name, '')
  521. self.a.name = '123'
  522. self.assertEqual(self.a.name, [49, 50, 51])
  523. self.assertEqual(self.a.name, '123')
  524. self.a.name = [52, 53, 54]
  525. self.assertEqual(self.a.name, [52, 53, 54])
  526. self.assertEqual(self.a.name, '456')
  527. print(repr(self.a.name))
  528. print(str(self.a.name))
  529. class TestFloats(unittest.TestCase):
  530. def test_basic(self):
  531. def make_float(bitlen):
  532. return parser.PrimitiveType(parser.PrimitiveType.KIND_FLOAT, bitlen,
  533. parser.PrimitiveType.CAST_MODE_TRUNCATED)
  534. # 64 bit
  535. a = transport.PrimitiveValue(make_float(64))
  536. print(a.value)
  537. self.assertEqual(a.value, 0)
  538. a.value = 123.456
  539. print(a.value)
  540. self.assertEqual(a.value, 123.456)
  541. # 16 bit
  542. a = transport.PrimitiveValue(make_float(16))
  543. print(a.value)
  544. self.assertEqual(a.value, 0)
  545. # nan
  546. a.value = float('nan')
  547. print(a.value)
  548. self.assertEqual(str(a.value), 'nan')
  549. # positive infinity
  550. a.value = float('inf')
  551. print(a.value)
  552. self.assertEqual(str(a.value), 'inf')
  553. # negative infinity
  554. a.value = float('-inf')
  555. print(a.value)
  556. self.assertEqual(str(a.value), '-inf')
  557. if __name__ == '__main__':
  558. unittest.main()