|
10 | 10 | import unittest |
11 | 11 |
|
12 | 12 | from libdestruct import c_int, c_long, c_str, c_uint, c_float, c_double, inflater, struct, ptr, ptr_to_self |
| 13 | +from libdestruct.backing.memory_resolver import MemoryResolver |
13 | 14 |
|
14 | 15 |
|
15 | 16 | class ObjFromBytesTest(unittest.TestCase): |
@@ -105,6 +106,74 @@ class test_t(struct): |
105 | 106 | s = str(test.p) |
106 | 107 | self.assertIn("0x0", s) |
107 | 108 |
|
| 109 | + def test_ptr_add(self): |
| 110 | + """ptr + 1 returns new ptr at addr + sizeof(target).""" |
| 111 | + # Array of 3 c_int values: [10, 20, 30] |
| 112 | + memory = bytearray(8 + 12) |
| 113 | + memory[0:8] = (8).to_bytes(8, "little") # pointer to offset 8 |
| 114 | + memory[8:12] = (10).to_bytes(4, "little") |
| 115 | + memory[12:16] = (20).to_bytes(4, "little") |
| 116 | + memory[16:20] = (30).to_bytes(4, "little") |
| 117 | + |
| 118 | + p = ptr(MemoryResolver(memory, 0), c_int) |
| 119 | + |
| 120 | + p2 = p + 1 |
| 121 | + self.assertEqual(p2.unwrap().value, 20) |
| 122 | + |
| 123 | + p3 = p + 2 |
| 124 | + self.assertEqual(p3.unwrap().value, 30) |
| 125 | + |
| 126 | + def test_ptr_sub(self): |
| 127 | + """ptr - 1 returns new ptr at addr - sizeof(target).""" |
| 128 | + memory = bytearray(8 + 12) |
| 129 | + memory[0:8] = (12).to_bytes(8, "little") # pointer to second element |
| 130 | + memory[8:12] = (10).to_bytes(4, "little") |
| 131 | + memory[12:16] = (20).to_bytes(4, "little") |
| 132 | + memory[16:20] = (30).to_bytes(4, "little") |
| 133 | + |
| 134 | + p = ptr(MemoryResolver(memory, 0), c_int) |
| 135 | + |
| 136 | + p2 = p - 1 |
| 137 | + self.assertEqual(p2.unwrap().value, 10) |
| 138 | + |
| 139 | + def test_ptr_add_raw(self): |
| 140 | + """Untyped ptr: ptr + n advances by n bytes.""" |
| 141 | + memory = bytearray(8 + 4) |
| 142 | + memory[0:8] = (8).to_bytes(8, "little") # pointer to offset 8 |
| 143 | + memory[8:12] = (0x44332211).to_bytes(4, "little") |
| 144 | + |
| 145 | + p = ptr(MemoryResolver(memory, 0)) |
| 146 | + # No wrapper set, so element size is 1 byte |
| 147 | + |
| 148 | + p2 = p + 2 |
| 149 | + self.assertEqual(p2.get(), 10) # 8 + 2 |
| 150 | + |
| 151 | + def test_ptr_getitem(self): |
| 152 | + """ptr[0] == unwrap(), ptr[1] == (ptr+1).unwrap().""" |
| 153 | + memory = bytearray(8 + 12) |
| 154 | + memory[0:8] = (8).to_bytes(8, "little") |
| 155 | + memory[8:12] = (100).to_bytes(4, "little") |
| 156 | + memory[12:16] = (200).to_bytes(4, "little") |
| 157 | + memory[16:20] = (300).to_bytes(4, "little") |
| 158 | + |
| 159 | + p = ptr(MemoryResolver(memory, 0), c_int) |
| 160 | + |
| 161 | + self.assertEqual(p[0].value, 100) |
| 162 | + self.assertEqual(p[1].value, 200) |
| 163 | + self.assertEqual(p[2].value, 300) |
| 164 | + |
| 165 | + def test_ptr_arithmetic_chain(self): |
| 166 | + """(ptr + 2)[0] accesses element at index 2.""" |
| 167 | + memory = bytearray(8 + 12) |
| 168 | + memory[0:8] = (8).to_bytes(8, "little") |
| 169 | + memory[8:12] = (1).to_bytes(4, "little") |
| 170 | + memory[12:16] = (2).to_bytes(4, "little") |
| 171 | + memory[16:20] = (3).to_bytes(4, "little") |
| 172 | + |
| 173 | + p = ptr(MemoryResolver(memory, 0), c_int) |
| 174 | + |
| 175 | + self.assertEqual((p + 2)[0].value, 3) |
| 176 | + |
108 | 177 |
|
109 | 178 | class FloatTest(unittest.TestCase): |
110 | 179 | """c_float and c_double types.""" |
|
0 commit comments