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
#![cfg(test)]
use thag_profiler::warn_once;
/// Tests for the `warn_once` macro
///
/// These tests verify that the warning suppression mechanisms work correctly
/// by ensuring warning functions are only called once.
#[cfg(test)]
mod tests {
use super::*;
use std::sync::{Arc, Mutex};
#[test]
fn test_warn_once_macro() {
// Counter for function calls
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
// Condition that's always true
let condition = true;
// Call warn_once multiple times
for _ in 0..5 {
warn_once!(condition, || {
let mut count = counter_clone.lock().unwrap();
*count += 1;
});
}
// The warning function should only be called once
assert_eq!(
*counter.lock().unwrap(),
1,
"Warning function should only be called once"
);
}
#[test]
fn test_warn_once_with_return() {
// Counter for function calls
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
// Variable to track early returns
let mut returns = 0;
// Call warn_once with early return multiple times
for _ in 0..5 {
warn_once!(
true,
|| {
let mut count = counter_clone.lock().unwrap();
*count += 1;
},
{
returns += 1;
continue;
}
);
// This should never execute due to the continue in the return expression
panic!("This should not be reached");
}
// The warning function should only be called once
assert_eq!(
*counter.lock().unwrap(),
1,
"Warning function should only be called once"
);
// But we should have returned 5 times
assert_eq!(returns, 5, "Early return should happen on every iteration");
}
#[test]
fn test_warn_once_false_condition() {
// Counter for function calls
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
// Call warn_once with false condition
for _ in 0..5 {
warn_once!(false, || {
let mut count = counter_clone.lock().unwrap();
*count += 1;
});
}
// The warning function should never be called
assert_eq!(
*counter.lock().unwrap(),
0,
"Warning function should not be called when condition is false"
);
}
}