Files
allwpilib/wpilibj/src/test/java/org/wpilib/simulation/AnalogInputSimTest.java

65 lines
1.8 KiB
Java
Raw Normal View History

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
2025-11-07 19:55:43 -05:00
package org.wpilib.simulation;
import static org.junit.jupiter.api.Assertions.assertEquals;
2021-08-12 02:04:14 -04:00
import static org.junit.jupiter.api.Assertions.assertTrue;
2025-11-07 19:55:43 -05:00
import org.wpilib.hardware.hal.HAL;
import org.wpilib.hardware.discrete.AnalogInput;
import org.wpilib.simulation.testutils.BooleanCallback;
import org.wpilib.simulation.testutils.DoubleCallback;
import org.junit.jupiter.api.Test;
class AnalogInputSimTest {
2021-08-12 02:04:14 -04:00
@Test
void setInitializeTest() {
HAL.initialize(500, 0);
AnalogInputSim sim = new AnalogInputSim(5);
BooleanCallback callback = new BooleanCallback();
try (CallbackStore cb = sim.registerInitializedCallback(callback, false);
AnalogInput input = new AnalogInput(5)) {
assertTrue(callback.wasTriggered());
assertTrue(callback.getSetValue());
}
}
@Test
2021-08-12 02:04:14 -04:00
void testSetVoltage() {
HAL.initialize(500, 0);
2021-08-12 02:04:14 -04:00
AnalogInputSim sim = new AnalogInputSim(5);
DoubleCallback callback = new DoubleCallback();
try (CallbackStore cb = sim.registerVoltageCallback(callback, false);
AnalogInput input = new AnalogInput(5)) {
// Bootstrap the voltage to be non-zero
sim.setVoltage(1.0);
for (double i = 0; i < 5.0; i += 0.1) {
2021-08-12 02:04:14 -04:00
callback.reset();
2021-08-12 02:04:14 -04:00
sim.setVoltage(0);
assertEquals(input.getVoltage(), 0, 0.001);
2021-08-12 02:04:14 -04:00
callback.reset();
sim.setVoltage(i);
assertEquals(input.getVoltage(), i, 0.001);
}
}
}
2021-08-12 02:04:14 -04:00
@Test
void testSetOverSampleBits() {
HAL.initialize(500, 0);
try (AnalogInput input = new AnalogInput(5)) {
input.setOversampleBits(3504);
assertEquals(3504, input.getOversampleBits());
}
}
}