()
| 122 | // Clamp to limit |
| 123 | self.count = self.limit; |
| 124 | // Return how much was actually added to reach the limit |
| 125 | self.limit - current_count |
| 126 | } else { |
| 127 | // Increase count by the full amount |
| 128 | self.count = potential_count; |
| 129 | amount // Full amount was added |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Reset the counter to zero |
| 134 | fn reset(&mut self) { |
| 135 | self.count = 0; |
| 136 | } |
| 137 | |
| 138 | // Enable the counter |
| 139 | fn enable(&mut self) { |
| 140 | self.enabled = true; |
| 141 | } |
| 142 | |
| 143 | // Disable the counter |
| 144 | fn disable(&mut self) { |
| 145 | self.enabled = false; |
| 146 | } |
| 147 | |
| 148 | // Set a new limit. Clamps count if necessary. |
| 149 | fn set_limit(&mut self, new_limit: u32) { |
| 150 | self.limit = new_limit; |
| 151 | // Clamp count if it now exceeds the new limit |
| 152 | if self.count > self.limit { |
| 153 | self.count = self.limit; |
| 154 | } |
| 155 | } |
| 156 | } // end impl NamedCounter |
| 157 | |
| 158 | |
| 159 | fn main() { |
| 160 | ObjectMethodCollision.wait(); |
| 161 | ObjectMethodCollision.notify(); |
| 162 | ObjectMethodCollision.notifyAll(); |
| 163 | ObjectWaitMillis.wait(1); |
| 164 | ObjectWaitMillisNanos.wait(1, 2); |
| 165 | assert!(NonConflictingWait.wait(41) == 42); |
| 166 | |
| 167 | // === Test Case 1: Basic Operations === |
| 168 | let mut counter1 = NamedCounter::new("Clicks", 5); |
| 169 | |
| 170 | // Initial state assertions |
| 171 | assert!(counter1.get_name() == "Clicks"); |
| 172 | assert!(counter1.get_count() == 0); |
| 173 | assert!(counter1.get_limit() == 5); |
| 174 | assert!(counter1.is_enabled()); |
| 175 | assert!(!counter1.is_at_limit()); |
| 176 | |
| 177 | // Test increment |
| 178 | assert!(counter1.increment()); // Should succeed (returns true) |
| 179 | assert!(counter1.get_count() == 1); |
| 180 | assert!(!counter1.is_at_limit()); |
| 181 |
nothing calls this directly
no test coverage detected