-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.rs
More file actions
48 lines (40 loc) · 1.44 KB
/
testing.rs
File metadata and controls
48 lines (40 loc) · 1.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
//! testing.rs
//!
//! Introduction to Rust testing — unit tests, integration tests, and the test
//! attribute. Shows how to write and run tests using the built-in test framework.
fn main() {
println!("Running the program...");
println!("Sum of 2 + 3 = {}", add(2, 3));
}
// A simple function to test
fn add(a: i32, b: i32) -> i32 {
a + b
}
// ──────────────────────────────────────────────────────────────
// Unit tests — placed in the same file for teaching purposes
// ──────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*; // bring add() into scope for testing
#[test]
fn test_add_positive_numbers() {
assert_eq!(add(2, 3), 5);
assert_eq!(add(10, 20), 30);
}
#[test]
fn test_add_negative_numbers() {
assert_eq!(add(-5, 3), -2);
assert_eq!(add(-10, -20), -30);
}
#[test]
fn test_add_zero() {
assert_eq!(add(0, 0), 0);
assert_eq!(add(5, 0), 5);
}
#[test]
#[should_panic(expected = "attempt to add with overflow")]
fn test_add_overflow() {
// This test is expected to panic on overflow (i32::MAX + 1)
let _ = add(i32::MAX, 1);
}
}