Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bin/generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
# Generate Zig practice-exercise test files from problem-specifications canonical data.
# Usage: bin/generate <slug> [<slug> ...] | --all | --check <slug> ...
set -euo pipefail
cd "$(dirname "$0")/.."
exec python3 generators/generate.py "$@"
16 changes: 16 additions & 0 deletions exercises/practice/affine-cipher/.meta/supplements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"cases": [
{
"description": "encode boundary characters",
"property": "encode",
"input": {
"phrase": "/09:@AMNZ[`amnz{",
"key": {
"a": 25,
"b": 12
}
},
"expected": "09maz nmazn"
}
]
}
25 changes: 25 additions & 0 deletions exercises/practice/binary-search-tree/.meta/supplements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"cases": [
{
"description": "empty tree has null root",
"property": "data",
"input": {
"treeData": []
},
"expected": null
},
{
"description": "can sort data",
"cases": [
{
"description": "can sort empty tree",
"property": "sortedData",
"input": {
"treeData": []
},
"expected": []
}
]
}
]
}
28 changes: 16 additions & 12 deletions exercises/practice/connect/test_connect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ test "an empty board has no winner" {
" . . . . .", //
" . . . . .", //
" . . . . .", //
" . . . . .",
" . . . . .", //
};
try testing.expectEqual('.', winner(testing.allocator, &board));
}

test "X can win on a 1x1 board" {
const board = [_][]const u8{"X"};
const board = [_][]const u8{
"X", //
};
try testing.expectEqual('X', winner(testing.allocator, &board));
}

test "O can win on a 1x1 board" {
const board = [_][]const u8{"O"};
const board = [_][]const u8{
"O", //
};
try testing.expectEqual('O', winner(testing.allocator, &board));
}

Expand All @@ -30,7 +34,7 @@ test "only edges does not make a winner" {
"O O O X", //
" X . . X", //
" X . . X", //
" X O O O",
" X O O O", //
};
try testing.expectEqual('.', winner(testing.allocator, &board));
}
Expand All @@ -41,7 +45,7 @@ test "illegal diagonal does not make a winner" {
" O X X X", //
" O X O .", //
" . O X .", //
" X X O O",
" X X O O", //
};
try testing.expectEqual('.', winner(testing.allocator, &board));
}
Expand All @@ -52,7 +56,7 @@ test "nobody wins crossing adjacent angles" {
" . X O .", //
" O . X O", //
" . O . X", //
" . . O .",
" . . O .", //
};
try testing.expectEqual('.', winner(testing.allocator, &board));
}
Expand All @@ -63,7 +67,7 @@ test "X wins crossing from left to right" {
" O X X X", //
" O X O .", //
" X X O X", //
" . O X .",
" . O X .", //
};
try testing.expectEqual('X', winner(testing.allocator, &board));
}
Expand All @@ -73,7 +77,7 @@ test "X wins with left-hand dead end fork" {
". . X .", //
" X X . .", //
" . X X X", //
" O O O O",
" O O O O", //
};
try testing.expectEqual('X', winner(testing.allocator, &board));
}
Expand All @@ -83,7 +87,7 @@ test "X wins with right-hand dead end fork" {
". . X X", //
" X X . .", //
" . X X .", //
" O O O O",
" O O O O", //
};
try testing.expectEqual('X', winner(testing.allocator, &board));
}
Expand All @@ -94,7 +98,7 @@ test "O wins crossing from top to bottom" {
" O X X X", //
" O O O .", //
" X X O X", //
" . O X .",
" . O X .", //
};
try testing.expectEqual('O', winner(testing.allocator, &board));
}
Expand All @@ -105,7 +109,7 @@ test "X wins using a convoluted path" {
" X . X . X", //
" . X . X .", //
" . X X . .", //
" O O O O O",
" O O O O O", //
};
try testing.expectEqual('X', winner(testing.allocator, &board));
}
Expand All @@ -120,7 +124,7 @@ test "X wins using a spiral path" {
" O X O O O X O X O", //
" O X X X X X O X O", //
" O O O O O O O X O", //
" X X X X X X X X O",
" X X X X X X X X O", //
};
try testing.expectEqual('X', winner(testing.allocator, &board));
}
26 changes: 13 additions & 13 deletions exercises/practice/flower-field/test_flower_field.zig
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const std = @import("std");
const mem = std.mem;
const testing = std.testing;

const annotate = @import("flower_field.zig").annotate;

fn free(slices: [][]u8) void {
for (slices) |slice| {
testing.allocator.free(slice);
const flower_field = @import("flower_field.zig");
fn annotateTest(
allocator: mem.Allocator,
expected: []const []const u8,
garden: []const []const u8,
) anyerror!void {
const actual = try flower_field.annotate(allocator, garden);
defer {
for (actual) |line| allocator.free(line);
allocator.free(actual);
}
testing.allocator.free(slices);
}

fn annotateTest(allocator: std.mem.Allocator, expected: []const []const u8, garden: []const []const u8) !void {
const actual = try annotate(allocator, garden);
defer free(actual);
try testing.expectEqual(expected.len, actual.len);
for (0..expected.len) |i| {
try testing.expectEqualStrings(expected[i], actual[i]);
for (expected, actual) |e, a| {
try testing.expectEqualStrings(e, a);
}
}

Expand Down
19 changes: 19 additions & 0 deletions exercises/practice/high-scores/.meta/supplements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"cases": [
{
"description": "Latest score can be outside top three",
"property": "latest",
"input": {
"scores": [
80,
70,
90,
10,
20,
30
]
},
"expected": 30
}
]
}
30 changes: 30 additions & 0 deletions exercises/practice/matrix/.meta/supplements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"cases": [
{
"description": "row with negative numbers",
"property": "row",
"input": {
"string": "1 2 4\n-57 9 -42\n10 0 65",
"index": 2
},
"expected": [
-57,
9,
-42
]
},
{
"description": "column with negative numbers",
"property": "column",
"input": {
"string": "1 2 -4\n-57 9 -42\n10 0 -465",
"index": 3
},
"expected": [
-4,
-42,
-465
]
}
]
}
26 changes: 26 additions & 0 deletions exercises/practice/meetup/.meta/supplements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"cases": [
{
"description": "when last Thursday in February in a non-leap year is not the 29th",
"property": "meetup",
"input": {
"year": 2300,
"month": 2,
"week": "last",
"dayofweek": "Thursday"
},
"expected": "2300-02-22"
},
{
"description": "when fourth Monday is the 23nd, the second day of the fourth week",
"property": "meetup",
"input": {
"year": 2468,
"month": 1,
"week": "fourth",
"dayofweek": "Monday"
},
"expected": "2468-01-23"
}
]
}
12 changes: 12 additions & 0 deletions exercises/practice/micro-blog/.meta/supplements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"cases": [
{
"description": "ideograms",
"property": "truncate",
"input": {
"phrase": "二兎を追う者は一兎をも得ず"
},
"expected": "二兎を追う"
}
]
}
28 changes: 28 additions & 0 deletions exercises/practice/nth-prime/.meta/supplements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"cases": [
{
"description": "third prime",
"property": "prime",
"input": { "number": 3 },
"expected": 5
},
{
"description": "fourth prime",
"property": "prime",
"input": { "number": 4 },
"expected": 7
},
{
"description": "fifth prime",
"property": "prime",
"input": { "number": 5 },
"expected": 11
},
{
"description": "seventh prime",
"property": "prime",
"input": { "number": 7 },
"expected": 17
}
]
}
4 changes: 2 additions & 2 deletions exercises/practice/nth-prime/test_nth_prime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ test "seventh prime" {
}

test "big prime" {
const p = try nth_prime.prime(testing.allocator, 10001);
try testing.expectEqual(104743, p);
const p = try nth_prime.prime(testing.allocator, 10_001);
try testing.expectEqual(104_743, p);
}
72 changes: 72 additions & 0 deletions exercises/practice/palindrome-products/.meta/supplements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"cases": [
{
"description": "smallest with large factors",
"property": "smallest",
"input": {
"min": 54773,
"max": 63245
},
"expected": {
"value": 3030220303,
"factors": [
[
54799,
55297
]
]
}
},
{
"description": "largest with large factors",
"property": "largest",
"input": {
"min": 54773,
"max": 63245
},
"expected": {
"value": 3956776593,
"factors": [
[
62799,
63007
]
]
}
},
{
"description": "smallest with very large factors",
"property": "smallest",
"input": {
"min": 3000100,
"max": 3141592
},
"expected": {
"value": 9003210123009,
"factors": [
[
3000131,
3000939
]
]
}
},
{
"description": "largest with very large factors",
"property": "largest",
"input": {
"min": 3100000,
"max": 3141592
},
"expected": {
"value": 9864278724689,
"factors": [
[
3140089,
3141401
]
]
}
}
]
}
Loading
Loading