diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-02-14 10:27:44 -0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-02-14 10:27:44 -0500 |
| commit | a8b36fbe34e4acfea1fcb348fbed321b05611fd3 (patch) | |
| tree | 23f489f85c427a003577f5c40b74201ba8c85ddb /lib/std/mem.zig | |
| parent | cdc5070f216a924d24588b8d0fe06400e036e6bf (diff) | |
| parent | 40b9db7cad6f876bb3e8fa32d7b32bbd4bc983ea (diff) | |
| download | zig-a8b36fbe34e4acfea1fcb348fbed321b05611fd3.tar.gz zig-a8b36fbe34e4acfea1fcb348fbed321b05611fd3.zip | |
Merge remote-tracking branch 'origin/master' into llvm10
Diffstat (limited to 'lib/std/mem.zig')
| -rw-r--r-- | lib/std/mem.zig | 44 |
1 files changed, 30 insertions, 14 deletions
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" { |
