my ziglings
0

Configure Feed

Select the types of activity you want to include in your feed.

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

Chris Boesch (Oct 25, 2025, 12:02 AM +0200) 7ad02b08 b72bcd7e

+26 -42
+17 -33
exercises/102_testing.zig
··· 37 37 const std = @import("std"); 38 38 const testing = std.testing; 39 39 40 - // This is a simple function 41 - // that builds a sum from the 42 - // passed parameters and returns. 40 + // This is a simple function that builds a sum from the passed parameters and 41 + // returns. 43 42 fn add(a: f16, b: f16) f16 { 44 43 return a + b; 45 44 } 46 45 47 - // The associated test. 48 - // It always starts with the keyword "test", 49 - // followed by a description of the tasks 50 - // of the test. This is followed by the 51 - // test cases in curly brackets. 46 + // The associated test. It always starts with the keyword "test", followed by a 47 + // description of the tasks of the test. This is followed by the test cases in 48 + // curly brackets. 52 49 test "add" { 53 50 54 - // The first test checks if the sum 55 - // of '41' and '1' gives '42', which 56 - // is correct. 51 + // The first test checks if the sum of '41' and '1' gives '42', which is 52 + // correct. 57 53 try testing.expect(add(41, 1) == 42); 58 54 59 - // Another way to perform this test 60 - // is as follows: 55 + // Another way to perform this test is as follows: 61 56 try testing.expectEqual(42, add(41, 1)); 62 57 63 - // This time a test with the addition 64 - // of a negative number: 58 + // This time a test with the addition of a negative number: 65 59 try testing.expect(add(5, -4) == 1); 66 60 67 61 // And a floating point operation: 68 62 try testing.expect(add(1.5, 1.5) == 3); 69 63 } 70 64 71 - // Another simple function 72 - // that returns the result 73 - // of subtracting the two 65 + // Another simple function that returns the result of subtracting the two 74 66 // parameters. 75 67 fn sub(a: f16, b: f16) f16 { 76 68 return a - b; 77 69 } 78 70 79 - // The corresponding test 80 - // is not much different 81 - // from the previous one. 82 - // Except that it contains 83 - // an error that you need 84 - // to correct. 71 + // The corresponding test is not much different from the previous one. Except 72 + // that it contains an error that you need to correct. 85 73 test "sub" { 86 74 try testing.expect(sub(10, 5) == 6); 87 75 88 76 try testing.expect(sub(3, 1.5) == 1.5); 89 77 } 90 78 91 - // This function divides the 92 - // numerator by the denominator. 93 - // Here it is important that the 94 - // denominator must not be zero. 95 - // This is checked and if it 96 - // occurs an error is returned. 79 + // This function divides the numerator by the denominator. Here it is important 80 + // that the denominator must not be zero. This is checked and if it occurs an 81 + // error is returned. 97 82 fn divide(a: f16, b: f16) !f16 { 98 83 if (b == 0) return error.DivisionByZero; 99 84 return a / b; ··· 105 90 try testing.expect(divide(10, 2) catch unreachable == 5); 106 91 try testing.expect(divide(1, 3) catch unreachable == 0.3333333333333333); 107 92 108 - // Now we test if the function returns an error 109 - // if we pass a zero as denominator. 110 - // But which error needs to be tested? 93 + // Now we test if the function returns an error if we pass a zero as 94 + // denominator. But which error needs to be tested? 111 95 try testing.expectError(error.???, divide(15, 0)); 112 96 }
+9 -9
patches/patches/102_testing.patch
··· 1 - --- exercises/102_testing.zig 2023-10-03 22:15:22.125574535 +0200 2 - +++ answers/102_testing.zig 2023-10-05 20:04:07.302771500 +0200 3 - @@ -83,7 +83,7 @@ 4 - // an error that you need 5 - // to correct. 1 + --- exercises/102_testing.zig 2025-10-24 12:54:56 2 + +++ answers/102_testing.zig 2025-10-24 12:56:33 3 + @@ -71,7 +71,7 @@ 4 + // The corresponding test is not much different from the previous one. Except 5 + // that it contains an error that you need to correct. 6 6 test "sub" { 7 7 - try testing.expect(sub(10, 5) == 6); 8 8 + try testing.expect(sub(10, 5) == 5); 9 9 10 10 try testing.expect(sub(3, 1.5) == 1.5); 11 11 } 12 - @@ -108,5 +108,5 @@ 13 - // Now we test if the function returns an error 14 - // if we pass a zero as denominator. 15 - // But which error needs to be tested? 12 + @@ -92,5 +92,5 @@ 13 + 14 + // Now we test if the function returns an error if we pass a zero as 15 + // denominator. But which error needs to be tested? 16 16 - try testing.expectError(error.???, divide(15, 0)); 17 17 + try testing.expectError(error.DivisionByZero, divide(15, 0)); 18 18 }