Merge pull request 'Wrap comment at 80 chars in 102' (#314) from whlr/ziglings-exercises:rewrap-102 into main

Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/314
This commit is contained in:
Chris Boesch 2025-10-25 00:02:26 +02:00
commit 7ad02b084f
2 changed files with 26 additions and 42 deletions

View File

@ -37,63 +37,48 @@
const std = @import("std"); const std = @import("std");
const testing = std.testing; const testing = std.testing;
// This is a simple function // This is a simple function that builds a sum from the passed parameters and
// that builds a sum from the // returns.
// passed parameters and returns.
fn add(a: f16, b: f16) f16 { fn add(a: f16, b: f16) f16 {
return a + b; return a + b;
} }
// The associated test. // The associated test. It always starts with the keyword "test", followed by a
// It always starts with the keyword "test", // description of the tasks of the test. This is followed by the test cases in
// followed by a description of the tasks // curly brackets.
// of the test. This is followed by the
// test cases in curly brackets.
test "add" { test "add" {
// The first test checks if the sum // The first test checks if the sum of '41' and '1' gives '42', which is
// of '41' and '1' gives '42', which // correct.
// is correct.
try testing.expect(add(41, 1) == 42); try testing.expect(add(41, 1) == 42);
// Another way to perform this test // Another way to perform this test is as follows:
// is as follows:
try testing.expectEqual(42, add(41, 1)); try testing.expectEqual(42, add(41, 1));
// This time a test with the addition // This time a test with the addition of a negative number:
// of a negative number:
try testing.expect(add(5, -4) == 1); try testing.expect(add(5, -4) == 1);
// And a floating point operation: // And a floating point operation:
try testing.expect(add(1.5, 1.5) == 3); try testing.expect(add(1.5, 1.5) == 3);
} }
// Another simple function // Another simple function that returns the result of subtracting the two
// that returns the result
// of subtracting the two
// parameters. // parameters.
fn sub(a: f16, b: f16) f16 { fn sub(a: f16, b: f16) f16 {
return a - b; return a - b;
} }
// The corresponding test // The corresponding test is not much different from the previous one. Except
// is not much different // that it contains an error that you need to correct.
// from the previous one.
// Except that it contains
// an error that you need
// to correct.
test "sub" { test "sub" {
try testing.expect(sub(10, 5) == 6); try testing.expect(sub(10, 5) == 6);
try testing.expect(sub(3, 1.5) == 1.5); try testing.expect(sub(3, 1.5) == 1.5);
} }
// This function divides the // This function divides the numerator by the denominator. Here it is important
// numerator by the denominator. // that the denominator must not be zero. This is checked and if it occurs an
// Here it is important that the // error is returned.
// denominator must not be zero.
// This is checked and if it
// occurs an error is returned.
fn divide(a: f16, b: f16) !f16 { fn divide(a: f16, b: f16) !f16 {
if (b == 0) return error.DivisionByZero; if (b == 0) return error.DivisionByZero;
return a / b; return a / b;
@ -105,8 +90,7 @@ test "divide" {
try testing.expect(divide(10, 2) catch unreachable == 5); try testing.expect(divide(10, 2) catch unreachable == 5);
try testing.expect(divide(1, 3) catch unreachable == 0.3333333333333333); try testing.expect(divide(1, 3) catch unreachable == 0.3333333333333333);
// Now we test if the function returns an error // Now we test if the function returns an error if we pass a zero as
// if we pass a zero as denominator. // denominator. But which error needs to be tested?
// But which error needs to be tested?
try testing.expectError(error.???, divide(15, 0)); try testing.expectError(error.???, divide(15, 0));
} }

View File

@ -1,18 +1,18 @@
--- exercises/102_testing.zig 2023-10-03 22:15:22.125574535 +0200 --- exercises/102_testing.zig 2025-10-24 12:54:56
+++ answers/102_testing.zig 2023-10-05 20:04:07.302771500 +0200 +++ answers/102_testing.zig 2025-10-24 12:56:33
@@ -83,7 +83,7 @@ @@ -71,7 +71,7 @@
// an error that you need // The corresponding test is not much different from the previous one. Except
// to correct. // that it contains an error that you need to correct.
test "sub" { test "sub" {
- try testing.expect(sub(10, 5) == 6); - try testing.expect(sub(10, 5) == 6);
+ try testing.expect(sub(10, 5) == 5); + try testing.expect(sub(10, 5) == 5);
try testing.expect(sub(3, 1.5) == 1.5); try testing.expect(sub(3, 1.5) == 1.5);
} }
@@ -108,5 +108,5 @@ @@ -92,5 +92,5 @@
// Now we test if the function returns an error
// if we pass a zero as denominator. // Now we test if the function returns an error if we pass a zero as
// But which error needs to be tested? // denominator. But which error needs to be tested?
- try testing.expectError(error.???, divide(15, 0)); - try testing.expectError(error.???, divide(15, 0));
+ try testing.expectError(error.DivisionByZero, divide(15, 0)); + try testing.expectError(error.DivisionByZero, divide(15, 0));
} }