-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage_integration.test.zig
More file actions
71 lines (56 loc) · 1.63 KB
/
coverage_integration.test.zig
File metadata and controls
71 lines (56 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const std = @import("std");
// These are simple tests to verify coverage tool integration works
// The framework will run these with kcov/grindcov when --coverage is enabled
test "coverage test: basic math" {
const a: i32 = 2 + 2;
try std.testing.expectEqual(@as(i32, 4), a);
const b: i32 = 10 - 5;
try std.testing.expectEqual(@as(i32, 5), b);
const c: i32 = 3 * 4;
try std.testing.expectEqual(@as(i32, 12), c);
}
test "coverage test: conditionals" {
const value = 42;
if (value > 0) {
try std.testing.expect(true);
} else {
try std.testing.expect(false);
}
if (value < 0) {
try std.testing.expect(false);
} else {
try std.testing.expect(true);
}
}
test "coverage test: loops" {
var sum: i32 = 0;
var i: i32 = 0;
while (i < 10) : (i += 1) {
sum += i;
}
try std.testing.expectEqual(@as(i32, 45), sum);
}
test "coverage test: functions" {
const result = addNumbers(5, 10);
try std.testing.expectEqual(@as(i32, 15), result);
const result2 = multiplyNumbers(3, 7);
try std.testing.expectEqual(@as(i32, 21), result2);
}
fn addNumbers(a: i32, b: i32) i32 {
return a + b;
}
fn multiplyNumbers(a: i32, b: i32) i32 {
return a * b;
}
test "coverage test: error handling" {
const result = divideNumbers(10, 2) catch unreachable;
try std.testing.expectEqual(@as(i32, 5), result);
const err_result = divideNumbers(10, 0);
try std.testing.expectError(error.DivisionByZero, err_result);
}
fn divideNumbers(a: i32, b: i32) !i32 {
if (b == 0) {
return error.DivisionByZero;
}
return @divTrunc(a, b);
}