···3737const std = @import("std");
3838const testing = std.testing;
39394040-// This is a simple function
4141-// that builds a sum from the
4242-// passed parameters and returns.
4040+// This is a simple function that builds a sum from the passed parameters and
4141+// returns.
4342fn add(a: f16, b: f16) f16 {
4443 return a + b;
4544}
46454747-// The associated test.
4848-// It always starts with the keyword "test",
4949-// followed by a description of the tasks
5050-// of the test. This is followed by the
5151-// test cases in curly brackets.
4646+// The associated test. It always starts with the keyword "test", followed by a
4747+// description of the tasks of the test. This is followed by the test cases in
4848+// curly brackets.
5249test "add" {
53505454- // The first test checks if the sum
5555- // of '41' and '1' gives '42', which
5656- // is correct.
5151+ // The first test checks if the sum of '41' and '1' gives '42', which is
5252+ // correct.
5753 try testing.expect(add(41, 1) == 42);
58545959- // Another way to perform this test
6060- // is as follows:
5555+ // Another way to perform this test is as follows:
6156 try testing.expectEqual(42, add(41, 1));
62576363- // This time a test with the addition
6464- // of a negative number:
5858+ // This time a test with the addition of a negative number:
6559 try testing.expect(add(5, -4) == 1);
66606761 // And a floating point operation:
6862 try testing.expect(add(1.5, 1.5) == 3);
6963}
70647171-// Another simple function
7272-// that returns the result
7373-// of subtracting the two
6565+// Another simple function that returns the result of subtracting the two
7466// parameters.
7567fn sub(a: f16, b: f16) f16 {
7668 return a - b;
7769}
78707979-// The corresponding test
8080-// is not much different
8181-// from the previous one.
8282-// Except that it contains
8383-// an error that you need
8484-// to correct.
7171+// The corresponding test is not much different from the previous one. Except
7272+// that it contains an error that you need to correct.
8573test "sub" {
8674 try testing.expect(sub(10, 5) == 6);
87758876 try testing.expect(sub(3, 1.5) == 1.5);
8977}
90789191-// This function divides the
9292-// numerator by the denominator.
9393-// Here it is important that the
9494-// denominator must not be zero.
9595-// This is checked and if it
9696-// occurs an error is returned.
7979+// This function divides the numerator by the denominator. Here it is important
8080+// that the denominator must not be zero. This is checked and if it occurs an
8181+// error is returned.
9782fn divide(a: f16, b: f16) !f16 {
9883 if (b == 0) return error.DivisionByZero;
9984 return a / b;
···10590 try testing.expect(divide(10, 2) catch unreachable == 5);
10691 try testing.expect(divide(1, 3) catch unreachable == 0.3333333333333333);
10792108108- // Now we test if the function returns an error
109109- // if we pass a zero as denominator.
110110- // But which error needs to be tested?
9393+ // Now we test if the function returns an error if we pass a zero as
9494+ // denominator. But which error needs to be tested?
11195 try testing.expectError(error.???, divide(15, 0));
11296}
+9-9
patches/patches/102_testing.patch
···11---- exercises/102_testing.zig 2023-10-03 22:15:22.125574535 +0200
22-+++ answers/102_testing.zig 2023-10-05 20:04:07.302771500 +0200
33-@@ -83,7 +83,7 @@
44- // an error that you need
55- // to correct.
11+--- exercises/102_testing.zig 2025-10-24 12:54:56
22++++ answers/102_testing.zig 2025-10-24 12:56:33
33+@@ -71,7 +71,7 @@
44+ // The corresponding test is not much different from the previous one. Except
55+ // that it contains an error that you need to correct.
66 test "sub" {
77- try testing.expect(sub(10, 5) == 6);
88+ try testing.expect(sub(10, 5) == 5);
991010 try testing.expect(sub(3, 1.5) == 1.5);
1111 }
1212-@@ -108,5 +108,5 @@
1313- // Now we test if the function returns an error
1414- // if we pass a zero as denominator.
1515- // But which error needs to be tested?
1212+@@ -92,5 +92,5 @@
1313+1414+ // Now we test if the function returns an error if we pass a zero as
1515+ // denominator. But which error needs to be tested?
1616- try testing.expectError(error.???, divide(15, 0));
1717+ try testing.expectError(error.DivisionByZero, divide(15, 0));
1818 }