From 6ea6d5a4bdcf6f37c43857570522ee1308115de7 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 14 Feb 2020 19:15:09 +1100 Subject: std: use testing.allocator in tests --- lib/std/mem.zig | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) (limited to 'lib/std/mem.zig') diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 8f4987e0c9..a1b4a3c809 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -1011,11 +1011,21 @@ pub fn join(allocator: *Allocator, separator: []const u8, slices: []const []cons } test "mem.join" { - var buf: [1024]u8 = undefined; - const a = &std.heap.FixedBufferAllocator.init(&buf).allocator; - testing.expect(eql(u8, try join(a, ",", &[_][]const u8{ "a", "b", "c" }), "a,b,c")); - testing.expect(eql(u8, try join(a, ",", &[_][]const u8{"a"}), "a")); - testing.expect(eql(u8, try join(a, ",", &[_][]const u8{ "a", "", "b", "", "c" }), "a,,b,,c")); + { + const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" }); + defer testing.allocator.free(str); + testing.expect(eql(u8, str, "a,b,c")); + } + { + const str = try join(testing.allocator, ",", &[_][]const u8{"a"}); + defer testing.allocator.free(str); + testing.expect(eql(u8, str, "a")); + } + { + const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "", "b", "", "c" }); + defer testing.allocator.free(str); + testing.expect(eql(u8, str, "a,,b,,c")); + } } /// 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 } test "concat" { - var buf: [1024]u8 = undefined; - const a = &std.heap.FixedBufferAllocator.init(&buf).allocator; - testing.expect(eql(u8, try concat(a, u8, &[_][]const u8{ "abc", "def", "ghi" }), "abcdefghi")); - testing.expect(eql(u32, try concat(a, u32, &[_][]const u32{ - &[_]u32{ 0, 1 }, - &[_]u32{ 2, 3, 4 }, - &[_]u32{}, - &[_]u32{5}, - }), &[_]u32{ 0, 1, 2, 3, 4, 5 })); + { + const str = try concat(testing.allocator, u8, &[_][]const u8{ "abc", "def", "ghi" }); + defer testing.allocator.free(str); + testing.expect(eql(u8, str, "abcdefghi")); + } + { + const str = try concat(testing.allocator, u32, &[_][]const u32{ + &[_]u32{ 0, 1 }, + &[_]u32{ 2, 3, 4 }, + &[_]u32{}, + &[_]u32{5}, + }); + defer testing.allocator.free(str); + testing.expect(eql(u32, str, &[_]u32{ 0, 1, 2, 3, 4, 5 })); + } } test "testStringEquality" { -- cgit v1.2.3