-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_testing.zig
More file actions
181 lines (146 loc) · 5.44 KB
/
Copy path06_testing.zig
File metadata and controls
181 lines (146 loc) · 5.44 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const std = @import("std");
// Function we want to test
fn add(a: i32, b: i32) i32 {
return a + b;
}
// Another function to test
fn fibonacci(n: u32) u32 {
if (n == 0 or n == 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
// A struct with methods we want to test
const Calculator = struct {
last_result: i32 = 0,
// Reset the calculator
pub fn reset(self: *Calculator) void {
self.last_result = 0;
}
// Add a number to the current result
pub fn add(self: *Calculator, value: i32) void {
self.last_result += value;
}
// Subtract a number from the current result
pub fn subtract(self: *Calculator, value: i32) void {
self.last_result -= value;
}
// Multiply the current result by a number
pub fn multiply(self: *Calculator, value: i32) void {
self.last_result *= value;
}
// Get the current result
pub fn getResult(self: Calculator) i32 {
return self.last_result;
}
};
// Basic test for add function
test "basic addition" {
const result = add(3, 4);
try std.testing.expectEqual(@as(i32, 7), result);
}
// Test multiple cases of the same function
test "multiple additions" {
try std.testing.expectEqual(@as(i32, 5), add(2, 3));
try std.testing.expectEqual(@as(i32, 0), add(0, 0));
try std.testing.expectEqual(@as(i32, -8), add(-5, -3));
try std.testing.expectEqual(@as(i32, 0), add(5, -5));
}
// Test fibonacci function
test "fibonacci sequence" {
try std.testing.expectEqual(@as(u32, 0), fibonacci(0));
try std.testing.expectEqual(@as(u32, 1), fibonacci(1));
try std.testing.expectEqual(@as(u32, 1), fibonacci(2));
try std.testing.expectEqual(@as(u32, 2), fibonacci(3));
try std.testing.expectEqual(@as(u32, 3), fibonacci(4));
try std.testing.expectEqual(@as(u32, 5), fibonacci(5));
try std.testing.expectEqual(@as(u32, 8), fibonacci(6));
try std.testing.expectEqual(@as(u32, 13), fibonacci(7));
}
// Test our Calculator struct
test "calculator operations" {
var calc = Calculator{};
// Initial state
try std.testing.expectEqual(@as(i32, 0), calc.getResult());
// Addition
calc.add(5);
try std.testing.expectEqual(@as(i32, 5), calc.getResult());
// Subtraction
calc.subtract(2);
try std.testing.expectEqual(@as(i32, 3), calc.getResult());
// Multiplication
calc.multiply(4);
try std.testing.expectEqual(@as(i32, 12), calc.getResult());
// Reset
calc.reset();
try std.testing.expectEqual(@as(i32, 0), calc.getResult());
}
// Test that demonstrates allocation with testing allocator
test "array list with testing allocator" {
// The testing allocator will detect memory leaks
const allocator = std.testing.allocator;
var list = std.ArrayList(i32).init(allocator);
defer list.deinit(); // Don't forget to free the memory!
try list.append(10);
try list.append(20);
try list.append(30);
try std.testing.expectEqual(@as(usize, 3), list.items.len);
try std.testing.expectEqual(@as(i32, 10), list.items[0]);
try std.testing.expectEqual(@as(i32, 20), list.items[1]);
try std.testing.expectEqual(@as(i32, 30), list.items[2]);
}
// Test that demonstrates expecting errors
test "expecting errors" {
// A function that should return an error
const result1 = std.fmt.parseInt(i32, "not a number", 10);
try std.testing.expectError(error.InvalidCharacter, result1);
// A function that should not return an error
const result2 = std.fmt.parseInt(i32, "42", 10);
try std.testing.expectEqual(@as(i32, 42), try result2);
}
// Test skipping example
test "skip this test" {
// When you want to temporarily skip a test
if (true) return error.SkipZigTest;
// This code won't be executed
unreachable;
}
// Test that demonstrates the use of test blocks in different scopes
const MathFunctions = struct {
// Nested test inside a struct
test "nested test in struct" {
try std.testing.expect(true);
}
pub fn square(x: i32) i32 {
return x * x;
}
pub fn cube(x: i32) i32 {
return x * x * x;
}
// Another nested test
test "square function" {
try std.testing.expectEqual(@as(i32, 4), square(2));
try std.testing.expectEqual(@as(i32, 9), square(3));
try std.testing.expectEqual(@as(i32, 0), square(0));
}
};
// Main function (not used during testing)
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
try stdout.print("\n=== Zig Testing Example ===\n\n", .{});
try stdout.print("This file is designed to be run with 'zig test' rather than 'zig run'.\n", .{});
try stdout.print("Try running it with: zig test 06_testing.zig\n", .{});
try stdout.print("\nAdd function demo: {d} + {d} = {d}\n", .{ 5, 7, add(5, 7) });
try stdout.print("\nCalculator demo:\n", .{});
var calc = Calculator{};
calc.add(10);
try stdout.print("Add 10: {d}\n", .{calc.getResult()});
calc.multiply(2);
try stdout.print("Multiply by 2: {d}\n", .{calc.getResult()});
calc.subtract(5);
try stdout.print("Subtract 5: {d}\n", .{calc.getResult()});
try stdout.print("\nFibonacci sequence (first 8 numbers):\n", .{});
for (0..8) |i| {
const n = @as(u32, @intCast(i));
try stdout.print("fibonacci({d}) = {d}\n", .{ n, fibonacci(n) });
}
try stdout.print("\n=== End of Testing Example ===\n", .{});
}