| 1 |
[case testLibrtStrings_librt] |
| 2 |
from typing import Any |
| 3 |
import base64 |
| 4 |
import binascii |
| 5 |
import random |
| 6 |
import struct |
| 7 |
|
| 8 |
from librt.strings import BytesWriter, StringWriter, write_i16_le, write_i16_be, read_i16_le, read_i16_be, write_i32_le, write_i32_be, read_i32_le, read_i32_be, write_i64_le, write_i64_be, read_i64_le, read_i64_be, write_f64_le, read_f64_le, write_f64_be, read_f64_be, write_f32_le, write_f32_be, read_f32_le, read_f32_be |
| 9 |
|
| 10 |
from testutil import assertRaises |
| 11 |
|
| 12 |
# Test values for i16 write/read operations, organized by byte size |
| 13 |
I16_TEST_VALUES: list[int] = [ |
| 14 |
# 1-byte values (0 to 127, and -128 to -1 in terms of low byte pattern) |
| 15 |
0, 1, 127, -1, -113, -128, # -113 is mypyc overlapping error value |
| 16 |
# 2-byte values (128 to 32767, -32768 to -129) |
| 17 |
128, 255, 256, 0x1234, 32767, -129, -256, -32768, |
| 18 |
] |
| 19 |
|
| 20 |
# Test values for i32 write/read operations: i16 test values plus 3-4 byte values |
| 21 |
I32_TEST_VALUES: list[int] = I16_TEST_VALUES + [ |
| 22 |
# 3-byte values (32768 to 8388607, -8388608 to -32769) |
| 23 |
32768, 65535, 65536, 8388607, -32769, -65536, -8388608, |
| 24 |
# 4-byte values (8388608 to 2147483647, -2147483648 to -8388609) |
| 25 |
8388608, 16777215, 16777216, 2147483647, -8388609, -16777216, -2147483648, |
| 26 |
] |
| 27 |
|
| 28 |
# Test values for i64 write/read operations: i32 test values plus 5-8 byte values |
| 29 |
I64_TEST_VALUES: list[int] = I32_TEST_VALUES + [ |
| 30 |
# 5-8 byte values (beyond i32 range) |
| 31 |
2147483648, # 2^31, just above i32 max |
| 32 |
4294967295, # 2^32 - 1 |
| 33 |
4294967296, # 2^32 |
| 34 |
1099511627775, # 2^40 - 1 |
| 35 |
281474976710655, # 2^48 - 1 |
| 36 |
72057594037927935, # 2^56 - 1 |
| 37 |
9223372036854775807, # max i64 |
| 38 |
-2147483649, # just below i32 min |
| 39 |
-4294967296, # -2^32 |
| 40 |
-9223372036854775808, # min i64 |
| 41 |
] |
| 42 |
|
| 43 |
# Test values for f64 write/read operations |
| 44 |
F64_TEST_VALUES: list[float] = [ |
| 45 |
0.0, -0.0, 1.0, -1.0, |
| 46 |
-113.0, # mypyc overlapping error value |
| 47 |
0.5, -0.5, 0.1, -0.1, |
| 48 |
1.5, 255.0, 256.0, |
| 49 |
1e10, -1e10, 1e100, -1e100, |
| 50 |
1e-10, 1e-100, |
| 51 |
1.7976931348623157e+308, # max float |
| 52 |
-1.7976931348623157e+308, # min float |
| 53 |
2.2250738585072014e-308, # min positive normal |
| 54 |
5e-324, # min positive subnormal |
| 55 |
float('inf'), float('-inf'), |
| 56 |
float('nan'), |
| 57 |
] |
| 58 |
|
| 59 |
# Test values for f32 write/read operations (values that fit in 32-bit float) |
| 60 |
F32_TEST_VALUES: list[float] = [ |
| 61 |
0.0, -0.0, 1.0, -1.0, |
| 62 |
-113.0, # mypyc overlapping error value |
| 63 |
0.5, -0.5, 0.1, -0.1, |
| 64 |
1.5, 255.0, 256.0, |
| 65 |
1e10, -1e10, |
| 66 |
1e-10, |
| 67 |
3.4028235e+38, # max float32 |
| 68 |
-3.4028235e+38, # min float32 |
| 69 |
1.1754944e-38, # min positive normal float32 |
| 70 |
float('inf'), float('-inf'), |
| 71 |
float('nan'), |
| 72 |
] |
| 73 |
|
| 74 |
def test_bytes_writer_basics() -> None: |
| 75 |
w = BytesWriter() |
| 76 |
assert w.getvalue() == b"" |
| 77 |
assert len(w) == 0 |
| 78 |
assert repr(w) == "BytesWriter(b'')" |
| 79 |
|
| 80 |
w = BytesWriter() |
| 81 |
w.append(ord('a')) |
| 82 |
w.write(b'bc') |
| 83 |
assert w.getvalue() == b"abc" |
| 84 |
assert repr(w) == "BytesWriter(b'abc')" |
| 85 |
|
| 86 |
def test_bytes_writer_get_item() -> None: |
| 87 |
w = BytesWriter() |
| 88 |
w.write(b"foobar") |
| 89 |
assert w[0] == ord("f") |
| 90 |
assert w[5] == ord("r") |
| 91 |
assert w[-1] == ord("r") |
| 92 |
assert w[-2] == ord("a") |
| 93 |
assert w[-6] == ord("f") |
| 94 |
|
| 95 |
with assertRaises(IndexError): |
| 96 |
w[6] |
| 97 |
with assertRaises(IndexError): |
| 98 |
w[-7] |
| 99 |
with assertRaises(IndexError): |
| 100 |
w[1 << 50] |
| 101 |
with assertRaises(IndexError): |
| 102 |
w[-(1 << 50)] |
| 103 |
|
| 104 |
def test_bytes_writer_set_item() -> None: |
| 105 |
w = BytesWriter() |
| 106 |
w.write(b"foobar") |
| 107 |
w[0] = 255 |
| 108 |
assert w[0] == 255 |
| 109 |
w[5] = 0 |
| 110 |
assert w[5] == 0 |
| 111 |
assert w.getvalue() == b"\xffooba\x00" |
| 112 |
|
| 113 |
with assertRaises(IndexError): |
| 114 |
w[6] = 0 |
| 115 |
with assertRaises(IndexError): |
| 116 |
w[-7] = 0 |
| 117 |
with assertRaises(IndexError): |
| 118 |
w[1 << 50] = 0 |
| 119 |
with assertRaises(IndexError): |
| 120 |
w[-(1 << 50)] = 0 |
| 121 |
|
| 122 |
with assertRaises(ValueError): |
| 123 |
w[0] = int() - 1 |
| 124 |
with assertRaises(ValueError): |
| 125 |
w[0] = int() + 256 |
| 126 |
|
| 127 |
# Grow BytesWriter |
| 128 |
w.write(b"xy" * 512) |
| 129 |
w[1024 + 5] = 66 |
| 130 |
assert w[1024 + 5] == 66 |
| 131 |
assert w[0] == 255 |
| 132 |
|
| 133 |
def test_bytes_writer_append_grow() -> None: |
| 134 |
w = BytesWriter() |
| 135 |
for i in range(16384): |
| 136 |
w.append((i ^ (i >> 8)) & 255) |
| 137 |
assert len(w) == i + 1 |
| 138 |
b = w.getvalue() |
| 139 |
for i in range(16384): |
| 140 |
assert b[i] == (i ^ (i >> 8)) & 255 |
| 141 |
|
| 142 |
def test_bytes_writer_write_grow() -> None: |
| 143 |
w = BytesWriter() |
| 144 |
for i in range(16384): |
| 145 |
w.write(bytes([(i ^ (i >> 8)) & 255])) |
| 146 |
b = w.getvalue() |
| 147 |
for i in range(16384): |
| 148 |
assert b[i] == (i ^ (i >> 8)) & 255 |
| 149 |
assert b[i] == w[i] |
| 150 |
|
| 151 |
w = BytesWriter() |
| 152 |
a = [] |
| 153 |
for i in range(16384): |
| 154 |
w.write(bytes()) |
| 155 |
if i & 1 == 0: |
| 156 |
segment = b"foobarz" |
| 157 |
else: |
| 158 |
segment = b"\x7f\x00ab!" |
| 159 |
w.write(segment) |
| 160 |
a.append(segment) |
| 161 |
assert w.getvalue() == b"".join(a) |
| 162 |
|
| 163 |
def test_bytes_writer_truncate() -> None: |
| 164 |
b = BytesWriter() |
| 165 |
b.write(b"foobar") |
| 166 |
b.truncate(6) # No-op |
| 167 |
assert len(b) == 6 |
| 168 |
assert b.getvalue() == b"foobar" |
| 169 |
b.truncate(5) |
| 170 |
assert len(b) == 5 |
| 171 |
assert b.getvalue() == b"fooba" |
| 172 |
b.truncate(0) |
| 173 |
assert len(b) == 0 |
| 174 |
assert b.getvalue() == b"" |
| 175 |
|
| 176 |
with assertRaises(ValueError): |
| 177 |
b.truncate(1) |
| 178 |
|
| 179 |
b = BytesWriter() |
| 180 |
b.write(b"foobar") |
| 181 |
with assertRaises(ValueError): |
| 182 |
b.truncate(-1) |
| 183 |
|
| 184 |
def test_bytes_writer_write_i16_le() -> None: |
| 185 |
# Test various i16 values from 1-byte to 2-byte range |
| 186 |
w = BytesWriter() |
| 187 |
for v in I16_TEST_VALUES: |
| 188 |
write_i16_le(w, v) |
| 189 |
assert w.getvalue() == b"".join(struct.pack("<h", v) for v in I16_TEST_VALUES) |
| 190 |
|
| 191 |
# Test mixing with other operations and buffer growth |
| 192 |
w = BytesWriter() |
| 193 |
w.append(0xFF) |
| 194 |
for i in range(1000): |
| 195 |
write_i16_le(w, i) |
| 196 |
w.append(0xEE) |
| 197 |
result = w.getvalue() |
| 198 |
assert len(result) == 2002 |
| 199 |
assert result[0] == 0xFF |
| 200 |
assert result[-1] == 0xEE |
| 201 |
# Check a few values in the middle |
| 202 |
assert result[1:3] == b"\x00\x00" # i=0 |
| 203 |
assert result[3:5] == b"\x01\x00" # i=1 |
| 204 |
assert result[1999:2001] == b"\xe7\x03" # i=999 |
| 205 |
|
| 206 |
def test_write_i16_via_any() -> None: |
| 207 |
# Test write_i16_le/be via Any to ensure C extension wrapper works |
| 208 |
# (tests fallback path when not using mypyc primitives) |
| 209 |
for write_func, fmt in zip((write_i16_le, write_i16_be), ("<h", ">h")): |
| 210 |
w: Any = BytesWriter() |
| 211 |
|
| 212 |
# Test 8-bit and 16-bit operations |
| 213 |
w.append(0x42) |
| 214 |
write_func(w, 0x1234) |
| 215 |
w.append(0xFF) |
| 216 |
assert w.getvalue() == b"\x42" + struct.pack(fmt, 0x1234) + b"\xFF" |
| 217 |
|
| 218 |
# Test buffer growth |
| 219 |
w2: Any = BytesWriter() |
| 220 |
for i in range(150): |
| 221 |
write_func(w2, i) |
| 222 |
result = w2.getvalue() |
| 223 |
assert len(result) == 300 |
| 224 |
assert result[0:2] == struct.pack(fmt, 0) # i=0 |
| 225 |
assert result[2:4] == struct.pack(fmt, 1) # i=1 |
| 226 |
assert result[298:300] == struct.pack(fmt, 149) # i=149 |
| 227 |
|
| 228 |
# Test values that don't fit in i16 |
| 229 |
w3: Any = BytesWriter() |
| 230 |
with assertRaises(ValueError, "int too large to convert to i16"): |
| 231 |
write_func(w3, 32768 + int()) |
| 232 |
with assertRaises(ValueError, "int too large to convert to i16"): |
| 233 |
write_func(w3, -32769 + int()) |
| 234 |
with assertRaises(ValueError, "int too large to convert to i16"): |
| 235 |
write_func(w3, 100000 + int()) |
| 236 |
|
| 237 |
def test_bytes_reader_read_i16_le() -> None: |
| 238 |
# Test various i16 values from 1-byte to 2-byte range |
| 239 |
data = b"".join(struct.pack("<h", v) for v in I16_TEST_VALUES) |
| 240 |
for i, v in enumerate(I16_TEST_VALUES): |
| 241 |
assert read_i16_le(data, i * 2) == v |
| 242 |
|
| 243 |
# Test round-trip with write_i16_le |
| 244 |
w = BytesWriter() |
| 245 |
for v in I16_TEST_VALUES: |
| 246 |
write_i16_le(w, v) |
| 247 |
result = w.getvalue() |
| 248 |
for i, v in enumerate(I16_TEST_VALUES): |
| 249 |
assert read_i16_le(result, i * 2) == v |
| 250 |
|
| 251 |
# Test unaligned offset |
| 252 |
data2 = b"\xFF" + struct.pack("<h", 0x1234) + b"\xFF" |
| 253 |
assert read_i16_le(data2, 1) == 0x1234 |
| 254 |
|
| 255 |
# Test barely out of bounds (only 1 byte available, need 2) |
| 256 |
with assertRaises(IndexError): |
| 257 |
read_i16_le(data, len(data) - 1) |
| 258 |
|
| 259 |
def test_bytes_reader_read_i16_be() -> None: |
| 260 |
# Test various i16 values from 1-byte to 2-byte range |
| 261 |
data = b"".join(struct.pack(">h", v) for v in I16_TEST_VALUES) |
| 262 |
for i, v in enumerate(I16_TEST_VALUES): |
| 263 |
assert read_i16_be(data, i * 2) == v |
| 264 |
|
| 265 |
# Test round-trip with write_i16_be |
| 266 |
w = BytesWriter() |
| 267 |
for v in I16_TEST_VALUES: |
| 268 |
write_i16_be(w, v) |
| 269 |
result = w.getvalue() |
| 270 |
for i, v in enumerate(I16_TEST_VALUES): |
| 271 |
assert read_i16_be(result, i * 2) == v |
| 272 |
|
| 273 |
# Test unaligned offset |
| 274 |
data2 = b"\xFF" + struct.pack(">h", 0x1234) + b"\xFF" |
| 275 |
assert read_i16_be(data2, 1) == 0x1234 |
| 276 |
|
| 277 |
# Test barely out of bounds (only 1 byte available, need 2) |
| 278 |
with assertRaises(IndexError): |
| 279 |
read_i16_be(data, len(data) - 1) |
| 280 |
|
| 281 |
def test_read_i16_via_any() -> None: |
| 282 |
# Test read_i16_le/be via Any to ensure C extension wrapper works |
| 283 |
for read_func, fmt in zip((read_i16_le, read_i16_be), ("<h", ">h")): |
| 284 |
data: Any = struct.pack(fmt, 0x1234) + struct.pack(fmt, -1) + struct.pack(fmt, 0) |
| 285 |
assert read_func(data, 0) == 0x1234 |
| 286 |
assert read_func(data, 2) == -1 |
| 287 |
assert read_func(data, 4) == 0 |
| 288 |
|
| 289 |
# Test error cases |
| 290 |
# Index out of range |
| 291 |
with assertRaises(IndexError, "index 10 out of range for bytes of length 6"): |
| 292 |
read_func(data, 10 + int()) |
| 293 |
with assertRaises(IndexError, "index 5 out of range for bytes of length 6"): |
| 294 |
read_func(data, 5 + int()) # Not enough bytes for i16 |
| 295 |
|
| 296 |
# Negative index |
| 297 |
with assertRaises(ValueError, "index must be non-negative"): |
| 298 |
read_func(data, -1 + int()) |
| 299 |
|
| 300 |
# Wrong type for bytes argument |
| 301 |
with assertRaises(TypeError): |
| 302 |
bad: Any = "not bytes" |
| 303 |
read_func(bad, 0 + int()) |
| 304 |
with assertRaises(TypeError): |
| 305 |
bad2: Any = bytearray(b"\x00\x00") |
| 306 |
read_func(bad2, 0 + int()) |
| 307 |
|
| 308 |
def test_bytes_writer_write_i16_be() -> None: |
| 309 |
# Test various i16 values from 1-byte to 2-byte range |
| 310 |
w = BytesWriter() |
| 311 |
for v in I16_TEST_VALUES: |
| 312 |
write_i16_be(w, v) |
| 313 |
assert w.getvalue() == b"".join(struct.pack(">h", v) for v in I16_TEST_VALUES) |
| 314 |
|
| 315 |
# Test mixing with other operations and buffer growth |
| 316 |
w = BytesWriter() |
| 317 |
w.append(0xFF) |
| 318 |
for i in range(1000): |
| 319 |
write_i16_be(w, i) |
| 320 |
w.append(0xEE) |
| 321 |
result = w.getvalue() |
| 322 |
assert len(result) == 2002 |
| 323 |
assert result[0] == 0xFF |
| 324 |
assert result[-1] == 0xEE |
| 325 |
# Check a few values in the middle |
| 326 |
assert result[1:3] == b"\x00\x00" # i=0 |
| 327 |
assert result[3:5] == b"\x00\x01" # i=1 |
| 328 |
assert result[1999:2001] == b"\x03\xe7" # i=999 |
| 329 |
|
| 330 |
def test_bytes_writer_write_i32_le() -> None: |
| 331 |
# Test various i32 values from 1-byte to 4-byte range |
| 332 |
w = BytesWriter() |
| 333 |
for v in I32_TEST_VALUES: |
| 334 |
write_i32_le(w, v) |
| 335 |
assert w.getvalue() == b"".join(struct.pack("<i", v) for v in I32_TEST_VALUES) |
| 336 |
|
| 337 |
# Test mixing with other operations and buffer growth |
| 338 |
w = BytesWriter() |
| 339 |
w.append(0xFF) |
| 340 |
for i in range(500): |
| 341 |
write_i32_le(w, i * 1000) |
| 342 |
w.append(0xEE) |
| 343 |
result = w.getvalue() |
| 344 |
assert len(result) == 2002 |
| 345 |
assert result[0] == 0xFF |
| 346 |
assert result[-1] == 0xEE |
| 347 |
# Check a few values in the middle |
| 348 |
assert result[1:5] == struct.pack("<i", 0) |
| 349 |
assert result[5:9] == struct.pack("<i", 1000) |
| 350 |
assert result[1997:2001] == struct.pack("<i", 499000) |
| 351 |
|
| 352 |
def test_bytes_writer_write_i32_be() -> None: |
| 353 |
# Test various i32 values from 1-byte to 4-byte range |
| 354 |
w = BytesWriter() |
| 355 |
for v in I32_TEST_VALUES: |
| 356 |
write_i32_be(w, v) |
| 357 |
assert w.getvalue() == b"".join(struct.pack(">i", v) for v in I32_TEST_VALUES) |
| 358 |
|
| 359 |
# Test mixing with other operations and buffer growth |
| 360 |
w = BytesWriter() |
| 361 |
w.append(0xFF) |
| 362 |
for i in range(500): |
| 363 |
write_i32_be(w, i * 1000) |
| 364 |
w.append(0xEE) |
| 365 |
result = w.getvalue() |
| 366 |
assert len(result) == 2002 |
| 367 |
assert result[0] == 0xFF |
| 368 |
assert result[-1] == 0xEE |
| 369 |
# Check a few values in the middle |
| 370 |
assert result[1:5] == struct.pack(">i", 0) |
| 371 |
assert result[5:9] == struct.pack(">i", 1000) |
| 372 |
assert result[1997:2001] == struct.pack(">i", 499000) |
| 373 |
|
| 374 |
def test_write_i32_via_any() -> None: |
| 375 |
# Test write_i32_le/be via Any to ensure C extension wrapper works |
| 376 |
# (tests fallback path when not using mypyc primitives) |
| 377 |
for write_func, fmt in zip((write_i32_le, write_i32_be), ("<i", ">i")): |
| 378 |
w: Any = BytesWriter() |
| 379 |
|
| 380 |
# Test 8-bit and 32-bit operations |
| 381 |
w.append(0x42) |
| 382 |
write_func(w, 0x12345678) |
| 383 |
w.append(0xFF) |
| 384 |
assert w.getvalue() == b"\x42" + struct.pack(fmt, 0x12345678) + b"\xFF" |
| 385 |
|
| 386 |
# Test buffer growth |
| 387 |
w2: Any = BytesWriter() |
| 388 |
for i in range(100): |
| 389 |
write_func(w2, i * 10000) |
| 390 |
result = w2.getvalue() |
| 391 |
assert len(result) == 400 |
| 392 |
assert result[0:4] == struct.pack(fmt, 0) |
| 393 |
assert result[4:8] == struct.pack(fmt, 10000) |
| 394 |
assert result[396:400] == struct.pack(fmt, 990000) |
| 395 |
|
| 396 |
# Test values that don't fit in i32 |
| 397 |
w3: Any = BytesWriter() |
| 398 |
with assertRaises(ValueError, "int too large to convert to i32"): |
| 399 |
write_func(w3, 2147483648 + int()) |
| 400 |
with assertRaises(ValueError, "int too large to convert to i32"): |
| 401 |
write_func(w3, -2147483649 + int()) |
| 402 |
with assertRaises(ValueError, "int too large to convert to i32"): |
| 403 |
write_func(w3, 10000000000 + int()) |
| 404 |
|
| 405 |
def test_bytes_reader_read_i32_le() -> None: |
| 406 |
# Test various i32 values from 1-byte to 4-byte range |
| 407 |
data = b"".join(struct.pack("<i", v) for v in I32_TEST_VALUES) |
| 408 |
for i, v in enumerate(I32_TEST_VALUES): |
| 409 |
assert read_i32_le(data, i * 4) == v |
| 410 |
|
| 411 |
# Test round-trip with write_i32_le |
| 412 |
w = BytesWriter() |
| 413 |
for v in I32_TEST_VALUES: |
| 414 |
write_i32_le(w, v) |
| 415 |
result = w.getvalue() |
| 416 |
for i, v in enumerate(I32_TEST_VALUES): |
| 417 |
assert read_i32_le(result, i * 4) == v |
| 418 |
|
| 419 |
# Test unaligned offset |
| 420 |
data2 = b"\xFF" + struct.pack("<i", 0x12345678) + b"\xFF" |
| 421 |
assert read_i32_le(data2, 1) == 0x12345678 |
| 422 |
|
| 423 |
# Test barely out of bounds (only 3 bytes available, need 4) |
| 424 |
with assertRaises(IndexError): |
| 425 |
read_i32_le(data, len(data) - 3) |
| 426 |
|
| 427 |
def test_bytes_reader_read_i32_be() -> None: |
| 428 |
# Test various i32 values from 1-byte to 4-byte range |
| 429 |
data = b"".join(struct.pack(">i", v) for v in I32_TEST_VALUES) |
| 430 |
for i, v in enumerate(I32_TEST_VALUES): |
| 431 |
assert read_i32_be(data, i * 4) == v |
| 432 |
|
| 433 |
# Test round-trip with write_i32_be |
| 434 |
w = BytesWriter() |
| 435 |
for v in I32_TEST_VALUES: |
| 436 |
write_i32_be(w, v) |
| 437 |
result = w.getvalue() |
| 438 |
for i, v in enumerate(I32_TEST_VALUES): |
| 439 |
assert read_i32_be(result, i * 4) == v |
| 440 |
|
| 441 |
# Test unaligned offset |
| 442 |
data2 = b"\xFF" + struct.pack(">i", 0x12345678) + b"\xFF" |
| 443 |
assert read_i32_be(data2, 1) == 0x12345678 |
| 444 |
|
| 445 |
# Test barely out of bounds (only 3 bytes available, need 4) |
| 446 |
with assertRaises(IndexError): |
| 447 |
read_i32_be(data, len(data) - 3) |
| 448 |
|
| 449 |
def test_read_i32_via_any() -> None: |
| 450 |
# Test read_i32_le/be via Any to ensure C extension wrapper works |
| 451 |
for read_func, fmt in zip((read_i32_le, read_i32_be), ("<i", ">i")): |
| 452 |
data: Any = struct.pack(fmt, 0x12345678) + struct.pack(fmt, -1) + struct.pack(fmt, 0) |
| 453 |
assert read_func(data, 0) == 0x12345678 |
| 454 |
assert read_func(data, 4) == -1 |
| 455 |
assert read_func(data, 8) == 0 |
| 456 |
|
| 457 |
# Test error cases |
| 458 |
# Index out of range |
| 459 |
with assertRaises(IndexError, "index 20 out of range for bytes of length 12"): |
| 460 |
read_func(data, 20 + int()) |
| 461 |
with assertRaises(IndexError, "index 9 out of range for bytes of length 12"): |
| 462 |
read_func(data, 9 + int()) # Not enough bytes for i32 |
| 463 |
|
| 464 |
# Negative index |
| 465 |
with assertRaises(ValueError, "index must be non-negative"): |
| 466 |
read_func(data, -1 + int()) |
| 467 |
|
| 468 |
# Wrong type for bytes argument |
| 469 |
with assertRaises(TypeError): |
| 470 |
bad: Any = "not bytes" |
| 471 |
read_func(bad, 0 + int()) |
| 472 |
with assertRaises(TypeError): |
| 473 |
bad2: Any = bytearray(b"\x00\x00\x00\x00") |
| 474 |
read_func(bad2, 0 + int()) |
| 475 |
|
| 476 |
def test_bytes_writer_write_i64_le() -> None: |
| 477 |
# Test all i64 values (includes all i32 values plus 5-8 byte values) |
| 478 |
w = BytesWriter() |
| 479 |
for v in I64_TEST_VALUES: |
| 480 |
write_i64_le(w, v) |
| 481 |
assert w.getvalue() == b"".join(struct.pack("<q", v) for v in I64_TEST_VALUES) |
| 482 |
|
| 483 |
# Test mixing with other operations and buffer growth |
| 484 |
w = BytesWriter() |
| 485 |
w.append(0xFF) |
| 486 |
for i in range(200): |
| 487 |
write_i64_le(w, i * 1000000000) |
| 488 |
w.append(0xEE) |
| 489 |
result = w.getvalue() |
| 490 |
assert len(result) == 1602 |
| 491 |
assert result[0] == 0xFF |
| 492 |
assert result[-1] == 0xEE |
| 493 |
assert result[1:9] == struct.pack("<q", 0) |
| 494 |
assert result[9:17] == struct.pack("<q", 1000000000) |
| 495 |
assert result[1593:1601] == struct.pack("<q", 199000000000) |
| 496 |
|
| 497 |
def test_bytes_writer_write_i64_be() -> None: |
| 498 |
# Test all i64 values (includes all i32 values plus 5-8 byte values) |
| 499 |
w = BytesWriter() |
| 500 |
for v in I64_TEST_VALUES: |
| 501 |
write_i64_be(w, v) |
| 502 |
assert w.getvalue() == b"".join(struct.pack(">q", v) for v in I64_TEST_VALUES) |
| 503 |
|
| 504 |
# Test mixing with other operations and buffer growth |
| 505 |
w = BytesWriter() |
| 506 |
w.append(0xFF) |
| 507 |
for i in range(200): |
| 508 |
write_i64_be(w, i * 1000000000) |
| 509 |
w.append(0xEE) |
| 510 |
result = w.getvalue() |
| 511 |
assert len(result) == 1602 |
| 512 |
assert result[0] == 0xFF |
| 513 |
assert result[-1] == 0xEE |
| 514 |
assert result[1:9] == struct.pack(">q", 0) |
| 515 |
assert result[9:17] == struct.pack(">q", 1000000000) |
| 516 |
assert result[1593:1601] == struct.pack(">q", 199000000000) |
| 517 |
|
| 518 |
def test_write_i64_via_any() -> None: |
| 519 |
# Test write_i64_le/be via Any to ensure C extension wrapper works |
| 520 |
for write_func, fmt in zip((write_i64_le, write_i64_be), ("<q", ">q")): |
| 521 |
w: Any = BytesWriter() |
| 522 |
|
| 523 |
w.append(0x42) |
| 524 |
write_func(w, 0x123456789ABCDEF0) |
| 525 |
w.append(0xFF) |
| 526 |
assert w.getvalue() == b"\x42" + struct.pack(fmt, 0x123456789ABCDEF0) + b"\xFF" |
| 527 |
|
| 528 |
# Test buffer growth |
| 529 |
w2: Any = BytesWriter() |
| 530 |
for i in range(50): |
| 531 |
write_func(w2, i * 10000000000) |
| 532 |
result = w2.getvalue() |
| 533 |
assert len(result) == 400 |
| 534 |
assert result[0:8] == struct.pack(fmt, 0) |
| 535 |
assert result[8:16] == struct.pack(fmt, 10000000000) |
| 536 |
assert result[392:400] == struct.pack(fmt, 490000000000) |
| 537 |
|
| 538 |
def test_bytes_reader_read_i64_le() -> None: |
| 539 |
# Test all i64 values (includes all i32 values plus 5-8 byte values) |
| 540 |
data = b"".join(struct.pack("<q", v) for v in I64_TEST_VALUES) |
| 541 |
for i, v in enumerate(I64_TEST_VALUES): |
| 542 |
assert read_i64_le(data, i * 8) == v |
| 543 |
|
| 544 |
# Test round-trip with write_i64_le |
| 545 |
w = BytesWriter() |
| 546 |
for v in I64_TEST_VALUES: |
| 547 |
write_i64_le(w, v) |
| 548 |
result = w.getvalue() |
| 549 |
for i, v in enumerate(I64_TEST_VALUES): |
| 550 |
assert read_i64_le(result, i * 8) == v |
| 551 |
|
| 552 |
# Test unaligned offset |
| 553 |
data2 = b"\xFF" + struct.pack("<q", 0x123456789ABCDEF0) + b"\xFF" |
| 554 |
assert read_i64_le(data2, 1) == 0x123456789ABCDEF0 |
| 555 |
|
| 556 |
# Test barely out of bounds (only 7 bytes available, need 8) |
| 557 |
with assertRaises(IndexError): |
| 558 |
read_i64_le(data, len(data) - 7) |
| 559 |
|
| 560 |
def test_bytes_reader_read_i64_be() -> None: |
| 561 |
# Test all i64 values (includes all i32 values plus 5-8 byte values) |
| 562 |
data = b"".join(struct.pack(">q", v) for v in I64_TEST_VALUES) |
| 563 |
for i, v in enumerate(I64_TEST_VALUES): |
| 564 |
assert read_i64_be(data, i * 8) == v |
| 565 |
|
| 566 |
# Test round-trip with write_i64_be |
| 567 |
w = BytesWriter() |
| 568 |
for v in I64_TEST_VALUES: |
| 569 |
write_i64_be(w, v) |
| 570 |
result = w.getvalue() |
| 571 |
for i, v in enumerate(I64_TEST_VALUES): |
| 572 |
assert read_i64_be(result, i * 8) == v |
| 573 |
|
| 574 |
# Test unaligned offset |
| 575 |
data2 = b"\xFF" + struct.pack(">q", 0x123456789ABCDEF0) + b"\xFF" |
| 576 |
assert read_i64_be(data2, 1) == 0x123456789ABCDEF0 |
| 577 |
|
| 578 |
# Test barely out of bounds (only 7 bytes available, need 8) |
| 579 |
with assertRaises(IndexError): |
| 580 |
read_i64_be(data, len(data) - 7) |
| 581 |
|
| 582 |
def test_read_i64_via_any() -> None: |
| 583 |
# Test read_i64_le/be via Any to ensure C extension wrapper works |
| 584 |
for read_func, fmt in zip((read_i64_le, read_i64_be), ("<q", ">q")): |
| 585 |
data: Any = struct.pack(fmt, 0x123456789ABCDEF0) + struct.pack(fmt, -1) + struct.pack(fmt, 0) |
| 586 |
assert read_func(data, 0) == 0x123456789ABCDEF0 |
| 587 |
assert read_func(data, 8) == -1 |
| 588 |
assert read_func(data, 16) == 0 |
| 589 |
|
| 590 |
# Test error cases |
| 591 |
# Index out of range |
| 592 |
with assertRaises(IndexError, "index 30 out of range for bytes of length 24"): |
| 593 |
read_func(data, 30 + int()) |
| 594 |
with assertRaises(IndexError, "index 17 out of range for bytes of length 24"): |
| 595 |
read_func(data, 17 + int()) # Not enough bytes for i64 |
| 596 |
|
| 597 |
# Negative index |
| 598 |
with assertRaises(ValueError, "index must be non-negative"): |
| 599 |
read_func(data, -1 + int()) |
| 600 |
|
| 601 |
# Wrong type for bytes argument |
| 602 |
with assertRaises(TypeError): |
| 603 |
bad: Any = "not bytes" |
| 604 |
read_func(bad, 0 + int()) |
| 605 |
with assertRaises(TypeError): |
| 606 |
bad2: Any = bytearray(b"\x00" * 8) |
| 607 |
read_func(bad2, 0 + int()) |
| 608 |
|
| 609 |
def test_bytes_writer_write_f64_le() -> None: |
| 610 |
# Test various f64 values |
| 611 |
w = BytesWriter() |
| 612 |
for v in F64_TEST_VALUES: |
| 613 |
write_f64_le(w, v) |
| 614 |
result = w.getvalue() |
| 615 |
expected = b"".join(struct.pack("<d", v) for v in F64_TEST_VALUES) |
| 616 |
# Compare byte-by-byte (NaN != NaN but bytes should match) |
| 617 |
assert result == expected |
| 618 |
|
| 619 |
# Test mixing with other operations and buffer growth |
| 620 |
w = BytesWriter() |
| 621 |
w.append(0xFF) |
| 622 |
for i in range(200): |
| 623 |
write_f64_le(w, float(i) * 1.5) |
| 624 |
w.append(0xEE) |
| 625 |
result = w.getvalue() |
| 626 |
assert len(result) == 1602 |
| 627 |
assert result[0] == 0xFF |
| 628 |
assert result[-1] == 0xEE |
| 629 |
assert result[1:9] == struct.pack("<d", 0.0) |
| 630 |
assert result[9:17] == struct.pack("<d", 1.5) |
| 631 |
assert result[1593:1601] == struct.pack("<d", 199.0 * 1.5) |
| 632 |
|
| 633 |
def test_bytes_writer_write_f64_be() -> None: |
| 634 |
# Test various f64 values |
| 635 |
w = BytesWriter() |
| 636 |
for v in F64_TEST_VALUES: |
| 637 |
write_f64_be(w, v) |
| 638 |
result = w.getvalue() |
| 639 |
expected = b"".join(struct.pack(">d", v) for v in F64_TEST_VALUES) |
| 640 |
# Compare byte-by-byte (NaN != NaN but bytes should match) |
| 641 |
assert result == expected |
| 642 |
|
| 643 |
# Test mixing with other operations and buffer growth |
| 644 |
w = BytesWriter() |
| 645 |
w.append(0xFF) |
| 646 |
for i in range(200): |
| 647 |
write_f64_be(w, float(i) * 1.5) |
| 648 |
w.append(0xEE) |
| 649 |
result = w.getvalue() |
| 650 |
assert len(result) == 1602 |
| 651 |
assert result[0] == 0xFF |
| 652 |
assert result[-1] == 0xEE |
| 653 |
assert result[1:9] == struct.pack(">d", 0.0) |
| 654 |
assert result[9:17] == struct.pack(">d", 1.5) |
| 655 |
assert result[1593:1601] == struct.pack(">d", 199.0 * 1.5) |
| 656 |
|
| 657 |
def test_bytes_reader_read_f64_le() -> None: |
| 658 |
import math |
| 659 |
# Test various f64 values |
| 660 |
data = b"".join(struct.pack("<d", v) for v in F64_TEST_VALUES) |
| 661 |
for i, v in enumerate(F64_TEST_VALUES): |
| 662 |
result = read_f64_le(data, i * 8) |
| 663 |
if math.isnan(v): |
| 664 |
assert math.isnan(result) |
| 665 |
else: |
| 666 |
assert result == v |
| 667 |
|
| 668 |
# Test round-trip with write_f64_le |
| 669 |
w = BytesWriter() |
| 670 |
for v in F64_TEST_VALUES: |
| 671 |
write_f64_le(w, v) |
| 672 |
result_bytes = w.getvalue() |
| 673 |
for i, v in enumerate(F64_TEST_VALUES): |
| 674 |
result = read_f64_le(result_bytes, i * 8) |
| 675 |
if math.isnan(v): |
| 676 |
assert math.isnan(result) |
| 677 |
else: |
| 678 |
assert result == v |
| 679 |
|
| 680 |
# Test unaligned offset |
| 681 |
data2 = b"\xFF" + struct.pack("<d", 1.5) + b"\xFF" |
| 682 |
assert read_f64_le(data2, 1) == 1.5 |
| 683 |
|
| 684 |
# Test barely out of bounds (only 7 bytes available, need 8) |
| 685 |
with assertRaises(IndexError): |
| 686 |
read_f64_le(data, len(data) - 7) |
| 687 |
|
| 688 |
def test_bytes_reader_read_f64_be() -> None: |
| 689 |
import math |
| 690 |
# Test various f64 values |
| 691 |
data = b"".join(struct.pack(">d", v) for v in F64_TEST_VALUES) |
| 692 |
for i, v in enumerate(F64_TEST_VALUES): |
| 693 |
result = read_f64_be(data, i * 8) |
| 694 |
if math.isnan(v): |
| 695 |
assert math.isnan(result) |
| 696 |
else: |
| 697 |
assert result == v |
| 698 |
|
| 699 |
# Test round-trip with write_f64_be |
| 700 |
w = BytesWriter() |
| 701 |
for v in F64_TEST_VALUES: |
| 702 |
write_f64_be(w, v) |
| 703 |
result_bytes = w.getvalue() |
| 704 |
for i, v in enumerate(F64_TEST_VALUES): |
| 705 |
result = read_f64_be(result_bytes, i * 8) |
| 706 |
if math.isnan(v): |
| 707 |
assert math.isnan(result) |
| 708 |
else: |
| 709 |
assert result == v |
| 710 |
|
| 711 |
# Test unaligned offset |
| 712 |
data2 = b"\xFF" + struct.pack(">d", 1.5) + b"\xFF" |
| 713 |
assert read_f64_be(data2, 1) == 1.5 |
| 714 |
|
| 715 |
# Test barely out of bounds (only 7 bytes available, need 8) |
| 716 |
with assertRaises(IndexError): |
| 717 |
read_f64_be(data, len(data) - 7) |
| 718 |
|
| 719 |
def test_read_f64_via_any() -> None: |
| 720 |
import math |
| 721 |
# Test read_f64_le/be via Any to ensure C extension wrapper works |
| 722 |
for read_func, fmt in zip((read_f64_le, read_f64_be), ("<d", ">d")): |
| 723 |
data: Any = struct.pack(fmt, 1.5) + struct.pack(fmt, -113.0) + struct.pack(fmt, 0.0) |
| 724 |
assert read_func(data, 0) == 1.5 |
| 725 |
assert read_func(data, 8) == -113.0 |
| 726 |
assert read_func(data, 16) == 0.0 |
| 727 |
|
| 728 |
# Test special values |
| 729 |
data2: Any = struct.pack(fmt, float('inf')) + struct.pack(fmt, float('nan')) |
| 730 |
assert read_func(data2, 0) == float('inf') |
| 731 |
assert math.isnan(read_func(data2, 8)) |
| 732 |
|
| 733 |
# Test error cases |
| 734 |
# Index out of range |
| 735 |
with assertRaises(IndexError, "index 30 out of range for bytes of length 24"): |
| 736 |
read_func(data, 30 + int()) |
| 737 |
with assertRaises(IndexError, "index 17 out of range for bytes of length 24"): |
| 738 |
read_func(data, 17 + int()) # Not enough bytes for f64 |
| 739 |
|
| 740 |
# Negative index |
| 741 |
with assertRaises(ValueError, "index must be non-negative"): |
| 742 |
read_func(data, -1 + int()) |
| 743 |
|
| 744 |
# Wrong type for bytes argument |
| 745 |
with assertRaises(TypeError): |
| 746 |
bad: Any = "not bytes" |
| 747 |
read_func(bad, 0 + int()) |
| 748 |
with assertRaises(TypeError): |
| 749 |
bad2: Any = bytearray(b"\x00" * 8) |
| 750 |
read_func(bad2, 0 + int()) |
| 751 |
|
| 752 |
def test_write_f64_via_any() -> None: |
| 753 |
import math |
| 754 |
# Test write_f64_le/be via Any to ensure C extension wrapper works |
| 755 |
for write_func, fmt in zip((write_f64_le, write_f64_be), ("<d", ">d")): |
| 756 |
w: Any = BytesWriter() |
| 757 |
|
| 758 |
w.append(0x42) |
| 759 |
write_func(w, 1.5) |
| 760 |
w.append(0xFF) |
| 761 |
assert w.getvalue() == b"\x42" + struct.pack(fmt, 1.5) + b"\xFF" |
| 762 |
|
| 763 |
# Test buffer growth |
| 764 |
w2: Any = BytesWriter() |
| 765 |
for i in range(50): |
| 766 |
write_func(w2, float(i) * 0.25) |
| 767 |
result = w2.getvalue() |
| 768 |
assert len(result) == 400 |
| 769 |
assert result[0:8] == struct.pack(fmt, 0.0) |
| 770 |
assert result[8:16] == struct.pack(fmt, 0.25) |
| 771 |
assert result[392:400] == struct.pack(fmt, 49.0 * 0.25) |
| 772 |
|
| 773 |
# Test special values |
| 774 |
w3: Any = BytesWriter() |
| 775 |
write_func(w3, float('inf')) |
| 776 |
write_func(w3, float('-inf')) |
| 777 |
write_func(w3, float('nan')) |
| 778 |
result = w3.getvalue() |
| 779 |
assert result[0:8] == struct.pack(fmt, float('inf')) |
| 780 |
assert result[8:16] == struct.pack(fmt, float('-inf')) |
| 781 |
assert math.isnan(struct.unpack(fmt, result[16:24])[0]) |
| 782 |
|
| 783 |
# Test wrong type |
| 784 |
w4: Any = BytesWriter() |
| 785 |
with assertRaises(TypeError): |
| 786 |
bad: Any = "not a float" + str() |
| 787 |
write_func(w4, bad) |
| 788 |
|
| 789 |
def test_bytes_writer_write_f32_le() -> None: |
| 790 |
import struct as s |
| 791 |
# Test various f32 values |
| 792 |
w = BytesWriter() |
| 793 |
for v in F32_TEST_VALUES: |
| 794 |
write_f32_le(w, v) |
| 795 |
result = w.getvalue() |
| 796 |
expected = b"".join(s.pack("<f", v) for v in F32_TEST_VALUES) |
| 797 |
# Compare byte-by-byte (NaN != NaN but bytes should match) |
| 798 |
assert result == expected |
| 799 |
|
| 800 |
# Test mixing with other operations and buffer growth |
| 801 |
w = BytesWriter() |
| 802 |
w.append(0xFF) |
| 803 |
for i in range(200): |
| 804 |
write_f32_le(w, float(i) * 1.5) |
| 805 |
w.append(0xEE) |
| 806 |
result = w.getvalue() |
| 807 |
assert len(result) == 802 |
| 808 |
assert result[0] == 0xFF |
| 809 |
assert result[-1] == 0xEE |
| 810 |
assert result[1:5] == s.pack("<f", 0.0) |
| 811 |
assert result[5:9] == s.pack("<f", 1.5) |
| 812 |
assert result[797:801] == s.pack("<f", 199.0 * 1.5) |
| 813 |
|
| 814 |
def test_bytes_writer_write_f32_be() -> None: |
| 815 |
import struct as s |
| 816 |
# Test various f32 values |
| 817 |
w = BytesWriter() |
| 818 |
for v in F32_TEST_VALUES: |
| 819 |
write_f32_be(w, v) |
| 820 |
result = w.getvalue() |
| 821 |
expected = b"".join(s.pack(">f", v) for v in F32_TEST_VALUES) |
| 822 |
# Compare byte-by-byte (NaN != NaN but bytes should match) |
| 823 |
assert result == expected |
| 824 |
|
| 825 |
# Test mixing with other operations and buffer growth |
| 826 |
w = BytesWriter() |
| 827 |
w.append(0xFF) |
| 828 |
for i in range(200): |
| 829 |
write_f32_be(w, float(i) * 1.5) |
| 830 |
w.append(0xEE) |
| 831 |
result = w.getvalue() |
| 832 |
assert len(result) == 802 |
| 833 |
assert result[0] == 0xFF |
| 834 |
assert result[-1] == 0xEE |
| 835 |
assert result[1:5] == s.pack(">f", 0.0) |
| 836 |
assert result[5:9] == s.pack(">f", 1.5) |
| 837 |
assert result[797:801] == s.pack(">f", 199.0 * 1.5) |
| 838 |
|
| 839 |
def test_write_f32_via_any() -> None: |
| 840 |
import math |
| 841 |
# Test write_f32_le/be via Any to ensure C extension wrapper works |
| 842 |
for write_func, fmt in zip((write_f32_le, write_f32_be), ("<f", ">f")): |
| 843 |
w: Any = BytesWriter() |
| 844 |
|
| 845 |
w.append(0x42) |
| 846 |
write_func(w, 1.5) |
| 847 |
w.append(0xFF) |
| 848 |
assert w.getvalue() == b"\x42" + struct.pack(fmt, 1.5) + b"\xFF" |
| 849 |
|
| 850 |
# Test buffer growth |
| 851 |
w2: Any = BytesWriter() |
| 852 |
for i in range(100): |
| 853 |
write_func(w2, float(i) * 0.25) |
| 854 |
result = w2.getvalue() |
| 855 |
assert len(result) == 400 |
| 856 |
assert result[0:4] == struct.pack(fmt, 0.0) |
| 857 |
assert result[4:8] == struct.pack(fmt, 0.25) |
| 858 |
assert result[396:400] == struct.pack(fmt, 99.0 * 0.25) |
| 859 |
|
| 860 |
# Test special values |
| 861 |
w3: Any = BytesWriter() |
| 862 |
write_func(w3, float('inf')) |
| 863 |
write_func(w3, float('-inf')) |
| 864 |
write_func(w3, float('nan')) |
| 865 |
result = w3.getvalue() |
| 866 |
assert result[0:4] == struct.pack(fmt, float('inf')) |
| 867 |
assert result[4:8] == struct.pack(fmt, float('-inf')) |
| 868 |
assert math.isnan(struct.unpack(fmt, result[8:12])[0]) |
| 869 |
|
| 870 |
# Test wrong type |
| 871 |
w4: Any = BytesWriter() |
| 872 |
with assertRaises(TypeError): |
| 873 |
bad: Any = "not a float" + str() |
| 874 |
write_func(w4, bad) |
| 875 |
|
| 876 |
def test_bytes_reader_read_f32_le() -> None: |
| 877 |
import math |
| 878 |
import struct as s |
| 879 |
# Test various f32 values (compare against f32-rounded expected value, |
| 880 |
# since some f64 values like 0.1 lose precision when stored as f32) |
| 881 |
data = b"".join(s.pack("<f", v) for v in F32_TEST_VALUES) |
| 882 |
for i, v in enumerate(F32_TEST_VALUES): |
| 883 |
result = read_f32_le(data, i * 4) |
| 884 |
expected = s.unpack("<f", s.pack("<f", v))[0] |
| 885 |
if math.isnan(v): |
| 886 |
assert math.isnan(result) |
| 887 |
else: |
| 888 |
assert result == expected |
| 889 |
|
| 890 |
# Test round-trip with write_f32_le |
| 891 |
w = BytesWriter() |
| 892 |
for v in F32_TEST_VALUES: |
| 893 |
write_f32_le(w, v) |
| 894 |
result_bytes = w.getvalue() |
| 895 |
for i, v in enumerate(F32_TEST_VALUES): |
| 896 |
result = read_f32_le(result_bytes, i * 4) |
| 897 |
expected = s.unpack("<f", s.pack("<f", v))[0] |
| 898 |
if math.isnan(v): |
| 899 |
assert math.isnan(result) |
| 900 |
else: |
| 901 |
assert result == expected |
| 902 |
|
| 903 |
# Test unaligned offset |
| 904 |
data2 = b"\xFF" + s.pack("<f", 1.5) + b"\xFF" |
| 905 |
assert read_f32_le(data2, 1) == 1.5 |
| 906 |
|
| 907 |
# Test barely out of bounds (only 3 bytes available, need 4) |
| 908 |
with assertRaises(IndexError): |
| 909 |
read_f32_le(data, len(data) - 3) |
| 910 |
|
| 911 |
def test_bytes_reader_read_f32_be() -> None: |
| 912 |
import math |
| 913 |
import struct as s |
| 914 |
# Test various f32 values (compare against f32-rounded expected value, |
| 915 |
# since some f64 values like 0.1 lose precision when stored as f32) |
| 916 |
data = b"".join(s.pack(">f", v) for v in F32_TEST_VALUES) |
| 917 |
for i, v in enumerate(F32_TEST_VALUES): |
| 918 |
result = read_f32_be(data, i * 4) |
| 919 |
expected = s.unpack(">f", s.pack(">f", v))[0] |
| 920 |
if math.isnan(v): |
| 921 |
assert math.isnan(result) |
| 922 |
else: |
| 923 |
assert result == expected |
| 924 |
|
| 925 |
# Test round-trip with write_f32_be |
| 926 |
w = BytesWriter() |
| 927 |
for v in F32_TEST_VALUES: |
| 928 |
write_f32_be(w, v) |
| 929 |
result_bytes = w.getvalue() |
| 930 |
for i, v in enumerate(F32_TEST_VALUES): |
| 931 |
result = read_f32_be(result_bytes, i * 4) |
| 932 |
expected = s.unpack(">f", s.pack(">f", v))[0] |
| 933 |
if math.isnan(v): |
| 934 |
assert math.isnan(result) |
| 935 |
else: |
| 936 |
assert result == expected |
| 937 |
|
| 938 |
# Test unaligned offset |
| 939 |
data2 = b"\xFF" + s.pack(">f", 1.5) + b"\xFF" |
| 940 |
assert read_f32_be(data2, 1) == 1.5 |
| 941 |
|
| 942 |
# Test barely out of bounds (only 3 bytes available, need 4) |
| 943 |
with assertRaises(IndexError): |
| 944 |
read_f32_be(data, len(data) - 3) |
| 945 |
|
| 946 |
def test_read_f32_via_any() -> None: |
| 947 |
import math |
| 948 |
# Test read_f32_le/be via Any to ensure C extension wrapper works |
| 949 |
for read_func, fmt in zip((read_f32_le, read_f32_be), ("<f", ">f")): |
| 950 |
data: Any = struct.pack(fmt, 1.5) + struct.pack(fmt, -113.0) + struct.pack(fmt, 0.0) |
| 951 |
assert read_func(data, 0) == 1.5 |
| 952 |
assert read_func(data, 4) == -113.0 |
| 953 |
assert read_func(data, 8) == 0.0 |
| 954 |
|
| 955 |
# Test special values |
| 956 |
data2: Any = struct.pack(fmt, float('inf')) + struct.pack(fmt, float('nan')) |
| 957 |
assert read_func(data2, 0) == float('inf') |
| 958 |
assert math.isnan(read_func(data2, 4)) |
| 959 |
|
| 960 |
# Test error cases |
| 961 |
# Index out of range |
| 962 |
with assertRaises(IndexError, "index 20 out of range for bytes of length 12"): |
| 963 |
read_func(data, 20 + int()) |
| 964 |
with assertRaises(IndexError, "index 9 out of range for bytes of length 12"): |
| 965 |
read_func(data, 9 + int()) # Not enough bytes for f32 |
| 966 |
|
| 967 |
# Negative index |
| 968 |
with assertRaises(ValueError, "index must be non-negative"): |
| 969 |
read_func(data, -1 + int()) |
| 970 |
|
| 971 |
# Wrong type for bytes argument |
| 972 |
with assertRaises(TypeError): |
| 973 |
bad: Any = "not bytes" |
| 974 |
read_func(bad, 0 + int()) |
| 975 |
with assertRaises(TypeError): |
| 976 |
bad2: Any = bytearray(b"\x00" * 4) |
| 977 |
read_func(bad2, 0 + int()) |
| 978 |
|
| 979 |
def test_write_bytearray() -> None: |
| 980 |
w = BytesWriter() |
| 981 |
w.write(bytearray(b"foobar")) |
| 982 |
w.write(bytearray(b"")) |
| 983 |
w.write(bytearray(b"\x00\xf8")) |
| 984 |
assert w.getvalue() == b"foobar\x00\xf8" |
| 985 |
|
| 986 |
def test_cast_bytes_writer() -> None: |
| 987 |
a: Any = BytesWriter() |
| 988 |
b: BytesWriter = a |
| 989 |
assert b.getvalue() == b"" |
| 990 |
|
| 991 |
a2: Any = "x" |
| 992 |
with assertRaises(TypeError): |
| 993 |
b = a2 |
| 994 |
|
| 995 |
def test_bytes_writer_wrapper_functions() -> None: |
| 996 |
cls: Any = BytesWriter |
| 997 |
b: Any = cls() |
| 998 |
assert repr(b) == "BytesWriter(b'')" |
| 999 |
assert len(b) == 0 |
| 1000 |
b.append(ord('a')) |
| 1001 |
b.append(0) |
| 1002 |
b.append(255) |
| 1003 |
b.write(b"foo") |
| 1004 |
b.write(bytearray(b"bar")) |
| 1005 |
assert b.getvalue() == b"a\x00\xfffoobar" |
| 1006 |
assert len(b) == 9 |
| 1007 |
|
| 1008 |
assert b[0] == ord('a') |
| 1009 |
assert b[8] == ord('r') |
| 1010 |
assert b[-1] == ord('r') |
| 1011 |
assert b[-9] == ord('a') |
| 1012 |
|
| 1013 |
b[0] = 215 |
| 1014 |
b[8] = 0 |
| 1015 |
assert b[0] == 215 |
| 1016 |
assert b[8] == 0 |
| 1017 |
b[-1] = 1 |
| 1018 |
assert b[8] == 1 |
| 1019 |
b[-9] = 2 |
| 1020 |
assert b[0] == 2 |
| 1021 |
|
| 1022 |
assert isinstance(b, cls) |
| 1023 |
|
| 1024 |
with assertRaises(TypeError): |
| 1025 |
b.append("x") |
| 1026 |
|
| 1027 |
with assertRaises(TypeError): |
| 1028 |
b.write("foo") |
| 1029 |
|
| 1030 |
with assertRaises(TypeError): |
| 1031 |
b.append(256) |
| 1032 |
|
| 1033 |
with assertRaises(TypeError): |
| 1034 |
b.append(-1) |
| 1035 |
|
| 1036 |
with assertRaises(IndexError): |
| 1037 |
b[9] |
| 1038 |
with assertRaises(IndexError): |
| 1039 |
b[-10] |
| 1040 |
with assertRaises(IndexError): |
| 1041 |
b[9] = 0 |
| 1042 |
with assertRaises(IndexError): |
| 1043 |
b[-10] = 0 |
| 1044 |
|
| 1045 |
with assertRaises(TypeError): |
| 1046 |
b[0] = -1 |
| 1047 |
with assertRaises(TypeError): |
| 1048 |
b[0] = 256 |
| 1049 |
|
| 1050 |
def test_string_writer_basics() -> None: |
| 1051 |
w = StringWriter() |
| 1052 |
assert w.getvalue() == "" |
| 1053 |
|
| 1054 |
def test_string_writer_repr() -> None: |
| 1055 |
# Kind 1 (ASCII) |
| 1056 |
w = StringWriter() |
| 1057 |
assert repr(w) == "StringWriter('')" |
| 1058 |
w.append(ord('h')) |
| 1059 |
w.append(ord('i')) |
| 1060 |
assert repr(w) == "StringWriter('hi')" |
| 1061 |
|
| 1062 |
# Kind 2 (UCS-2) |
| 1063 |
w2 = StringWriter() |
| 1064 |
w2.append(0x100) |
| 1065 |
w2.append(0x200) |
| 1066 |
assert repr(w2) == "StringWriter('" + chr(0x100) + chr(0x200) + "')" |
| 1067 |
|
| 1068 |
# Kind 4 (UCS-4) |
| 1069 |
w3 = StringWriter() |
| 1070 |
w3.append(0x10000) |
| 1071 |
expected = "StringWriter('" + chr(0x10000) + "')" |
| 1072 |
assert repr(w3) == expected |
| 1073 |
|
| 1074 |
def test_string_writer_repr_escaping() -> None: |
| 1075 |
# Kind 1: Test escaping of newline, nul, tab, backslash |
| 1076 |
w = StringWriter() |
| 1077 |
w.append(ord('a')) |
| 1078 |
w.append(ord('\n')) |
| 1079 |
w.append(0) |
| 1080 |
w.append(ord('\t')) |
| 1081 |
w.append(ord('\\')) |
| 1082 |
assert repr(w) == "StringWriter('a\\n\\x00\\t\\\\')" |
| 1083 |
|
| 1084 |
# Kind 2: escaping with UCS-2 |
| 1085 |
w2 = StringWriter() |
| 1086 |
w2.append(0x100) |
| 1087 |
w2.append(ord('\n')) |
| 1088 |
assert repr(w2) == "StringWriter('" + chr(0x100) + "\\n')" |
| 1089 |
|
| 1090 |
# Kind 4: escaping with UCS-4 |
| 1091 |
w3 = StringWriter() |
| 1092 |
w3.append(0x10000) |
| 1093 |
w3.append(0) |
| 1094 |
assert repr(w3) == "StringWriter('" + chr(0x10000) + "\\x00')" |
| 1095 |
|
| 1096 |
def test_string_writer_len() -> None: |
| 1097 |
# Kind 1 (ASCII) |
| 1098 |
w = StringWriter() |
| 1099 |
assert len(w) == 0 |
| 1100 |
w.append(ord('a')) |
| 1101 |
assert len(w) == 1 |
| 1102 |
w.append(ord('b')) |
| 1103 |
w.append(ord('c')) |
| 1104 |
assert len(w) == 3 |
| 1105 |
|
| 1106 |
# Kind 2 (UCS-2) |
| 1107 |
w2 = StringWriter() |
| 1108 |
w2.append(0x100) |
| 1109 |
assert len(w2) == 1 |
| 1110 |
for i in range(10): |
| 1111 |
w2.append(0x200 + i) |
| 1112 |
assert len(w2) == 11 |
| 1113 |
|
| 1114 |
# Kind 4 (UCS-4) |
| 1115 |
w3 = StringWriter() |
| 1116 |
w3.append(0x10000) |
| 1117 |
assert len(w3) == 1 |
| 1118 |
w3.append(0x10001) |
| 1119 |
w3.append(0x10002) |
| 1120 |
assert len(w3) == 3 |
| 1121 |
|
| 1122 |
# Test len after growing buffer |
| 1123 |
w4 = StringWriter() |
| 1124 |
for i in range(500): |
| 1125 |
w4.append(ord('X')) |
| 1126 |
assert len(w4) == 500 |
| 1127 |
|
| 1128 |
def test_string_writer_get_item() -> None: |
| 1129 |
# Kind 1 (ASCII) |
| 1130 |
w = StringWriter() |
| 1131 |
w.append(ord('f')) |
| 1132 |
w.append(ord('o')) |
| 1133 |
w.append(ord('o')) |
| 1134 |
assert w[0 + int()] == ord('f') |
| 1135 |
assert w[1 + int()] == ord('o') |
| 1136 |
assert w[2 + int()] == ord('o') |
| 1137 |
assert w[-1 + int()] == ord('o') |
| 1138 |
assert w[-2 + int()] == ord('o') |
| 1139 |
assert w[-3 + int()] == ord('f') |
| 1140 |
|
| 1141 |
with assertRaises(IndexError): |
| 1142 |
w[3 + int()] |
| 1143 |
with assertRaises(IndexError): |
| 1144 |
w[-4 + int()] |
| 1145 |
with assertRaises(IndexError): |
| 1146 |
w[1 << 50] |
| 1147 |
with assertRaises(IndexError): |
| 1148 |
w[-(1 << 50)] |
| 1149 |
|
| 1150 |
# Kind 2 (UCS-2) |
| 1151 |
w2 = StringWriter() |
| 1152 |
w2.append(0x100) |
| 1153 |
w2.append(0x200) |
| 1154 |
w2.append(0x300) |
| 1155 |
assert w2[0 + int()] == 0x100 |
| 1156 |
assert w2[1 + int()] == 0x200 |
| 1157 |
assert w2[2 + int()] == 0x300 |
| 1158 |
assert w2[-1 + int()] == 0x300 |
| 1159 |
assert w2[-2 + int()] == 0x200 |
| 1160 |
assert w2[-3 + int()] == 0x100 |
| 1161 |
|
| 1162 |
with assertRaises(IndexError): |
| 1163 |
w2[3 + int()] |
| 1164 |
with assertRaises(IndexError): |
| 1165 |
w2[-4 + int()] |
| 1166 |
|
| 1167 |
# Kind 4 (UCS-4) |
| 1168 |
w3 = StringWriter() |
| 1169 |
w3.append(0x10000) |
| 1170 |
w3.append(0x10001) |
| 1171 |
w3.append(0x10002) |
| 1172 |
assert w3[0 + int()] == 0x10000 |
| 1173 |
assert w3[1 + int()] == 0x10001 |
| 1174 |
assert w3[2 + int()] == 0x10002 |
| 1175 |
assert w3[-1 + int()] == 0x10002 |
| 1176 |
assert w3[-2 + int()] == 0x10001 |
| 1177 |
assert w3[-3 + int()] == 0x10000 |
| 1178 |
|
| 1179 |
with assertRaises(IndexError): |
| 1180 |
w3[3 + int()] |
| 1181 |
with assertRaises(IndexError): |
| 1182 |
w3[-4 + int()] |
| 1183 |
|
| 1184 |
# Test get_item after buffer growth |
| 1185 |
w4 = StringWriter() |
| 1186 |
for i in range(1000): |
| 1187 |
w4.append(ord('a') + (i % 26)) |
| 1188 |
assert w4[0 + int()] == ord('a') |
| 1189 |
assert w4[999 + int()] == ord('a') + (999 % 26) |
| 1190 |
assert w4[500 + int()] == ord('a') + (500 % 26) |
| 1191 |
assert w4[-1 + int()] == ord('a') + (999 % 26) |
| 1192 |
assert w4[-1000 + int()] == ord('a') |
| 1193 |
|
| 1194 |
def test_string_writer_append() -> None: |
| 1195 |
w = StringWriter() |
| 1196 |
w.append(ord('a')) |
| 1197 |
assert w.getvalue() == "a" |
| 1198 |
w.append(0xff) |
| 1199 |
assert w.getvalue() == "a\xff" |
| 1200 |
|
| 1201 |
# Switch kind 1->2 |
| 1202 |
w.append(0x100) |
| 1203 |
assert w.getvalue() == "a\xff\u0100", w.getvalue() |
| 1204 |
w.append(0xffff) |
| 1205 |
assert w.getvalue() == "a\xff\u0100\uffff" |
| 1206 |
|
| 1207 |
# Switch kind 2->4 |
| 1208 |
w.append(0x10000) |
| 1209 |
assert w.getvalue() == "a\xff\u0100\uffff" + chr(0x10000) |
| 1210 |
|
| 1211 |
# Maximum valid Unicode code point (0x10FFFF = 1114111) |
| 1212 |
w2 = StringWriter() |
| 1213 |
w2.append(0x10FFFF) |
| 1214 |
assert w2.getvalue() == chr(0x10FFFF) |
| 1215 |
|
| 1216 |
# Invalid code points |
| 1217 |
w3 = StringWriter() |
| 1218 |
with assertRaises(ValueError, "code point 1114112 is outside valid Unicode range (0-1114111)"): |
| 1219 |
w3.append(0x110000) |
| 1220 |
|
| 1221 |
w4 = StringWriter() |
| 1222 |
with assertRaises(ValueError, "code point -1 is outside valid Unicode range (0-1114111)"): |
| 1223 |
w4.append(-1) |
| 1224 |
|
| 1225 |
w5 = StringWriter() |
| 1226 |
with assertRaises(ValueError, "code point 2097152 is outside valid Unicode range (0-1114111)"): |
| 1227 |
w5.append(0x200000) |
| 1228 |
|
| 1229 |
def test_string_writer_write() -> None: |
| 1230 |
# Kind 1: Write ASCII strings |
| 1231 |
w = StringWriter() |
| 1232 |
w.write("hello") |
| 1233 |
assert w.getvalue() == "hello" |
| 1234 |
w.write(" world") |
| 1235 |
assert w.getvalue() == "hello world" |
| 1236 |
|
| 1237 |
# Write empty string |
| 1238 |
w.write("") |
| 1239 |
assert w.getvalue() == "hello world" |
| 1240 |
|
| 1241 |
# Kind 1 -> Kind 2: Write string with UCS-2 characters |
| 1242 |
w2 = StringWriter() |
| 1243 |
w2.write("abc") |
| 1244 |
assert w2.getvalue() == "abc" |
| 1245 |
w2.write(chr(0x100) + chr(0x200)) |
| 1246 |
assert w2.getvalue() == "abc" + chr(0x100) + chr(0x200) |
| 1247 |
w2.write("xyz") |
| 1248 |
assert w2.getvalue() == "abc" + chr(0x100) + chr(0x200) + "xyz" |
| 1249 |
|
| 1250 |
# Kind 2: Write all UCS-2 |
| 1251 |
w3 = StringWriter() |
| 1252 |
w3.append(0x100) |
| 1253 |
w3.write(chr(0x200) + chr(0x300)) |
| 1254 |
assert w3.getvalue() == chr(0x100) + chr(0x200) + chr(0x300) |
| 1255 |
|
| 1256 |
# Kind 2 -> Kind 4: Write string with UCS-4 characters |
| 1257 |
w4 = StringWriter() |
| 1258 |
w4.write(chr(0x100)) |
| 1259 |
w4.write(chr(0x10000)) |
| 1260 |
assert w4.getvalue() == chr(0x100) + chr(0x10000) |
| 1261 |
|
| 1262 |
# Kind 4: Write mixed |
| 1263 |
w5 = StringWriter() |
| 1264 |
w5.append(0x10000) |
| 1265 |
w5.write("abc") |
| 1266 |
w5.write(chr(0x200)) |
| 1267 |
w5.write(chr(0x10001)) |
| 1268 |
assert w5.getvalue() == chr(0x10000) + "abc" + chr(0x200) + chr(0x10001) |
| 1269 |
|
| 1270 |
# Test with longer strings to trigger buffer growth |
| 1271 |
w6 = StringWriter() |
| 1272 |
for _ in range(100): |
| 1273 |
w6.write("hello") |
| 1274 |
assert w6.getvalue() == "hello" * 100 |
| 1275 |
assert len(w6) == 500 |
| 1276 |
|
| 1277 |
def test_string_writer_append_grow_same_kind() -> None: |
| 1278 |
# Test growing buffer while staying in kind 1 (ASCII) |
| 1279 |
w = StringWriter() |
| 1280 |
# Append enough ASCII characters to grow beyond embedded buffer |
| 1281 |
for i in range(1000): |
| 1282 |
w.append(ord('a') + (i % 26)) |
| 1283 |
assert len(w) == i + 1 |
| 1284 |
result = w.getvalue() |
| 1285 |
assert len(result) == 1000 |
| 1286 |
for i in range(1000): |
| 1287 |
assert result[i] == chr(ord('a') + (i % 26)) |
| 1288 |
|
| 1289 |
# Test growing buffer while staying in kind 2 |
| 1290 |
w2 = StringWriter() |
| 1291 |
w2.append(0x100) # Switch to kind 2 |
| 1292 |
for i in range(1000): |
| 1293 |
w2.append(0x100 + (i % 100)) |
| 1294 |
assert len(w2) == i + 2 |
| 1295 |
result2 = w2.getvalue() |
| 1296 |
assert len(result2) == 1001 |
| 1297 |
assert result2[0] == chr(0x100) |
| 1298 |
for i in range(1000): |
| 1299 |
assert result2[i + 1] == chr(0x100 + (i % 100)) |
| 1300 |
|
| 1301 |
# Test growing buffer while staying in kind 4 |
| 1302 |
w3 = StringWriter() |
| 1303 |
w3.append(0x10000) # Switch to kind 4 |
| 1304 |
for i in range(500): |
| 1305 |
w3.append(0x10000 + (i % 100)) |
| 1306 |
assert len(w3) == i + 2 |
| 1307 |
result3 = w3.getvalue() |
| 1308 |
assert len(result3) == 501 |
| 1309 |
assert result3[0] == chr(0x10000) |
| 1310 |
for i in range(500): |
| 1311 |
assert result3[i + 1] == chr(0x10000 + (i % 100)) |
| 1312 |
|
| 1313 |
def test_string_writer_append_grow_and_switch_kind() -> None: |
| 1314 |
# Test growing buffer AND switching from kind 1 to kind 2 |
| 1315 |
w = StringWriter() |
| 1316 |
# Fill with ASCII to grow buffer |
| 1317 |
for i in range(500): |
| 1318 |
w.append(ord('A')) |
| 1319 |
assert len(w) == 500 |
| 1320 |
# Now append non-ASCII that requires kind 2, triggering both grow and kind switch |
| 1321 |
for i in range(500): |
| 1322 |
w.append(0x100 + i) |
| 1323 |
result = w.getvalue() |
| 1324 |
assert len(result) == 1000 |
| 1325 |
for i in range(500): |
| 1326 |
assert result[i] == 'A' |
| 1327 |
for i in range(500): |
| 1328 |
assert result[500 + i] == chr(0x100 + i) |
| 1329 |
|
| 1330 |
# Test growing buffer AND switching from kind 2 to kind 4 |
| 1331 |
w2 = StringWriter() |
| 1332 |
w2.append(0x100) # Switch to kind 2 |
| 1333 |
# Fill with kind 2 characters to grow buffer |
| 1334 |
for i in range(300): |
| 1335 |
w2.append(0x200 + (i % 100)) |
| 1336 |
assert len(w2) == 301 |
| 1337 |
# Now append characters that require kind 4, triggering both grow and kind switch |
| 1338 |
for i in range(300): |
| 1339 |
w2.append(0x10000 + i) |
| 1340 |
result2 = w2.getvalue() |
| 1341 |
assert len(result2) == 601 |
| 1342 |
assert result2[0] == chr(0x100) |
| 1343 |
for i in range(300): |
| 1344 |
assert result2[1 + i] == chr(0x200 + (i % 100)) |
| 1345 |
for i in range(300): |
| 1346 |
assert result2[301 + i] == chr(0x10000 + i) |
| 1347 |
|
| 1348 |
# Test switching kind 1->4 with buffer growth |
| 1349 |
w3 = StringWriter() |
| 1350 |
for i in range(300): |
| 1351 |
w3.append(ord('X')) |
| 1352 |
# Jump directly to kind 4 |
| 1353 |
w3.append(0x10000) |
| 1354 |
result3 = w3.getvalue() |
| 1355 |
assert len(result3) == 301 |
| 1356 |
for i in range(300): |
| 1357 |
assert result3[i] == 'X' |
| 1358 |
assert result3[300] == chr(0x10000) |
| 1359 |
|
| 1360 |
def test_string_writer_wrapper_functions() -> None: |
| 1361 |
cls: Any = StringWriter |
| 1362 |
s: Any = cls() |
| 1363 |
assert repr(s) == "StringWriter('')" |
| 1364 |
assert len(s) == 0 |
| 1365 |
s.append(ord('a')) |
| 1366 |
s.append(0x100) |
| 1367 |
s.append(0x10000) |
| 1368 |
s.write("foo") |
| 1369 |
assert s.getvalue() == "a" + chr(0x100) + chr(0x10000) + "foo" |
| 1370 |
assert len(s) == 6 |
| 1371 |
|
| 1372 |
assert s[0] == ord('a') |
| 1373 |
assert s[1] == 0x100 |
| 1374 |
assert s[2] == 0x10000 |
| 1375 |
assert s[-1] == ord('o') |
| 1376 |
assert s[-6] == ord('a') |
| 1377 |
|
| 1378 |
assert isinstance(s, cls) |
| 1379 |
|
| 1380 |
with assertRaises(TypeError): |
| 1381 |
s.append("x") |
| 1382 |
|
| 1383 |
with assertRaises(TypeError): |
| 1384 |
s.write(b"foo") |
| 1385 |
|
| 1386 |
with assertRaises(ValueError): |
| 1387 |
s.append(0x110000) |
| 1388 |
|
| 1389 |
with assertRaises(ValueError): |
| 1390 |
s.append(-1) |
| 1391 |
|
| 1392 |
with assertRaises(IndexError): |
| 1393 |
s[6] |
| 1394 |
with assertRaises(IndexError): |
| 1395 |
s[-7] |
| 1396 |
|
| 1397 |
def test_explicit_init_resets_via_any() -> None: |
| 1398 |
# Calling __init__ explicitly on an already-initialized writer resets it. |
| 1399 |
# Any previously-allocated heap buffer must be freed to avoid leaks. |
| 1400 |
bw: Any = BytesWriter() |
| 1401 |
bw.write(b"hello") |
| 1402 |
bw.__init__() |
| 1403 |
assert bw.getvalue() == b"" |
| 1404 |
bw.write(b"world") |
| 1405 |
assert bw.getvalue() == b"world" |
| 1406 |
|
| 1407 |
# Also trigger a heap-allocated buffer before the explicit __init__ call, |
| 1408 |
# to exercise the path that would otherwise leak memory. |
| 1409 |
bw2: Any = BytesWriter() |
| 1410 |
bw2.write(b"x" * 10000) |
| 1411 |
bw2.__init__() |
| 1412 |
assert bw2.getvalue() == b"" |
| 1413 |
bw2.write(b"short") |
| 1414 |
assert bw2.getvalue() == b"short" |
| 1415 |
|
| 1416 |
sw: Any = StringWriter() |
| 1417 |
sw.write("hello") |
| 1418 |
sw.__init__() |
| 1419 |
assert sw.getvalue() == "" |
| 1420 |
sw.write("world") |
| 1421 |
assert sw.getvalue() == "world" |
| 1422 |
|
| 1423 |
sw2: Any = StringWriter() |
| 1424 |
sw2.write("y" * 10000) |
| 1425 |
sw2.__init__() |
| 1426 |
assert sw2.getvalue() == "" |
| 1427 |
sw2.write("short") |
| 1428 |
assert sw2.getvalue() == "short" |
| 1429 |
|
| 1430 |
def test_new_without_init_is_usable() -> None: |
| 1431 |
# BytesWriter.__new__(BytesWriter) must return a fully initialized object |
| 1432 |
# that does not require an explicit __init__ call to be usable. |
| 1433 |
bw: Any = BytesWriter.__new__(BytesWriter) |
| 1434 |
assert bw.getvalue() == b"" |
| 1435 |
bw.write(b"hello") |
| 1436 |
assert bw.getvalue() == b"hello" |
| 1437 |
|
| 1438 |
sw: Any = StringWriter.__new__(StringWriter) |
| 1439 |
assert sw.getvalue() == "" |
| 1440 |
sw.write("hello") |
| 1441 |
assert sw.getvalue() == "hello" |
| 1442 |
|
| 1443 |
[case testLibrtStringsCodepointClassifiers_librt] |
| 1444 |
from typing import Any |
| 1445 |
from mypy_extensions import i32 |
| 1446 |
from librt.strings import isspace, isdigit, isalnum, isalpha, isidentifier |
| 1447 |
|
| 1448 |
from testutil import assertRaises |
| 1449 |
|
| 1450 |
|
| 1451 |
def test_codepoint_classifiers() -> None: |
| 1452 |
# Negative values are not codepoints. |
| 1453 |
for bad in (i32(-1), i32(-113)): |
| 1454 |
assert not isspace(bad) |
| 1455 |
assert not isdigit(bad) |
| 1456 |
assert not isalnum(bad) |
| 1457 |
assert not isalpha(bad) |
| 1458 |
assert not isidentifier(bad) |
| 1459 |
# Verify each codepoint primitive agrees with the matching str method |
| 1460 |
# across all Unicode codepoints, including the ord(chr(i)) round-trip. |
| 1461 |
# Any forces generic dispatch on the str side. |
| 1462 |
for i in range(0x110000): |
| 1463 |
c = chr(i) |
| 1464 |
a: Any = c |
| 1465 |
o = ord(c) |
| 1466 |
assert isspace(o) == isspace(i) == a.isspace() |
| 1467 |
assert isdigit(o) == isdigit(i) == a.isdigit() |
| 1468 |
assert isalnum(o) == isalnum(i) == a.isalnum() |
| 1469 |
assert isalpha(o) == isalpha(i) == a.isalpha() |
| 1470 |
assert isidentifier(o) == isidentifier(i) == a.isidentifier() |
| 1471 |
|
| 1472 |
|
| 1473 |
def test_codepoint_classifiers_via_any() -> None: |
| 1474 |
# Iterate so the callee is opaque to mypyc and dispatch falls back to |
| 1475 |
# the PyMethodDef wrapper, exercising the i32 range check. |
| 1476 |
for fn, true_input, false_input in ( |
| 1477 |
(isspace, " ", "a"), |
| 1478 |
(isdigit, "5", "a"), |
| 1479 |
(isalnum, "A", " "), |
| 1480 |
(isalpha, "A", " "), |
| 1481 |
(isidentifier, "A", "0"), |
| 1482 |
): |
| 1483 |
f: Any = fn |
| 1484 |
assert f(ord(true_input)) is True |
| 1485 |
assert f(ord(false_input)) is False |
| 1486 |
# Negative values are valid i32, just not codepoints. |
| 1487 |
assert f(-1) is False |
| 1488 |
# Inputs outside i32 range raise OverflowError through the wrapper. |
| 1489 |
with assertRaises(OverflowError, "codepoint out of i32 range"): |
| 1490 |
f(1 << 40) |
| 1491 |
with assertRaises(OverflowError, "codepoint out of i32 range"): |
| 1492 |
f(-(1 << 40)) |
| 1493 |
|
| 1494 |
[case testLibrtStringsCodepointCaseConversion_librt] |
| 1495 |
from typing import Any |
| 1496 |
from mypy_extensions import i32 |
| 1497 |
from librt.strings import toupper, tolower |
| 1498 |
|
| 1499 |
from testutil import assertRaises |
| 1500 |
|
| 1501 |
|
| 1502 |
def _expect(c: str, method: str) -> int: |
| 1503 |
# The contract: i32 -> i32 when conversion yields exactly one codepoint, |
| 1504 |
# else return the input unchanged. |
| 1505 |
converted = getattr(c, method)() |
| 1506 |
if len(converted) == 1: |
| 1507 |
return ord(converted) |
| 1508 |
return ord(c) |
| 1509 |
|
| 1510 |
|
| 1511 |
def test_codepoint_case_conversion() -> None: |
| 1512 |
# Negative inputs return unchanged. |
| 1513 |
for bad in (i32(-1), i32(-113)): |
| 1514 |
assert toupper(bad) == bad |
| 1515 |
assert tolower(bad) == bad |
| 1516 |
# Agree with str.upper / str.lower across the full Unicode range |
| 1517 |
# whenever the conversion is single-codepoint; otherwise return input. |
| 1518 |
for i in range(0x110000): |
| 1519 |
c = chr(i) |
| 1520 |
o = ord(c) |
| 1521 |
assert toupper(o) == _expect(c, "upper") |
| 1522 |
assert tolower(o) == _expect(c, "lower") |
| 1523 |
|
| 1524 |
|
| 1525 |
def test_codepoint_case_conversion_via_any() -> None: |
| 1526 |
# Iterate to force generic dispatch through the PyMethodDef wrapper. |
| 1527 |
for fn, in_cp, out_cp in ( |
| 1528 |
(toupper, ord("a"), ord("A")), |
| 1529 |
(toupper, ord("A"), ord("A")), |
| 1530 |
(tolower, ord("Z"), ord("z")), |
| 1531 |
(tolower, ord("z"), ord("z")), |
| 1532 |
): |
| 1533 |
f: Any = fn |
| 1534 |
assert f(in_cp) == out_cp |
| 1535 |
# Negative values are valid i32, returned unchanged. |
| 1536 |
assert f(-1) == -1 |
| 1537 |
# Inputs outside i32 range raise OverflowError through the wrapper. |
| 1538 |
with assertRaises(OverflowError, "codepoint out of i32 range"): |
| 1539 |
f(1 << 40) |
| 1540 |
with assertRaises(OverflowError, "codepoint out of i32 range"): |
| 1541 |
f(-(1 << 40)) |