authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-13 16:17:21-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-02-13 16:17:21-05:00
loge8dfc5e7f615c0c8a529623560d6adc3dc029c1b
tree5f852c0a8cde55562d2fe86db81b745e9ffb4f1b
parentde23c571334b6df172751f45e6eda02a138f7cdf
parenta81ae1223d4ea4aea689fc8a03004abfb865a884
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4442 from fengb/testing-allocator-calls

Migrate tests from FixedBufferAllocator to testing.allocator

16 files changed, 77 insertions(+), 117 deletions(-)

lib/std/array_list.zig+3-11
...@@ -244,10 +244,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {...@@ -244,10 +244,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
244}244}
245245
246test "std.ArrayList.init" {246test "std.ArrayList.init" {
247 var bytes: [1024]u8 = undefined;247 var list = ArrayList(i32).init(testing.allocator);
248 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
249
250 var list = ArrayList(i32).init(allocator);
251 defer list.deinit();248 defer list.deinit();
252249
253 testing.expect(list.len == 0);250 testing.expect(list.len == 0);
...@@ -255,19 +252,14 @@ test "std.ArrayList.init" {...@@ -255,19 +252,14 @@ test "std.ArrayList.init" {
255}252}
256253
257test "std.ArrayList.initCapacity" {254test "std.ArrayList.initCapacity" {
258 var bytes: [1024]u8 = undefined;255 var list = try ArrayList(i8).initCapacity(testing.allocator, 200);
259 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
260 var list = try ArrayList(i8).initCapacity(allocator, 200);
261 defer list.deinit();256 defer list.deinit();
262 testing.expect(list.len == 0);257 testing.expect(list.len == 0);
263 testing.expect(list.capacity() >= 200);258 testing.expect(list.capacity() >= 200);
264}259}
265260
266test "std.ArrayList.basic" {261test "std.ArrayList.basic" {
267 var bytes: [1024]u8 = undefined;262 var list = ArrayList(i32).init(testing.allocator);
268 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
269
270 var list = ArrayList(i32).init(allocator);
271 defer list.deinit();263 defer list.deinit();
272264
273 // setting on empty list is out of bounds265 // setting on empty list is out of bounds
lib/std/ascii.zig+2-3
...@@ -236,9 +236,8 @@ pub fn allocLowerString(allocator: *std.mem.Allocator, ascii_string: []const u8)...@@ -236,9 +236,8 @@ pub fn allocLowerString(allocator: *std.mem.Allocator, ascii_string: []const u8)
236}236}
237237
238test "allocLowerString" {238test "allocLowerString" {
239 var buf: [100]u8 = undefined;239 const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
240 const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;240 defer std.testing.allocator.free(result);
241 const result = try allocLowerString(allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
242 std.testing.expect(std.mem.eql(u8, "abcdefghijklmnopqrst0234+💩!", result));241 std.testing.expect(std.mem.eql(u8, "abcdefghijklmnopqrst0234+💩!", result));
243}242}
244243
lib/std/cstr.zig+2-3
...@@ -41,9 +41,8 @@ pub fn addNullByte(allocator: *mem.Allocator, slice: []const u8) ![:0]u8 {...@@ -41,9 +41,8 @@ pub fn addNullByte(allocator: *mem.Allocator, slice: []const u8) ![:0]u8 {
41}41}
4242
43test "addNullByte" {43test "addNullByte" {
44 var buf: [30]u8 = undefined;44 const slice = try addNullByte(std.testing.allocator, "hello"[0..4]);
45 const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;45 defer std.testing.allocator.free(slice);
46 const slice = try addNullByte(allocator, "hello"[0..4]);
47 testing.expect(slice.len == 4);46 testing.expect(slice.len == 4);
48 testing.expect(slice[4] == 0);47 testing.expect(slice[4] == 0);
49}48}
lib/std/fs/get_app_data_dir.zig+2-4
...@@ -56,9 +56,7 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD...@@ -56,9 +56,7 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD
56}56}
5757
58test "getAppDataDir" {58test "getAppDataDir" {
59 var buf: [512]u8 = undefined;
60 const allocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;
61
62 // We can't actually validate the result59 // We can't actually validate the result
63 _ = getAppDataDir(allocator, "zig") catch return;60 const dir = getAppDataDir(std.testing.allocator, "zig") catch return;
61 defer std.testing.allocator.free(dir);
64}62}
lib/std/fs/path.zig+4-6
...@@ -89,16 +89,14 @@ pub fn joinPosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {...@@ -89,16 +89,14 @@ pub fn joinPosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
89}89}
9090
91fn testJoinWindows(paths: []const []const u8, expected: []const u8) void {91fn testJoinWindows(paths: []const []const u8, expected: []const u8) void {
92 var buf: [1024]u8 = undefined;92 const actual = joinWindows(testing.allocator, paths) catch @panic("fail");
93 const a = &std.heap.FixedBufferAllocator.init(&buf).allocator;93 defer testing.allocator.free(actual);
94 const actual = joinWindows(a, paths) catch @panic("fail");
95 testing.expectEqualSlices(u8, expected, actual);94 testing.expectEqualSlices(u8, expected, actual);
96}95}
9796
98fn testJoinPosix(paths: []const []const u8, expected: []const u8) void {97fn testJoinPosix(paths: []const []const u8, expected: []const u8) void {
99 var buf: [1024]u8 = undefined;98 const actual = joinPosix(testing.allocator, paths) catch @panic("fail");
100 const a = &std.heap.FixedBufferAllocator.init(&buf).allocator;99 defer testing.allocator.free(actual);
101 const actual = joinPosix(a, paths) catch @panic("fail");
102 testing.expectEqualSlices(u8, expected, actual);100 testing.expectEqualSlices(u8, expected, actual);
103}101}
104102
lib/std/http/headers.zig+22-26
...@@ -83,12 +83,8 @@ const HeaderEntry = struct {...@@ -83,12 +83,8 @@ const HeaderEntry = struct {
83 }83 }
84};84};
8585
86var test_memory: [32 * 1024]u8 = undefined;
87var test_fba_state = std.heap.FixedBufferAllocator.init(&test_memory);
88const test_allocator = &test_fba_state.allocator;
89
90test "HeaderEntry" {86test "HeaderEntry" {
91 var e = try HeaderEntry.init(test_allocator, "foo", "bar", null);87 var e = try HeaderEntry.init(testing.allocator, "foo", "bar", null);
92 defer e.deinit();88 defer e.deinit();
93 testing.expectEqualSlices(u8, "foo", e.name);89 testing.expectEqualSlices(u8, "foo", e.name);
94 testing.expectEqualSlices(u8, "bar", e.value);90 testing.expectEqualSlices(u8, "bar", e.value);
...@@ -368,7 +364,7 @@ pub const Headers = struct {...@@ -368,7 +364,7 @@ pub const Headers = struct {
368};364};
369365
370test "Headers.iterator" {366test "Headers.iterator" {
371 var h = Headers.init(test_allocator);367 var h = Headers.init(testing.allocator);
372 defer h.deinit();368 defer h.deinit();
373 try h.append("foo", "bar", null);369 try h.append("foo", "bar", null);
374 try h.append("cookie", "somevalue", null);370 try h.append("cookie", "somevalue", null);
...@@ -390,7 +386,7 @@ test "Headers.iterator" {...@@ -390,7 +386,7 @@ test "Headers.iterator" {
390}386}
391387
392test "Headers.contains" {388test "Headers.contains" {
393 var h = Headers.init(test_allocator);389 var h = Headers.init(testing.allocator);
394 defer h.deinit();390 defer h.deinit();
395 try h.append("foo", "bar", null);391 try h.append("foo", "bar", null);
396 try h.append("cookie", "somevalue", null);392 try h.append("cookie", "somevalue", null);
...@@ -400,7 +396,7 @@ test "Headers.contains" {...@@ -400,7 +396,7 @@ test "Headers.contains" {
400}396}
401397
402test "Headers.delete" {398test "Headers.delete" {
403 var h = Headers.init(test_allocator);399 var h = Headers.init(testing.allocator);
404 defer h.deinit();400 defer h.deinit();
405 try h.append("foo", "bar", null);401 try h.append("foo", "bar", null);
406 try h.append("baz", "qux", null);402 try h.append("baz", "qux", null);
...@@ -428,7 +424,7 @@ test "Headers.delete" {...@@ -428,7 +424,7 @@ test "Headers.delete" {
428}424}
429425
430test "Headers.orderedRemove" {426test "Headers.orderedRemove" {
431 var h = Headers.init(test_allocator);427 var h = Headers.init(testing.allocator);
432 defer h.deinit();428 defer h.deinit();
433 try h.append("foo", "bar", null);429 try h.append("foo", "bar", null);
434 try h.append("baz", "qux", null);430 try h.append("baz", "qux", null);
...@@ -451,7 +447,7 @@ test "Headers.orderedRemove" {...@@ -451,7 +447,7 @@ test "Headers.orderedRemove" {
451}447}
452448
453test "Headers.swapRemove" {449test "Headers.swapRemove" {
454 var h = Headers.init(test_allocator);450 var h = Headers.init(testing.allocator);
455 defer h.deinit();451 defer h.deinit();
456 try h.append("foo", "bar", null);452 try h.append("foo", "bar", null);
457 try h.append("baz", "qux", null);453 try h.append("baz", "qux", null);
...@@ -474,7 +470,7 @@ test "Headers.swapRemove" {...@@ -474,7 +470,7 @@ test "Headers.swapRemove" {
474}470}
475471
476test "Headers.at" {472test "Headers.at" {
477 var h = Headers.init(test_allocator);473 var h = Headers.init(testing.allocator);
478 defer h.deinit();474 defer h.deinit();
479 try h.append("foo", "bar", null);475 try h.append("foo", "bar", null);
480 try h.append("cookie", "somevalue", null);476 try h.append("cookie", "somevalue", null);
...@@ -494,7 +490,7 @@ test "Headers.at" {...@@ -494,7 +490,7 @@ test "Headers.at" {
494}490}
495491
496test "Headers.getIndices" {492test "Headers.getIndices" {
497 var h = Headers.init(test_allocator);493 var h = Headers.init(testing.allocator);
498 defer h.deinit();494 defer h.deinit();
499 try h.append("foo", "bar", null);495 try h.append("foo", "bar", null);
500 try h.append("set-cookie", "x=1", null);496 try h.append("set-cookie", "x=1", null);
...@@ -506,27 +502,27 @@ test "Headers.getIndices" {...@@ -506,27 +502,27 @@ test "Headers.getIndices" {
506}502}
507503
508test "Headers.get" {504test "Headers.get" {
509 var h = Headers.init(test_allocator);505 var h = Headers.init(testing.allocator);
510 defer h.deinit();506 defer h.deinit();
511 try h.append("foo", "bar", null);507 try h.append("foo", "bar", null);
512 try h.append("set-cookie", "x=1", null);508 try h.append("set-cookie", "x=1", null);
513 try h.append("set-cookie", "y=2", null);509 try h.append("set-cookie", "y=2", null);
514510
515 {511 {
516 const v = try h.get(test_allocator, "not-present");512 const v = try h.get(testing.allocator, "not-present");
517 testing.expect(null == v);513 testing.expect(null == v);
518 }514 }
519 {515 {
520 const v = (try h.get(test_allocator, "foo")).?;516 const v = (try h.get(testing.allocator, "foo")).?;
521 defer test_allocator.free(v);517 defer testing.allocator.free(v);
522 const e = v[0];518 const e = v[0];
523 testing.expectEqualSlices(u8, "foo", e.name);519 testing.expectEqualSlices(u8, "foo", e.name);
524 testing.expectEqualSlices(u8, "bar", e.value);520 testing.expectEqualSlices(u8, "bar", e.value);
525 testing.expectEqual(false, e.never_index);521 testing.expectEqual(false, e.never_index);
526 }522 }
527 {523 {
528 const v = (try h.get(test_allocator, "set-cookie")).?;524 const v = (try h.get(testing.allocator, "set-cookie")).?;
529 defer test_allocator.free(v);525 defer testing.allocator.free(v);
530 {526 {
531 const e = v[0];527 const e = v[0];
532 testing.expectEqualSlices(u8, "set-cookie", e.name);528 testing.expectEqualSlices(u8, "set-cookie", e.name);
...@@ -543,30 +539,30 @@ test "Headers.get" {...@@ -543,30 +539,30 @@ test "Headers.get" {
543}539}
544540
545test "Headers.getCommaSeparated" {541test "Headers.getCommaSeparated" {
546 var h = Headers.init(test_allocator);542 var h = Headers.init(testing.allocator);
547 defer h.deinit();543 defer h.deinit();
548 try h.append("foo", "bar", null);544 try h.append("foo", "bar", null);
549 try h.append("set-cookie", "x=1", null);545 try h.append("set-cookie", "x=1", null);
550 try h.append("set-cookie", "y=2", null);546 try h.append("set-cookie", "y=2", null);
551547
552 {548 {
553 const v = try h.getCommaSeparated(test_allocator, "not-present");549 const v = try h.getCommaSeparated(testing.allocator, "not-present");
554 testing.expect(null == v);550 testing.expect(null == v);
555 }551 }
556 {552 {
557 const v = (try h.getCommaSeparated(test_allocator, "foo")).?;553 const v = (try h.getCommaSeparated(testing.allocator, "foo")).?;
558 defer test_allocator.free(v);554 defer testing.allocator.free(v);
559 testing.expectEqualSlices(u8, "bar", v);555 testing.expectEqualSlices(u8, "bar", v);
560 }556 }
561 {557 {
562 const v = (try h.getCommaSeparated(test_allocator, "set-cookie")).?;558 const v = (try h.getCommaSeparated(testing.allocator, "set-cookie")).?;
563 defer test_allocator.free(v);559 defer testing.allocator.free(v);
564 testing.expectEqualSlices(u8, "x=1,y=2", v);560 testing.expectEqualSlices(u8, "x=1,y=2", v);
565 }561 }
566}562}
567563
568test "Headers.sort" {564test "Headers.sort" {
569 var h = Headers.init(test_allocator);565 var h = Headers.init(testing.allocator);
570 defer h.deinit();566 defer h.deinit();
571 try h.append("foo", "bar", null);567 try h.append("foo", "bar", null);
572 try h.append("cookie", "somevalue", null);568 try h.append("cookie", "somevalue", null);
...@@ -587,7 +583,7 @@ test "Headers.sort" {...@@ -587,7 +583,7 @@ test "Headers.sort" {
587}583}
588584
589test "Headers.format" {585test "Headers.format" {
590 var h = Headers.init(test_allocator);586 var h = Headers.init(testing.allocator);
591 defer h.deinit();587 defer h.deinit();
592 try h.append("foo", "bar", null);588 try h.append("foo", "bar", null);
593 try h.append("cookie", "somevalue", null);589 try h.append("cookie", "somevalue", null);
lib/std/io.zig+4-8
...@@ -223,15 +223,13 @@ test "io.BufferedInStream" {...@@ -223,15 +223,13 @@ test "io.BufferedInStream" {
223 }223 }
224 };224 };
225225
226 var buf: [100]u8 = undefined;
227 const allocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;
228
229 const str = "This is a test";226 const str = "This is a test";
230 var one_byte_stream = OneByteReadInStream.init(str);227 var one_byte_stream = OneByteReadInStream.init(str);
231 var buf_in_stream = BufferedInStream(OneByteReadInStream.Error).init(&one_byte_stream.stream);228 var buf_in_stream = BufferedInStream(OneByteReadInStream.Error).init(&one_byte_stream.stream);
232 const stream = &buf_in_stream.stream;229 const stream = &buf_in_stream.stream;
233230
234 const res = try stream.readAllAlloc(allocator, str.len + 1);231 const res = try stream.readAllAlloc(testing.allocator, str.len + 1);
232 defer testing.allocator.free(res);
235 testing.expectEqualSlices(u8, str, res);233 testing.expectEqualSlices(u8, str, res);
236}234}
237235
...@@ -874,10 +872,8 @@ pub fn readLineFrom(stream: var, buf: *std.Buffer) ![]u8 {...@@ -874,10 +872,8 @@ pub fn readLineFrom(stream: var, buf: *std.Buffer) ![]u8 {
874}872}
875873
876test "io.readLineFrom" {874test "io.readLineFrom" {
877 var bytes: [128]u8 = undefined;875 var buf = try std.Buffer.initSize(testing.allocator, 0);
878 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;876 defer buf.deinit();
879
880 var buf = try std.Buffer.initSize(allocator, 0);
881 var mem_stream = SliceInStream.init(877 var mem_stream = SliceInStream.init(
882 \\Line 1878 \\Line 1
883 \\Line 22879 \\Line 22
lib/std/io/test.zig+4-9
...@@ -11,9 +11,6 @@ const fs = std.fs;...@@ -11,9 +11,6 @@ const fs = std.fs;
11const File = std.fs.File;11const File = std.fs.File;
1212
13test "write a file, read it, then delete it" {13test "write a file, read it, then delete it" {
14 var raw_bytes: [200 * 1024]u8 = undefined;
15 var allocator = &std.heap.FixedBufferAllocator.init(raw_bytes[0..]).allocator;
16
17 const cwd = fs.cwd();14 const cwd = fs.cwd();
1815
19 var data: [1024]u8 = undefined;16 var data: [1024]u8 = undefined;
...@@ -53,8 +50,8 @@ test "write a file, read it, then delete it" {...@@ -53,8 +50,8 @@ test "write a file, read it, then delete it" {
53 var file_in_stream = file.inStream();50 var file_in_stream = file.inStream();
54 var buf_stream = io.BufferedInStream(File.ReadError).init(&file_in_stream.stream);51 var buf_stream = io.BufferedInStream(File.ReadError).init(&file_in_stream.stream);
55 const st = &buf_stream.stream;52 const st = &buf_stream.stream;
56 const contents = try st.readAllAlloc(allocator, 2 * 1024);53 const contents = try st.readAllAlloc(std.testing.allocator, 2 * 1024);
57 defer allocator.free(contents);54 defer std.testing.allocator.free(contents);
5855
59 expect(mem.eql(u8, contents[0.."begin".len], "begin"));56 expect(mem.eql(u8, contents[0.."begin".len], "begin"));
60 expect(mem.eql(u8, contents["begin".len .. contents.len - "end".len], &data));57 expect(mem.eql(u8, contents["begin".len .. contents.len - "end".len], &data));
...@@ -64,10 +61,8 @@ test "write a file, read it, then delete it" {...@@ -64,10 +61,8 @@ test "write a file, read it, then delete it" {
64}61}
6562
66test "BufferOutStream" {63test "BufferOutStream" {
67 var bytes: [100]u8 = undefined;64 var buffer = try std.Buffer.initSize(std.testing.allocator, 0);
68 var allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;65 defer buffer.deinit();
69
70 var buffer = try std.Buffer.initSize(allocator, 0);
71 var buf_stream = &std.io.BufferOutStream.init(&buffer).stream;66 var buf_stream = &std.io.BufferOutStream.init(&buffer).stream;
7267
73 const x: i32 = 42;68 const x: i32 = 42;
lib/std/json.zig+5-8
...@@ -1495,10 +1495,7 @@ fn unescapeString(output: []u8, input: []const u8) !void {...@@ -1495,10 +1495,7 @@ fn unescapeString(output: []u8, input: []const u8) !void {
1495}1495}
14961496
1497test "json.parser.dynamic" {1497test "json.parser.dynamic" {
1498 var memory: [1024 * 16]u8 = undefined;1498 var p = Parser.init(testing.allocator, false);
1499 var buf_alloc = std.heap.FixedBufferAllocator.init(&memory);
1500
1501 var p = Parser.init(&buf_alloc.allocator, false);
1502 defer p.deinit();1499 defer p.deinit();
15031500
1504 const s =1501 const s =
...@@ -1588,10 +1585,10 @@ test "write json then parse it" {...@@ -1588,10 +1585,10 @@ test "write json then parse it" {
15881585
1589 try jw.endObject();1586 try jw.endObject();
15901587
1591 var mem_buffer: [1024 * 20]u8 = undefined;1588 var parser = Parser.init(testing.allocator, false);
1592 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;1589 defer parser.deinit();
1593 var parser = Parser.init(allocator, false);1590 var tree = try parser.parse(slice_out_stream.getWritten());
1594 const tree = try parser.parse(slice_out_stream.getWritten());1591 defer tree.deinit();
15951592
1596 testing.expect(tree.root.Object.get("f").?.value.Bool == false);1593 testing.expect(tree.root.Object.get("f").?.value.Bool == false);
1597 testing.expect(tree.root.Object.get("t").?.value.Bool == true);1594 testing.expect(tree.root.Object.get("t").?.value.Bool == true);
lib/std/json/test.zig+14-17
...@@ -8,19 +8,18 @@ const std = @import("../std.zig");...@@ -8,19 +8,18 @@ const std = @import("../std.zig");
8fn ok(comptime s: []const u8) void {8fn ok(comptime s: []const u8) void {
9 std.testing.expect(std.json.validate(s));9 std.testing.expect(std.json.validate(s));
1010
11 var mem_buffer: [1024 * 20]u8 = undefined;11 var p = std.json.Parser.init(std.testing.allocator, false);
12 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;12 defer p.deinit();
13 var p = std.json.Parser.init(allocator, false);
1413
15 _ = p.parse(s) catch unreachable;14 var tree = p.parse(s) catch unreachable;
15 defer tree.deinit();
16}16}
1717
18fn err(comptime s: []const u8) void {18fn err(comptime s: []const u8) void {
19 std.testing.expect(!std.json.validate(s));19 std.testing.expect(!std.json.validate(s));
2020
21 var mem_buffer: [1024 * 20]u8 = undefined;21 var p = std.json.Parser.init(std.testing.allocator, false);
22 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;22 defer p.deinit();
23 var p = std.json.Parser.init(allocator, false);
2423
25 if (p.parse(s)) |_| {24 if (p.parse(s)) |_| {
26 unreachable;25 unreachable;
...@@ -30,9 +29,8 @@ fn err(comptime s: []const u8) void {...@@ -30,9 +29,8 @@ fn err(comptime s: []const u8) void {
30fn utf8Error(comptime s: []const u8) void {29fn utf8Error(comptime s: []const u8) void {
31 std.testing.expect(!std.json.validate(s));30 std.testing.expect(!std.json.validate(s));
3231
33 var mem_buffer: [1024 * 20]u8 = undefined;32 var p = std.json.Parser.init(std.testing.allocator, false);
34 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;33 defer p.deinit();
35 var p = std.json.Parser.init(allocator, false);
3634
37 if (p.parse(s)) |_| {35 if (p.parse(s)) |_| {
38 unreachable;36 unreachable;
...@@ -44,19 +42,18 @@ fn utf8Error(comptime s: []const u8) void {...@@ -44,19 +42,18 @@ fn utf8Error(comptime s: []const u8) void {
44fn any(comptime s: []const u8) void {42fn any(comptime s: []const u8) void {
45 _ = std.json.validate(s);43 _ = std.json.validate(s);
4644
47 var mem_buffer: [1024 * 20]u8 = undefined;45 var p = std.json.Parser.init(std.testing.allocator, false);
48 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;46 defer p.deinit();
49 var p = std.json.Parser.init(allocator, false);
5047
51 _ = p.parse(s) catch {};48 var tree = p.parse(s) catch return;
49 defer tree.deinit();
52}50}
5351
54fn anyStreamingErrNonStreaming(comptime s: []const u8) void {52fn anyStreamingErrNonStreaming(comptime s: []const u8) void {
55 _ = std.json.validate(s);53 _ = std.json.validate(s);
5654
57 var mem_buffer: [1024 * 20]u8 = undefined;55 var p = std.json.Parser.init(std.testing.allocator, false);
58 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;56 defer p.deinit();
59 var p = std.json.Parser.init(allocator, false);
6057
61 if (p.parse(s)) |_| {58 if (p.parse(s)) |_| {
62 unreachable;59 unreachable;
lib/std/os/test.zig+2-3
...@@ -95,8 +95,6 @@ test "cpu count" {...@@ -95,8 +95,6 @@ test "cpu count" {
95}95}
9696
97test "AtomicFile" {97test "AtomicFile" {
98 var buffer: [1024]u8 = undefined;
99 const allocator = &std.heap.FixedBufferAllocator.init(buffer[0..]).allocator;
100 const test_out_file = "tmp_atomic_file_test_dest.txt";98 const test_out_file = "tmp_atomic_file_test_dest.txt";
101 const test_content =99 const test_content =
102 \\ hello!100 \\ hello!
...@@ -108,7 +106,8 @@ test "AtomicFile" {...@@ -108,7 +106,8 @@ test "AtomicFile" {
108 try af.file.write(test_content);106 try af.file.write(test_content);
109 try af.finish();107 try af.finish();
110 }108 }
111 const content = try io.readFileAlloc(allocator, test_out_file);109 const content = try io.readFileAlloc(testing.allocator, test_out_file);
110 defer testing.allocator.free(content);
112 expect(mem.eql(u8, content, test_content));111 expect(mem.eql(u8, content, test_content));
113112
114 try fs.cwd().deleteFile(test_out_file);113 try fs.cwd().deleteFile(test_out_file);
lib/std/process.zig+2-4
...@@ -27,10 +27,8 @@ pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {...@@ -27,10 +27,8 @@ pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {
27}27}
2828
29test "getCwdAlloc" {29test "getCwdAlloc" {
30 // at least call it so it gets compiled30 const cwd = try getCwdAlloc(testing.allocator);
31 var buf: [1000]u8 = undefined;31 testing.allocator.free(cwd);
32 const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
33 _ = getCwdAlloc(allocator) catch undefined;
34}32}
3533
36/// Caller must free result when done.34/// Caller must free result when done.
lib/std/sort.zig+4-4
...@@ -1220,16 +1220,16 @@ test "sort fuzz testing" {...@@ -1220,16 +1220,16 @@ test "sort fuzz testing" {
1220 const test_case_count = 10;1220 const test_case_count = 10;
1221 var i: usize = 0;1221 var i: usize = 0;
1222 while (i < test_case_count) : (i += 1) {1222 while (i < test_case_count) : (i += 1) {
1223 fuzzTest(&prng.random);1223 try fuzzTest(&prng.random);
1224 }1224 }
1225}1225}
12261226
1227var fixed_buffer_mem: [100 * 1024]u8 = undefined;1227var fixed_buffer_mem: [100 * 1024]u8 = undefined;
12281228
1229fn fuzzTest(rng: *std.rand.Random) void {1229fn fuzzTest(rng: *std.rand.Random) !void {
1230 const array_size = rng.range(usize, 0, 1000);1230 const array_size = rng.range(usize, 0, 1000);
1231 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);1231 var array = try testing.allocator.alloc(IdAndValue, array_size);
1232 var array = fixed_allocator.allocator.alloc(IdAndValue, array_size) catch unreachable;1232 defer testing.allocator.free(array);
1233 // populate with random data1233 // populate with random data
1234 for (array) |*item, index| {1234 for (array) |*item, index| {
1235 item.id = index;1235 item.id = index;
lib/std/unicode.zig+4-6
...@@ -617,16 +617,14 @@ test "utf8ToUtf16Le" {...@@ -617,16 +617,14 @@ test "utf8ToUtf16Le" {
617617
618test "utf8ToUtf16LeWithNull" {618test "utf8ToUtf16LeWithNull" {
619 {619 {
620 var bytes: [128]u8 = undefined;620 const utf16 = try utf8ToUtf16LeWithNull(testing.allocator, "𐐷");
621 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;621 defer testing.allocator.free(utf16);
622 const utf16 = try utf8ToUtf16LeWithNull(allocator, "𐐷");
623 testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc", @sliceToBytes(utf16[0..]));622 testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc", @sliceToBytes(utf16[0..]));
624 testing.expect(utf16[2] == 0);623 testing.expect(utf16[2] == 0);
625 }624 }
626 {625 {
627 var bytes: [128]u8 = undefined;626 const utf16 = try utf8ToUtf16LeWithNull(testing.allocator, "\u{10FFFF}");
628 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;627 defer testing.allocator.free(utf16);
629 const utf16 = try utf8ToUtf16LeWithNull(allocator, "\u{10FFFF}");
630 testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", @sliceToBytes(utf16[0..]));628 testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", @sliceToBytes(utf16[0..]));
631 testing.expect(utf16[2] == 0);629 testing.expect(utf16[2] == 0);
632 }630 }
lib/std/zig/parser_test.zig+1-2
...@@ -2886,8 +2886,7 @@ fn testCanonical(source: []const u8) !void {...@@ -2886,8 +2886,7 @@ fn testCanonical(source: []const u8) !void {
2886}2886}
28872887
2888fn testError(source: []const u8) !void {2888fn testError(source: []const u8) !void {
2889 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);2889 const tree = try std.zig.parse(std.testing.allocator, source);
2890 const tree = try std.zig.parse(&fixed_allocator.allocator, source);
2891 defer tree.deinit();2890 defer tree.deinit();
28922891
2893 std.testing.expect(tree.errors.len != 0);2892 std.testing.expect(tree.errors.len != 0);
test/stage1/behavior/bugs/1851.zig+2-3
...@@ -6,10 +6,9 @@ test "allocation and looping over 3-byte integer" {...@@ -6,10 +6,9 @@ test "allocation and looping over 3-byte integer" {
6 expect(@sizeOf([1]u24) == 4);6 expect(@sizeOf([1]u24) == 4);
7 expect(@alignOf(u24) == 4);7 expect(@alignOf(u24) == 4);
8 expect(@alignOf([1]u24) == 4);8 expect(@alignOf([1]u24) == 4);
9 var buffer: [100]u8 = undefined;
10 const a = &std.heap.FixedBufferAllocator.init(&buffer).allocator;
119
12 var x = a.alloc(u24, 2) catch unreachable;10 var x = try std.testing.allocator.alloc(u24, 2);
11 defer std.testing.allocator.free(x);
13 expect(x.len == 2);12 expect(x.len == 2);
14 x[0] = 0xFFFFFF;13 x[0] = 0xFFFFFF;
15 x[1] = 0xFFFFFF;14 x[1] = 0xFFFFFF;