SCRIPT Move java files

This commit is contained in:
PJ Reiniger
2025-11-07 19:55:40 -05:00
committed by Peter Johnson
parent 7ca1be9bae
commit c350c5f112
1486 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
// 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.
package edu.wpi.first.wpilibj;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.simulation.DIOSim;
import org.junit.jupiter.api.Test;
class DigitalOutputTest {
@Test
void testDefaultFunctions() {
try (DigitalOutput output = new DigitalOutput(0)) {
assertFalse(output.isPulsing());
}
}
@Test
void testPwmFunctionsWithoutInitialization() {
HAL.initialize(500, 0);
try (DigitalOutput output = new DigitalOutput(0)) {
assertDoesNotThrow(() -> output.updateDutyCycle(0.6));
assertDoesNotThrow(output::disablePWM);
}
}
@Test
void testPwmFunctionsWithInitialization() {
HAL.initialize(500, 0);
try (DigitalOutput output = new DigitalOutput(0)) {
DIOSim sim = new DIOSim(output);
assertEquals(0, sim.getPulseLength());
output.enablePWM(0.5);
output.updateDutyCycle(0.6);
}
}
}

View File

@@ -0,0 +1,132 @@
// 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.
package edu.wpi.first.wpilibj;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import edu.wpi.first.wpilibj.util.Color;
import edu.wpi.first.wpilibj.util.Color8Bit;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class AddressableLEDBufferTest {
@ParameterizedTest
@MethodSource("hsvToRgbProvider")
void hsvConvertTest(int h, int s, int v, int r, int g, int b) {
var buffer = new AddressableLEDBuffer(1);
buffer.setHSV(0, h, s, v);
assertAll(
() -> assertEquals((byte) r, buffer.m_buffer[0], "R value didn't match"),
() -> assertEquals((byte) g, buffer.m_buffer[1], "G value didn't match"),
() -> assertEquals((byte) b, buffer.m_buffer[2], "B value didn't match"));
}
static Stream<Arguments> hsvToRgbProvider() {
return Stream.of(
arguments(0, 0, 0, 0, 0, 0), // Black
arguments(0, 0, 255, 255, 255, 255), // White
arguments(0, 255, 255, 255, 0, 0), // Red
arguments(60, 255, 255, 0, 255, 0), // Lime
arguments(120, 255, 255, 0, 0, 255), // Blue
arguments(30, 255, 255, 255, 255, 0), // Yellow
arguments(90, 255, 255, 0, 255, 255), // Cyan
arguments(150, 255, 255, 255, 0, 255), // Magenta
arguments(0, 0, 191, 191, 191, 191), // Silver
arguments(0, 0, 128, 128, 128, 128), // Gray
arguments(0, 255, 128, 128, 0, 0), // Maroon
arguments(30, 255, 128, 128, 128, 0), // Olive
arguments(60, 255, 128, 0, 128, 0), // Green
arguments(150, 255, 128, 128, 0, 128), // Purple
arguments(90, 255, 128, 0, 128, 128), // Teal
arguments(120, 255, 128, 0, 0, 128) // Navy
);
}
@Test
void getColorTest() {
AddressableLEDBuffer buffer = new AddressableLEDBuffer(4);
final Color8Bit denimColor8Bit = new Color8Bit(Color.kDenim);
final Color8Bit firstBlueColor8Bit = new Color8Bit(Color.kFirstBlue);
final Color8Bit firstRedColor8Bit = new Color8Bit(Color.kFirstRed);
buffer.setLED(0, Color.kFirstBlue);
buffer.setLED(1, denimColor8Bit);
buffer.setLED(2, Color.kFirstRed);
buffer.setLED(3, Color.kFirstBlue);
assertEquals(Color.kFirstBlue, buffer.getLED(0));
assertEquals(Color.kDenim, buffer.getLED(1));
assertEquals(Color.kFirstRed, buffer.getLED(2));
assertEquals(Color.kFirstBlue, buffer.getLED(3));
assertEquals(firstBlueColor8Bit, buffer.getLED8Bit(0));
assertEquals(denimColor8Bit, buffer.getLED8Bit(1));
assertEquals(firstRedColor8Bit, buffer.getLED8Bit(2));
assertEquals(firstBlueColor8Bit, buffer.getLED8Bit(3));
}
@Test
void getRed() {
var buffer = new AddressableLEDBuffer(1);
buffer.setRGB(0, 127, 128, 129);
assertEquals(127, buffer.getRed(0));
}
@Test
void getGreen() {
var buffer = new AddressableLEDBuffer(1);
buffer.setRGB(0, 127, 128, 129);
assertEquals(128, buffer.getGreen(0));
}
@Test
void getBlue() {
var buffer = new AddressableLEDBuffer(1);
buffer.setRGB(0, 127, 128, 129);
assertEquals(129, buffer.getBlue(0));
}
@Test
void forEach() {
var buffer = new AddressableLEDBuffer(3);
buffer.setRGB(0, 1, 2, 3);
buffer.setRGB(1, 4, 5, 6);
buffer.setRGB(2, 7, 8, 9);
buffer.forEach(
(index, r, g, b) -> {
switch (index) {
case 0 ->
assertAll(
() -> assertEquals(1, r, "red at index 0"),
() -> assertEquals(2, g, "green at index 0"),
() -> assertEquals(3, b, "blue at index 0"));
case 1 ->
assertAll(
() -> assertEquals(4, r, "red at index 1"),
() -> assertEquals(5, g, "green at index 1"),
() -> assertEquals(6, b, "blue at index 1"));
case 2 ->
assertAll(
() -> assertEquals(7, r, "red at index 2"),
() -> assertEquals(8, g, "green at index 2"),
() -> assertEquals(9, b, "blue at index 2"));
default -> fail("Unexpected index " + index);
}
});
}
@Test
void forEachOnEmptyBuffer() {
var buffer = new AddressableLEDBuffer(0);
buffer.forEach((i, r, g, b) -> fail("Iterator should not be called on an empty buffer"));
}
}

View File

@@ -0,0 +1,69 @@
// 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.
package edu.wpi.first.wpilibj;
import static org.junit.jupiter.api.Assertions.assertEquals;
import edu.wpi.first.wpilibj.util.Color;
import org.junit.jupiter.api.Test;
class AddressableLEDBufferViewTest {
@Test
void singleLED() {
var buffer = new AddressableLEDBuffer(10);
var view = new AddressableLEDBufferView(buffer, 5, 5);
var color = Color.kAqua;
view.setLED(0, color);
assertEquals(color, buffer.getLED(5));
assertEquals(color, view.getLED(0));
}
@Test
void segment() {
var buffer = new AddressableLEDBuffer(10);
var view = new AddressableLEDBufferView(buffer, 2, 8);
view.setLED(0, Color.kAqua);
assertEquals(Color.kAqua, buffer.getLED(2));
view.setLED(6, Color.kAzure);
assertEquals(Color.kAzure, buffer.getLED(8));
}
@Test
void manualReversed() {
var buffer = new AddressableLEDBuffer(10);
var view = new AddressableLEDBufferView(buffer, 8, 2);
// LED 0 in the view should write to LED 8 on the real buffer
view.setLED(0, Color.kAqua);
assertEquals(Color.kAqua, buffer.getLED(8));
// .. and LED 6 in the view should write to LED 2 on the real buffer
view.setLED(6, Color.kAzure);
assertEquals(Color.kAzure, buffer.getLED(2));
}
@Test
void fullManualReversed() {
var buffer = new AddressableLEDBuffer(10);
var view = new AddressableLEDBufferView(buffer, 9, 0);
view.setLED(0, Color.kWhite);
assertEquals(Color.kWhite, buffer.getLED(9));
buffer.setLED(8, Color.kRed);
assertEquals(Color.kRed, view.getLED(1));
}
@Test
void reversed() {
var buffer = new AddressableLEDBuffer(10);
var view = new AddressableLEDBufferView(buffer, 0, 9).reversed();
view.setLED(0, Color.kWhite);
assertEquals(Color.kWhite, buffer.getLED(9));
view.setLED(9, Color.kRed);
assertEquals(Color.kRed, buffer.getLED(0));
}
}

View File

@@ -0,0 +1,997 @@
// 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.
package edu.wpi.first.wpilibj;
import static edu.wpi.first.units.Units.Centimeters;
import static edu.wpi.first.units.Units.Meters;
import static edu.wpi.first.units.Units.MetersPerSecond;
import static edu.wpi.first.units.Units.Microsecond;
import static edu.wpi.first.units.Units.Microseconds;
import static edu.wpi.first.units.Units.Percent;
import static edu.wpi.first.units.Units.Seconds;
import static edu.wpi.first.units.Units.Value;
import static edu.wpi.first.wpilibj.LEDPattern.GradientType.kContinuous;
import static edu.wpi.first.wpilibj.LEDPattern.GradientType.kDiscontinuous;
import static edu.wpi.first.wpilibj.util.Color.kBlack;
import static edu.wpi.first.wpilibj.util.Color.kBlue;
import static edu.wpi.first.wpilibj.util.Color.kGreen;
import static edu.wpi.first.wpilibj.util.Color.kLime;
import static edu.wpi.first.wpilibj.util.Color.kMagenta;
import static edu.wpi.first.wpilibj.util.Color.kMidnightBlue;
import static edu.wpi.first.wpilibj.util.Color.kPurple;
import static edu.wpi.first.wpilibj.util.Color.kRed;
import static edu.wpi.first.wpilibj.util.Color.kWhite;
import static edu.wpi.first.wpilibj.util.Color.kYellow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import edu.wpi.first.wpilibj.util.Color;
import edu.wpi.first.wpilibj.util.Color8Bit;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class LEDPatternTest {
long m_mockTime;
// Applies a pattern of White, Yellow, Purple to LED triplets
LEDPattern m_whiteYellowPurple =
(reader, writer) -> {
for (int led = 0; led < reader.getLength(); led++) {
switch (led % 3) {
case 0:
writer.setLED(led, kWhite);
break;
case 1:
writer.setLED(led, kYellow);
break;
case 2:
writer.setLED(led, kPurple);
break;
default:
fail("Bad test setup");
break;
}
}
};
@BeforeEach
void setUp() {
m_mockTime = 0;
RobotController.setTimeSource(() -> m_mockTime);
}
@AfterEach
void tearDown() {
RobotController.setTimeSource(RobotController::getFPGATime);
}
@Test
void solidColor() {
LEDPattern pattern = LEDPattern.solid(kYellow);
AddressableLEDBuffer buffer = new AddressableLEDBuffer(99);
pattern.applyTo(buffer);
for (int i = 0; i < buffer.getLength(); i++) {
assertEquals(kYellow, buffer.getLED(i));
}
}
@Test
void gradient0SetsToBlack() {
LEDPattern pattern = LEDPattern.gradient(kContinuous);
AddressableLEDBuffer buffer = new AddressableLEDBuffer(99);
for (int i = 0; i < buffer.getLength(); i++) {
buffer.setRGB(i, 127, 128, 129);
}
pattern.applyTo(buffer);
for (int i = 0; i < buffer.getLength(); i++) {
assertEquals(kBlack, buffer.getLED(i));
}
}
@Test
void gradient1SetsToSolid() {
LEDPattern pattern = LEDPattern.gradient(kContinuous, kYellow);
AddressableLEDBuffer buffer = new AddressableLEDBuffer(99);
pattern.applyTo(buffer);
for (int i = 0; i < buffer.getLength(); i++) {
assertEquals(kYellow, buffer.getLED(i));
}
}
@Test
void continuousGradient2Colors() {
LEDPattern pattern = LEDPattern.gradient(kContinuous, kYellow, kPurple);
AddressableLEDBuffer buffer = new AddressableLEDBuffer(99);
pattern.applyTo(buffer);
assertColorEquals(kYellow, buffer.getLED(0));
assertColorEquals(Color.lerpRGB(kYellow, kPurple, 25 / 49.0), buffer.getLED(25));
assertColorEquals(kPurple, buffer.getLED(49));
assertColorEquals(Color.lerpRGB(kYellow, kPurple, 25 / 49.0), buffer.getLED(73));
assertColorEquals(kYellow, buffer.getLED(98));
}
@Test
void discontinuousGradient2Colors() {
LEDPattern pattern = LEDPattern.gradient(kDiscontinuous, kYellow, kPurple);
AddressableLEDBuffer buffer = new AddressableLEDBuffer(99);
pattern.applyTo(buffer);
assertColorEquals(kYellow, buffer.getLED(0));
assertColorEquals(Color.lerpRGB(kYellow, kPurple, 0.5), buffer.getLED(49));
assertColorEquals(kPurple, buffer.getLED(98));
}
@Test
void gradient3Colors() {
LEDPattern pattern = LEDPattern.gradient(kContinuous, kYellow, kPurple, kWhite);
AddressableLEDBuffer buffer = new AddressableLEDBuffer(99);
pattern.applyTo(buffer);
assertColorEquals(kYellow, buffer.getLED(0));
assertColorEquals(Color.lerpRGB(kYellow, kPurple, 25.0 / 33.0), buffer.getLED(25));
assertColorEquals(kPurple, buffer.getLED(33));
assertColorEquals(Color.lerpRGB(kPurple, kWhite, 25.0 / 33.0), buffer.getLED(58));
assertColorEquals(kWhite, buffer.getLED(66));
assertColorEquals(Color.lerpRGB(kWhite, kYellow, 25.0 / 33.0), buffer.getLED(91));
assertColorEquals(Color.lerpRGB(kWhite, kYellow, 32.0 / 33.0), buffer.getLED(98));
}
@Test
void discontinuousGradient3Colors() {
LEDPattern pattern = LEDPattern.gradient(kDiscontinuous, kYellow, kPurple, kWhite);
AddressableLEDBuffer buffer = new AddressableLEDBuffer(101);
pattern.applyTo(buffer);
assertColorEquals(kYellow, buffer.getLED(0));
assertColorEquals(Color.lerpRGB(kYellow, kPurple, 0.5), buffer.getLED(25));
assertColorEquals(kPurple, buffer.getLED(50));
assertColorEquals(Color.lerpRGB(kPurple, kWhite, 0.5), buffer.getLED(75));
assertColorEquals(kWhite, buffer.getLED(100));
}
@Test
void step0SetsToBlack() {
LEDPattern pattern = LEDPattern.steps(Map.of());
AddressableLEDBuffer buffer = new AddressableLEDBuffer(99);
for (int i = 0; i < buffer.getLength(); i++) {
buffer.setRGB(i, 127, 128, 129);
}
pattern.applyTo(buffer);
for (int i = 0; i < 99; i++) {
assertColorEquals(kBlack, buffer.getLED(i));
}
}
@Test
void step1SetsToSolid() {
LEDPattern pattern = LEDPattern.steps(Map.of(0.0, kYellow));
AddressableLEDBuffer buffer = new AddressableLEDBuffer(99);
pattern.applyTo(buffer);
for (int i = 0; i < 99; i++) {
assertColorEquals(kYellow, buffer.getLED(i));
}
}
@Test
void step1HalfSetsToHalfOffHalfColor() {
LEDPattern pattern = LEDPattern.steps(Map.of(0.50, kYellow));
AddressableLEDBuffer buffer = new AddressableLEDBuffer(99);
pattern.applyTo(buffer);
// [0, 48] should be black...
for (int i = 0; i < 49; i++) {
assertColorEquals(kBlack, buffer.getLED(i));
}
// ... and [49, <end>] should be the color that was set
for (int i = 49; i < buffer.getLength(); i++) {
assertColorEquals(kYellow, buffer.getLED(i));
}
}
@Test
void scrollForward() {
var buffer = new AddressableLEDBuffer(256);
LEDPattern base =
(reader, writer) -> {
for (int led = 0; led < reader.getLength(); led++) {
writer.setRGB(led, led % 256, led % 256, led % 256);
}
};
// scroll forwards 1/256th (1 LED) per microsecond - this makes mock time easier
var scroll = base.scrollAtRelativeSpeed(Value.per(Microsecond).of(1 / 256.0));
for (int time = 0; time < 500; time++) {
m_mockTime = time;
scroll.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
// Base: [(0, 0, 0) (1, 1, 1) (2, 2, 2) (3, 3, 3) (4, 4, 4) ... (255, 255, 255)]
// Value for every channel should DECREASE by 1 in each timestep, wrapping around 0 and 255
// t=0, channel value = (0, 1, 2, ..., 254, 255)
// t=1, channel value = (255, 0, 1, ..., 253, 254)
// t=2, channel value = (254, 255, 0, ..., 252, 253)
// t=255, channel value = (1, 2, 3, ..., 255, 0)
// t=256, channel value = (0, 1, 2, ..., 254, 255)
int ch = Math.floorMod(led - time, 256);
assertEquals(new Color8Bit(ch, ch, ch), buffer.getLED8Bit(led));
}
}
}
@Test
void scrollBackward() {
var buffer = new AddressableLEDBuffer(256);
LEDPattern base =
(reader, writer) -> {
for (int led = 0; led < reader.getLength(); led++) {
writer.setRGB(led, led % 256, led % 256, led % 256);
}
};
// scroll backwards 1/256th (1 LED) per microsecond - this makes mock time easier
var scroll = base.scrollAtRelativeSpeed(Value.per(Microsecond).of(-1 / 256.0));
for (int time = 0; time < 500; time++) {
m_mockTime = time;
scroll.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
// Base: [(0, 0, 0) (1, 1, 1) (2, 2, 2) (3, 3, 3) (4, 4, 4) ... (255, 255, 255)]
// Value for every channel should INCREASE by 1 in each timestep, wrapping around 0 and 255
// t=0, channel value = (0, 1, 2, ..., 254, 255)
// t=1, channel value = (1, 2, 3, ..., 255, 0)
// t=2, channel value = (2, 3, 4, ..., 0, 1)
// t=255, channel value = (255, 0, 1, ..., 253, 254)
// t=256, channel value = (0, 1, 2, ..., 254, 255)
int ch = Math.floorMod(led + time, 256);
assertEquals(new Color8Bit(ch, ch, ch), buffer.getLED8Bit(led));
}
}
}
@Test
void scrollAbsoluteSpeedForward() {
var buffer = new AddressableLEDBuffer(256);
LEDPattern base =
(reader, writer) -> {
for (int led = 0; led < reader.getLength(); led++) {
writer.setRGB(led, led % 256, led % 256, led % 256);
}
};
// scroll at 16 m/s, LED spacing = 2cm
// buffer is 256 LEDs, so total length = 512cm = 5.12m
// scrolling at 16 m/s yields a period of 0.32 seconds, or 0.00125 seconds per LED (800 LEDs/s)
var scroll = base.scrollAtAbsoluteSpeed(MetersPerSecond.of(16), Centimeters.of(2));
for (int time = 0; time < 500; time++) {
m_mockTime = time * 1_250; // 1.25ms per LED
scroll.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
// Base: [(0, 0, 0) (1, 1, 1) (2, 2, 2) (3, 3, 3) (4, 4, 4) ... (255, 255, 255)]
// Value for every channel should DECREASE by 1 in each timestep, wrapping around 0 and 255
// t=0, channel value = (0, 1, 2, ..., 254, 255)
// t=1, channel value = (255, 0, 1, ..., 253, 254)
// t=2, channel value = (254, 255, 0, ..., 252, 253)
// t=255, channel value = (1, 2, 3, ..., 255, 0)
// t=256, channel value = (0, 1, 2, ..., 254, 255)
int ch = Math.floorMod(led - time, 256);
assertEquals(new Color8Bit(ch, ch, ch), buffer.getLED8Bit(led));
}
}
}
@Test
void scrollAbsoluteSpeedBackward() {
var buffer = new AddressableLEDBuffer(256);
LEDPattern base =
(reader, writer) -> {
for (int led = 0; led < reader.getLength(); led++) {
writer.setRGB(led, led % 256, led % 256, led % 256);
}
};
// scroll at 16 m/s, LED spacing = 2cm
// buffer is 256 LEDs, so total length = 512cm = 5.12m
// scrolling at 16 m/s yields a period of 0.32 seconds, or 0.00125 seconds per LED (800 LEDs/s)
var scroll = base.scrollAtAbsoluteSpeed(MetersPerSecond.of(-16), Centimeters.of(2));
for (int time = 0; time < 500; time++) {
m_mockTime = time * 1_250; // 1.25ms per LED
scroll.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
// Base: [(0, 0, 0) (1, 1, 1) (2, 2, 2) (3, 3, 3) (4, 4, 4) ... (255, 255, 255)]
// Value for every channel should DECREASE by 1 in each timestep, wrapping around 0 and 255
// t=0, channel value = (0, 1, 2, ..., 254, 255)
// t=1, channel value = (255, 0, 1, ..., 253, 254)
// t=2, channel value = (254, 255, 0, ..., 252, 253)
// t=255, channel value = (1, 2, 3, ..., 255, 0)
// t=256, channel value = (0, 1, 2, ..., 254, 255)
int ch = Math.floorMod(led + time, 256);
assertEquals(new Color8Bit(ch, ch, ch), buffer.getLED8Bit(led));
}
}
}
@Test
void rainbowAtFullSize() {
var buffer = new AddressableLEDBuffer(180);
int saturation = 255;
int value = 255;
var pattern = LEDPattern.rainbow(saturation, value);
pattern.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
assertColorEquals(Color.fromHSV(led, saturation, value), buffer.getLED(led));
}
}
@Test
void rainbowAtHalfSize() {
var buffer = new AddressableLEDBuffer(90);
int saturation = 42;
int value = 87;
var pattern = LEDPattern.rainbow(saturation, value);
pattern.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
assertColorEquals(Color.fromHSV(led * 2, saturation, value), buffer.getLED(led));
}
}
@Test
void rainbowAtOneThirdSize() {
var buffer = new AddressableLEDBuffer(60);
int saturation = 191;
int value = 255;
var pattern = LEDPattern.rainbow(saturation, value);
pattern.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
assertColorEquals(Color.fromHSV(led * 3, saturation, value), buffer.getLED(led));
}
}
@Test
void rainbowAtDoubleSize() {
var buffer = new AddressableLEDBuffer(360);
int saturation = 212;
int value = 93;
var pattern = LEDPattern.rainbow(saturation, value);
pattern.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
assertColorEquals(Color.fromHSV(led / 2, saturation, value), buffer.getLED(led));
}
}
@Test
void rainbowOddSize() {
var buffer = new AddressableLEDBuffer(127);
double scale = 180.0 / buffer.getLength();
int saturation = 73;
int value = 128;
var pattern = LEDPattern.rainbow(saturation, value);
pattern.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
assertColorEquals(Color.fromHSV((int) (led * scale), saturation, value), buffer.getLED(led));
}
}
@Test
void reverseSolid() {
var buffer = new AddressableLEDBuffer(90);
var pattern = LEDPattern.solid(Color.kRosyBrown).reversed();
pattern.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
assertColorEquals(Color.kRosyBrown, buffer.getLED(led));
}
}
@Test
void reverseSteps() {
var buffer = new AddressableLEDBuffer(100);
var pattern = LEDPattern.steps(Map.of(0, kWhite, 0.5, kYellow)).reversed();
pattern.applyTo(buffer);
// colors should be swapped; yellow first, then white
for (int led = 0; led < buffer.getLength(); led++) {
if (led < 50) {
assertColorEquals(kYellow, buffer.getLED(led));
} else {
assertColorEquals(kWhite, buffer.getLED(led));
}
}
}
@Test
void offsetPositive() {
var buffer = new AddressableLEDBuffer(21);
// offset repeats PWY
var offset = m_whiteYellowPurple.offsetBy(1);
offset.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
Color color = buffer.getLED(led);
switch (led % 3) {
case 0:
assertColorEquals(kPurple, color);
break;
case 1:
assertColorEquals(kWhite, color);
break;
case 2:
assertColorEquals(kYellow, color);
break;
default:
fail("Bad test setup");
break;
}
}
}
@Test
void offsetNegative() {
var buffer = new AddressableLEDBuffer(21);
// offset repeats YPW
var offset = m_whiteYellowPurple.offsetBy(-1);
offset.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
Color color = buffer.getLED(led);
switch (led % 3) {
case 0:
assertColorEquals(kYellow, color);
break;
case 1:
assertColorEquals(kPurple, color);
break;
case 2:
assertColorEquals(kWhite, color);
break;
default:
fail("Bad test setup");
break;
}
}
}
@Test
void offsetZero() {
var buffer = new AddressableLEDBuffer(21);
// offset copies the base pattern, WYP
var offset = m_whiteYellowPurple.offsetBy(0);
offset.applyTo(buffer);
for (int led = 0; led < buffer.getLength(); led++) {
Color color = buffer.getLED(led);
switch (led % 3) {
case 0:
assertColorEquals(kWhite, color);
break;
case 1:
assertColorEquals(kYellow, color);
break;
case 2:
assertColorEquals(kPurple, color);
break;
default:
fail("Bad test setup");
break;
}
}
}
@Test
void blinkSymmetric() {
// on for 2 seconds, off for 2 seconds
var pattern = LEDPattern.solid(kWhite).blink(Seconds.of(2));
var buffer = new AddressableLEDBuffer(1);
for (int t = 0; t < 8; t++) {
m_mockTime = t * 1_000_000L; // time travel 1 second
pattern.applyTo(buffer);
Color color = buffer.getLED(0);
switch (t) {
case 0:
case 1:
case 4:
case 5:
assertColorEquals(kWhite, color);
break;
case 2:
case 3:
case 6:
case 7:
assertColorEquals(kBlack, color);
break;
default:
fail("Bad test setup");
break;
}
}
}
@Test
void blinkAsymmetric() {
// on for 3 seconds, off for 1 second
var pattern = LEDPattern.solid(kWhite).blink(Seconds.of(3), Seconds.of(1));
var buffer = new AddressableLEDBuffer(1);
for (int t = 0; t < 8; t++) {
m_mockTime = t * 1_000_000L; // time travel 1 second
pattern.applyTo(buffer);
Color color = buffer.getLED(0);
switch (t) {
case 0:
case 1:
case 2: // first period
case 4:
case 5:
case 6: // second period
assertColorEquals(kWhite, color);
break;
case 3:
case 7:
assertColorEquals(kBlack, color);
break;
default:
fail("Bad test setup");
break;
}
}
}
@Test
void blinkInSync() {
AtomicBoolean condition = new AtomicBoolean(false);
var pattern = LEDPattern.solid(kWhite).synchronizedBlink(condition::get);
var buffer = new AddressableLEDBuffer(1);
pattern.applyTo(buffer);
assertColorEquals(kBlack, buffer.getLED(0));
condition.set(true);
pattern.applyTo(buffer);
assertColorEquals(kWhite, buffer.getLED(0));
condition.set(false);
pattern.applyTo(buffer);
assertColorEquals(kBlack, buffer.getLED(0));
}
@Test
void breathe() {
final Color midGray = new Color(0.5, 0.5, 0.5);
var pattern = LEDPattern.solid(kWhite).breathe(Microseconds.of(4));
var buffer = new AddressableLEDBuffer(1);
{
m_mockTime = 0; // start
pattern.applyTo(buffer);
assertColorEquals(kWhite, buffer.getLED(0));
}
{
m_mockTime = 1; // midway (down)
pattern.applyTo(buffer);
assertColorEquals(midGray, buffer.getLED(0));
}
{
m_mockTime = 2; // bottom
pattern.applyTo(buffer);
assertColorEquals(kBlack, buffer.getLED(0));
}
{
m_mockTime = 3; // midway (up)
pattern.applyTo(buffer);
assertColorEquals(midGray, buffer.getLED(0));
}
{
m_mockTime = 4; // back to start
pattern.applyTo(buffer);
assertColorEquals(kWhite, buffer.getLED(0));
}
}
@Test
void overlaySolidOnSolid() {
var overlay = LEDPattern.solid(kYellow).overlayOn(LEDPattern.solid(kWhite));
var buffer = new AddressableLEDBuffer(1);
overlay.applyTo(buffer);
assertColorEquals(kYellow, buffer.getLED(0));
}
@Test
void overlayNearlyBlack() {
Color overlayColor = new Color(new Color8Bit(1, 0, 0));
var overlay = LEDPattern.solid(overlayColor).overlayOn(LEDPattern.solid(kWhite));
var buffer = new AddressableLEDBuffer(1);
overlay.applyTo(buffer);
assertColorEquals(overlayColor, buffer.getLED(0));
}
@Test
void overlayMixed() {
var overlay =
LEDPattern.steps(Map.of(0, kYellow, 0.5, kBlack)).overlayOn(LEDPattern.solid(kWhite));
var buffer = new AddressableLEDBuffer(2);
overlay.applyTo(buffer);
assertColorEquals(kYellow, buffer.getLED(0));
assertColorEquals(kWhite, buffer.getLED(1));
}
@Test
void blend() {
var pattern1 = LEDPattern.solid(kBlue);
var pattern2 = LEDPattern.solid(kRed);
var blend = pattern1.blend(pattern2);
var buffer = new AddressableLEDBuffer(1);
blend.applyTo(buffer);
// Individual RGB channels are averaged; #0000FF blended with #FF0000 yields #7F007F
assertColorEquals(new Color(127, 0, 127), buffer.getLED(0));
}
@Test
void binaryMask() {
Color color = new Color(123, 123, 123);
var base = LEDPattern.solid(color);
// first 50% mask on, last 50% mask off
var mask = LEDPattern.steps(Map.of(0, kWhite, 0.5, kBlack));
var masked = base.mask(mask);
var buffer = new AddressableLEDBuffer(10);
masked.applyTo(buffer);
for (int i = 0; i < 5; i++) {
assertColorEquals(color, buffer.getLED(i));
}
for (int i = 5; i < 10; i++) {
assertColorEquals(kBlack, buffer.getLED(i));
}
}
@Test
void channelwiseMask() {
Color baseColor = new Color(123, 123, 123);
Color halfGray = new Color(0.5, 0.5, 0.5);
var base = LEDPattern.solid(baseColor);
var mask =
LEDPattern.steps(Map.of(0, kRed, 0.2, kLime, 0.4, kBlue, 0.6, halfGray, 0.8, kWhite));
var masked = base.mask(mask);
var buffer = new AddressableLEDBuffer(5);
masked.applyTo(buffer);
assertColorEquals(new Color(123, 0, 0), buffer.getLED(0)); // red channel only
assertColorEquals(new Color(0, 123, 0), buffer.getLED(1)); // green channel only
assertColorEquals(new Color(0, 0, 123), buffer.getLED(2)); // blue channel only
// mask channels are all 0b00111111, base is 0b00111011,
// so the AND should give us the unmodified base color
assertColorEquals(baseColor, buffer.getLED(3));
assertColorEquals(baseColor, buffer.getLED(4)); // full color allowed
}
@Test
void progressMaskLayer() {
var progress = new AtomicReference<>(0.0);
var maskLayer = LEDPattern.progressMaskLayer(progress::get);
var buffer = new AddressableLEDBuffer(100);
for (double t = 0; t <= 1.0; t += 0.01) {
progress.set(t);
maskLayer.applyTo(buffer);
int lastMaskedLED = (int) (t * 100);
for (int i = 0; i < lastMaskedLED; i++) {
assertColorEquals(
kWhite,
buffer.getLED(i),
"Progress " + lastMaskedLED + "%, LED " + i + " should be WHITE");
}
for (int i = lastMaskedLED; i < 100; i++) {
assertColorEquals(
kBlack,
buffer.getLED(i),
"Progress " + lastMaskedLED + "% , LED " + i + " should be BLACK");
}
}
}
@Test
void zeroBrightness() {
var pattern = LEDPattern.solid(kRed).atBrightness(Percent.of(0));
var buffer = new AddressableLEDBuffer(1);
pattern.applyTo(buffer);
assertColorEquals(kBlack, buffer.getLED(0));
}
@Test
void sameBrightness() {
var pattern = LEDPattern.solid(kMagenta).atBrightness(Percent.of(100));
var buffer = new AddressableLEDBuffer(1);
pattern.applyTo(buffer);
assertColorEquals(kMagenta, buffer.getLED(0));
}
@Test
void higherBrightness() {
var pattern = LEDPattern.solid(kMagenta).atBrightness(Value.of(4 / 3.0));
var buffer = new AddressableLEDBuffer(1);
pattern.applyTo(buffer);
assertColorEquals(kMagenta, buffer.getLED(0));
}
@Test
void negativeBrightness() {
var pattern = LEDPattern.solid(kWhite).atBrightness(Percent.of(-1000));
var buffer = new AddressableLEDBuffer(1);
pattern.applyTo(buffer);
assertColorEquals(kBlack, buffer.getLED(0));
}
@Test
void clippingBrightness() {
var pattern = LEDPattern.solid(kMidnightBlue).atBrightness(Percent.of(10000));
var buffer = new AddressableLEDBuffer(1);
pattern.applyTo(buffer);
assertColorEquals(kWhite, buffer.getLED(0));
}
@Test
void reverseMask() {
var pattern =
LEDPattern.steps(Map.of(0, kRed, 0.25, kBlue, 0.5, kYellow, 0.75, kGreen))
.mask(LEDPattern.steps(Map.of(0, kWhite, 0.5, kBlack)))
.reversed();
var buffer = new AddressableLEDBuffer(8);
pattern.applyTo(buffer);
assertColorEquals(kRed, buffer.getLED(7));
assertColorEquals(kRed, buffer.getLED(6));
assertColorEquals(kBlue, buffer.getLED(5));
assertColorEquals(kBlue, buffer.getLED(4));
assertColorEquals(kBlack, buffer.getLED(3));
assertColorEquals(kBlack, buffer.getLED(2));
assertColorEquals(kBlack, buffer.getLED(1));
assertColorEquals(kBlack, buffer.getLED(0));
}
@Test
void offsetMask() {
var pattern =
LEDPattern.steps(Map.of(0, kRed, 0.25, kBlue, 0.5, kYellow, 0.75, kGreen))
.mask(LEDPattern.steps(Map.of(0, kWhite, 0.5, kBlack)))
.offsetBy(4);
var buffer = new AddressableLEDBuffer(8);
pattern.applyTo(buffer);
assertColorEquals(kBlack, buffer.getLED(0));
assertColorEquals(kBlack, buffer.getLED(1));
assertColorEquals(kBlack, buffer.getLED(2));
assertColorEquals(kBlack, buffer.getLED(3));
assertColorEquals(kRed, buffer.getLED(4));
assertColorEquals(kRed, buffer.getLED(5));
assertColorEquals(kBlue, buffer.getLED(6));
assertColorEquals(kBlue, buffer.getLED(7));
}
@Test
void relativeScrollingMask() {
// [red, red, blue, blue, yellow, yellow, green, green]
// under a mask of first 50% on, last 50% off
// [red, red, blue, blue, black, black, black, black]
// all scrolling at 1 LED per microsecond
var pattern =
LEDPattern.steps(Map.of(0, kRed, 0.25, kBlue, 0.5, kYellow, 0.75, kGreen))
.mask(LEDPattern.steps(Map.of(0, kWhite, 0.5, kBlack)))
.scrollAtRelativeSpeed(Percent.per(Microsecond).of(12.5));
var buffer = new AddressableLEDBuffer(8);
{
m_mockTime = 0; // start
pattern.applyTo(buffer);
assertColorEquals(kRed, buffer.getLED(0));
assertColorEquals(kRed, buffer.getLED(1));
assertColorEquals(kBlue, buffer.getLED(2));
assertColorEquals(kBlue, buffer.getLED(3));
assertColorEquals(kBlack, buffer.getLED(4));
assertColorEquals(kBlack, buffer.getLED(5));
assertColorEquals(kBlack, buffer.getLED(6));
assertColorEquals(kBlack, buffer.getLED(7));
}
{
m_mockTime = 1;
pattern.applyTo(buffer);
assertColorEquals(kBlack, buffer.getLED(0));
assertColorEquals(kRed, buffer.getLED(1));
assertColorEquals(kRed, buffer.getLED(2));
assertColorEquals(kBlue, buffer.getLED(3));
assertColorEquals(kBlue, buffer.getLED(4));
assertColorEquals(kBlack, buffer.getLED(5));
assertColorEquals(kBlack, buffer.getLED(6));
assertColorEquals(kBlack, buffer.getLED(7));
}
{
m_mockTime = 2;
pattern.applyTo(buffer);
assertColorEquals(kBlack, buffer.getLED(0));
assertColorEquals(kBlack, buffer.getLED(1));
assertColorEquals(kRed, buffer.getLED(2));
assertColorEquals(kRed, buffer.getLED(3));
assertColorEquals(kBlue, buffer.getLED(4));
assertColorEquals(kBlue, buffer.getLED(5));
assertColorEquals(kBlack, buffer.getLED(6));
assertColorEquals(kBlack, buffer.getLED(7));
}
{
m_mockTime = 3;
pattern.applyTo(buffer);
assertColorEquals(kBlack, buffer.getLED(0));
assertColorEquals(kBlack, buffer.getLED(1));
assertColorEquals(kBlack, buffer.getLED(2));
assertColorEquals(kRed, buffer.getLED(3));
assertColorEquals(kRed, buffer.getLED(4));
assertColorEquals(kBlue, buffer.getLED(5));
assertColorEquals(kBlue, buffer.getLED(6));
assertColorEquals(kBlack, buffer.getLED(7));
}
}
@Test
void absoluteScrollingMask() {
// [red, red, blue, blue, yellow, yellow, green, green]
// under a mask of first 50% on, last 50% off
// [red, red, blue, blue, black, black, black, black]
// all scrolling at 1 LED per microsecond
var pattern =
LEDPattern.steps(Map.of(0, kRed, 0.25, kBlue, 0.5, kYellow, 0.75, kGreen))
.mask(LEDPattern.steps(Map.of(0, kWhite, 0.5, kBlack)))
.scrollAtAbsoluteSpeed(Meters.per(Microsecond).of(1), Meters.one());
var buffer = new AddressableLEDBuffer(8);
{
m_mockTime = 0; // start
pattern.applyTo(buffer);
assertColorEquals(kRed, buffer.getLED(0));
assertColorEquals(kRed, buffer.getLED(1));
assertColorEquals(kBlue, buffer.getLED(2));
assertColorEquals(kBlue, buffer.getLED(3));
assertColorEquals(kBlack, buffer.getLED(4));
assertColorEquals(kBlack, buffer.getLED(5));
assertColorEquals(kBlack, buffer.getLED(6));
assertColorEquals(kBlack, buffer.getLED(7));
}
{
m_mockTime = 1;
pattern.applyTo(buffer);
assertColorEquals(kBlack, buffer.getLED(0));
assertColorEquals(kRed, buffer.getLED(1));
assertColorEquals(kRed, buffer.getLED(2));
assertColorEquals(kBlue, buffer.getLED(3));
assertColorEquals(kBlue, buffer.getLED(4));
assertColorEquals(kBlack, buffer.getLED(5));
assertColorEquals(kBlack, buffer.getLED(6));
assertColorEquals(kBlack, buffer.getLED(7));
}
{
m_mockTime = 2;
pattern.applyTo(buffer);
assertColorEquals(kBlack, buffer.getLED(0));
assertColorEquals(kBlack, buffer.getLED(1));
assertColorEquals(kRed, buffer.getLED(2));
assertColorEquals(kRed, buffer.getLED(3));
assertColorEquals(kBlue, buffer.getLED(4));
assertColorEquals(kBlue, buffer.getLED(5));
assertColorEquals(kBlack, buffer.getLED(6));
assertColorEquals(kBlack, buffer.getLED(7));
}
{
m_mockTime = 3;
pattern.applyTo(buffer);
assertColorEquals(kBlack, buffer.getLED(0));
assertColorEquals(kBlack, buffer.getLED(1));
assertColorEquals(kBlack, buffer.getLED(2));
assertColorEquals(kRed, buffer.getLED(3));
assertColorEquals(kRed, buffer.getLED(4));
assertColorEquals(kBlue, buffer.getLED(5));
assertColorEquals(kBlue, buffer.getLED(6));
assertColorEquals(kBlack, buffer.getLED(7));
}
}
void assertColorEquals(Color expected, Color actual) {
assertEquals(new Color8Bit(expected), new Color8Bit(actual));
}
void assertColorEquals(Color expected, Color actual, String message) {
assertEquals(new Color8Bit(expected), new Color8Bit(actual), message);
}
}

View File

@@ -0,0 +1,41 @@
// 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.
package edu.wpi.first.wpilibj.motorcontrol;
@SuppressWarnings("removal")
public class MockMotorController implements MotorController {
private double m_speed;
private boolean m_isInverted;
@Override
public void set(double speed) {
m_speed = m_isInverted ? -speed : speed;
}
@Override
public double get() {
return m_speed;
}
@Override
public void setInverted(boolean isInverted) {
m_isInverted = isInverted;
}
@Override
public boolean getInverted() {
return m_isInverted;
}
@Override
public void disable() {
m_speed = 0;
}
@Override
public void stopMotor() {
disable();
}
}

View File

@@ -0,0 +1,34 @@
// 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.
package edu.wpi.first.wpilibj.motorcontrol;
public class MockPWMMotorController {
private double m_speed;
private boolean m_isInverted;
public void set(double speed) {
m_speed = m_isInverted ? -speed : speed;
}
public double get() {
return m_speed;
}
public void setInverted(boolean isInverted) {
m_isInverted = isInverted;
}
public boolean getInverted() {
return m_isInverted;
}
public void disable() {
m_speed = 0;
}
public void stopMotor() {
disable();
}
}

View File

@@ -0,0 +1,102 @@
// 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.
package edu.wpi.first.wpilibj.motorcontrol;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@SuppressWarnings("removal")
class MotorControllerGroupTest {
private static Stream<Arguments> motorControllerArguments() {
return IntStream.of(1, 2, 3)
.mapToObj(
number -> {
MotorController[] motorControllers =
Stream.generate(MockMotorController::new)
.limit(number)
.toArray(MotorController[]::new);
MotorControllerGroup group =
new MotorControllerGroup(
motorControllers[0],
Arrays.copyOfRange(motorControllers, 1, motorControllers.length));
return Arguments.of(group, motorControllers);
});
}
@ParameterizedTest
@MethodSource("motorControllerArguments")
void setTest(final MotorControllerGroup group, final MotorController[] motorControllers) {
group.set(1.0);
assertArrayEquals(
DoubleStream.generate(() -> 1.0).limit(motorControllers.length).toArray(),
Arrays.stream(motorControllers).mapToDouble(MotorController::get).toArray(),
0.00005);
}
@ParameterizedTest
@MethodSource("motorControllerArguments")
void getInvertedTest(final MotorControllerGroup group, final MotorController[] motorControllers) {
group.setInverted(true);
assertTrue(group.getInverted());
}
@ParameterizedTest
@MethodSource("motorControllerArguments")
void setInvertedDoesNotModifyMotorControllersTest(
final MotorControllerGroup group, final MotorController[] motorControllers) {
group.setInverted(true);
assertArrayEquals(
Stream.generate(() -> false).limit(motorControllers.length).toArray(),
Arrays.stream(motorControllers).map(MotorController::getInverted).toArray());
}
@ParameterizedTest
@MethodSource("motorControllerArguments")
void setInvertedDoesInvertTest(
final MotorControllerGroup group, final MotorController[] motorControllers) {
group.setInverted(true);
group.set(1.0);
assertArrayEquals(
DoubleStream.generate(() -> -1.0).limit(motorControllers.length).toArray(),
Arrays.stream(motorControllers).mapToDouble(MotorController::get).toArray(),
0.00005);
}
@ParameterizedTest
@MethodSource("motorControllerArguments")
void disableTest(final MotorControllerGroup group, final MotorController[] motorControllers) {
group.set(1.0);
group.disable();
assertArrayEquals(
DoubleStream.generate(() -> 0.0).limit(motorControllers.length).toArray(),
Arrays.stream(motorControllers).mapToDouble(MotorController::get).toArray(),
0.00005);
}
@ParameterizedTest
@MethodSource("motorControllerArguments")
void stopMotorTest(final MotorControllerGroup group, final MotorController[] motorControllers) {
group.set(1.0);
group.stopMotor();
assertArrayEquals(
DoubleStream.generate(() -> 0.0).limit(motorControllers.length).toArray(),
Arrays.stream(motorControllers).mapToDouble(MotorController::get).toArray(),
0.00005);
}
}

View File

@@ -0,0 +1,94 @@
// 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.
package edu.wpi.first.wpilibj;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import edu.wpi.first.hal.util.AllocationException;
import org.junit.jupiter.api.Test;
class DoubleSolenoidTestCTRE {
@Test
void testValidInitialization() {
try (DoubleSolenoid solenoid = new DoubleSolenoid(0, 3, PneumaticsModuleType.CTREPCM, 2, 3)) {
solenoid.set(DoubleSolenoid.Value.kReverse);
assertEquals(DoubleSolenoid.Value.kReverse, solenoid.get());
solenoid.set(DoubleSolenoid.Value.kForward);
assertEquals(DoubleSolenoid.Value.kForward, solenoid.get());
solenoid.set(DoubleSolenoid.Value.kOff);
assertEquals(DoubleSolenoid.Value.kOff, solenoid.get());
}
}
@Test
void testThrowForwardPortAlreadyInitialized() {
try (
// Single solenoid that is reused for forward port
Solenoid solenoid = new Solenoid(0, 5, PneumaticsModuleType.CTREPCM, 2)) {
assertThrows(
AllocationException.class,
() -> new DoubleSolenoid(0, 5, PneumaticsModuleType.CTREPCM, 2, 3));
}
}
@Test
void testThrowReversePortAlreadyInitialized() {
try (
// Single solenoid that is reused for forward port
Solenoid solenoid = new Solenoid(0, 6, PneumaticsModuleType.CTREPCM, 3)) {
assertThrows(
AllocationException.class,
() -> new DoubleSolenoid(0, 6, PneumaticsModuleType.CTREPCM, 2, 3));
}
}
@Test
void testThrowBothPortsAlreadyInitialized() {
try (
// Single solenoid that is reused for forward port
Solenoid solenoid0 = new Solenoid(0, 6, PneumaticsModuleType.CTREPCM, 2);
Solenoid solenoid1 = new Solenoid(0, 6, PneumaticsModuleType.CTREPCM, 3)) {
assertThrows(
AllocationException.class,
() -> new DoubleSolenoid(0, 6, PneumaticsModuleType.CTREPCM, 2, 3));
}
}
@Test
void testToggle() {
try (DoubleSolenoid solenoid = new DoubleSolenoid(0, 4, PneumaticsModuleType.CTREPCM, 2, 3)) {
// Bootstrap it into reverse
solenoid.set(DoubleSolenoid.Value.kReverse);
solenoid.toggle();
assertEquals(DoubleSolenoid.Value.kForward, solenoid.get());
solenoid.toggle();
assertEquals(DoubleSolenoid.Value.kReverse, solenoid.get());
// Of shouldn't do anything on toggle
solenoid.set(DoubleSolenoid.Value.kOff);
solenoid.toggle();
assertEquals(DoubleSolenoid.Value.kOff, solenoid.get());
}
}
@Test
void testInvalidForwardPort() {
assertThrows(
IllegalArgumentException.class,
() -> new DoubleSolenoid(0, 0, PneumaticsModuleType.CTREPCM, 100, 1));
}
@Test
void testInvalidReversePort() {
assertThrows(
IllegalArgumentException.class,
() -> new DoubleSolenoid(0, 0, PneumaticsModuleType.CTREPCM, 0, 100));
}
}

View File

@@ -0,0 +1,94 @@
// 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.
package edu.wpi.first.wpilibj;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import edu.wpi.first.hal.util.AllocationException;
import org.junit.jupiter.api.Test;
class DoubleSolenoidTestREV {
@Test
void testValidInitialization() {
try (DoubleSolenoid solenoid = new DoubleSolenoid(0, 3, PneumaticsModuleType.REVPH, 2, 3)) {
solenoid.set(DoubleSolenoid.Value.kReverse);
assertEquals(DoubleSolenoid.Value.kReverse, solenoid.get());
solenoid.set(DoubleSolenoid.Value.kForward);
assertEquals(DoubleSolenoid.Value.kForward, solenoid.get());
solenoid.set(DoubleSolenoid.Value.kOff);
assertEquals(DoubleSolenoid.Value.kOff, solenoid.get());
}
}
@Test
void testThrowForwardPortAlreadyInitialized() {
try (
// Single solenoid that is reused for forward port
Solenoid solenoid = new Solenoid(0, 5, PneumaticsModuleType.REVPH, 2)) {
assertThrows(
AllocationException.class,
() -> new DoubleSolenoid(0, 5, PneumaticsModuleType.REVPH, 2, 3));
}
}
@Test
void testThrowReversePortAlreadyInitialized() {
try (
// Single solenoid that is reused for forward port
Solenoid solenoid = new Solenoid(0, 6, PneumaticsModuleType.REVPH, 3)) {
assertThrows(
AllocationException.class,
() -> new DoubleSolenoid(0, 6, PneumaticsModuleType.REVPH, 2, 3));
}
}
@Test
void testThrowBothPortsAlreadyInitialized() {
try (
// Single solenoid that is reused for forward port
Solenoid solenoid0 = new Solenoid(0, 6, PneumaticsModuleType.REVPH, 2);
Solenoid solenoid1 = new Solenoid(0, 6, PneumaticsModuleType.REVPH, 3)) {
assertThrows(
AllocationException.class,
() -> new DoubleSolenoid(0, 6, PneumaticsModuleType.REVPH, 2, 3));
}
}
@Test
void testToggle() {
try (DoubleSolenoid solenoid = new DoubleSolenoid(0, 4, PneumaticsModuleType.REVPH, 2, 3)) {
// Bootstrap it into reverse
solenoid.set(DoubleSolenoid.Value.kReverse);
solenoid.toggle();
assertEquals(DoubleSolenoid.Value.kForward, solenoid.get());
solenoid.toggle();
assertEquals(DoubleSolenoid.Value.kReverse, solenoid.get());
// Of shouldn't do anything on toggle
solenoid.set(DoubleSolenoid.Value.kOff);
solenoid.toggle();
assertEquals(DoubleSolenoid.Value.kOff, solenoid.get());
}
}
@Test
void testInvalidForwardPort() {
assertThrows(
IllegalArgumentException.class,
() -> new DoubleSolenoid(0, 1, PneumaticsModuleType.REVPH, 100, 1));
}
@Test
void testInvalidReversePort() {
assertThrows(
IllegalArgumentException.class,
() -> new DoubleSolenoid(0, 1, PneumaticsModuleType.REVPH, 0, 100));
}
}

View File

@@ -0,0 +1,65 @@
// 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.
package edu.wpi.first.wpilibj;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import edu.wpi.first.hal.util.AllocationException;
import org.junit.jupiter.api.Test;
class SolenoidTestCTRE {
@Test
void testValidInitialization() {
try (Solenoid solenoid = new Solenoid(0, 3, PneumaticsModuleType.CTREPCM, 2)) {
assertEquals(2, solenoid.getChannel());
solenoid.set(true);
assertTrue(solenoid.get());
solenoid.set(false);
assertFalse(solenoid.get());
}
}
@Test
void testDoubleInitialization() {
try (Solenoid solenoid = new Solenoid(0, 3, PneumaticsModuleType.CTREPCM, 2)) {
assertThrows(
AllocationException.class, () -> new Solenoid(0, 3, PneumaticsModuleType.CTREPCM, 2));
}
}
@Test
void testDoubleInitializationFromDoubleSolenoid() {
try (DoubleSolenoid solenoid = new DoubleSolenoid(0, 3, PneumaticsModuleType.CTREPCM, 2, 3)) {
assertThrows(
AllocationException.class, () -> new Solenoid(0, 3, PneumaticsModuleType.CTREPCM, 2));
}
}
@Test
void testInvalidChannel() {
assertThrows(
IllegalArgumentException.class,
() -> new Solenoid(0, 3, PneumaticsModuleType.CTREPCM, 100));
}
@Test
void testToggle() {
try (Solenoid solenoid = new Solenoid(0, 3, PneumaticsModuleType.CTREPCM, 2)) {
solenoid.set(true);
assertTrue(solenoid.get());
solenoid.toggle();
assertFalse(solenoid.get());
solenoid.toggle();
assertTrue(solenoid.get());
}
}
}

View File

@@ -0,0 +1,64 @@
// 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.
package edu.wpi.first.wpilibj;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import edu.wpi.first.hal.util.AllocationException;
import org.junit.jupiter.api.Test;
class SolenoidTestREV {
@Test
void testValidInitialization() {
try (Solenoid solenoid = new Solenoid(0, 3, PneumaticsModuleType.REVPH, 2)) {
assertEquals(2, solenoid.getChannel());
solenoid.set(true);
assertTrue(solenoid.get());
solenoid.set(false);
assertFalse(solenoid.get());
}
}
@Test
void testDoubleInitialization() {
try (Solenoid solenoid = new Solenoid(0, 3, PneumaticsModuleType.REVPH, 2)) {
assertThrows(
AllocationException.class, () -> new Solenoid(0, 3, PneumaticsModuleType.REVPH, 2));
}
}
@Test
void testDoubleInitializationFromDoubleSolenoid() {
try (DoubleSolenoid solenoid = new DoubleSolenoid(0, 3, PneumaticsModuleType.REVPH, 2, 3)) {
assertThrows(
AllocationException.class, () -> new Solenoid(0, 3, PneumaticsModuleType.REVPH, 2));
}
}
@Test
void testInvalidChannel() {
assertThrows(
IllegalArgumentException.class, () -> new Solenoid(0, 3, PneumaticsModuleType.REVPH, 100));
}
@Test
void testToggle() {
try (Solenoid solenoid = new Solenoid(0, 3, PneumaticsModuleType.REVPH, 2)) {
solenoid.set(true);
assertTrue(solenoid.get());
solenoid.toggle();
assertFalse(solenoid.get());
solenoid.toggle();
assertTrue(solenoid.get());
}
}
}

View File

@@ -0,0 +1,29 @@
// 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.
package edu.wpi.first.wpilibj;
import static org.junit.jupiter.api.Assertions.assertEquals;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.PowerDistribution.ModuleType;
import edu.wpi.first.wpilibj.simulation.PDPSim;
import org.junit.jupiter.api.Test;
class PowerDistributionTest {
@Test
void testGetAllCurrents() {
HAL.initialize(500, 0);
PowerDistribution pdp = new PowerDistribution(0, 1, ModuleType.kRev);
PDPSim sim = new PDPSim(pdp);
for (int i = 0; i < pdp.getNumChannels(); i++) {
sim.setCurrent(i, 24 - i);
}
double[] currents = pdp.getAllCurrents();
for (int i = 0; i < pdp.getNumChannels(); i++) {
assertEquals(24 - i, currents[i]);
}
}
}

View File

@@ -0,0 +1,27 @@
// 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.
package edu.wpi.first.wpilibj;
import static org.junit.jupiter.api.Assertions.assertEquals;
import edu.wpi.first.wpilibj.simulation.SharpIRSim;
import org.junit.jupiter.api.Test;
class SharpIRTest {
@Test
void testSharpIR() {
try (SharpIR s = SharpIR.GP2Y0A02YK0F(1)) {
SharpIRSim sim = new SharpIRSim(s);
assertEquals(0.2, s.getRange());
sim.setRange(0.3);
assertEquals(0.3, s.getRange());
sim.setRange(3);
assertEquals(1.5, s.getRange());
}
}
}

View File

@@ -0,0 +1,104 @@
// 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.
package edu.wpi.first.wpilibj;
import static org.junit.jupiter.api.Assertions.assertEquals;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.simulation.AnalogInputSim;
import edu.wpi.first.wpilibj.simulation.RoboRioSim;
import org.junit.jupiter.api.Test;
class AnalogPotentiometerTest {
@Test
void testInitializeWithAnalogInput() {
HAL.initialize(500, 0);
try (AnalogInput ai = new AnalogInput(0);
AnalogPotentiometer pot = new AnalogPotentiometer(ai)) {
AnalogInputSim sim = new AnalogInputSim(ai);
RoboRioSim.resetData();
sim.setVoltage(2.8);
assertEquals(2.8 / 3.3, pot.get());
}
}
@Test
void testInitializeWithAnalogInputAndScale() {
HAL.initialize(500, 0);
try (AnalogInput ai = new AnalogInput(0);
AnalogPotentiometer pot = new AnalogPotentiometer(ai, 270.0)) {
RoboRioSim.resetData();
AnalogInputSim sim = new AnalogInputSim(ai);
sim.setVoltage(3.3);
assertEquals(270.0, pot.get());
sim.setVoltage(2.5);
assertEquals((2.5 / 3.3) * 270.0, pot.get());
sim.setVoltage(0.0);
assertEquals(0.0, pot.get());
}
}
@Test
void testInitializeWithChannel() {
HAL.initialize(500, 0);
try (AnalogPotentiometer pot = new AnalogPotentiometer(1)) {
RoboRioSim.resetData();
AnalogInputSim sim = new AnalogInputSim(1);
sim.setVoltage(3.3);
assertEquals(1.0, pot.get());
}
}
@Test
void testInitializeWithChannelAndScale() {
HAL.initialize(500, 0);
try (AnalogPotentiometer pot = new AnalogPotentiometer(1, 180.0)) {
RoboRioSim.resetData();
AnalogInputSim sim = new AnalogInputSim(1);
sim.setVoltage(3.3);
assertEquals(180.0, pot.get());
sim.setVoltage(0.0);
assertEquals(0.0, pot.get());
}
}
@Test
void testWithModifiedBatteryVoltage() {
try (AnalogPotentiometer pot = new AnalogPotentiometer(1, 180.0, 90.0)) {
RoboRioSim.resetData();
AnalogInputSim sim = new AnalogInputSim(1);
// Test at 3.3v
sim.setVoltage(3.3);
assertEquals(270, pot.get());
sim.setVoltage(0.0);
assertEquals(90, pot.get());
// Simulate a lower battery voltage
RoboRioSim.setUserVoltage3V3(2.5);
sim.setVoltage(2.5);
assertEquals(270, pot.get());
sim.setVoltage(2.0);
assertEquals(234, pot.get());
sim.setVoltage(0.0);
assertEquals(90, pot.get());
}
}
}