Skip to content

Commit

Permalink
exercises: tests: remove comptime
Browse files Browse the repository at this point in the history
  • Loading branch information
ee7 committed Mar 3, 2023
1 parent 718b96a commit 17c148d
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 147 deletions.
30 changes: 18 additions & 12 deletions exercises/practice/binary-search/test_binary_search.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,39 @@ const binarySearch = binary_search.binarySearch;
const SearchError = binary_search.SearchError;

test "finds a value in an array with one element" {
const actual = comptime try binarySearch(i4, 6, &[_]i4{6});
try testing.expectEqual(0, actual);
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" {
const actual = comptime try binarySearch(u4, 6, &[_]u4{ 1, 3, 4, 6, 8, 9, 11 });
try testing.expectEqual(3, actual);
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" {
const actual = comptime try binarySearch(i8, 1, &[_]i8{ 1, 3, 4, 6, 8, 9, 11 });
try testing.expectEqual(0, actual);
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" {
const actual = comptime try binarySearch(u8, 11, &[_]u8{ 1, 3, 4, 6, 8, 9, 11 });
try testing.expectEqual(6, actual);
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" {
const actual = comptime try binarySearch(i16, 21, &[_]i16{ 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634 });
try testing.expectEqual(5, actual);
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" {
const actual = comptime try binarySearch(u16, 21, &[_]u16{ 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 });
try testing.expectEqual(5, actual);
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" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ const collatz_conjecture = @import("collatz_conjecture.zig");
const ComputationError = collatz_conjecture.ComputationError;

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

test "divide if even" {
const expected = 4;
const actual = comptime try collatz_conjecture.steps(16);
const expected: usize = 4;
const actual = try collatz_conjecture.steps(16);
try testing.expectEqual(expected, actual);
}

test "even and odd steps" {
const expected = 9;
const actual = comptime try collatz_conjecture.steps(12);
const expected: usize = 9;
const actual = try collatz_conjecture.steps(12);
try testing.expectEqual(expected, actual);
}

test "large number of even and odd steps" {
const expected = 152;
const actual = comptime try collatz_conjecture.steps(1_000_000);
const expected: usize = 152;
const actual = try collatz_conjecture.steps(1_000_000);
try testing.expectEqual(expected, actual);
}

test "zero is an error" {
const expected = ComputationError.IllegalArgument;
const actual = comptime collatz_conjecture.steps(0);
const actual = collatz_conjecture.steps(0);
try testing.expectError(expected, actual);
}
78 changes: 39 additions & 39 deletions exercises/practice/darts/test_darts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,92 @@ const testing = std.testing;
const darts = @import("darts.zig");

test "missed target" {
const coordinate = comptime darts.Coordinate.init(-9.0, 9.0);
const expected = 0;
const actual = comptime coordinate.score();
const expected: usize = 0;
const coordinate = darts.Coordinate.init(-9.0, 9.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}

test "on the outer circle" {
const coordinate = comptime darts.Coordinate.init(0.0, 10.0);
const expected = 1;
const actual = comptime coordinate.score();
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" {
const coordinate = comptime darts.Coordinate.init(-5.0, 0.0);
const expected = 5;
const actual = comptime coordinate.score();
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" {
const coordinate = comptime darts.Coordinate.init(0.0, -1.0);
const expected = 10;
const actual = comptime coordinate.score();
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" {
const coordinate = comptime darts.Coordinate.init(0.0, 0.0);
const expected = 10;
const actual = comptime coordinate.score();
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" {
const coordinate = comptime darts.Coordinate.init(-0.1, -0.1);
const expected = 10;
const actual = comptime coordinate.score();
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" {
const coordinate = comptime darts.Coordinate.init(0.7, 0.7);
const expected = 10;
const actual = comptime coordinate.score();
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" {
const coordinate = comptime darts.Coordinate.init(0.8, -0.8);
const expected = 5;
const actual = comptime coordinate.score();
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" {
const coordinate = comptime darts.Coordinate.init(3.5, -3.5);
const expected = 5;
const actual = comptime coordinate.score();
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" {
const coordinate = comptime darts.Coordinate.init(-3.6, -3.6);
const expected = 1;
const actual = comptime coordinate.score();
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" {
const coordinate = comptime darts.Coordinate.init(-7.0, 7.0);
const expected = 1;
const actual = comptime coordinate.score();
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" {
const coordinate = comptime darts.Coordinate.init(7.1, -7.1);
const expected = 0;
const actual = comptime coordinate.score();
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" {
const coordinate = comptime darts.Coordinate.init(0.5, -4.0);
const expected = 5;
const actual = comptime coordinate.score();
const expected: usize = 5;
const coordinate = darts.Coordinate.init(0.5, -4.0);
const actual = coordinate.score();
try testing.expectEqual(expected, actual);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,55 @@ const testing = std.testing;
const difference_of_squares = @import("difference_of_squares.zig");

test "square of sum up to 1" {
const expected = 1;
const actual = comptime difference_of_squares.squareOfSum(1);
const expected: usize = 1;
const actual = difference_of_squares.squareOfSum(1);
try testing.expectEqual(expected, actual);
}

test "square of sum up to 5" {
const expected = 225;
const actual = comptime difference_of_squares.squareOfSum(5);
const expected: usize = 225;
const actual = difference_of_squares.squareOfSum(5);
try testing.expectEqual(expected, actual);
}

test "square of sum up to 100" {
const expected = 25_502_500;
const actual = comptime difference_of_squares.squareOfSum(100);
const expected: usize = 25_502_500;
const actual = difference_of_squares.squareOfSum(100);
try testing.expectEqual(expected, actual);
}

test "sum of squares up to 1" {
const expected = 1;
const actual = comptime difference_of_squares.sumOfSquares(1);
const expected: usize = 1;
const actual = difference_of_squares.sumOfSquares(1);
try testing.expectEqual(expected, actual);
}

test "sum of squares up to 5" {
const expected = 55;
const actual = comptime difference_of_squares.sumOfSquares(5);
const expected: usize = 55;
const actual = difference_of_squares.sumOfSquares(5);
try testing.expectEqual(expected, actual);
}

test "sum of squares up to 100" {
const expected = 338_350;
const actual = comptime difference_of_squares.sumOfSquares(100);
const expected: usize = 338_350;
const actual = difference_of_squares.sumOfSquares(100);
try testing.expectEqual(expected, actual);
}

test "difference of squares up to 1" {
const expected = 0;
const actual = comptime difference_of_squares.differenceOfSquares(1);
const expected: usize = 0;
const actual = difference_of_squares.differenceOfSquares(1);
try testing.expectEqual(expected, actual);
}

test "difference of squares up to 5" {
const expected = 170;
const actual = comptime difference_of_squares.differenceOfSquares(5);
const expected: usize = 170;
const actual = difference_of_squares.differenceOfSquares(5);
try testing.expectEqual(expected, actual);
}

test "difference of squares up to 100" {
const expected = 25_164_150;
const actual = comptime difference_of_squares.differenceOfSquares(100);
const expected: usize = 25_164_150;
const actual = difference_of_squares.differenceOfSquares(100);
try testing.expectEqual(expected, actual);
}
36 changes: 18 additions & 18 deletions exercises/practice/grains/test_grains.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,61 @@ const grains = @import("grains.zig");
const ChessboardError = grains.ChessboardError;

test "grains on square 1" {
const expected = 1;
const actual = comptime try grains.square(1);
const expected: usize = 1;
const actual = try grains.square(1);
try testing.expectEqual(expected, actual);
}

test "grains on square 2" {
const expected = 2;
const actual = comptime try grains.square(2);
const expected: usize = 2;
const actual = try grains.square(2);
try testing.expectEqual(expected, actual);
}

test "grains on square 3" {
const expected = 4;
const actual = comptime try grains.square(3);
const expected: usize = 4;
const actual = try grains.square(3);
try testing.expectEqual(expected, actual);
}

test "grains on square 4" {
const expected = 8;
const actual = comptime try grains.square(4);
const expected: usize = 8;
const actual = try grains.square(4);
try testing.expectEqual(expected, actual);
}

test "grains on square 16" {
const expected = 32_768;
const actual = comptime try grains.square(16);
const expected: usize = 32_768;
const actual = try grains.square(16);
try testing.expectEqual(expected, actual);
}

test "grains on square 32" {
const expected = 2_147_483_648;
const actual = comptime try grains.square(32);
const expected: usize = 2_147_483_648;
const actual = try grains.square(32);
try testing.expectEqual(expected, actual);
}

test "grains on square 64" {
const expected = 9_223_372_036_854_775_808;
const actual = comptime try grains.square(64);
const expected: usize = 9_223_372_036_854_775_808;
const actual = try grains.square(64);
try testing.expectEqual(expected, actual);
}

test "square 0 produces an error" {
const expected = ChessboardError.IndexOutOfBounds;
const actual = comptime grains.square(0);
const actual = grains.square(0);
try testing.expectError(expected, actual);
}

test "square greater than 64 produces an error" {
const expected = ChessboardError.IndexOutOfBounds;
const actual = comptime grains.square(65);
const actual = grains.square(65);
try testing.expectError(expected, actual);
}

test "returns the total number of grains on the board" {
const expected = 18_446_744_073_709_551_615;
const actual = comptime grains.total();
const expected: usize = 18_446_744_073_709_551_615;
const actual = grains.total();
try testing.expectEqual(expected, actual);
}
Loading

0 comments on commit 17c148d

Please sign in to comment.