Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zig TDD Exercises #251

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
42 changes: 42 additions & 0 deletions exercises/practice/armstrong-numbers/test_armstrong_numbers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,96 @@ const testing = std.testing;

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

// Adding "return error.SkipZigTest" to the top of each test results in a compiler error
// This wrapper function around error.SkipZigTest appeases the compiler
fn skipTest() !void {
return error.SkipZigTest;
}

test "zero is an armstrong number" {
try testing.expect(isArmstrongNumber(0));
}

test "single-digit numbers are armstrong numbers" {
// Delete or comment out below line to run test
try skipTest();

try testing.expect(isArmstrongNumber(5));
}

test "there are no two-digit armstrong numbers" {
// Delete or comment out below line to run test
try skipTest();

try testing.expect(!isArmstrongNumber(10));
}

test "three-digit number that is an armstrong number" {
// Delete or comment out below line to run test
try skipTest();

try testing.expect(isArmstrongNumber(153));
}

test "three-digit number that is not an armstrong number" {
// Delete or comment out below line to run test
try skipTest();

try testing.expect(!isArmstrongNumber(100));
}

test "four-digit number that is an armstrong number" {
// Delete or comment out below line to run test
try skipTest();

try testing.expect(isArmstrongNumber(9_474));
}

test "four-digit number that is not an armstrong number" {
// Delete or comment out below line to run test
try skipTest();

try testing.expect(!isArmstrongNumber(9_475));
}

test "seven-digit number that is an armstrong number" {
// Delete or comment out below line to run test
try skipTest();

try testing.expect(isArmstrongNumber(9_926_315));
}

test "seven-digit number that is not an armstrong number" {
// Delete or comment out below line to run test
try skipTest();

try testing.expect(!isArmstrongNumber(9_926_314));
}

test "33-digit number that is an armstrong number" {
// Delete or comment out below line to run test
try skipTest();

try testing.expect(isArmstrongNumber(186_709_961_001_538_790_100_634_132_976_990));
}

test "38-digit number that is not an armstrong number" {
// Delete or comment out below line to run test
try skipTest();

try testing.expect(!isArmstrongNumber(99_999_999_999_999_999_999_999_999_999_999_999_999));
}

test "the largest and last armstrong number" {
// Delete or comment out below line to run test
try skipTest();

try testing.expect(isArmstrongNumber(115_132_219_018_763_992_565_095_597_973_971_522_401));
}

test "the largest 128-bit unsigned integer value is not an armstrong number" {
// Delete or comment out below line to run test
try skipTest();

try testing.expect(!isArmstrongNumber(340_282_366_920_938_463_463_374_607_431_768_211_455));
}
36 changes: 36 additions & 0 deletions exercises/practice/binary-search/test_binary_search.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,94 @@ const binary_search = @import("binary_search.zig");
const binarySearch = binary_search.binarySearch;
const SearchError = binary_search.SearchError;

// Adding "return error.SkipZigTest" to the top of each test results in a compiler error
// This wrapper function around error.SkipZigTest appeases the compiler
fn skipTest() !void {
return error.SkipZigTest;
}

test "finds a value in an array with one element" {
const expected: usize = 0;
const actual = try binarySearch(i4, 6, &[_]i4{6});
try testing.expectEqual(expected, actual);
}

test "finds a value in the middle of an array" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 3;
const actual = try binarySearch(u4, 6, &[_]u4{ 1, 3, 4, 6, 8, 9, 11 });
try testing.expectEqual(expected, actual);
}

test "finds a value at the beginning of an array" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 0;
const actual = try binarySearch(i8, 1, &[_]i8{ 1, 3, 4, 6, 8, 9, 11 });
try testing.expectEqual(expected, actual);
}

test "finds a value at the end of an array" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 6;
const actual = try binarySearch(u8, 11, &[_]u8{ 1, 3, 4, 6, 8, 9, 11 });
try testing.expectEqual(expected, actual);
}

test "finds a value in an array of odd length" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 5;
const actual = try binarySearch(i16, 21, &[_]i16{ 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634 });
try testing.expectEqual(expected, actual);
}

test "finds a value in an array of even length" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 5;
const actual = try binarySearch(u16, 21, &[_]u16{ 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 });
try testing.expectEqual(expected, actual);
}

test "identifies that a value is not included in the array" {
// Delete or comment out below line to run test
try skipTest();

try testing.expectError(SearchError.ValueAbsent, binarySearch(i32, 7, &[_]i32{ 1, 3, 4, 6, 8, 9, 11 }));
}

test "a value smaller than the array's smallest value is not found" {
// Delete or comment out below line to run test
try skipTest();

try testing.expectError(SearchError.ValueAbsent, binarySearch(u32, 0, &[_]u32{ 1, 3, 4, 6, 8, 9, 11 }));
}

test "a value larger than the array's largest value is not found" {
// Delete or comment out below line to run test
try skipTest();

try testing.expectError(SearchError.ValueAbsent, binarySearch(i64, 13, &[_]i64{ 1, 3, 4, 6, 8, 9, 11 }));
}

test "nothing is found in an empty array" {
// Delete or comment out below line to run test
try skipTest();

try testing.expectError(SearchError.EmptyBuffer, binarySearch(u64, 13, &[_]u64{}));
}

test "nothing is found when the left and right bounds cross" {
// Delete or comment out below line to run test
try skipTest();

try testing.expectError(SearchError.ValueAbsent, binarySearch(isize, 13, &[_]isize{ 1, 2 }));
}
18 changes: 18 additions & 0 deletions exercises/practice/collatz-conjecture/test_collatz_conjecture.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,49 @@ const testing = std.testing;
const collatz_conjecture = @import("collatz_conjecture.zig");
const ComputationError = collatz_conjecture.ComputationError;

// Adding "return error.SkipZigTest" to the top of each test results in a compiler error
// This wrapper function around error.SkipZigTest appeases the compiler
fn skipTest() !void {
return error.SkipZigTest;
}

test "zero steps for one" {
const expected: usize = 0;
const actual = try collatz_conjecture.steps(1);
try testing.expectEqual(expected, actual);
}

test "divide if even" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 4;
const actual = try collatz_conjecture.steps(16);
try testing.expectEqual(expected, actual);
}

test "even and odd steps" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 9;
const actual = try collatz_conjecture.steps(12);
try testing.expectEqual(expected, actual);
}

test "large number of even and odd steps" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 152;
const actual = try collatz_conjecture.steps(1_000_000);
try testing.expectEqual(expected, actual);
}

test "zero is an error" {
// Delete or comment out below line to run test
try skipTest();

const expected = ComputationError.IllegalArgument;
const actual = collatz_conjecture.steps(0);
try testing.expectError(expected, actual);
Expand Down
42 changes: 42 additions & 0 deletions exercises/practice/darts/test_darts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ const testing = std.testing;

const darts = @import("darts.zig");

// Adding "return error.SkipZigTest" to the top of each test results in a compiler error
// This wrapper function around error.SkipZigTest appeases the compiler
fn skipTest() !void {
return error.SkipZigTest;
}

test "missed target" {
const expected: usize = 0;
const coordinate = darts.Coordinate.init(-9.0, 9.0);
Expand All @@ -11,83 +17,119 @@ test "missed target" {
}

test "on the outer circle" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 1;
const coordinate = darts.Coordinate.init(0.0, 10.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "on the middle circle" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 5;
const coordinate = darts.Coordinate.init(-5.0, 0.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "on the inner circle" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 10;
const coordinate = darts.Coordinate.init(0.0, -1.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "exactly on center" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 10;
const coordinate = darts.Coordinate.init(0.0, 0.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "near the center" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 10;
const coordinate = darts.Coordinate.init(-0.1, -0.1);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "just within the inner circle" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 10;
const coordinate = darts.Coordinate.init(0.7, 0.7);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "just outside the inner circle" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 5;
const coordinate = darts.Coordinate.init(0.8, -0.8);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "just within the middle circle" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 5;
const coordinate = darts.Coordinate.init(3.5, -3.5);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "just outside the middle circle" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 1;
const coordinate = darts.Coordinate.init(-3.6, -3.6);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "just within the outer circle" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 1;
const coordinate = darts.Coordinate.init(-7.0, 7.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "just outside the outer circle" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 0;
const coordinate = darts.Coordinate.init(7.1, -7.1);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "asymmetric position between the inner and middle circles" {
// Delete or comment out below line to run test
try skipTest();

const expected: usize = 5;
const coordinate = darts.Coordinate.init(0.5, -4.0);
const actual = coordinate.score();
Expand Down
Loading