-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensorTest.java
More file actions
34 lines (29 loc) · 1.22 KB
/
Copy pathSensorTest.java
File metadata and controls
34 lines (29 loc) · 1.22 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
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class SensorTest {
private static final double DELTA = 0.01; // Increased delta for floating-point tolerance
@Test
public void testMoistureReadingBounds() {
assertEquals(0.0, convertToVoltage(0), DELTA, "0 should convert to 0V");
assertEquals(5.0, convertToVoltage(1023), DELTA, "1023 should convert to 5V");
assertEquals(2.5, convertToVoltage(512), DELTA, "512 should convert to ~2.5V");
}
@Test
public void testNegativeMoistureValue() {
assertThrows(IllegalArgumentException.class,
() -> convertToVoltage(-1),
"Negative values should throw exception");
}
@Test
public void testAboveMaxMoistureValue() {
assertThrows(IllegalArgumentException.class,
() -> convertToVoltage(1100),
"Values > 1023 should throw exception");
}
private double convertToVoltage(int sensorValue) {
if (sensorValue < 0 || sensorValue > 1023) {
throw new IllegalArgumentException("Sensor value out of range (0-1023)");
}
return (sensorValue / 1023.0) * 5;
}
}