| 1 |
const std = @import("std"); |
| 2 |
const parseInt = std.fmt.parseInt; |
| 3 |
|
| 4 |
test "parse integers" { |
| 5 |
const input = "123 67 89,99"; |
| 6 |
const ally = std.testing.allocator; |
| 7 |
|
| 8 |
var list = std.ArrayList(u32).init(ally); |
| 9 |
// Ensure the list is freed at scope exit. |
| 10 |
// Try commenting out this line! |
| 11 |
defer list.deinit(); |
| 12 |
|
| 13 |
var it = std.mem.tokenize(u8, input, " ,"); |
| 14 |
while (it.next()) |num| { |
| 15 |
const n = try parseInt(u32, num, 10); |
| 16 |
try list.append(n); |
| 17 |
} |
| 18 |
|
| 19 |
const expected = [_]u32{ 123, 67, 89, 99 }; |
| 20 |
|
| 21 |
for (expected, list.items) |exp, actual| { |
| 22 |
try std.testing.expectEqual(exp, actual); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
// from https://ziglang.org |