1pub struct LoopGuard {
5 max_ticks: i32,
6 count: i32,
7 message: String,
8}
9
10impl LoopGuard {
11 pub fn new(max: i32) -> LoopGuard {
27 LoopGuard {
28 max_ticks: max,
29 count: 0,
30 message: String::from("Max number of ticks reached"),
31 }
32 }
33
34 pub fn set_panic_message(mut self, message: &str) -> Self {
36 self.message = String::from(message);
37 self
38 }
39
40 pub fn protect(&mut self) {
43 self.count += 1;
44 if self.count > self.max_ticks {
45 panic!("{}", self.message);
46 }
47 }
48}
49
50#[test]
51#[should_panic(expected = "Max number of ticks reached")]
52fn infinite_loop_with_guard() {
53 let mut guard = LoopGuard::new(1000);
55
56 loop {
58 guard.protect();
60 }
61}
62
63#[test]
64#[should_panic(expected = "Infinite Loop 2: Electric Boogaloo")]
65fn infinite_loop_with_guard_with_custom_message() {
66 let mut guard = LoopGuard::new(10).set_panic_message("Infinite Loop 2: Electric Boogaloo");
68
69 loop {
71 guard.protect();
72 }
73}
74
75#[test]
76#[should_panic(expected = "Max number of ticks reached")]
77fn for_loop_surpasses_max_ticks() {
78 let mut guard = LoopGuard::new(10);
80
81 for _i in 0..11 {
83 guard.protect();
84 }
85}
86
87#[test]
88fn for_loop_does_not_surpass_max_ticks() {
89 let mut guard = LoopGuard::new(10);
91
92 for _i in 0..10 {
94 guard.protect();
95 }
96}