| 1 | const std = @import("std"); |
| 2 | const Allocator = std.mem.Allocator; |
| 3 | const expectEqualStrings = std.testing.expectEqualStrings; |
| 4 | |
| 5 | test "using an allocator" { |
| 6 | var buffer: [100]u8 = undefined; |
| 7 | var fba = std.heap.FixedBufferAllocator.init(&buffer); |
| 8 | const allocator = fba.allocator(); |
| 9 | const result = try concat(allocator, "foo", "bar"); |
| 10 | try expectEqualStrings("foobar", result); |
| 11 | } |
| 12 | |
| 13 | fn concat(allocator: Allocator, a: []const u8, b: []const u8) ![]u8 { |
| 14 | const result = try allocator.alloc(u8, a.len + b.len); |
| 15 | @memcpy(result[0..a.len], a); |
| 16 | @memcpy(result[a.len..], b); |
| 17 | return result; |
| 18 | } |
| 19 | |
| 20 | // test |