|
5 | 5 | # |
6 | 6 |
|
7 | 7 | import ctypes |
| 8 | +import math |
| 9 | +import struct as pystruct |
8 | 10 | import unittest |
9 | 11 |
|
10 | | -from libdestruct import c_int, c_long, c_str, c_uint, inflater, struct, ptr, ptr_to_self |
| 12 | +from libdestruct import c_int, c_long, c_str, c_uint, c_float, c_double, inflater, struct, ptr, ptr_to_self |
11 | 13 |
|
12 | 14 |
|
13 | 15 | class ObjFromBytesTest(unittest.TestCase): |
@@ -104,6 +106,82 @@ class test_t(struct): |
104 | 106 | self.assertIn("0x0", s) |
105 | 107 |
|
106 | 108 |
|
| 109 | +class FloatTest(unittest.TestCase): |
| 110 | + """c_float and c_double types.""" |
| 111 | + |
| 112 | + def test_c_float_read(self): |
| 113 | + memory = pystruct.pack("<f", 3.14) |
| 114 | + obj = c_float.from_bytes(memory) |
| 115 | + self.assertAlmostEqual(obj.value, 3.14, places=5) |
| 116 | + |
| 117 | + def test_c_double_read(self): |
| 118 | + memory = pystruct.pack("<d", 2.718281828) |
| 119 | + obj = c_double.from_bytes(memory) |
| 120 | + self.assertAlmostEqual(obj.value, 2.718281828, places=8) |
| 121 | + |
| 122 | + def test_c_float_write(self): |
| 123 | + memory = bytearray(4) |
| 124 | + lib = inflater(memory) |
| 125 | + obj = lib.inflate(c_float, 0) |
| 126 | + |
| 127 | + obj.value = 1.5 |
| 128 | + self.assertAlmostEqual(obj.value, 1.5) |
| 129 | + self.assertEqual(memory, pystruct.pack("<f", 1.5)) |
| 130 | + |
| 131 | + def test_c_float_to_bytes_round_trip(self): |
| 132 | + original = pystruct.pack("<f", -42.5) |
| 133 | + obj = c_float.from_bytes(original) |
| 134 | + self.assertEqual(obj.to_bytes(), original) |
| 135 | + |
| 136 | + def test_c_double_to_bytes_round_trip(self): |
| 137 | + original = pystruct.pack("<d", 123456.789) |
| 138 | + obj = c_double.from_bytes(original) |
| 139 | + self.assertEqual(obj.to_bytes(), original) |
| 140 | + |
| 141 | + def test_c_float_in_struct(self): |
| 142 | + class test_t(struct): |
| 143 | + x: c_float |
| 144 | + y: c_float |
| 145 | + |
| 146 | + memory = pystruct.pack("<ff", 1.0, 2.0) |
| 147 | + test = test_t.from_bytes(memory) |
| 148 | + self.assertAlmostEqual(test.x.value, 1.0) |
| 149 | + self.assertAlmostEqual(test.y.value, 2.0) |
| 150 | + |
| 151 | + def test_c_float_size(self): |
| 152 | + self.assertEqual(c_float.size, 4) |
| 153 | + |
| 154 | + def test_c_double_size(self): |
| 155 | + self.assertEqual(c_double.size, 8) |
| 156 | + |
| 157 | + def test_c_float_special_values(self): |
| 158 | + for val in [0.0, float("inf"), float("-inf")]: |
| 159 | + memory = pystruct.pack("<f", val) |
| 160 | + obj = c_float.from_bytes(memory) |
| 161 | + self.assertEqual(obj.value, val) |
| 162 | + |
| 163 | + # NaN |
| 164 | + memory = pystruct.pack("<f", float("nan")) |
| 165 | + obj = c_float.from_bytes(memory) |
| 166 | + self.assertTrue(math.isnan(obj.value)) |
| 167 | + |
| 168 | + def test_c_float_freeze(self): |
| 169 | + memory = bytearray(pystruct.pack("<f", 9.5)) |
| 170 | + lib = inflater(memory) |
| 171 | + obj = lib.inflate(c_float, 0) |
| 172 | + |
| 173 | + obj.freeze() |
| 174 | + self.assertAlmostEqual(obj.value, 9.5) |
| 175 | + |
| 176 | + with self.assertRaises(ValueError): |
| 177 | + obj.value = 1.0 |
| 178 | + |
| 179 | + def test_c_float_dunder(self): |
| 180 | + memory = pystruct.pack("<f", 3.14) |
| 181 | + obj = c_float.from_bytes(memory) |
| 182 | + self.assertAlmostEqual(float(obj), 3.14, places=5) |
| 183 | + |
| 184 | + |
107 | 185 | class CStrTest(unittest.TestCase): |
108 | 186 | """c_str indexing, iteration, and mutation.""" |
109 | 187 |
|
|
0 commit comments