|
4 | 4 |
|
5 | 5 | use KDuma\BinaryTools\BinaryString; |
6 | 6 | use KDuma\BinaryTools\BinaryWriter; |
| 7 | +use KDuma\BinaryTools\IntType; |
7 | 8 | use PHPUnit\Framework\Attributes\CoversClass; |
| 9 | +use PHPUnit\Framework\Attributes\DataProvider; |
8 | 10 | use PHPUnit\Framework\TestCase; |
9 | 11 |
|
10 | 12 | #[CoversClass(BinaryWriter::class)] |
@@ -63,7 +65,7 @@ public function testWriteUint16BE() |
63 | 65 | $this->writer->writeUint16BE(65535 + 1); |
64 | 66 | $this->fail("Expected exception not thrown"); |
65 | 67 | } catch (\InvalidArgumentException $exception) { |
66 | | - $this->assertEquals('Uint16 value must be between 0 and 65535', $exception->getMessage()); |
| 68 | + $this->assertEquals('Value 65536 is out of range for UINT16', $exception->getMessage()); |
67 | 69 | $this->assertEquals(0, $this->writer->getLength()); |
68 | 70 | } |
69 | 71 | } |
@@ -179,4 +181,111 @@ public function testWriteString() |
179 | 181 | $this->assertEquals(0, $this->writer->getLength()); |
180 | 182 | } |
181 | 183 | } |
| 184 | + |
| 185 | + public static function writeIntProvider(): iterable |
| 186 | + { |
| 187 | + yield 'uint8 zero' => [0, "\x00", IntType::UINT8]; |
| 188 | + yield 'uint8 max' => [255, "\xFF", IntType::UINT8]; |
| 189 | + yield 'int8 positive' => [127, "\x7F", IntType::INT8]; |
| 190 | + yield 'int8 negative' => [-1, "\xFF", IntType::INT8]; |
| 191 | + yield 'int8 min' => [-128, "\x80", IntType::INT8]; |
| 192 | + yield 'uint16 positive' => [1234, "\x04\xD2", IntType::UINT16]; |
| 193 | + yield 'uint16 little endian positive' => [1234, "\xD2\x04", IntType::UINT16_LE]; |
| 194 | + yield 'int16 positive' => [1234, "\x04\xD2", IntType::INT16]; |
| 195 | + yield 'int16 little endian positive' => [1234, "\xD2\x04", IntType::INT16_LE]; |
| 196 | + yield 'int16 negative' => [-1234, "\xFB\x2E", IntType::INT16]; |
| 197 | + yield 'int16 little endian negative' => [-1234, "\x2E\xFB", IntType::INT16_LE]; |
| 198 | + yield 'uint32 positive' => [0xDEADBEEF, "\xDE\xAD\xBE\xEF", IntType::UINT32]; |
| 199 | + yield 'uint32 little endian positive' => [0xDEADBEEF, "\xEF\xBE\xAD\xDE", IntType::UINT32_LE]; |
| 200 | + yield 'int32 positive' => [1234, "\x00\x00\x04\xD2", IntType::INT32]; |
| 201 | + yield 'int32 little endian positive' => [1234, "\xD2\x04\x00\x00", IntType::INT32_LE]; |
| 202 | + yield 'int32 negative' => [-1234, "\xFF\xFF\xFB\x2E", IntType::INT32]; |
| 203 | + yield 'int32 little endian negative' => [-1234, "\x2E\xFB\xFF\xFF", IntType::INT32_LE]; |
| 204 | + yield 'uint64 positive' => [0x0123456789ABCDEF, "\x01\x23\x45\x67\x89\xAB\xCD\xEF", IntType::UINT64]; |
| 205 | + yield 'uint64 little endian positive' => [0x0123456789ABCDEF, "\xEF\xCD\xAB\x89\x67\x45\x23\x01", IntType::UINT64_LE]; |
| 206 | + yield 'int64 positive' => [1234, "\x00\x00\x00\x00\x00\x00\x04\xD2", IntType::INT64]; |
| 207 | + yield 'int64 little endian positive' => [1234, "\xD2\x04\x00\x00\x00\x00\x00\x00", IntType::INT64_LE]; |
| 208 | + yield 'int64 negative' => [-1234, "\xFF\xFF\xFF\xFF\xFF\xFF\xFB\x2E", IntType::INT64]; |
| 209 | + yield 'int64 little endian negative' => [-1234, "\x2E\xFB\xFF\xFF\xFF\xFF\xFF\xFF", IntType::INT64_LE]; |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * @dataProvider writeIntProvider |
| 214 | + */ |
| 215 | + public function testWriteInt(int $value, string $expected, IntType $type): void |
| 216 | + { |
| 217 | + if (!$type->isSupported()) { |
| 218 | + $this->markTestSkipped(sprintf('IntType %s is not supported on this platform', $type->name)); |
| 219 | + } |
| 220 | + |
| 221 | + $this->writer->reset(); |
| 222 | + $this->writer->writeInt($type, $value); |
| 223 | + $this->assertEquals($expected, $this->writer->getBuffer()->toString()); |
| 224 | + } |
| 225 | + |
| 226 | + public function testWriteIntUnsupportedType(): void |
| 227 | + { |
| 228 | + // This test only runs on 32-bit systems where 64-bit integers are not supported |
| 229 | + if (PHP_INT_SIZE >= 8) { |
| 230 | + $this->markTestSkipped('64-bit integers are supported on this platform'); |
| 231 | + } |
| 232 | + |
| 233 | + $this->writer->reset(); |
| 234 | + |
| 235 | + $this->expectException(\RuntimeException::class); |
| 236 | + $this->expectExceptionMessage('Cannot write 8-byte integers on 4-byte platform'); |
| 237 | + |
| 238 | + $this->writer->writeInt(IntType::UINT64, 1234); |
| 239 | + } |
| 240 | + |
| 241 | + public function testWriteIntOutOfRange(): void |
| 242 | + { |
| 243 | + $this->writer->reset(); |
| 244 | + |
| 245 | + // Test uint8 overflow |
| 246 | + $this->expectException(\InvalidArgumentException::class); |
| 247 | + $this->expectExceptionMessage('Value 256 is out of range for UINT8'); |
| 248 | + |
| 249 | + $this->writer->writeInt(IntType::UINT8, 256); |
| 250 | + } |
| 251 | + |
| 252 | + public function testWriteIntNegativeUnsigned(): void |
| 253 | + { |
| 254 | + $this->writer->reset(); |
| 255 | + |
| 256 | + // Test negative value for unsigned type |
| 257 | + $this->expectException(\InvalidArgumentException::class); |
| 258 | + $this->expectExceptionMessage('Value -1 is out of range for UINT8'); |
| 259 | + |
| 260 | + $this->writer->writeInt(IntType::UINT8, -1); |
| 261 | + } |
| 262 | + |
| 263 | + public function testWriteIntSignedOverflow(): void |
| 264 | + { |
| 265 | + $this->writer->reset(); |
| 266 | + |
| 267 | + // Test int8 overflow |
| 268 | + $this->expectException(\InvalidArgumentException::class); |
| 269 | + $this->expectExceptionMessage('Value 128 is out of range for INT8'); |
| 270 | + |
| 271 | + $this->writer->writeInt(IntType::INT8, 128); |
| 272 | + } |
| 273 | + |
| 274 | + public function testWriteIntSignedUnderflow(): void |
| 275 | + { |
| 276 | + $this->writer->reset(); |
| 277 | + |
| 278 | + // Test int8 underflow |
| 279 | + $this->expectException(\InvalidArgumentException::class); |
| 280 | + $this->expectExceptionMessage('Value -129 is out of range for INT8'); |
| 281 | + |
| 282 | + $this->writer->writeInt(IntType::INT8, -129); |
| 283 | + } |
| 284 | + |
| 285 | + public function testWriteUint16BEDeprecated(): void |
| 286 | + { |
| 287 | + $this->writer->reset(); |
| 288 | + $this->writer->writeUint16BE(1234); |
| 289 | + $this->assertEquals("\x04\xD2", $this->writer->getBuffer()->toString()); |
| 290 | + } |
182 | 291 | } |
0 commit comments