| author | |
| committer | |
| log | 077b003c50065a8b193dcad793ca5c4474222af6 |
| tree | c30a616f3130b2ed70ddfb418bf28e36ec15ad0a |
| parent | 88d927a511922efd82c4ec862c8e2aa83df84391 |
- Previously, some of the compress tests used `@src()` in combination with `dirname` and `openDirAbsolute` to read test files at runtime, which both excludes platforms that `openDirAbsolute` is not implemented for (WASI) and platforms that `SourceLocation.file` is not absolute (this was true for me locally on Windows). Instead of converting the tests to use `fs.cwd().openDir`, they have been converted to use `@embedFile` to avoid any potential problems with the runtime cwd.
- In order to use `@embedFile`, some of the `[]u8` parameters needed to be changed to `[]const u8`; none of them needed to be non-const anyway
- The tests now use `expectEqual` and `expectEqualSlices` where appropriate for better diagnostics12 files changed, 112 insertions(+), 169 deletions(-)
lib/std/compress/deflate/bits_utils.zig+1-2| ... | ... | @@ -8,7 +8,6 @@ pub fn bitReverse(comptime T: type, value: T, N: usize) T { |
| 8 | 8 | |
| 9 | 9 | test "bitReverse" { |
| 10 | 10 | const std = @import("std"); |
| 11 | const expect = std.testing.expect; | |
| 12 | 11 | |
| 13 | 12 | const ReverseBitsTest = struct { |
| 14 | 13 | in: u16, |
| ... | ... | @@ -29,6 +28,6 @@ test "bitReverse" { |
| 29 | 28 | |
| 30 | 29 | for (reverse_bits_tests) |h| { |
| 31 | 30 | var v = bitReverse(u16, h.in, h.bit_count); |
| 32 | try expect(v == h.out); | |
| 31 | try std.testing.expectEqual(h.out, v); | |
| 33 | 32 | } |
| 34 | 33 | } |
lib/std/compress/deflate/compressor.zig+2-2| ... | ... | @@ -1079,7 +1079,7 @@ test "deflate" { |
| 1079 | 1079 | try comp.close(); |
| 1080 | 1080 | comp.deinit(); |
| 1081 | 1081 | |
| 1082 | try expect(mem.eql(u8, output.items, dt.out)); | |
| 1082 | try testing.expectEqualSlices(u8, dt.out, output.items); | |
| 1083 | 1083 | } |
| 1084 | 1084 | } |
| 1085 | 1085 | |
| ... | ... | @@ -1104,7 +1104,7 @@ test "bulkHash4" { |
| 1104 | 1104 | _ = bulkHash4(y, dst); |
| 1105 | 1105 | for (dst) |got, i| { |
| 1106 | 1106 | var want = hash4(y[i..]); |
| 1107 | try expect(got == want); | |
| 1107 | try testing.expectEqual(want, got); | |
| 1108 | 1108 | } |
| 1109 | 1109 | } |
| 1110 | 1110 | } |
lib/std/compress/deflate/compressor_test.zig+19-35| ... | ... | @@ -62,8 +62,8 @@ fn testSync(level: deflate.Compression, input: []const u8) !void { |
| 62 | 62 | defer testing.allocator.free(decompressed); |
| 63 | 63 | |
| 64 | 64 | var read = try decomp.reader().readAll(decompressed); // read at least half |
| 65 | try expect(read == half_len); | |
| 66 | try expect(mem.eql(u8, input[0..half], decompressed)); | |
| 65 | try testing.expectEqual(half_len, read); | |
| 66 | try testing.expectEqualSlices(u8, input[0..half], decompressed); | |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | // Write last half of the input and close() |
| ... | ... | @@ -79,13 +79,13 @@ fn testSync(level: deflate.Compression, input: []const u8) !void { |
| 79 | 79 | defer testing.allocator.free(decompressed); |
| 80 | 80 | |
| 81 | 81 | var read = try decomp.reader().readAll(decompressed); |
| 82 | try expect(read == half_len); | |
| 83 | try expect(mem.eql(u8, input[half..], decompressed)); | |
| 82 | try testing.expectEqual(half_len, read); | |
| 83 | try testing.expectEqualSlices(u8, input[half..], decompressed); | |
| 84 | 84 | |
| 85 | 85 | // Extra read |
| 86 | 86 | var final: [10]u8 = undefined; |
| 87 | 87 | read = try decomp.reader().readAll(&final); |
| 88 | try expect(read == 0); // expect ended stream to return 0 bytes | |
| 88 | try testing.expectEqual(@as(usize, 0), read); // expect ended stream to return 0 bytes | |
| 89 | 89 | |
| 90 | 90 | _ = decomp.close(); |
| 91 | 91 | } |
| ... | ... | @@ -105,7 +105,7 @@ fn testSync(level: deflate.Compression, input: []const u8) !void { |
| 105 | 105 | _ = try decomp.reader().readAll(decompressed); |
| 106 | 106 | _ = decomp.close(); |
| 107 | 107 | |
| 108 | try expect(mem.eql(u8, input, decompressed)); | |
| 108 | try testing.expectEqualSlices(u8, input, decompressed); | |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | fn testToFromWithLevelAndLimit(level: deflate.Compression, input: []const u8, limit: u32) !void { |
| ... | ... | @@ -130,8 +130,8 @@ fn testToFromWithLevelAndLimit(level: deflate.Compression, input: []const u8, li |
| 130 | 130 | defer testing.allocator.free(decompressed); |
| 131 | 131 | |
| 132 | 132 | var read: usize = try decomp.reader().readAll(decompressed); |
| 133 | try expect(read == input.len); | |
| 134 | try expect(mem.eql(u8, input, decompressed)); | |
| 133 | try testing.expectEqual(input.len, read); | |
| 134 | try testing.expectEqualSlices(u8, input, decompressed); | |
| 135 | 135 | |
| 136 | 136 | if (false) { |
| 137 | 137 | // TODO: this test has regressed |
| ... | ... | @@ -237,7 +237,7 @@ test "very long sparse chunk" { |
| 237 | 237 | read = try reader.read(&buf); |
| 238 | 238 | written += try writer.write(buf[0..read]); |
| 239 | 239 | } |
| 240 | try expect(written == 0x23e8); | |
| 240 | try testing.expectEqual(@as(usize, 0x23e8), written); | |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | test "compressor reset" { |
| ... | ... | @@ -287,7 +287,7 @@ fn testWriterReset(level: deflate.Compression, dict: ?[]const u8) !void { |
| 287 | 287 | try filler.writeData(&comp); |
| 288 | 288 | try comp.close(); |
| 289 | 289 | |
| 290 | try expect(mem.eql(u8, buf1.items, buf2.items)); | |
| 290 | try testing.expectEqualSlices(u8, buf1.items, buf2.items); | |
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | test "decompressor dictionary" { |
| ... | ... | @@ -327,7 +327,7 @@ test "decompressor dictionary" { |
| 327 | 327 | defer decomp.deinit(); |
| 328 | 328 | |
| 329 | 329 | _ = try decomp.reader().readAll(decompressed); |
| 330 | try expect(mem.eql(u8, decompressed, "hello again world")); | |
| 330 | try testing.expectEqualSlices(u8, "hello again world", decompressed); | |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | test "compressor dictionary" { |
| ... | ... | @@ -371,7 +371,7 @@ test "compressor dictionary" { |
| 371 | 371 | try comp_d.writer().writeAll(text); |
| 372 | 372 | try comp_d.close(); |
| 373 | 373 | |
| 374 | try expect(mem.eql(u8, compressed_nd.readableSlice(0), compressed_d.items)); | |
| 374 | try testing.expectEqualSlices(u8, compressed_d.items, compressed_nd.readableSlice(0)); | |
| 375 | 375 | } |
| 376 | 376 | |
| 377 | 377 | // Update the hash for best_speed only if d.index < d.maxInsertIndex |
| ... | ... | @@ -394,18 +394,12 @@ test "Go non-regression test for 2508" { |
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | test "deflate/inflate string" { |
| 397 | // Skip wasi because it does not support std.fs.openDirAbsolute() | |
| 398 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | |
| 399 | ||
| 400 | const current_dir = try std.fs.openDirAbsolute(std.fs.path.dirname(@src().file).?, .{}); | |
| 401 | const testdata_dir = try current_dir.openDir("testdata", .{}); | |
| 402 | ||
| 403 | 397 | const StringTest = struct { |
| 404 | 398 | filename: []const u8, |
| 405 | 399 | limit: [11]u32, |
| 406 | 400 | }; |
| 407 | 401 | |
| 408 | var deflate_inflate_string_tests = [_]StringTest{ | |
| 402 | const deflate_inflate_string_tests = [_]StringTest{ | |
| 409 | 403 | .{ |
| 410 | 404 | .filename = "compress-e.txt", |
| 411 | 405 | .limit = [11]u32{ |
| ... | ... | @@ -440,12 +434,8 @@ test "deflate/inflate string" { |
| 440 | 434 | }, |
| 441 | 435 | }; |
| 442 | 436 | |
| 443 | for (deflate_inflate_string_tests) |t| { | |
| 444 | const golden_file = try testdata_dir.openFile(t.filename, .{}); | |
| 445 | defer golden_file.close(); | |
| 446 | var golden = try golden_file.reader().readAllAlloc(testing.allocator, math.maxInt(usize)); | |
| 447 | defer testing.allocator.free(golden); | |
| 448 | ||
| 437 | inline for (deflate_inflate_string_tests) |t| { | |
| 438 | var golden = @embedFile("testdata/" ++ t.filename); | |
| 449 | 439 | try testToFromWithLimit(golden, t.limit); |
| 450 | 440 | } |
| 451 | 441 | } |
| ... | ... | @@ -492,11 +482,8 @@ test "inflate reset" { |
| 492 | 482 | |
| 493 | 483 | _ = decomp.close(); |
| 494 | 484 | |
| 495 | try expect(strings[0].len == decompressed_0.len); | |
| 496 | try expect(strings[1].len == decompressed_1.len); | |
| 497 | ||
| 498 | try expect(mem.eql(u8, strings[0], decompressed_0)); | |
| 499 | try expect(mem.eql(u8, strings[1], decompressed_1)); | |
| 485 | try testing.expectEqualSlices(u8, strings[0], decompressed_0); | |
| 486 | try testing.expectEqualSlices(u8, strings[1], decompressed_1); | |
| 500 | 487 | } |
| 501 | 488 | |
| 502 | 489 | test "inflate reset dictionary" { |
| ... | ... | @@ -542,9 +529,6 @@ test "inflate reset dictionary" { |
| 542 | 529 | |
| 543 | 530 | _ = decomp.close(); |
| 544 | 531 | |
| 545 | try expect(strings[0].len == decompressed_0.len); | |
| 546 | try expect(strings[1].len == decompressed_1.len); | |
| 547 | ||
| 548 | try expect(mem.eql(u8, strings[0], decompressed_0)); | |
| 549 | try expect(mem.eql(u8, strings[1], decompressed_1)); | |
| 532 | try testing.expectEqualSlices(u8, strings[0], decompressed_0); | |
| 533 | try testing.expectEqualSlices(u8, strings[1], decompressed_1); | |
| 550 | 534 | } |
lib/std/compress/deflate/decompressor.zig+3-4| ... | ... | @@ -895,7 +895,6 @@ pub fn Decompressor(comptime ReaderType: type) type { |
| 895 | 895 | } |
| 896 | 896 | |
| 897 | 897 | // tests |
| 898 | const expect = std.testing.expect; | |
| 899 | 898 | const expectError = std.testing.expectError; |
| 900 | 899 | const io = std.io; |
| 901 | 900 | const testing = std.testing; |
| ... | ... | @@ -928,7 +927,7 @@ test "truncated input" { |
| 928 | 927 | |
| 929 | 928 | var output = [1]u8{0} ** 12; |
| 930 | 929 | try expectError(error.UnexpectedEndOfStream, zr.readAll(&output)); |
| 931 | try expect(mem.eql(u8, output[0..t.output.len], t.output)); | |
| 930 | try testing.expectEqualSlices(u8, t.output, output[0..t.output.len]); | |
| 932 | 931 | } |
| 933 | 932 | } |
| 934 | 933 | |
| ... | ... | @@ -1026,8 +1025,8 @@ test "inflate A Tale of Two Cities (1859) intro" { |
| 1026 | 1025 | |
| 1027 | 1026 | var got: [700]u8 = undefined; |
| 1028 | 1027 | var got_len = try decomp.reader().read(&got); |
| 1029 | try expect(got_len == 616); | |
| 1030 | try expect(mem.eql(u8, got[0..expected.len], expected)); | |
| 1028 | try testing.expectEqual(@as(usize, 616), got_len); | |
| 1029 | try testing.expectEqualSlices(u8, expected, got[0..expected.len]); | |
| 1031 | 1030 | } |
| 1032 | 1031 | |
| 1033 | 1032 | test "lengths overflow" { |
lib/std/compress/deflate/deflate_fast.zig+20-21| ... | ... | @@ -354,7 +354,7 @@ pub const DeflateFast = struct { |
| 354 | 354 | }; |
| 355 | 355 | |
| 356 | 356 | test "best speed match 1/3" { |
| 357 | const expect = std.testing.expect; | |
| 357 | const expectEqual = std.testing.expectEqual; | |
| 358 | 358 | |
| 359 | 359 | { |
| 360 | 360 | var previous = [_]u8{ 0, 0, 0, 1, 2 }; |
| ... | ... | @@ -367,7 +367,7 @@ test "best speed match 1/3" { |
| 367 | 367 | }; |
| 368 | 368 | var current = [_]u8{ 3, 4, 5, 0, 1, 2, 3, 4, 5 }; |
| 369 | 369 | var got: i32 = e.matchLen(3, -3, &current); |
| 370 | try expect(got == 6); | |
| 370 | try expectEqual(@as(i32, 6), got); | |
| 371 | 371 | } |
| 372 | 372 | { |
| 373 | 373 | var previous = [_]u8{ 0, 0, 0, 1, 2 }; |
| ... | ... | @@ -380,7 +380,7 @@ test "best speed match 1/3" { |
| 380 | 380 | }; |
| 381 | 381 | var current = [_]u8{ 2, 4, 5, 0, 1, 2, 3, 4, 5 }; |
| 382 | 382 | var got: i32 = e.matchLen(3, -3, &current); |
| 383 | try expect(got == 3); | |
| 383 | try expectEqual(@as(i32, 3), got); | |
| 384 | 384 | } |
| 385 | 385 | { |
| 386 | 386 | var previous = [_]u8{ 0, 0, 0, 1, 1 }; |
| ... | ... | @@ -393,7 +393,7 @@ test "best speed match 1/3" { |
| 393 | 393 | }; |
| 394 | 394 | var current = [_]u8{ 3, 4, 5, 0, 1, 2, 3, 4, 5 }; |
| 395 | 395 | var got: i32 = e.matchLen(3, -3, &current); |
| 396 | try expect(got == 2); | |
| 396 | try expectEqual(@as(i32, 2), got); | |
| 397 | 397 | } |
| 398 | 398 | { |
| 399 | 399 | var previous = [_]u8{ 0, 0, 0, 1, 2 }; |
| ... | ... | @@ -406,7 +406,7 @@ test "best speed match 1/3" { |
| 406 | 406 | }; |
| 407 | 407 | var current = [_]u8{ 2, 2, 2, 2, 1, 2, 3, 4, 5 }; |
| 408 | 408 | var got: i32 = e.matchLen(0, -1, &current); |
| 409 | try expect(got == 4); | |
| 409 | try expectEqual(@as(i32, 4), got); | |
| 410 | 410 | } |
| 411 | 411 | { |
| 412 | 412 | var previous = [_]u8{ 0, 0, 0, 1, 2, 3, 4, 5, 2, 2 }; |
| ... | ... | @@ -419,7 +419,7 @@ test "best speed match 1/3" { |
| 419 | 419 | }; |
| 420 | 420 | var current = [_]u8{ 2, 2, 2, 2, 1, 2, 3, 4, 5 }; |
| 421 | 421 | var got: i32 = e.matchLen(4, -7, &current); |
| 422 | try expect(got == 5); | |
| 422 | try expectEqual(@as(i32, 5), got); | |
| 423 | 423 | } |
| 424 | 424 | { |
| 425 | 425 | var previous = [_]u8{ 9, 9, 9, 9, 9 }; |
| ... | ... | @@ -432,7 +432,7 @@ test "best speed match 1/3" { |
| 432 | 432 | }; |
| 433 | 433 | var current = [_]u8{ 2, 2, 2, 2, 1, 2, 3, 4, 5 }; |
| 434 | 434 | var got: i32 = e.matchLen(0, -1, &current); |
| 435 | try expect(got == 0); | |
| 435 | try expectEqual(@as(i32, 0), got); | |
| 436 | 436 | } |
| 437 | 437 | { |
| 438 | 438 | var previous = [_]u8{ 9, 9, 9, 9, 9 }; |
| ... | ... | @@ -445,12 +445,12 @@ test "best speed match 1/3" { |
| 445 | 445 | }; |
| 446 | 446 | var current = [_]u8{ 9, 2, 2, 2, 1, 2, 3, 4, 5 }; |
| 447 | 447 | var got: i32 = e.matchLen(1, 0, &current); |
| 448 | try expect(got == 0); | |
| 448 | try expectEqual(@as(i32, 0), got); | |
| 449 | 449 | } |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | test "best speed match 2/3" { |
| 453 | const expect = std.testing.expect; | |
| 453 | const expectEqual = std.testing.expectEqual; | |
| 454 | 454 | |
| 455 | 455 | { |
| 456 | 456 | var previous = [_]u8{}; |
| ... | ... | @@ -463,7 +463,7 @@ test "best speed match 2/3" { |
| 463 | 463 | }; |
| 464 | 464 | var current = [_]u8{ 9, 2, 2, 2, 1, 2, 3, 4, 5 }; |
| 465 | 465 | var got: i32 = e.matchLen(1, -5, &current); |
| 466 | try expect(got == 0); | |
| 466 | try expectEqual(@as(i32, 0), got); | |
| 467 | 467 | } |
| 468 | 468 | { |
| 469 | 469 | var previous = [_]u8{}; |
| ... | ... | @@ -476,7 +476,7 @@ test "best speed match 2/3" { |
| 476 | 476 | }; |
| 477 | 477 | var current = [_]u8{ 9, 2, 2, 2, 1, 2, 3, 4, 5 }; |
| 478 | 478 | var got: i32 = e.matchLen(1, -1, &current); |
| 479 | try expect(got == 0); | |
| 479 | try expectEqual(@as(i32, 0), got); | |
| 480 | 480 | } |
| 481 | 481 | { |
| 482 | 482 | var previous = [_]u8{}; |
| ... | ... | @@ -489,7 +489,7 @@ test "best speed match 2/3" { |
| 489 | 489 | }; |
| 490 | 490 | var current = [_]u8{ 2, 2, 2, 2, 1, 2, 3, 4, 5 }; |
| 491 | 491 | var got: i32 = e.matchLen(1, 0, &current); |
| 492 | try expect(got == 3); | |
| 492 | try expectEqual(@as(i32, 3), got); | |
| 493 | 493 | } |
| 494 | 494 | { |
| 495 | 495 | var previous = [_]u8{ 3, 4, 5 }; |
| ... | ... | @@ -502,13 +502,13 @@ test "best speed match 2/3" { |
| 502 | 502 | }; |
| 503 | 503 | var current = [_]u8{ 3, 4, 5 }; |
| 504 | 504 | var got: i32 = e.matchLen(0, -3, &current); |
| 505 | try expect(got == 3); | |
| 505 | try expectEqual(@as(i32, 3), got); | |
| 506 | 506 | } |
| 507 | 507 | } |
| 508 | 508 | |
| 509 | 509 | test "best speed match 2/2" { |
| 510 | 510 | const testing = std.testing; |
| 511 | const expect = testing.expect; | |
| 511 | const expectEqual = testing.expectEqual; | |
| 512 | 512 | |
| 513 | 513 | const Case = struct { |
| 514 | 514 | previous: u32, |
| ... | ... | @@ -580,7 +580,7 @@ test "best speed match 2/2" { |
| 580 | 580 | .cur = 0, |
| 581 | 581 | }; |
| 582 | 582 | var got: i32 = e.matchLen(c.s, c.t, current); |
| 583 | try expect(got == c.expected); | |
| 583 | try expectEqual(@as(i32, c.expected), got); | |
| 584 | 584 | } |
| 585 | 585 | } |
| 586 | 586 | |
| ... | ... | @@ -623,16 +623,16 @@ test "best speed shift offsets" { |
| 623 | 623 | tokens_count = 0; |
| 624 | 624 | enc.encode(&tokens, &tokens_count, &test_data); |
| 625 | 625 | var got = tokens_count; |
| 626 | try expect(want_first_tokens == got); | |
| 626 | try testing.expectEqual(want_first_tokens, got); | |
| 627 | 627 | |
| 628 | 628 | // Verify we are about to wrap. |
| 629 | try expect(enc.cur == buffer_reset); | |
| 629 | try testing.expectEqual(@as(i32, buffer_reset), enc.cur); | |
| 630 | 630 | |
| 631 | 631 | // Part 2 should match clean state as well even if wrapped. |
| 632 | 632 | tokens_count = 0; |
| 633 | 633 | enc.encode(&tokens, &tokens_count, &test_data); |
| 634 | 634 | got = tokens_count; |
| 635 | try expect(want_second_tokens == got); | |
| 635 | try testing.expectEqual(want_second_tokens, got); | |
| 636 | 636 | |
| 637 | 637 | // Verify that we wrapped. |
| 638 | 638 | try expect(enc.cur < buffer_reset); |
| ... | ... | @@ -645,13 +645,12 @@ test "best speed shift offsets" { |
| 645 | 645 | tokens_count = 0; |
| 646 | 646 | enc.encode(&tokens, &tokens_count, &test_data); |
| 647 | 647 | got = tokens_count; |
| 648 | try expect(want_first_tokens == got); | |
| 648 | try testing.expectEqual(want_first_tokens, got); | |
| 649 | 649 | } |
| 650 | 650 | |
| 651 | 651 | test "best speed reset" { |
| 652 | 652 | // test that encoding is consistent across a warparound of the table offset. |
| 653 | 653 | // See https://github.com/golang/go/issues/34121 |
| 654 | const expect = std.testing.expect; | |
| 655 | 654 | const fmt = std.fmt; |
| 656 | 655 | const testing = std.testing; |
| 657 | 656 | |
| ... | ... | @@ -716,6 +715,6 @@ test "best speed reset" { |
| 716 | 715 | try comp.close(); |
| 717 | 716 | |
| 718 | 717 | // output must match at wraparound |
| 719 | try expect(mem.eql(u8, got.items, want.items)); | |
| 718 | try testing.expectEqualSlices(u8, want.items, got.items); | |
| 720 | 719 | } |
| 721 | 720 | } |
lib/std/compress/deflate/deflate_fast_test.zig+4-4| ... | ... | @@ -85,8 +85,8 @@ test "best speed" { |
| 85 | 85 | var read = try decomp.reader().readAll(decompressed); |
| 86 | 86 | _ = decomp.close(); |
| 87 | 87 | |
| 88 | try expect(read == want.items.len); | |
| 89 | try expect(mem.eql(u8, want.items, decompressed)); | |
| 88 | try testing.expectEqual(want.items.len, read); | |
| 89 | try testing.expectEqualSlices(u8, want.items, decompressed); | |
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | 92 | } |
| ... | ... | @@ -152,8 +152,8 @@ test "best speed max match offset" { |
| 152 | 152 | var read = try decomp.reader().readAll(decompressed); |
| 153 | 153 | _ = decomp.close(); |
| 154 | 154 | |
| 155 | try expect(read == src.len); | |
| 156 | try expect(mem.eql(u8, decompressed, src)); | |
| 155 | try testing.expectEqual(src.len, read); | |
| 156 | try testing.expectEqualSlices(u8, src, decompressed); | |
| 157 | 157 | } |
| 158 | 158 | } |
| 159 | 159 | } |
lib/std/compress/deflate/dict_decoder.zig+1-2| ... | ... | @@ -206,7 +206,6 @@ pub const DictDecoder = struct { |
| 206 | 206 | |
| 207 | 207 | test "dictionary decoder" { |
| 208 | 208 | const ArrayList = std.ArrayList; |
| 209 | const expect = std.testing.expect; | |
| 210 | 209 | const testing = std.testing; |
| 211 | 210 | |
| 212 | 211 | const abc = "ABC\n"; |
| ... | ... | @@ -416,5 +415,5 @@ test "dictionary decoder" { |
| 416 | 415 | _ = try want.write(want_list.items[want_list.items.len - dd.histSize() ..][0..10]); |
| 417 | 416 | |
| 418 | 417 | _ = try got.write(dd.readFlush()); |
| 419 | try expect(mem.eql(u8, got_list.items, want_list.items)); | |
| 418 | try testing.expectEqualSlices(u8, want_list.items, got_list.items); | |
| 420 | 419 | } |
lib/std/compress/deflate/huffman_bit_writer.zig+26-62| ... | ... | @@ -121,7 +121,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type { |
| 121 | 121 | self.nbytes = 0; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | fn write(self: *Self, b: []u8) Error!void { | |
| 124 | fn write(self: *Self, b: []const u8) Error!void { | |
| 125 | 125 | if (self.err) { |
| 126 | 126 | return; |
| 127 | 127 | } |
| ... | ... | @@ -155,7 +155,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type { |
| 155 | 155 | } |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | pub fn writeBytes(self: *Self, bytes: []u8) Error!void { | |
| 158 | pub fn writeBytes(self: *Self, bytes: []const u8) Error!void { | |
| 159 | 159 | if (self.err) { |
| 160 | 160 | return; |
| 161 | 161 | } |
| ... | ... | @@ -323,7 +323,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type { |
| 323 | 323 | // storedSizeFits calculates the stored size, including header. |
| 324 | 324 | // The function returns the size in bits and whether the block |
| 325 | 325 | // fits inside a single block. |
| 326 | fn storedSizeFits(in: ?[]u8) StoredSize { | |
| 326 | fn storedSizeFits(in: ?[]const u8) StoredSize { | |
| 327 | 327 | if (in == null) { |
| 328 | 328 | return .{ .size = 0, .storable = false }; |
| 329 | 329 | } |
| ... | ... | @@ -453,7 +453,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type { |
| 453 | 453 | self: *Self, |
| 454 | 454 | tokens: []const token.Token, |
| 455 | 455 | eof: bool, |
| 456 | input: ?[]u8, | |
| 456 | input: ?[]const u8, | |
| 457 | 457 | ) Error!void { |
| 458 | 458 | if (self.err) { |
| 459 | 459 | return; |
| ... | ... | @@ -546,7 +546,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type { |
| 546 | 546 | self: *Self, |
| 547 | 547 | tokens: []const token.Token, |
| 548 | 548 | eof: bool, |
| 549 | input: ?[]u8, | |
| 549 | input: ?[]const u8, | |
| 550 | 550 | ) Error!void { |
| 551 | 551 | if (self.err) { |
| 552 | 552 | return; |
| ... | ... | @@ -685,7 +685,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type { |
| 685 | 685 | |
| 686 | 686 | // Encodes a block of bytes as either Huffman encoded literals or uncompressed bytes |
| 687 | 687 | // if the results only gains very little from compression. |
| 688 | pub fn writeBlockHuff(self: *Self, eof: bool, input: []u8) Error!void { | |
| 688 | pub fn writeBlockHuff(self: *Self, eof: bool, input: []const u8) Error!void { | |
| 689 | 689 | if (self.err) { |
| 690 | 690 | return; |
| 691 | 691 | } |
| ... | ... | @@ -828,7 +828,7 @@ pub fn huffmanBitWriter(allocator: Allocator, writer: anytype) !HuffmanBitWriter |
| 828 | 828 | // histogram accumulates a histogram of b in h. |
| 829 | 829 | // |
| 830 | 830 | // h.len must be >= 256, and h's elements must be all zeroes. |
| 831 | fn histogram(b: []u8, h: *[]u16) void { | |
| 831 | fn histogram(b: []const u8, h: *[]u16) void { | |
| 832 | 832 | var lh = h.*[0..256]; |
| 833 | 833 | for (b) |t| { |
| 834 | 834 | lh[t] += 1; |
| ... | ... | @@ -891,21 +891,9 @@ test "writeBlockHuff" { |
| 891 | 891 | ); |
| 892 | 892 | } |
| 893 | 893 | |
| 894 | fn testBlockHuff(in_name: []const u8, want_name: []const u8) !void { | |
| 895 | // Skip wasi because it does not support std.fs.openDirAbsolute() | |
| 896 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | |
| 897 | ||
| 898 | const current_dir = try std.fs.openDirAbsolute(std.fs.path.dirname(@src().file).?, .{}); | |
| 899 | const testdata_dir = try current_dir.openDir("testdata", .{}); | |
| 900 | const in_file = try testdata_dir.openFile(in_name, .{}); | |
| 901 | defer in_file.close(); | |
| 902 | const want_file = try testdata_dir.openFile(want_name, .{}); | |
| 903 | defer want_file.close(); | |
| 904 | ||
| 905 | var in = try in_file.reader().readAllAlloc(testing.allocator, math.maxInt(usize)); | |
| 906 | defer testing.allocator.free(in); | |
| 907 | var want = try want_file.reader().readAllAlloc(testing.allocator, math.maxInt(usize)); | |
| 908 | defer testing.allocator.free(want); | |
| 894 | fn testBlockHuff(comptime in_name: []const u8, comptime want_name: []const u8) !void { | |
| 895 | const in: []const u8 = @embedFile("testdata/" ++ in_name); | |
| 896 | const want: []const u8 = @embedFile("testdata/" ++ want_name); | |
| 909 | 897 | |
| 910 | 898 | var buf = ArrayList(u8).init(testing.allocator); |
| 911 | 899 | defer buf.deinit(); |
| ... | ... | @@ -914,7 +902,7 @@ fn testBlockHuff(in_name: []const u8, want_name: []const u8) !void { |
| 914 | 902 | try bw.writeBlockHuff(false, in); |
| 915 | 903 | try bw.flush(); |
| 916 | 904 | |
| 917 | try expect(mem.eql(u8, buf.items, want)); | |
| 905 | try std.testing.expectEqualSlices(u8, want, buf.items); | |
| 918 | 906 | |
| 919 | 907 | // Test if the writer produces the same output after reset. |
| 920 | 908 | var buf_after_reset = ArrayList(u8).init(testing.allocator); |
| ... | ... | @@ -925,8 +913,8 @@ fn testBlockHuff(in_name: []const u8, want_name: []const u8) !void { |
| 925 | 913 | try bw.writeBlockHuff(false, in); |
| 926 | 914 | try bw.flush(); |
| 927 | 915 | |
| 928 | try expect(mem.eql(u8, buf_after_reset.items, buf.items)); | |
| 929 | try expect(mem.eql(u8, buf_after_reset.items, want)); | |
| 916 | try std.testing.expectEqualSlices(u8, buf.items, buf_after_reset.items); | |
| 917 | try std.testing.expectEqualSlices(u8, want, buf_after_reset.items); | |
| 930 | 918 | |
| 931 | 919 | try testWriterEOF(.write_huffman_block, &[0]token.Token{}, in); |
| 932 | 920 | } |
| ... | ... | @@ -1617,38 +1605,18 @@ test "writeBlockDynamic" { |
| 1617 | 1605 | |
| 1618 | 1606 | // testBlock tests a block against its references, |
| 1619 | 1607 | // or regenerate the references, if "-update" flag is set. |
| 1620 | fn testBlock(comptime ht: HuffTest, ttype: TestType) !void { | |
| 1621 | // Skip wasi because it does not support std.fs.openDirAbsolute() | |
| 1622 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | |
| 1623 | ||
| 1624 | var want_name: []u8 = undefined; | |
| 1625 | var want_name_no_input: []u8 = undefined; | |
| 1626 | var input: []u8 = undefined; | |
| 1627 | var want: []u8 = undefined; | |
| 1628 | var want_ni: []u8 = undefined; // want no input: what we expect when input is empty | |
| 1629 | ||
| 1630 | const current_dir = try std.fs.openDirAbsolute(std.fs.path.dirname(@src().file).?, .{}); | |
| 1631 | const testdata_dir = try current_dir.openDir("testdata", .{}); | |
| 1632 | ||
| 1633 | var want_name_type = if (ht.want.len == 0) .{} else .{ttype.to_s()}; | |
| 1634 | want_name = try fmt.allocPrint(testing.allocator, ht.want, want_name_type); | |
| 1635 | defer testing.allocator.free(want_name); | |
| 1636 | ||
| 1637 | if (!mem.eql(u8, ht.input, "")) { | |
| 1638 | const in_file = try testdata_dir.openFile(ht.input, .{}); | |
| 1639 | input = try in_file.reader().readAllAlloc(testing.allocator, math.maxInt(usize)); | |
| 1640 | defer testing.allocator.free(input); | |
| 1641 | ||
| 1642 | const want_file = try testdata_dir.openFile(want_name, .{}); | |
| 1643 | want = try want_file.reader().readAllAlloc(testing.allocator, math.maxInt(usize)); | |
| 1644 | defer testing.allocator.free(want); | |
| 1608 | fn testBlock(comptime ht: HuffTest, comptime ttype: TestType) !void { | |
| 1609 | if (ht.input.len != 0 and ht.want.len != 0) { | |
| 1610 | const want_name = comptime fmt.comptimePrint(ht.want, .{ttype.to_s()}); | |
| 1611 | const input = @embedFile("testdata/" ++ ht.input); | |
| 1612 | const want = @embedFile("testdata/" ++ want_name); | |
| 1645 | 1613 | |
| 1646 | 1614 | var buf = ArrayList(u8).init(testing.allocator); |
| 1647 | 1615 | var bw = try huffmanBitWriter(testing.allocator, buf.writer()); |
| 1648 | 1616 | try writeToType(ttype, &bw, ht.tokens, input); |
| 1649 | 1617 | |
| 1650 | 1618 | var got = buf.items; |
| 1651 | try expect(mem.eql(u8, got, want)); // expect writeBlock to yield expected result | |
| 1619 | try testing.expectEqualSlices(u8, want, got); // expect writeBlock to yield expected result | |
| 1652 | 1620 | |
| 1653 | 1621 | // Test if the writer produces the same output after reset. |
| 1654 | 1622 | buf.deinit(); |
| ... | ... | @@ -1661,16 +1629,12 @@ fn testBlock(comptime ht: HuffTest, ttype: TestType) !void { |
| 1661 | 1629 | try writeToType(ttype, &bw, ht.tokens, input); |
| 1662 | 1630 | try bw.flush(); |
| 1663 | 1631 | got = buf.items; |
| 1664 | try expect(mem.eql(u8, got, want)); // expect writeBlock to yield expected result | |
| 1632 | try testing.expectEqualSlices(u8, want, got); // expect writeBlock to yield expected result | |
| 1665 | 1633 | try testWriterEOF(.write_block, ht.tokens, input); |
| 1666 | 1634 | } |
| 1667 | 1635 | |
| 1668 | want_name_no_input = try fmt.allocPrint(testing.allocator, ht.want_no_input, .{ttype.to_s()}); | |
| 1669 | defer testing.allocator.free(want_name_no_input); | |
| 1670 | ||
| 1671 | const want_no_input_file = try testdata_dir.openFile(want_name_no_input, .{}); | |
| 1672 | want_ni = try want_no_input_file.reader().readAllAlloc(testing.allocator, math.maxInt(usize)); | |
| 1673 | defer testing.allocator.free(want_ni); | |
| 1636 | const want_name_no_input = comptime fmt.comptimePrint(ht.want_no_input, .{ttype.to_s()}); | |
| 1637 | const want_ni = @embedFile("testdata/" ++ want_name_no_input); | |
| 1674 | 1638 | |
| 1675 | 1639 | var buf = ArrayList(u8).init(testing.allocator); |
| 1676 | 1640 | var bw = try huffmanBitWriter(testing.allocator, buf.writer()); |
| ... | ... | @@ -1678,7 +1642,7 @@ fn testBlock(comptime ht: HuffTest, ttype: TestType) !void { |
| 1678 | 1642 | try writeToType(ttype, &bw, ht.tokens, null); |
| 1679 | 1643 | |
| 1680 | 1644 | var got = buf.items; |
| 1681 | try expect(mem.eql(u8, got, want_ni)); // expect writeBlock to yield expected result | |
| 1645 | try testing.expectEqualSlices(u8, want_ni, got); // expect writeBlock to yield expected result | |
| 1682 | 1646 | try expect(got[0] & 1 != 1); // expect no EOF |
| 1683 | 1647 | |
| 1684 | 1648 | // Test if the writer produces the same output after reset. |
| ... | ... | @@ -1693,11 +1657,11 @@ fn testBlock(comptime ht: HuffTest, ttype: TestType) !void { |
| 1693 | 1657 | try bw.flush(); |
| 1694 | 1658 | got = buf.items; |
| 1695 | 1659 | |
| 1696 | try expect(mem.eql(u8, got, want_ni)); // expect writeBlock to yield expected result | |
| 1660 | try testing.expectEqualSlices(u8, want_ni, got); // expect writeBlock to yield expected result | |
| 1697 | 1661 | try testWriterEOF(.write_block, ht.tokens, &[0]u8{}); |
| 1698 | 1662 | } |
| 1699 | 1663 | |
| 1700 | fn writeToType(ttype: TestType, bw: anytype, tok: []const token.Token, input: ?[]u8) !void { | |
| 1664 | fn writeToType(ttype: TestType, bw: anytype, tok: []const token.Token, input: ?[]const u8) !void { | |
| 1701 | 1665 | switch (ttype) { |
| 1702 | 1666 | .write_block => try bw.writeBlock(tok, false, input), |
| 1703 | 1667 | .write_dyn_block => try bw.writeBlockDynamic(tok, false, input), |
| ... | ... | @@ -1707,7 +1671,7 @@ fn writeToType(ttype: TestType, bw: anytype, tok: []const token.Token, input: ?[ |
| 1707 | 1671 | } |
| 1708 | 1672 | |
| 1709 | 1673 | // Tests if the written block contains an EOF marker. |
| 1710 | fn testWriterEOF(ttype: TestType, ht_tokens: []const token.Token, input: []u8) !void { | |
| 1674 | fn testWriterEOF(ttype: TestType, ht_tokens: []const token.Token, input: []const u8) !void { | |
| 1711 | 1675 | var buf = ArrayList(u8).init(testing.allocator); |
| 1712 | 1676 | defer buf.deinit(); |
| 1713 | 1677 | var bw = try huffmanBitWriter(testing.allocator, buf.writer()); |
lib/std/compress/deflate/huffman_code.zig+33-33| ... | ... | @@ -386,39 +386,39 @@ test "generate a Huffman code from an array of frequencies" { |
| 386 | 386 | defer enc.deinit(); |
| 387 | 387 | enc.generate(freqs[0..], 7); |
| 388 | 388 | |
| 389 | try testing.expect(enc.bitLength(freqs[0..]) == 141); | |
| 390 | ||
| 391 | try testing.expect(enc.codes[0].len == 3); | |
| 392 | try testing.expect(enc.codes[1].len == 6); | |
| 393 | try testing.expect(enc.codes[2].len == 6); | |
| 394 | try testing.expect(enc.codes[3].len == 5); | |
| 395 | try testing.expect(enc.codes[4].len == 3); | |
| 396 | try testing.expect(enc.codes[5].len == 2); | |
| 397 | try testing.expect(enc.codes[6].len == 2); | |
| 398 | try testing.expect(enc.codes[7].len == 6); | |
| 399 | try testing.expect(enc.codes[8].len == 0); | |
| 400 | try testing.expect(enc.codes[9].len == 0); | |
| 401 | try testing.expect(enc.codes[10].len == 0); | |
| 402 | try testing.expect(enc.codes[11].len == 0); | |
| 403 | try testing.expect(enc.codes[12].len == 0); | |
| 404 | try testing.expect(enc.codes[13].len == 0); | |
| 405 | try testing.expect(enc.codes[14].len == 0); | |
| 406 | try testing.expect(enc.codes[15].len == 0); | |
| 407 | try testing.expect(enc.codes[16].len == 6); | |
| 408 | try testing.expect(enc.codes[17].len == 5); | |
| 409 | try testing.expect(enc.codes[18].len == 3); | |
| 410 | ||
| 411 | try testing.expect(enc.codes[5].code == 0x0); | |
| 412 | try testing.expect(enc.codes[6].code == 0x2); | |
| 413 | try testing.expect(enc.codes[0].code == 0x1); | |
| 414 | try testing.expect(enc.codes[4].code == 0x5); | |
| 415 | try testing.expect(enc.codes[18].code == 0x3); | |
| 416 | try testing.expect(enc.codes[3].code == 0x7); | |
| 417 | try testing.expect(enc.codes[17].code == 0x17); | |
| 418 | try testing.expect(enc.codes[1].code == 0x0f); | |
| 419 | try testing.expect(enc.codes[2].code == 0x2f); | |
| 420 | try testing.expect(enc.codes[7].code == 0x1f); | |
| 421 | try testing.expect(enc.codes[16].code == 0x3f); | |
| 389 | try testing.expectEqual(@as(u32, 141), enc.bitLength(freqs[0..])); | |
| 390 | ||
| 391 | try testing.expectEqual(@as(usize, 3), enc.codes[0].len); | |
| 392 | try testing.expectEqual(@as(usize, 6), enc.codes[1].len); | |
| 393 | try testing.expectEqual(@as(usize, 6), enc.codes[2].len); | |
| 394 | try testing.expectEqual(@as(usize, 5), enc.codes[3].len); | |
| 395 | try testing.expectEqual(@as(usize, 3), enc.codes[4].len); | |
| 396 | try testing.expectEqual(@as(usize, 2), enc.codes[5].len); | |
| 397 | try testing.expectEqual(@as(usize, 2), enc.codes[6].len); | |
| 398 | try testing.expectEqual(@as(usize, 6), enc.codes[7].len); | |
| 399 | try testing.expectEqual(@as(usize, 0), enc.codes[8].len); | |
| 400 | try testing.expectEqual(@as(usize, 0), enc.codes[9].len); | |
| 401 | try testing.expectEqual(@as(usize, 0), enc.codes[10].len); | |
| 402 | try testing.expectEqual(@as(usize, 0), enc.codes[11].len); | |
| 403 | try testing.expectEqual(@as(usize, 0), enc.codes[12].len); | |
| 404 | try testing.expectEqual(@as(usize, 0), enc.codes[13].len); | |
| 405 | try testing.expectEqual(@as(usize, 0), enc.codes[14].len); | |
| 406 | try testing.expectEqual(@as(usize, 0), enc.codes[15].len); | |
| 407 | try testing.expectEqual(@as(usize, 6), enc.codes[16].len); | |
| 408 | try testing.expectEqual(@as(usize, 5), enc.codes[17].len); | |
| 409 | try testing.expectEqual(@as(usize, 3), enc.codes[18].len); | |
| 410 | ||
| 411 | try testing.expectEqual(@as(u16, 0x0), enc.codes[5].code); | |
| 412 | try testing.expectEqual(@as(u16, 0x2), enc.codes[6].code); | |
| 413 | try testing.expectEqual(@as(u16, 0x1), enc.codes[0].code); | |
| 414 | try testing.expectEqual(@as(u16, 0x5), enc.codes[4].code); | |
| 415 | try testing.expectEqual(@as(u16, 0x3), enc.codes[18].code); | |
| 416 | try testing.expectEqual(@as(u16, 0x7), enc.codes[3].code); | |
| 417 | try testing.expectEqual(@as(u16, 0x17), enc.codes[17].code); | |
| 418 | try testing.expectEqual(@as(u16, 0x0f), enc.codes[1].code); | |
| 419 | try testing.expectEqual(@as(u16, 0x2f), enc.codes[2].code); | |
| 420 | try testing.expectEqual(@as(u16, 0x1f), enc.codes[7].code); | |
| 421 | try testing.expectEqual(@as(u16, 0x3f), enc.codes[16].code); | |
| 422 | 422 | } |
| 423 | 423 | |
| 424 | 424 | test "generate a Huffman code for the fixed litteral table specific to Deflate" { |
lib/std/compress/deflate/token.zig+1-2| ... | ... | @@ -99,6 +99,5 @@ pub fn offsetCode(off: u32) u32 { |
| 99 | 99 | |
| 100 | 100 | test { |
| 101 | 101 | const std = @import("std"); |
| 102 | const expect = std.testing.expect; | |
| 103 | try expect(matchToken(555, 555) == 3_401_581_099); | |
| 102 | try std.testing.expectEqual(@as(Token, 3_401_581_099), matchToken(555, 555)); | |
| 104 | 103 | } |
lib/std/compress/gzip.zig+1-1| ... | ... | @@ -163,7 +163,7 @@ fn testReader(data: []const u8, comptime expected: []const u8) !void { |
| 163 | 163 | defer testing.allocator.free(buf); |
| 164 | 164 | |
| 165 | 165 | // Check against the reference |
| 166 | try testing.expectEqualSlices(u8, buf, expected); | |
| 166 | try testing.expectEqualSlices(u8, expected, buf); | |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | // All the test cases are obtained by compressing the RFC1952 text |
lib/std/compress/zlib.zig+1-1| ... | ... | @@ -99,7 +99,7 @@ fn testReader(data: []const u8, expected: []const u8) !void { |
| 99 | 99 | defer testing.allocator.free(buf); |
| 100 | 100 | |
| 101 | 101 | // Check against the reference |
| 102 | try testing.expectEqualSlices(u8, buf, expected); | |
| 102 | try testing.expectEqualSlices(u8, expected, buf); | |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | // All the test cases are obtained by compressing the RFC1951 text |