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
18 changes: 18 additions & 0 deletions exercises/practice/line-up/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,24 @@ description = "format non-exceptional ordinal numeral 13"
[2bdcebc5-c029-4874-b6cc-e9bec80d603a]
description = "format exceptional ordinal numeral 21"

[a98e2e22-ab41-4557-a7c2-efedc19c16da]
description = "format exceptional ordinal numeral 22 ending in nd even though it is a multiple of 11"

[ab45d2fb-e0ee-4016-b605-76917584db0a]
description = "format exceptional ordinal numeral 33 ending in rd even though it is a multiple of 11"

[c9243603-9f17-45b3-9a41-db9ebdbf08e1]
description = "format exceptional ordinal numeral 52 ending in nd even though it is a multiple of 13"

[74ee2317-0295-49d2-baf0-d56bcefa14e3]
description = "format exceptional ordinal numeral 62"

[3f6c408c-4331-42b6-bb6c-3ad0823e568a]
description = "format non-exceptional ordinal numeral 72 ending in nd even though it is a multiple of 12"

[8db52cd9-9689-413f-a812-6c36fcfd0d07]
description = "format exceptional ordinal numeral 91 ending in st even though it is a multiple of 13"

[b37c332d-7f68-40e3-8503-e43cbd67a0c4]
description = "format exceptional ordinal numeral 100"

Expand All @@ -65,3 +80,6 @@ description = "format non-exceptional ordinal numeral 112"

[06b62efe-199e-4ce7-970d-4bf73945713f]
description = "format exceptional ordinal numeral 123"

[6792c54e-59a7-4faf-839a-c4bb61014229]
description = "format large number 972 ending in nd even though it is a multiple of 12"
226 changes: 149 additions & 77 deletions exercises/practice/line-up/test_line_up.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,137 +2,209 @@ const std = @import("std");
const testing = std.testing;

const line_up = @import("line_up.zig");
const format = line_up.format;

test "format smallest non-exceptional ordinal numeral 4" {
const expected: []const u8 = "Gianna, you are the 4th customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Gianna", 4);
defer testing.allocator.free(actual);
fn testFormat(allocator: std.mem.Allocator, expected: []const u8, name: []const u8, number: u10) !void {
const actual = try line_up.format(allocator, name, number);
defer allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "format smallest non-exceptional ordinal numeral 4" {
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Gianna, you are the 4th customer we serve today. Thank you!", "Gianna", 4 },
);
}

test "format greatest single digit non-exceptional ordinal numeral 9" {
const expected: []const u8 = "Maarten, you are the 9th customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Maarten", 9);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Maarten, you are the 9th customer we serve today. Thank you!", "Maarten", 9 },
);
}

test "format non-exceptional ordinal numeral 5" {
const expected: []const u8 = "Petronila, you are the 5th customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Petronila", 5);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Petronila, you are the 5th customer we serve today. Thank you!", "Petronila", 5 },
);
}

test "format non-exceptional ordinal numeral 6" {
const expected: []const u8 = "Attakullakulla, you are the 6th customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Attakullakulla", 6);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Attakullakulla, you are the 6th customer we serve today. Thank you!", "Attakullakulla", 6 },
);
}

test "format non-exceptional ordinal numeral 7" {
const expected: []const u8 = "Kate, you are the 7th customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Kate", 7);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Kate, you are the 7th customer we serve today. Thank you!", "Kate", 7 },
);
}

test "format non-exceptional ordinal numeral 8" {
const expected: []const u8 = "Maximiliano, you are the 8th customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Maximiliano", 8);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Maximiliano, you are the 8th customer we serve today. Thank you!", "Maximiliano", 8 },
);
}

test "format exceptional ordinal numeral 1" {
const expected: []const u8 = "Mary, you are the 1st customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Mary", 1);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Mary, you are the 1st customer we serve today. Thank you!", "Mary", 1 },
);
}

test "format exceptional ordinal numeral 2" {
const expected: []const u8 = "Haruto, you are the 2nd customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Haruto", 2);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Haruto, you are the 2nd customer we serve today. Thank you!", "Haruto", 2 },
);
}

test "format exceptional ordinal numeral 3" {
const expected: []const u8 = "Henriette, you are the 3rd customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Henriette", 3);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Henriette, you are the 3rd customer we serve today. Thank you!", "Henriette", 3 },
);
}

test "format smallest two digit non-exceptional ordinal numeral 10" {
const expected: []const u8 = "Alvarez, you are the 10th customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Alvarez", 10);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Alvarez, you are the 10th customer we serve today. Thank you!", "Alvarez", 10 },
);
}

test "format non-exceptional ordinal numeral 11" {
const expected: []const u8 = "Jacqueline, you are the 11th customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Jacqueline", 11);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Jacqueline, you are the 11th customer we serve today. Thank you!", "Jacqueline", 11 },
);
}

test "format non-exceptional ordinal numeral 12" {
const expected: []const u8 = "Juan, you are the 12th customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Juan", 12);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Juan, you are the 12th customer we serve today. Thank you!", "Juan", 12 },
);
}

test "format non-exceptional ordinal numeral 13" {
const expected: []const u8 = "Patricia, you are the 13th customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Patricia", 13);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Patricia, you are the 13th customer we serve today. Thank you!", "Patricia", 13 },
);
}

test "format exceptional ordinal numeral 21" {
const expected: []const u8 = "Washi, you are the 21st customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Washi", 21);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Washi, you are the 21st customer we serve today. Thank you!", "Washi", 21 },
);
}

test "format exceptional ordinal numeral 22 ending in nd even though it is a multiple of 11" {
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Ingrid, you are the 22nd customer we serve today. Thank you!", "Ingrid", 22 },
);
}

test "format exceptional ordinal numeral 33 ending in rd even though it is a multiple of 11" {
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Mario, you are the 33rd customer we serve today. Thank you!", "Mario", 33 },
);
}

test "format exceptional ordinal numeral 52 ending in nd even though it is a multiple of 13" {
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Quentin, you are the 52nd customer we serve today. Thank you!", "Quentin", 52 },
);
}

test "format exceptional ordinal numeral 62" {
const expected: []const u8 = "Nayra, you are the 62nd customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Nayra", 62);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Nayra, you are the 62nd customer we serve today. Thank you!", "Nayra", 62 },
);
}

test "format non-exceptional ordinal numeral 72 ending in nd even though it is a multiple of 12" {
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Ugo, you are the 72nd customer we serve today. Thank you!", "Ugo", 72 },
);
}

test "format exceptional ordinal numeral 91 ending in st even though it is a multiple of 13" {
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Boris, you are the 91st customer we serve today. Thank you!", "Boris", 91 },
);
}

test "format exceptional ordinal numeral 100" {
const expected: []const u8 = "John, you are the 100th customer we serve today. Thank you!";
const actual = try format(testing.allocator, "John", 100);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "John, you are the 100th customer we serve today. Thank you!", "John", 100 },
);
}

test "format exceptional ordinal numeral 101" {
const expected: []const u8 = "Zeinab, you are the 101st customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Zeinab", 101);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Zeinab, you are the 101st customer we serve today. Thank you!", "Zeinab", 101 },
);
}

test "format non-exceptional ordinal numeral 112" {
const expected: []const u8 = "Knud, you are the 112th customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Knud", 112);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Knud, you are the 112th customer we serve today. Thank you!", "Knud", 112 },
);
}

test "format exceptional ordinal numeral 123" {
const expected: []const u8 = "Yma, you are the 123rd customer we serve today. Thank you!";
const actual = try format(testing.allocator, "Yma", 123);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Yma, you are the 123rd customer we serve today. Thank you!", "Yma", 123 },
);
}

test "format large number 972 ending in nd even though it is a multiple of 12" {
try testing.checkAllAllocationFailures(
testing.allocator,
testFormat,
.{ "Elias, you are the 972nd customer we serve today. Thank you!", "Elias", 972 },
);
}
17 changes: 12 additions & 5 deletions generators/exercises/line_up.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
from lib import zstr

HEADER = "const format = line_up.format;\n"
HEADER = r"""
fn testFormat(allocator: std.mem.Allocator, expected: []const u8, name: []const u8, number: u10) !void {
const actual = try line_up.format(allocator, name, number);
defer allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}
"""


def gen_case(case):
name = case["input"]["name"]
number = case["input"]["number"]
expected = case["expected"]
return (
f" const expected: []const u8 = {zstr(expected)};\n"
f" const actual = try format(testing.allocator, {zstr(name)}, {number});\n"
f" defer testing.allocator.free(actual);\n"
f" try testing.expectEqualStrings(expected, actual);\n"
" try testing.checkAllAllocationFailures(\n"
" testing.allocator,\n"
" testFormat,\n"
f" .{{ {zstr(expected)}, {zstr(name)}, {number} }},\n"
" );\n"
)
Loading