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
14 changes: 14 additions & 0 deletions exercises/practice/all-your-base/.meta/supplements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"cases": [
{
"description": "empty list - second call returns different memory",
"property": "rebase",
"input": {
"inputBase": 10,
"digits": [],
"outputBase": 2
},
"expected": [0]
}
]
}
141 changes: 27 additions & 114 deletions exercises/practice/all-your-base/test_all_your_base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,84 +5,46 @@ const all_your_base = @import("all_your_base.zig");
const convert = all_your_base.convert;
const ConversionError = all_your_base.ConversionError;

test "single bit one to decimal" {
const expected = [_]u32{1};
const digits = [_]u32{1};
const input_base = 2;
const output_base = 10;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
fn testConvert(digits: []const u32, input_base: u32, output_base: u32, expected: []const u32) !void {
const actual = try convert(testing.allocator, digits, input_base, output_base);
defer testing.allocator.free(actual);
try testing.expectEqualSlices(u32, &expected, actual);
try testing.expectEqualSlices(u32, expected, actual);
}

fn testConvertError(digits: []const u32, input_base: u32, output_base: u32, expected: ConversionError) !void {
try testing.expectError(expected, convert(testing.allocator, digits, input_base, output_base));
}

test "single bit one to decimal" {
try testConvert(&[_]u32{1}, 2, 10, &[_]u32{1});
}

test "binary to single decimal" {
const expected = [_]u32{5};
const digits = [_]u32{ 1, 0, 1 };
const input_base = 2;
const output_base = 10;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
defer testing.allocator.free(actual);
try testing.expectEqualSlices(u32, &expected, actual);
try testConvert(&[_]u32{ 1, 0, 1 }, 2, 10, &[_]u32{5});
}

test "single decimal to binary" {
const expected = [_]u32{ 1, 0, 1 };
const digits = [_]u32{5};
const input_base = 10;
const output_base = 2;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
defer testing.allocator.free(actual);
try testing.expectEqualSlices(u32, &expected, actual);
try testConvert(&[_]u32{5}, 10, 2, &[_]u32{ 1, 0, 1 });
}

test "binary to multiple decimal" {
const expected = [_]u32{ 4, 2 };
const digits = [_]u32{ 1, 0, 1, 0, 1, 0 };
const input_base = 2;
const output_base = 10;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
defer testing.allocator.free(actual);
try testing.expectEqualSlices(u32, &expected, actual);
try testConvert(&[_]u32{ 1, 0, 1, 0, 1, 0 }, 2, 10, &[_]u32{ 4, 2 });
}

test "decimal to binary" {
const expected = [_]u32{ 1, 0, 1, 0, 1, 0 };
const digits = [_]u32{ 4, 2 };
const input_base = 10;
const output_base = 2;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
defer testing.allocator.free(actual);
try testing.expectEqualSlices(u32, &expected, actual);
try testConvert(&[_]u32{ 4, 2 }, 10, 2, &[_]u32{ 1, 0, 1, 0, 1, 0 });
}

test "trinary to hexadecimal" {
const expected = [_]u32{ 2, 10 };
const digits = [_]u32{ 1, 1, 2, 0 };
const input_base = 3;
const output_base = 16;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
defer testing.allocator.free(actual);
try testing.expectEqualSlices(u32, &expected, actual);
try testConvert(&[_]u32{ 1, 1, 2, 0 }, 3, 16, &[_]u32{ 2, 10 });
}

test "hexadecimal to trinary" {
const expected = [_]u32{ 1, 1, 2, 0 };
const digits = [_]u32{ 2, 10 };
const input_base = 16;
const output_base = 3;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
defer testing.allocator.free(actual);
try testing.expectEqualSlices(u32, &expected, actual);
try testConvert(&[_]u32{ 2, 10 }, 16, 3, &[_]u32{ 1, 1, 2, 0 });
}

test "15-bit integer" {
const expected = [_]u32{ 6, 10, 45 };
const digits = [_]u32{ 3, 46, 60 };
const input_base = 97;
const output_base = 73;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
defer testing.allocator.free(actual);
try testing.expectEqualSlices(u32, &expected, actual);
try testConvert(&[_]u32{ 3, 46, 60 }, 97, 73, &[_]u32{ 6, 10, 45 });
}

test "empty list - second call returns different memory" {
Expand All @@ -103,86 +65,37 @@ test "empty list - second call returns different memory" {
}

test "empty list" {
const expected = [_]u32{0};
const digits = [_]u32{};
const input_base = 2;
const output_base = 10;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
defer testing.allocator.free(actual);
try testing.expectEqualSlices(u32, &expected, actual);
try testConvert(&[_]u32{}, 2, 10, &[_]u32{0});
}

test "single zero" {
const expected = [_]u32{0};
const digits = [_]u32{0};
const input_base = 10;
const output_base = 2;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
defer testing.allocator.free(actual);
try testing.expectEqualSlices(u32, &expected, actual);
try testConvert(&[_]u32{0}, 10, 2, &[_]u32{0});
}

test "multiple zeros" {
const expected = [_]u32{0};
const digits = [_]u32{ 0, 0, 0 };
const input_base = 10;
const output_base = 2;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
defer testing.allocator.free(actual);
try testing.expectEqualSlices(u32, &expected, actual);
try testConvert(&[_]u32{ 0, 0, 0 }, 10, 2, &[_]u32{0});
}

test "leading zeros" {
const expected = [_]u32{ 4, 2 };
const digits = [_]u32{ 0, 6, 0 };
const input_base = 7;
const output_base = 10;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
defer testing.allocator.free(actual);
try testing.expectEqualSlices(u32, &expected, actual);
try testConvert(&[_]u32{ 0, 6, 0 }, 7, 10, &[_]u32{ 4, 2 });
}

test "input base is one" {
const expected = ConversionError.InvalidInputBase;
const digits = [_]u32{0};
const input_base = 1;
const output_base = 10;
const actual = convert(testing.allocator, &digits, input_base, output_base);
try testing.expectError(expected, actual);
try testConvertError(&[_]u32{0}, 1, 10, ConversionError.InvalidInputBase);
}

test "input base is zero" {
const expected = ConversionError.InvalidInputBase;
const digits = [_]u32{};
const input_base = 0;
const output_base = 10;
const actual = convert(testing.allocator, &digits, input_base, output_base);
try testing.expectError(expected, actual);
try testConvertError(&[_]u32{}, 0, 10, ConversionError.InvalidInputBase);
}

test "invalid positive digit" {
const expected = ConversionError.InvalidDigit;
const digits = [_]u32{ 1, 2, 1, 0, 1, 0 };
const input_base = 2;
const output_base = 10;
const actual = convert(testing.allocator, &digits, input_base, output_base);
try testing.expectError(expected, actual);
try testConvertError(&[_]u32{ 1, 2, 1, 0, 1, 0 }, 2, 10, ConversionError.InvalidDigit);
}

test "output base is one" {
const expected = ConversionError.InvalidOutputBase;
const digits = [_]u32{ 1, 0, 1, 0, 1, 0 };
const input_base = 2;
const output_base = 1;
const actual = convert(testing.allocator, &digits, input_base, output_base);
try testing.expectError(expected, actual);
try testConvertError(&[_]u32{ 1, 0, 1, 0, 1, 0 }, 2, 1, ConversionError.InvalidOutputBase);
}

test "output base is zero" {
const expected = ConversionError.InvalidOutputBase;
const digits = [_]u32{7};
const input_base = 10;
const output_base = 0;
const actual = convert(testing.allocator, &digits, input_base, output_base);
try testing.expectError(expected, actual);
try testConvertError(&[_]u32{7}, 10, 0, ConversionError.InvalidOutputBase);
}
20 changes: 20 additions & 0 deletions exercises/practice/armstrong-numbers/.meta/supplements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"cases": [
{
"description": "38-digit number that is not an armstrong number",
"property": "isArmstrongNumber",
"input": {
"number": 99999999999999999999999999999999999999
},
"expected": false
},
{
"description": "the largest 128-bit unsigned integer value is not an armstrong number",
"property": "isArmstrongNumber",
"input": {
"number": 340282366920938463463374607431768211455
},
"expected": false
}
]
}
54 changes: 29 additions & 25 deletions exercises/practice/armstrong-numbers/test_armstrong_numbers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,58 @@ const testing = std.testing;

const isArmstrongNumber = @import("armstrong_numbers.zig").isArmstrongNumber;

test "zero is an armstrong number" {
try testing.expect(isArmstrongNumber(0));
fn testIsArmstrongNumber(number: u128, expected: bool) !void {
try testing.expectEqual(expected, isArmstrongNumber(number));
}

test "single-digit numbers are armstrong numbers" {
try testing.expect(isArmstrongNumber(5));
test "Zero is an Armstrong number" {
try testIsArmstrongNumber(0, true);
}

test "there are no two-digit armstrong numbers" {
try testing.expect(!isArmstrongNumber(10));
test "Single-digit numbers are Armstrong numbers" {
try testIsArmstrongNumber(5, true);
}

test "three-digit number that is an armstrong number" {
try testing.expect(isArmstrongNumber(153));
test "There are no two-digit Armstrong numbers" {
try testIsArmstrongNumber(10, false);
}

test "three-digit number that is not an armstrong number" {
try testing.expect(!isArmstrongNumber(100));
test "Three-digit number that is an Armstrong number" {
try testIsArmstrongNumber(153, true);
}

test "four-digit number that is an armstrong number" {
try testing.expect(isArmstrongNumber(9_474));
test "Three-digit number that is not an Armstrong number" {
try testIsArmstrongNumber(100, false);
}

test "four-digit number that is not an armstrong number" {
try testing.expect(!isArmstrongNumber(9_475));
test "Four-digit number that is an Armstrong number" {
try testIsArmstrongNumber(9_474, true);
}

test "seven-digit number that is an armstrong number" {
try testing.expect(isArmstrongNumber(9_926_315));
test "Four-digit number that is not an Armstrong number" {
try testIsArmstrongNumber(9_475, false);
}

test "seven-digit number that is not an armstrong number" {
try testing.expect(!isArmstrongNumber(9_926_314));
test "Seven-digit number that is an Armstrong number" {
try testIsArmstrongNumber(9_926_315, true);
}

test "33-digit number that is an armstrong number" {
try testing.expect(isArmstrongNumber(186_709_961_001_538_790_100_634_132_976_990));
test "Seven-digit number that is not an Armstrong number" {
try testIsArmstrongNumber(9_926_314, false);
}

test "38-digit number that is not an armstrong number" {
try testing.expect(!isArmstrongNumber(99_999_999_999_999_999_999_999_999_999_999_999_999));
test "Armstrong number containing seven zeroes" {
try testIsArmstrongNumber(186_709_961_001_538_790_100_634_132_976_990, true);
}

test "The largest and last Armstrong number" {
try testIsArmstrongNumber(115_132_219_018_763_992_565_095_597_973_971_522_401, true);
}

test "the largest and last armstrong number" {
try testing.expect(isArmstrongNumber(115_132_219_018_763_992_565_095_597_973_971_522_401));
test "38-digit number that is not an armstrong number" {
try testIsArmstrongNumber(99_999_999_999_999_999_999_999_999_999_999_999_999, false);
}

test "the largest 128-bit unsigned integer value is not an armstrong number" {
try testing.expect(!isArmstrongNumber(340_282_366_920_938_463_463_374_607_431_768_211_455));
try testIsArmstrongNumber(340_282_366_920_938_463_463_374_607_431_768_211_455, false);
}
31 changes: 31 additions & 0 deletions exercises/practice/yacht/.meta/supplements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"cases": [
{
"description": "Full house three small, two big, alternative order",
"property": "score",
"input": {
"dice": [4, 4, 2, 2, 2],
"category": "full house"
},
"expected": 14
},
{
"description": "Full house two small, three big, alternative order",
"property": "score",
"input": {
"dice": [3, 5, 5, 3, 5],
"category": "full house"
},
"expected": 21
},
{
"description": "Four of a Kind alternative order",
"property": "score",
"input": {
"dice": [4, 4, 6, 4, 4],
"category": "four of a kind"
},
"expected": 16
}
]
}
Loading
Loading