| ... | ... | @@ -253,7 +253,8 @@ test "BitOutStream" { |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime is_packed: bool) !void { |
| 256 | | const max_test_bitsize = 17; |
| 256 | //@NOTE: if this test is taking too long, reduce the maximum tested bitsize |
| 257 | const max_test_bitsize = 128; |
| 257 | 258 | |
| 258 | 259 | const total_bytes = comptime blk: { |
| 259 | 260 | var bytes = 0; |
| ... | ... | @@ -278,7 +279,7 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime is_pa |
| 278 | 279 | const U = @IntType(false, i); |
| 279 | 280 | const S = @IntType(true, i); |
| 280 | 281 | try serializer.serializeInt(U(i)); |
| 281 | | if (i != 0) try serializer.serializeInt(S(-1)); |
| 282 | if (i != 0) try serializer.serializeInt(S(-1)) else try serializer.serialize(S(0)); |
| 282 | 283 | } |
| 283 | 284 | try serializer.flush(); |
| 284 | 285 | |
| ... | ... | @@ -287,9 +288,9 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime is_pa |
| 287 | 288 | const U = @IntType(false, i); |
| 288 | 289 | const S = @IntType(true, i); |
| 289 | 290 | const x = try deserializer.deserializeInt(U); |
| 290 | | const y = if (i != 0) try deserializer.deserializeInt(S); |
| 291 | const y = try deserializer.deserializeInt(S); |
| 291 | 292 | assert(x == U(i)); |
| 292 | | if (i != 0) assert(y == S(-1)); |
| 293 | if (i != 0) assert(y == S(-1)) else assert(y == 0); |
| 293 | 294 | } |
| 294 | 295 | |
| 295 | 296 | const u8_bit_count = comptime meta.bitCount(u8); |
| ... | ... | @@ -299,8 +300,6 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime is_pa |
| 299 | 300 | const extra_packed_byte = @boolToInt(total_bits % u8_bit_count > 0); |
| 300 | 301 | const total_packed_bytes = (total_bits / u8_bit_count) + extra_packed_byte; |
| 301 | 302 | |
| 302 | | |
| 303 | | |
| 304 | 303 | assert(in.pos == if (is_packed) total_packed_bytes else total_bytes); |
| 305 | 304 | } |
| 306 | 305 | |
| ... | ... | @@ -311,6 +310,56 @@ test "Serializer/Deserializer Int" { |
| 311 | 310 | try testIntSerializerDeserializer(builtin.Endian.Little, true); |
| 312 | 311 | } |
| 313 | 312 | |
| 313 | fn testIntSerializerDeserializerInfNaN(comptime endian: builtin.Endian, |
| 314 | comptime is_packed: bool) !void |
| 315 | { |
| 316 | const mem_size = (16*2 + 32*2 + 64*2 + 128*2) / comptime meta.bitCount(u8); |
| 317 | var data_mem: [mem_size]u8 = undefined; |
| 318 | |
| 319 | var out = io.SliceOutStream.init(data_mem[0..]); |
| 320 | const OutError = io.SliceOutStream.Error; |
| 321 | var out_stream = &out.stream; |
| 322 | var serializer = io.Serializer(endian, is_packed, OutError).init(out_stream); |
| 323 | |
| 324 | var in = io.SliceInStream.init(data_mem[0..]); |
| 325 | const InError = io.SliceInStream.Error; |
| 326 | var in_stream = &in.stream; |
| 327 | var deserializer = io.Deserializer(endian, is_packed, InError).init(in_stream); |
| 328 | |
| 329 | //@TODO: isInf/isNan not currently implemented for f128. |
| 330 | try serializer.serialize(std.math.nan(f16)); |
| 331 | try serializer.serialize(std.math.inf(f16)); |
| 332 | try serializer.serialize(std.math.nan(f32)); |
| 333 | try serializer.serialize(std.math.inf(f32)); |
| 334 | try serializer.serialize(std.math.nan(f64)); |
| 335 | try serializer.serialize(std.math.inf(f64)); |
| 336 | //try serializer.serialize(std.math.nan(f128)); |
| 337 | //try serializer.serialize(std.math.inf(f128)); |
| 338 | const nan_check_f16 = try deserializer.deserialize(f16); |
| 339 | const inf_check_f16 = try deserializer.deserialize(f16); |
| 340 | const nan_check_f32 = try deserializer.deserialize(f32); |
| 341 | const inf_check_f32 = try deserializer.deserialize(f32); |
| 342 | const nan_check_f64 = try deserializer.deserialize(f64); |
| 343 | const inf_check_f64 = try deserializer.deserialize(f64); |
| 344 | //const nan_check_f128 = try deserializer.deserialize(f128); |
| 345 | //const inf_check_f128 = try deserializer.deserialize(f128); |
| 346 | assert(std.math.isNan(nan_check_f16)); |
| 347 | assert(std.math.isInf(inf_check_f16)); |
| 348 | assert(std.math.isNan(nan_check_f32)); |
| 349 | assert(std.math.isInf(inf_check_f32)); |
| 350 | assert(std.math.isNan(nan_check_f64)); |
| 351 | assert(std.math.isInf(inf_check_f64)); |
| 352 | //assert(std.math.isNan(nan_check_f128)); |
| 353 | //assert(std.math.isInf(inf_check_f128)); |
| 354 | } |
| 355 | |
| 356 | test "Serializer/Deserializer Int: Inf/NaN" { |
| 357 | try testIntSerializerDeserializerInfNaN(builtin.Endian.Big, false); |
| 358 | try testIntSerializerDeserializerInfNaN(builtin.Endian.Little, false); |
| 359 | try testIntSerializerDeserializerInfNaN(builtin.Endian.Big, true); |
| 360 | try testIntSerializerDeserializerInfNaN(builtin.Endian.Little, true); |
| 361 | } |
| 362 | |
| 314 | 363 | fn testSerializerDeserializer(comptime endian: builtin.Endian, comptime is_packed: bool) !void { |
| 315 | 364 | const ColorType = enum(u4) { |
| 316 | 365 | RGB8 = 1, |
| ... | ... | @@ -410,7 +459,6 @@ fn testSerializerDeserializer(comptime endian: builtin.Endian, comptime is_packe |
| 410 | 459 | try serializer.serialize(my_inst); |
| 411 | 460 | |
| 412 | 461 | const my_copy = try deserializer.deserialize(MyStruct); |
| 413 | | |
| 414 | 462 | assert(meta.eql(my_copy, my_inst)); |
| 415 | 463 | } |
| 416 | 464 | |