authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-02-14 19:15:09+11:00
committergravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-02-14 19:15:09+11:00
log6ea6d5a4bdcf6f37c43857570522ee1308115de7
tree4d6052e4b8073a3f1ea7b836172925f600892772
parentca41567924d0576be9a000076d29ec5f46a93e95
signaturelock-open Commit is signed but in an unrecognized format.

std: use testing.allocator in tests


5 files changed, 147 insertions(+), 87 deletions(-)

lib/std/json.zig+15-14
......@@ -1598,21 +1598,21 @@ test "write json then parse it" {
15981598 testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello"));
15991599}
16001600
1601fn test_parse(memory: []u8, json_str: []const u8) !Value {
1602 // buf_alloc goes out of scope, but we don't use it after parsing
1603 var buf_alloc = std.heap.FixedBufferAllocator.init(memory);
1604 var p = Parser.init(&buf_alloc.allocator, false);
1601fn test_parse(arena_allocator: *std.mem.Allocator, json_str: []const u8) !Value {
1602 var p = Parser.init(arena_allocator, false);
16051603 return (try p.parse(json_str)).root;
16061604}
16071605
16081606test "parsing empty string gives appropriate error" {
1609 var memory: [1024 * 4]u8 = undefined;
1610 testing.expectError(error.UnexpectedEndOfJson, test_parse(&memory, ""));
1607 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
1608 defer arena_allocator.deinit();
1609 testing.expectError(error.UnexpectedEndOfJson, test_parse(&arena_allocator.allocator, ""));
16111610}
16121611
16131612test "integer after float has proper type" {
1614 var memory: [1024 * 8]u8 = undefined;
1615 const json = try test_parse(&memory,
1613 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
1614 defer arena_allocator.deinit();
1615 const json = try test_parse(&arena_allocator.allocator,
16161616 \\{
16171617 \\ "float": 3.14,
16181618 \\ "ints": [1, 2, 3]
......@@ -1622,7 +1622,8 @@ test "integer after float has proper type" {
16221622}
16231623
16241624test "escaped characters" {
1625 var memory: [1024 * 16]u8 = undefined;
1625 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
1626 defer arena_allocator.deinit();
16261627 const input =
16271628 \\{
16281629 \\ "backslash": "\\",
......@@ -1638,7 +1639,7 @@ test "escaped characters" {
16381639 \\}
16391640 ;
16401641
1641 const obj = (try test_parse(&memory, input)).Object;
1642 const obj = (try test_parse(&arena_allocator.allocator, input)).Object;
16421643
16431644 testing.expectEqualSlices(u8, obj.get("backslash").?.value.String, "\\");
16441645 testing.expectEqualSlices(u8, obj.get("forwardslash").?.value.String, "/");
......@@ -1662,13 +1663,13 @@ test "string copy option" {
16621663 \\}
16631664 ;
16641665
1665 var mem_buffer: [1024 * 16]u8 = undefined;
1666 var buf_alloc = std.heap.FixedBufferAllocator.init(&mem_buffer);
1666 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
1667 defer arena_allocator.deinit();
16671668
1668 const tree_nocopy = try Parser.init(&buf_alloc.allocator, false).parse(input);
1669 const tree_nocopy = try Parser.init(&arena_allocator.allocator, false).parse(input);
16691670 const obj_nocopy = tree_nocopy.root.Object;
16701671
1671 const tree_copy = try Parser.init(&buf_alloc.allocator, true).parse(input);
1672 const tree_copy = try Parser.init(&arena_allocator.allocator, true).parse(input);
16721673 const obj_copy = tree_copy.root.Object;
16731674
16741675 for ([_][]const u8{ "noescape", "simple", "unicode", "surrogatepair" }) |field_name| {
lib/std/json/write_stream.zig+3-3
......@@ -254,11 +254,11 @@ test "json write stream" {
254254 var slice_stream = std.io.SliceOutStream.init(&out_buf);
255255 const out = &slice_stream.stream;
256256
257 var mem_buf: [1024 * 10]u8 = undefined;
258 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buf).allocator;
257 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
258 defer arena_allocator.deinit();
259259
260260 var w = std.json.WriteStream(@TypeOf(out).Child, 10).init(out);
261 try w.emitJson(try getJson(allocator));
261 try w.emitJson(try getJson(&arena_allocator.allocator));
262262
263263 const result = slice_stream.getWritten();
264264 const expected =
lib/std/math/big/rational.zig+98-53
......@@ -587,14 +587,13 @@ fn gcdLehmer(r: *Int, xa: Int, ya: Int) !void {
587587 r.swap(&x);
588588}
589589
590var buffer: [64 * 8192]u8 = undefined;
591var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);
592var al = &fixed.allocator;
593
594590test "big.rational gcd non-one small" {
595 var a = try Int.initSet(al, 17);
596 var b = try Int.initSet(al, 97);
597 var r = try Int.init(al);
591 var a = try Int.initSet(testing.allocator, 17);
592 defer a.deinit();
593 var b = try Int.initSet(testing.allocator, 97);
594 defer b.deinit();
595 var r = try Int.init(testing.allocator);
596 defer r.deinit();
598597
599598 try gcd(&r, a, b);
600599
......@@ -602,9 +601,12 @@ test "big.rational gcd non-one small" {
602601}
603602
604603test "big.rational gcd non-one small" {
605 var a = try Int.initSet(al, 4864);
606 var b = try Int.initSet(al, 3458);
607 var r = try Int.init(al);
604 var a = try Int.initSet(testing.allocator, 4864);
605 defer a.deinit();
606 var b = try Int.initSet(testing.allocator, 3458);
607 defer b.deinit();
608 var r = try Int.init(testing.allocator);
609 defer r.deinit();
608610
609611 try gcd(&r, a, b);
610612
......@@ -612,9 +614,12 @@ test "big.rational gcd non-one small" {
612614}
613615
614616test "big.rational gcd non-one large" {
615 var a = try Int.initSet(al, 0xffffffffffffffff);
616 var b = try Int.initSet(al, 0xffffffffffffffff7777);
617 var r = try Int.init(al);
617 var a = try Int.initSet(testing.allocator, 0xffffffffffffffff);
618 defer a.deinit();
619 var b = try Int.initSet(testing.allocator, 0xffffffffffffffff7777);
620 defer b.deinit();
621 var r = try Int.init(testing.allocator);
622 defer r.deinit();
618623
619624 try gcd(&r, a, b);
620625
......@@ -622,9 +627,12 @@ test "big.rational gcd non-one large" {
622627}
623628
624629test "big.rational gcd large multi-limb result" {
625 var a = try Int.initSet(al, 0x12345678123456781234567812345678123456781234567812345678);
626 var b = try Int.initSet(al, 0x12345671234567123456712345671234567123456712345671234567);
627 var r = try Int.init(al);
630 var a = try Int.initSet(testing.allocator, 0x12345678123456781234567812345678123456781234567812345678);
631 defer a.deinit();
632 var b = try Int.initSet(testing.allocator, 0x12345671234567123456712345671234567123456712345671234567);
633 defer b.deinit();
634 var r = try Int.init(testing.allocator);
635 defer r.deinit();
628636
629637 try gcd(&r, a, b);
630638
......@@ -632,9 +640,12 @@ test "big.rational gcd large multi-limb result" {
632640}
633641
634642test "big.rational gcd one large" {
635 var a = try Int.initSet(al, 1897056385327307);
636 var b = try Int.initSet(al, 2251799813685248);
637 var r = try Int.init(al);
643 var a = try Int.initSet(testing.allocator, 1897056385327307);
644 defer a.deinit();
645 var b = try Int.initSet(testing.allocator, 2251799813685248);
646 defer b.deinit();
647 var r = try Int.init(testing.allocator);
648 defer r.deinit();
638649
639650 try gcd(&r, a, b);
640651
......@@ -661,7 +672,8 @@ fn extractLowBits(a: Int, comptime T: type) T {
661672}
662673
663674test "big.rational extractLowBits" {
664 var a = try Int.initSet(al, 0x11112222333344441234567887654321);
675 var a = try Int.initSet(testing.allocator, 0x11112222333344441234567887654321);
676 defer a.deinit();
665677
666678 const a1 = extractLowBits(a, u8);
667679 testing.expect(a1 == 0x21);
......@@ -680,7 +692,8 @@ test "big.rational extractLowBits" {
680692}
681693
682694test "big.rational set" {
683 var a = try Rational.init(al);
695 var a = try Rational.init(testing.allocator);
696 defer a.deinit();
684697
685698 try a.setInt(5);
686699 testing.expect((try a.p.to(u32)) == 5);
......@@ -708,7 +721,8 @@ test "big.rational set" {
708721}
709722
710723test "big.rational setFloat" {
711 var a = try Rational.init(al);
724 var a = try Rational.init(testing.allocator);
725 defer a.deinit();
712726
713727 try a.setFloat(f64, 2.5);
714728 testing.expect((try a.p.to(i32)) == 5);
......@@ -732,7 +746,8 @@ test "big.rational setFloat" {
732746}
733747
734748test "big.rational setFloatString" {
735 var a = try Rational.init(al);
749 var a = try Rational.init(testing.allocator);
750 defer a.deinit();
736751
737752 try a.setFloatString("72.14159312071241458852455252781510353");
738753
......@@ -742,7 +757,8 @@ test "big.rational setFloatString" {
742757}
743758
744759test "big.rational toFloat" {
745 var a = try Rational.init(al);
760 var a = try Rational.init(testing.allocator);
761 defer a.deinit();
746762
747763 // = 3.14159297943115234375
748764 try a.setRatio(3294199, 1048576);
......@@ -754,7 +770,8 @@ test "big.rational toFloat" {
754770}
755771
756772test "big.rational set/to Float round-trip" {
757 var a = try Rational.init(al);
773 var a = try Rational.init(testing.allocator);
774 defer a.deinit();
758775 var prng = std.rand.DefaultPrng.init(0x5EED);
759776 var i: usize = 0;
760777 while (i < 512) : (i += 1) {
......@@ -765,23 +782,29 @@ test "big.rational set/to Float round-trip" {
765782}
766783
767784test "big.rational copy" {
768 var a = try Rational.init(al);
785 var a = try Rational.init(testing.allocator);
786 defer a.deinit();
769787
770 const b = try Int.initSet(al, 5);
788 const b = try Int.initSet(testing.allocator, 5);
789 defer b.deinit();
771790
772791 try a.copyInt(b);
773792 testing.expect((try a.p.to(u32)) == 5);
774793 testing.expect((try a.q.to(u32)) == 1);
775794
776 const c = try Int.initSet(al, 7);
777 const d = try Int.initSet(al, 3);
795 const c = try Int.initSet(testing.allocator, 7);
796 defer c.deinit();
797 const d = try Int.initSet(testing.allocator, 3);
798 defer d.deinit();
778799
779800 try a.copyRatio(c, d);
780801 testing.expect((try a.p.to(u32)) == 7);
781802 testing.expect((try a.q.to(u32)) == 3);
782803
783 const e = try Int.initSet(al, 9);
784 const f = try Int.initSet(al, 3);
804 const e = try Int.initSet(testing.allocator, 9);
805 defer e.deinit();
806 const f = try Int.initSet(testing.allocator, 3);
807 defer f.deinit();
785808
786809 try a.copyRatio(e, f);
787810 testing.expect((try a.p.to(u32)) == 3);
......@@ -789,7 +812,8 @@ test "big.rational copy" {
789812}
790813
791814test "big.rational negate" {
792 var a = try Rational.init(al);
815 var a = try Rational.init(testing.allocator);
816 defer a.deinit();
793817
794818 try a.setInt(-50);
795819 testing.expect((try a.p.to(i32)) == -50);
......@@ -805,7 +829,8 @@ test "big.rational negate" {
805829}
806830
807831test "big.rational abs" {
808 var a = try Rational.init(al);
832 var a = try Rational.init(testing.allocator);
833 defer a.deinit();
809834
810835 try a.setInt(-50);
811836 testing.expect((try a.p.to(i32)) == -50);
......@@ -821,8 +846,10 @@ test "big.rational abs" {
821846}
822847
823848test "big.rational swap" {
824 var a = try Rational.init(al);
825 var b = try Rational.init(al);
849 var a = try Rational.init(testing.allocator);
850 defer a.deinit();
851 var b = try Rational.init(testing.allocator);
852 defer b.deinit();
826853
827854 try a.setRatio(50, 23);
828855 try b.setRatio(17, 3);
......@@ -843,8 +870,10 @@ test "big.rational swap" {
843870}
844871
845872test "big.rational cmp" {
846 var a = try Rational.init(al);
847 var b = try Rational.init(al);
873 var a = try Rational.init(testing.allocator);
874 defer a.deinit();
875 var b = try Rational.init(testing.allocator);
876 defer b.deinit();
848877
849878 try a.setRatio(500, 231);
850879 try b.setRatio(18903, 8584);
......@@ -856,8 +885,10 @@ test "big.rational cmp" {
856885}
857886
858887test "big.rational add single-limb" {
859 var a = try Rational.init(al);
860 var b = try Rational.init(al);
888 var a = try Rational.init(testing.allocator);
889 defer a.deinit();
890 var b = try Rational.init(testing.allocator);
891 defer b.deinit();
861892
862893 try a.setRatio(500, 231);
863894 try b.setRatio(18903, 8584);
......@@ -869,9 +900,12 @@ test "big.rational add single-limb" {
869900}
870901
871902test "big.rational add" {
872 var a = try Rational.init(al);
873 var b = try Rational.init(al);
874 var r = try Rational.init(al);
903 var a = try Rational.init(testing.allocator);
904 defer a.deinit();
905 var b = try Rational.init(testing.allocator);
906 defer b.deinit();
907 var r = try Rational.init(testing.allocator);
908 defer r.deinit();
875909
876910 try a.setRatio(78923, 23341);
877911 try b.setRatio(123097, 12441414);
......@@ -882,9 +916,12 @@ test "big.rational add" {
882916}
883917
884918test "big.rational sub" {
885 var a = try Rational.init(al);
886 var b = try Rational.init(al);
887 var r = try Rational.init(al);
919 var a = try Rational.init(testing.allocator);
920 defer a.deinit();
921 var b = try Rational.init(testing.allocator);
922 defer b.deinit();
923 var r = try Rational.init(testing.allocator);
924 defer r.deinit();
888925
889926 try a.setRatio(78923, 23341);
890927 try b.setRatio(123097, 12441414);
......@@ -895,9 +932,12 @@ test "big.rational sub" {
895932}
896933
897934test "big.rational mul" {
898 var a = try Rational.init(al);
899 var b = try Rational.init(al);
900 var r = try Rational.init(al);
935 var a = try Rational.init(testing.allocator);
936 defer a.deinit();
937 var b = try Rational.init(testing.allocator);
938 defer b.deinit();
939 var r = try Rational.init(testing.allocator);
940 defer r.deinit();
901941
902942 try a.setRatio(78923, 23341);
903943 try b.setRatio(123097, 12441414);
......@@ -908,9 +948,12 @@ test "big.rational mul" {
908948}
909949
910950test "big.rational div" {
911 var a = try Rational.init(al);
912 var b = try Rational.init(al);
913 var r = try Rational.init(al);
951 var a = try Rational.init(testing.allocator);
952 defer a.deinit();
953 var b = try Rational.init(testing.allocator);
954 defer b.deinit();
955 var r = try Rational.init(testing.allocator);
956 defer r.deinit();
914957
915958 try a.setRatio(78923, 23341);
916959 try b.setRatio(123097, 12441414);
......@@ -921,8 +964,10 @@ test "big.rational div" {
921964}
922965
923966test "big.rational div" {
924 var a = try Rational.init(al);
925 var r = try Rational.init(al);
967 var a = try Rational.init(testing.allocator);
968 defer a.deinit();
969 var r = try Rational.init(testing.allocator);
970 defer r.deinit();
926971
927972 try a.setRatio(78923, 23341);
928973 a.invert();
lib/std/mem.zig+30-14
......@@ -1011,11 +1011,21 @@ pub fn join(allocator: *Allocator, separator: []const u8, slices: []const []cons
10111011}
10121012
10131013test "mem.join" {
1014 var buf: [1024]u8 = undefined;
1015 const a = &std.heap.FixedBufferAllocator.init(&buf).allocator;
1016 testing.expect(eql(u8, try join(a, ",", &[_][]const u8{ "a", "b", "c" }), "a,b,c"));
1017 testing.expect(eql(u8, try join(a, ",", &[_][]const u8{"a"}), "a"));
1018 testing.expect(eql(u8, try join(a, ",", &[_][]const u8{ "a", "", "b", "", "c" }), "a,,b,,c"));
1014 {
1015 const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" });
1016 defer testing.allocator.free(str);
1017 testing.expect(eql(u8, str, "a,b,c"));
1018 }
1019 {
1020 const str = try join(testing.allocator, ",", &[_][]const u8{"a"});
1021 defer testing.allocator.free(str);
1022 testing.expect(eql(u8, str, "a"));
1023 }
1024 {
1025 const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "", "b", "", "c" });
1026 defer testing.allocator.free(str);
1027 testing.expect(eql(u8, str, "a,,b,,c"));
1028 }
10191029}
10201030
10211031/// Copies each T from slices into a new slice that exactly holds all the elements.
......@@ -1044,15 +1054,21 @@ pub fn concat(allocator: *Allocator, comptime T: type, slices: []const []const T
10441054}
10451055
10461056test "concat" {
1047 var buf: [1024]u8 = undefined;
1048 const a = &std.heap.FixedBufferAllocator.init(&buf).allocator;
1049 testing.expect(eql(u8, try concat(a, u8, &[_][]const u8{ "abc", "def", "ghi" }), "abcdefghi"));
1050 testing.expect(eql(u32, try concat(a, u32, &[_][]const u32{
1051 &[_]u32{ 0, 1 },
1052 &[_]u32{ 2, 3, 4 },
1053 &[_]u32{},
1054 &[_]u32{5},
1055 }), &[_]u32{ 0, 1, 2, 3, 4, 5 }));
1057 {
1058 const str = try concat(testing.allocator, u8, &[_][]const u8{ "abc", "def", "ghi" });
1059 defer testing.allocator.free(str);
1060 testing.expect(eql(u8, str, "abcdefghi"));
1061 }
1062 {
1063 const str = try concat(testing.allocator, u32, &[_][]const u32{
1064 &[_]u32{ 0, 1 },
1065 &[_]u32{ 2, 3, 4 },
1066 &[_]u32{},
1067 &[_]u32{5},
1068 });
1069 defer testing.allocator.free(str);
1070 testing.expect(eql(u32, str, &[_]u32{ 0, 1, 2, 3, 4, 5 }));
1071 }
10561072}
10571073
10581074test "testStringEquality" {
lib/std/net/test.zig+1-3
......@@ -67,10 +67,8 @@ test "resolve DNS" {
6767 // DNS resolution not implemented on Windows yet.
6868 return error.SkipZigTest;
6969 }
70 var buf: [1000 * 10]u8 = undefined;
71 const a = &std.heap.FixedBufferAllocator.init(&buf).allocator;
7270
73 const address_list = net.getAddressList(a, "example.com", 80) catch |err| switch (err) {
71 const address_list = net.getAddressList(testing.allocator, "example.com", 80) catch |err| switch (err) {
7472 // The tests are required to work even when there is no Internet connection,
7573 // so some of these errors we must accept and skip the test.
7674 error.UnknownHostName => return error.SkipZigTest,