authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2022-12-14 21:30:35-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-15 05:13:21-05:00
log077b003c50065a8b193dcad793ca5c4474222af6
treec30a616f3130b2ed70ddfb418bf28e36ec15ad0a
parent88d927a511922efd82c4ec862c8e2aa83df84391

std.compress: Improve tests, remove reliance on openDirAbsolute

- 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 diagnostics

12 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 {
88
99test "bitReverse" {
1010 const std = @import("std");
11 const expect = std.testing.expect;
1211
1312 const ReverseBitsTest = struct {
1413 in: u16,
......@@ -29,6 +28,6 @@ test "bitReverse" {
2928
3029 for (reverse_bits_tests) |h| {
3130 var v = bitReverse(u16, h.in, h.bit_count);
32 try expect(v == h.out);
31 try std.testing.expectEqual(h.out, v);
3332 }
3433}
lib/std/compress/deflate/compressor.zig+2-2
......@@ -1079,7 +1079,7 @@ test "deflate" {
10791079 try comp.close();
10801080 comp.deinit();
10811081
1082 try expect(mem.eql(u8, output.items, dt.out));
1082 try testing.expectEqualSlices(u8, dt.out, output.items);
10831083 }
10841084}
10851085
......@@ -1104,7 +1104,7 @@ test "bulkHash4" {
11041104 _ = bulkHash4(y, dst);
11051105 for (dst) |got, i| {
11061106 var want = hash4(y[i..]);
1107 try expect(got == want);
1107 try testing.expectEqual(want, got);
11081108 }
11091109 }
11101110 }
lib/std/compress/deflate/compressor_test.zig+19-35
......@@ -62,8 +62,8 @@ fn testSync(level: deflate.Compression, input: []const u8) !void {
6262 defer testing.allocator.free(decompressed);
6363
6464 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);
6767 }
6868
6969 // Write last half of the input and close()
......@@ -79,13 +79,13 @@ fn testSync(level: deflate.Compression, input: []const u8) !void {
7979 defer testing.allocator.free(decompressed);
8080
8181 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);
8484
8585 // Extra read
8686 var final: [10]u8 = undefined;
8787 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
8989
9090 _ = decomp.close();
9191 }
......@@ -105,7 +105,7 @@ fn testSync(level: deflate.Compression, input: []const u8) !void {
105105 _ = try decomp.reader().readAll(decompressed);
106106 _ = decomp.close();
107107
108 try expect(mem.eql(u8, input, decompressed));
108 try testing.expectEqualSlices(u8, input, decompressed);
109109}
110110
111111fn testToFromWithLevelAndLimit(level: deflate.Compression, input: []const u8, limit: u32) !void {
......@@ -130,8 +130,8 @@ fn testToFromWithLevelAndLimit(level: deflate.Compression, input: []const u8, li
130130 defer testing.allocator.free(decompressed);
131131
132132 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);
135135
136136 if (false) {
137137 // TODO: this test has regressed
......@@ -237,7 +237,7 @@ test "very long sparse chunk" {
237237 read = try reader.read(&buf);
238238 written += try writer.write(buf[0..read]);
239239 }
240 try expect(written == 0x23e8);
240 try testing.expectEqual(@as(usize, 0x23e8), written);
241241}
242242
243243test "compressor reset" {
......@@ -287,7 +287,7 @@ fn testWriterReset(level: deflate.Compression, dict: ?[]const u8) !void {
287287 try filler.writeData(&comp);
288288 try comp.close();
289289
290 try expect(mem.eql(u8, buf1.items, buf2.items));
290 try testing.expectEqualSlices(u8, buf1.items, buf2.items);
291291}
292292
293293test "decompressor dictionary" {
......@@ -327,7 +327,7 @@ test "decompressor dictionary" {
327327 defer decomp.deinit();
328328
329329 _ = try decomp.reader().readAll(decompressed);
330 try expect(mem.eql(u8, decompressed, "hello again world"));
330 try testing.expectEqualSlices(u8, "hello again world", decompressed);
331331}
332332
333333test "compressor dictionary" {
......@@ -371,7 +371,7 @@ test "compressor dictionary" {
371371 try comp_d.writer().writeAll(text);
372372 try comp_d.close();
373373
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));
375375}
376376
377377// Update the hash for best_speed only if d.index < d.maxInsertIndex
......@@ -394,18 +394,12 @@ test "Go non-regression test for 2508" {
394394}
395395
396396test "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
403397 const StringTest = struct {
404398 filename: []const u8,
405399 limit: [11]u32,
406400 };
407401
408 var deflate_inflate_string_tests = [_]StringTest{
402 const deflate_inflate_string_tests = [_]StringTest{
409403 .{
410404 .filename = "compress-e.txt",
411405 .limit = [11]u32{
......@@ -440,12 +434,8 @@ test "deflate/inflate string" {
440434 },
441435 };
442436
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);
449439 try testToFromWithLimit(golden, t.limit);
450440 }
451441}
......@@ -492,11 +482,8 @@ test "inflate reset" {
492482
493483 _ = decomp.close();
494484
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);
500487}
501488
502489test "inflate reset dictionary" {
......@@ -542,9 +529,6 @@ test "inflate reset dictionary" {
542529
543530 _ = decomp.close();
544531
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);
550534}
lib/std/compress/deflate/decompressor.zig+3-4
......@@ -895,7 +895,6 @@ pub fn Decompressor(comptime ReaderType: type) type {
895895}
896896
897897// tests
898const expect = std.testing.expect;
899898const expectError = std.testing.expectError;
900899const io = std.io;
901900const testing = std.testing;
......@@ -928,7 +927,7 @@ test "truncated input" {
928927
929928 var output = [1]u8{0} ** 12;
930929 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]);
932931 }
933932}
934933
......@@ -1026,8 +1025,8 @@ test "inflate A Tale of Two Cities (1859) intro" {
10261025
10271026 var got: [700]u8 = undefined;
10281027 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]);
10311030}
10321031
10331032test "lengths overflow" {
lib/std/compress/deflate/deflate_fast.zig+20-21
......@@ -354,7 +354,7 @@ pub const DeflateFast = struct {
354354};
355355
356356test "best speed match 1/3" {
357 const expect = std.testing.expect;
357 const expectEqual = std.testing.expectEqual;
358358
359359 {
360360 var previous = [_]u8{ 0, 0, 0, 1, 2 };
......@@ -367,7 +367,7 @@ test "best speed match 1/3" {
367367 };
368368 var current = [_]u8{ 3, 4, 5, 0, 1, 2, 3, 4, 5 };
369369 var got: i32 = e.matchLen(3, -3, &current);
370 try expect(got == 6);
370 try expectEqual(@as(i32, 6), got);
371371 }
372372 {
373373 var previous = [_]u8{ 0, 0, 0, 1, 2 };
......@@ -380,7 +380,7 @@ test "best speed match 1/3" {
380380 };
381381 var current = [_]u8{ 2, 4, 5, 0, 1, 2, 3, 4, 5 };
382382 var got: i32 = e.matchLen(3, -3, &current);
383 try expect(got == 3);
383 try expectEqual(@as(i32, 3), got);
384384 }
385385 {
386386 var previous = [_]u8{ 0, 0, 0, 1, 1 };
......@@ -393,7 +393,7 @@ test "best speed match 1/3" {
393393 };
394394 var current = [_]u8{ 3, 4, 5, 0, 1, 2, 3, 4, 5 };
395395 var got: i32 = e.matchLen(3, -3, &current);
396 try expect(got == 2);
396 try expectEqual(@as(i32, 2), got);
397397 }
398398 {
399399 var previous = [_]u8{ 0, 0, 0, 1, 2 };
......@@ -406,7 +406,7 @@ test "best speed match 1/3" {
406406 };
407407 var current = [_]u8{ 2, 2, 2, 2, 1, 2, 3, 4, 5 };
408408 var got: i32 = e.matchLen(0, -1, &current);
409 try expect(got == 4);
409 try expectEqual(@as(i32, 4), got);
410410 }
411411 {
412412 var previous = [_]u8{ 0, 0, 0, 1, 2, 3, 4, 5, 2, 2 };
......@@ -419,7 +419,7 @@ test "best speed match 1/3" {
419419 };
420420 var current = [_]u8{ 2, 2, 2, 2, 1, 2, 3, 4, 5 };
421421 var got: i32 = e.matchLen(4, -7, &current);
422 try expect(got == 5);
422 try expectEqual(@as(i32, 5), got);
423423 }
424424 {
425425 var previous = [_]u8{ 9, 9, 9, 9, 9 };
......@@ -432,7 +432,7 @@ test "best speed match 1/3" {
432432 };
433433 var current = [_]u8{ 2, 2, 2, 2, 1, 2, 3, 4, 5 };
434434 var got: i32 = e.matchLen(0, -1, &current);
435 try expect(got == 0);
435 try expectEqual(@as(i32, 0), got);
436436 }
437437 {
438438 var previous = [_]u8{ 9, 9, 9, 9, 9 };
......@@ -445,12 +445,12 @@ test "best speed match 1/3" {
445445 };
446446 var current = [_]u8{ 9, 2, 2, 2, 1, 2, 3, 4, 5 };
447447 var got: i32 = e.matchLen(1, 0, &current);
448 try expect(got == 0);
448 try expectEqual(@as(i32, 0), got);
449449 }
450450}
451451
452452test "best speed match 2/3" {
453 const expect = std.testing.expect;
453 const expectEqual = std.testing.expectEqual;
454454
455455 {
456456 var previous = [_]u8{};
......@@ -463,7 +463,7 @@ test "best speed match 2/3" {
463463 };
464464 var current = [_]u8{ 9, 2, 2, 2, 1, 2, 3, 4, 5 };
465465 var got: i32 = e.matchLen(1, -5, &current);
466 try expect(got == 0);
466 try expectEqual(@as(i32, 0), got);
467467 }
468468 {
469469 var previous = [_]u8{};
......@@ -476,7 +476,7 @@ test "best speed match 2/3" {
476476 };
477477 var current = [_]u8{ 9, 2, 2, 2, 1, 2, 3, 4, 5 };
478478 var got: i32 = e.matchLen(1, -1, &current);
479 try expect(got == 0);
479 try expectEqual(@as(i32, 0), got);
480480 }
481481 {
482482 var previous = [_]u8{};
......@@ -489,7 +489,7 @@ test "best speed match 2/3" {
489489 };
490490 var current = [_]u8{ 2, 2, 2, 2, 1, 2, 3, 4, 5 };
491491 var got: i32 = e.matchLen(1, 0, &current);
492 try expect(got == 3);
492 try expectEqual(@as(i32, 3), got);
493493 }
494494 {
495495 var previous = [_]u8{ 3, 4, 5 };
......@@ -502,13 +502,13 @@ test "best speed match 2/3" {
502502 };
503503 var current = [_]u8{ 3, 4, 5 };
504504 var got: i32 = e.matchLen(0, -3, &current);
505 try expect(got == 3);
505 try expectEqual(@as(i32, 3), got);
506506 }
507507}
508508
509509test "best speed match 2/2" {
510510 const testing = std.testing;
511 const expect = testing.expect;
511 const expectEqual = testing.expectEqual;
512512
513513 const Case = struct {
514514 previous: u32,
......@@ -580,7 +580,7 @@ test "best speed match 2/2" {
580580 .cur = 0,
581581 };
582582 var got: i32 = e.matchLen(c.s, c.t, current);
583 try expect(got == c.expected);
583 try expectEqual(@as(i32, c.expected), got);
584584 }
585585}
586586
......@@ -623,16 +623,16 @@ test "best speed shift offsets" {
623623 tokens_count = 0;
624624 enc.encode(&tokens, &tokens_count, &test_data);
625625 var got = tokens_count;
626 try expect(want_first_tokens == got);
626 try testing.expectEqual(want_first_tokens, got);
627627
628628 // Verify we are about to wrap.
629 try expect(enc.cur == buffer_reset);
629 try testing.expectEqual(@as(i32, buffer_reset), enc.cur);
630630
631631 // Part 2 should match clean state as well even if wrapped.
632632 tokens_count = 0;
633633 enc.encode(&tokens, &tokens_count, &test_data);
634634 got = tokens_count;
635 try expect(want_second_tokens == got);
635 try testing.expectEqual(want_second_tokens, got);
636636
637637 // Verify that we wrapped.
638638 try expect(enc.cur < buffer_reset);
......@@ -645,13 +645,12 @@ test "best speed shift offsets" {
645645 tokens_count = 0;
646646 enc.encode(&tokens, &tokens_count, &test_data);
647647 got = tokens_count;
648 try expect(want_first_tokens == got);
648 try testing.expectEqual(want_first_tokens, got);
649649}
650650
651651test "best speed reset" {
652652 // test that encoding is consistent across a warparound of the table offset.
653653 // See https://github.com/golang/go/issues/34121
654 const expect = std.testing.expect;
655654 const fmt = std.fmt;
656655 const testing = std.testing;
657656
......@@ -716,6 +715,6 @@ test "best speed reset" {
716715 try comp.close();
717716
718717 // output must match at wraparound
719 try expect(mem.eql(u8, got.items, want.items));
718 try testing.expectEqualSlices(u8, want.items, got.items);
720719 }
721720}
lib/std/compress/deflate/deflate_fast_test.zig+4-4
......@@ -85,8 +85,8 @@ test "best speed" {
8585 var read = try decomp.reader().readAll(decompressed);
8686 _ = decomp.close();
8787
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);
9090 }
9191 }
9292 }
......@@ -152,8 +152,8 @@ test "best speed max match offset" {
152152 var read = try decomp.reader().readAll(decompressed);
153153 _ = decomp.close();
154154
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);
157157 }
158158 }
159159 }
lib/std/compress/deflate/dict_decoder.zig+1-2
......@@ -206,7 +206,6 @@ pub const DictDecoder = struct {
206206
207207test "dictionary decoder" {
208208 const ArrayList = std.ArrayList;
209 const expect = std.testing.expect;
210209 const testing = std.testing;
211210
212211 const abc = "ABC\n";
......@@ -416,5 +415,5 @@ test "dictionary decoder" {
416415 _ = try want.write(want_list.items[want_list.items.len - dd.histSize() ..][0..10]);
417416
418417 _ = 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);
420419}
lib/std/compress/deflate/huffman_bit_writer.zig+26-62
......@@ -121,7 +121,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type {
121121 self.nbytes = 0;
122122 }
123123
124 fn write(self: *Self, b: []u8) Error!void {
124 fn write(self: *Self, b: []const u8) Error!void {
125125 if (self.err) {
126126 return;
127127 }
......@@ -155,7 +155,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type {
155155 }
156156 }
157157
158 pub fn writeBytes(self: *Self, bytes: []u8) Error!void {
158 pub fn writeBytes(self: *Self, bytes: []const u8) Error!void {
159159 if (self.err) {
160160 return;
161161 }
......@@ -323,7 +323,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type {
323323 // storedSizeFits calculates the stored size, including header.
324324 // The function returns the size in bits and whether the block
325325 // fits inside a single block.
326 fn storedSizeFits(in: ?[]u8) StoredSize {
326 fn storedSizeFits(in: ?[]const u8) StoredSize {
327327 if (in == null) {
328328 return .{ .size = 0, .storable = false };
329329 }
......@@ -453,7 +453,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type {
453453 self: *Self,
454454 tokens: []const token.Token,
455455 eof: bool,
456 input: ?[]u8,
456 input: ?[]const u8,
457457 ) Error!void {
458458 if (self.err) {
459459 return;
......@@ -546,7 +546,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type {
546546 self: *Self,
547547 tokens: []const token.Token,
548548 eof: bool,
549 input: ?[]u8,
549 input: ?[]const u8,
550550 ) Error!void {
551551 if (self.err) {
552552 return;
......@@ -685,7 +685,7 @@ pub fn HuffmanBitWriter(comptime WriterType: type) type {
685685
686686 // Encodes a block of bytes as either Huffman encoded literals or uncompressed bytes
687687 // 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 {
689689 if (self.err) {
690690 return;
691691 }
......@@ -828,7 +828,7 @@ pub fn huffmanBitWriter(allocator: Allocator, writer: anytype) !HuffmanBitWriter
828828// histogram accumulates a histogram of b in h.
829829//
830830// h.len must be >= 256, and h's elements must be all zeroes.
831fn histogram(b: []u8, h: *[]u16) void {
831fn histogram(b: []const u8, h: *[]u16) void {
832832 var lh = h.*[0..256];
833833 for (b) |t| {
834834 lh[t] += 1;
......@@ -891,21 +891,9 @@ test "writeBlockHuff" {
891891 );
892892}
893893
894fn 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);
894fn 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);
909897
910898 var buf = ArrayList(u8).init(testing.allocator);
911899 defer buf.deinit();
......@@ -914,7 +902,7 @@ fn testBlockHuff(in_name: []const u8, want_name: []const u8) !void {
914902 try bw.writeBlockHuff(false, in);
915903 try bw.flush();
916904
917 try expect(mem.eql(u8, buf.items, want));
905 try std.testing.expectEqualSlices(u8, want, buf.items);
918906
919907 // Test if the writer produces the same output after reset.
920908 var buf_after_reset = ArrayList(u8).init(testing.allocator);
......@@ -925,8 +913,8 @@ fn testBlockHuff(in_name: []const u8, want_name: []const u8) !void {
925913 try bw.writeBlockHuff(false, in);
926914 try bw.flush();
927915
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);
930918
931919 try testWriterEOF(.write_huffman_block, &[0]token.Token{}, in);
932920}
......@@ -1617,38 +1605,18 @@ test "writeBlockDynamic" {
16171605
16181606// testBlock tests a block against its references,
16191607// or regenerate the references, if "-update" flag is set.
1620fn 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);
1608fn 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);
16451613
16461614 var buf = ArrayList(u8).init(testing.allocator);
16471615 var bw = try huffmanBitWriter(testing.allocator, buf.writer());
16481616 try writeToType(ttype, &bw, ht.tokens, input);
16491617
16501618 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
16521620
16531621 // Test if the writer produces the same output after reset.
16541622 buf.deinit();
......@@ -1661,16 +1629,12 @@ fn testBlock(comptime ht: HuffTest, ttype: TestType) !void {
16611629 try writeToType(ttype, &bw, ht.tokens, input);
16621630 try bw.flush();
16631631 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
16651633 try testWriterEOF(.write_block, ht.tokens, input);
16661634 }
16671635
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);
16741638
16751639 var buf = ArrayList(u8).init(testing.allocator);
16761640 var bw = try huffmanBitWriter(testing.allocator, buf.writer());
......@@ -1678,7 +1642,7 @@ fn testBlock(comptime ht: HuffTest, ttype: TestType) !void {
16781642 try writeToType(ttype, &bw, ht.tokens, null);
16791643
16801644 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
16821646 try expect(got[0] & 1 != 1); // expect no EOF
16831647
16841648 // Test if the writer produces the same output after reset.
......@@ -1693,11 +1657,11 @@ fn testBlock(comptime ht: HuffTest, ttype: TestType) !void {
16931657 try bw.flush();
16941658 got = buf.items;
16951659
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
16971661 try testWriterEOF(.write_block, ht.tokens, &[0]u8{});
16981662}
16991663
1700fn writeToType(ttype: TestType, bw: anytype, tok: []const token.Token, input: ?[]u8) !void {
1664fn writeToType(ttype: TestType, bw: anytype, tok: []const token.Token, input: ?[]const u8) !void {
17011665 switch (ttype) {
17021666 .write_block => try bw.writeBlock(tok, false, input),
17031667 .write_dyn_block => try bw.writeBlockDynamic(tok, false, input),
......@@ -1707,7 +1671,7 @@ fn writeToType(ttype: TestType, bw: anytype, tok: []const token.Token, input: ?[
17071671}
17081672
17091673// Tests if the written block contains an EOF marker.
1710fn testWriterEOF(ttype: TestType, ht_tokens: []const token.Token, input: []u8) !void {
1674fn testWriterEOF(ttype: TestType, ht_tokens: []const token.Token, input: []const u8) !void {
17111675 var buf = ArrayList(u8).init(testing.allocator);
17121676 defer buf.deinit();
17131677 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" {
386386 defer enc.deinit();
387387 enc.generate(freqs[0..], 7);
388388
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);
422422}
423423
424424test "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 {
9999
100100test {
101101 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));
104103}
lib/std/compress/gzip.zig+1-1
......@@ -163,7 +163,7 @@ fn testReader(data: []const u8, comptime expected: []const u8) !void {
163163 defer testing.allocator.free(buf);
164164
165165 // Check against the reference
166 try testing.expectEqualSlices(u8, buf, expected);
166 try testing.expectEqualSlices(u8, expected, buf);
167167}
168168
169169// 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 {
9999 defer testing.allocator.free(buf);
100100
101101 // Check against the reference
102 try testing.expectEqualSlices(u8, buf, expected);
102 try testing.expectEqualSlices(u8, expected, buf);
103103}
104104
105105// All the test cases are obtained by compressing the RFC1951 text