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,243 @@
// 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.assertTrue;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.StringArraySubscriber;
import edu.wpi.first.wpilibj.Alert.AlertType;
import edu.wpi.first.wpilibj.simulation.SimHooks;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.parallel.ResourceLock;
class AlertTest {
private NetworkTableInstance m_inst;
private String m_groupName;
@BeforeEach
void setup(TestInfo info) {
m_groupName = "AlertTest_" + info.getDisplayName();
m_inst = NetworkTableInstance.create();
SmartDashboard.setNetworkTableInstance(m_inst);
}
@AfterEach
void checkClean() {
update();
assertEquals(0, getActiveAlerts(AlertType.kError).length);
assertEquals(0, getActiveAlerts(AlertType.kWarning).length);
assertEquals(0, getActiveAlerts(AlertType.kInfo).length);
m_inst.close();
SmartDashboard.setNetworkTableInstance(NetworkTableInstance.getDefault());
}
private String getSubtableName(Alert.AlertType type) {
switch (type) {
case kError:
return "errors";
case kWarning:
return "warnings";
case kInfo:
return "infos";
default:
return "unknown";
}
}
private StringArraySubscriber getSubscriberForType(Alert.AlertType type) {
return m_inst
.getStringArrayTopic("/SmartDashboard/" + m_groupName + "/" + getSubtableName(type))
.subscribe(new String[] {});
}
private String[] getActiveAlerts(AlertType type) {
update();
try (var sub = getSubscriberForType(type)) {
return sub.get();
}
}
private void update() {
SmartDashboard.updateValues();
}
private boolean isAlertActive(String text, Alert.AlertType type) {
return Arrays.asList(getActiveAlerts(type)).contains(text);
}
private void assertState(Alert.AlertType type, List<String> state) {
assertEquals(state, Arrays.asList(getActiveAlerts(type)));
}
private Alert makeAlert(String text, Alert.AlertType type) {
return new Alert(m_groupName, text, type);
}
@Test
void setUnsetSingle() {
try (var one = makeAlert("one", AlertType.kInfo)) {
assertFalse(isAlertActive("one", AlertType.kInfo));
one.set(true);
assertTrue(isAlertActive("one", AlertType.kInfo));
one.set(false);
assertFalse(isAlertActive("one", AlertType.kInfo));
}
}
@Test
void setUnsetMultiple() {
try (var one = makeAlert("one", AlertType.kError);
var two = makeAlert("two", AlertType.kInfo)) {
assertFalse(isAlertActive("one", AlertType.kError));
assertFalse(isAlertActive("two", AlertType.kInfo));
one.set(true);
assertTrue(isAlertActive("one", AlertType.kError));
assertFalse(isAlertActive("two", AlertType.kInfo));
one.set(true);
two.set(true);
assertTrue(isAlertActive("one", AlertType.kError));
assertTrue(isAlertActive("two", AlertType.kInfo));
one.set(false);
assertFalse(isAlertActive("one", AlertType.kError));
assertTrue(isAlertActive("two", AlertType.kInfo));
}
}
@Test
void setIsIdempotent() {
try (var a = makeAlert("A", AlertType.kInfo);
var b = makeAlert("B", AlertType.kInfo);
var c = makeAlert("C", AlertType.kInfo)) {
a.set(true);
b.set(true);
c.set(true);
var startState = List.of(getActiveAlerts(AlertType.kInfo));
b.set(true);
assertState(AlertType.kInfo, startState);
a.set(true);
assertState(AlertType.kInfo, startState);
}
}
@Test
void closeUnsetsAlert() {
try (var alert = makeAlert("alert", AlertType.kWarning)) {
alert.set(true);
assertTrue(isAlertActive("alert", AlertType.kWarning));
}
assertFalse(isAlertActive("alert", AlertType.kWarning));
}
@Test
void setTextWhileUnset() {
try (var alert = makeAlert("BEFORE", AlertType.kInfo)) {
assertEquals("BEFORE", alert.getText());
alert.set(true);
assertTrue(isAlertActive("BEFORE", AlertType.kInfo));
alert.set(false);
assertFalse(isAlertActive("BEFORE", AlertType.kInfo));
alert.setText("AFTER");
assertEquals("AFTER", alert.getText());
alert.set(true);
assertFalse(isAlertActive("BEFORE", AlertType.kInfo));
assertTrue(isAlertActive("AFTER", AlertType.kInfo));
}
}
@Test
void setTextWhileSet() {
try (var alert = makeAlert("BEFORE", AlertType.kInfo)) {
assertEquals("BEFORE", alert.getText());
alert.set(true);
assertTrue(isAlertActive("BEFORE", AlertType.kInfo));
alert.setText("AFTER");
assertEquals("AFTER", alert.getText());
assertFalse(isAlertActive("BEFORE", AlertType.kInfo));
assertTrue(isAlertActive("AFTER", AlertType.kInfo));
}
}
@ResourceLock("timing")
@Test
void setTextDoesNotAffectFirstOrderSort() {
SimHooks.pauseTiming();
try (var a = makeAlert("A", AlertType.kInfo);
var b = makeAlert("B", AlertType.kInfo);
var c = makeAlert("C", AlertType.kInfo)) {
a.set(true);
SimHooks.stepTiming(1);
b.set(true);
SimHooks.stepTiming(1);
c.set(true);
var expectedEndState = new ArrayList<>(List.of(getActiveAlerts(AlertType.kInfo)));
expectedEndState.replaceAll(s -> "B".equals(s) ? "AFTER" : s);
b.setText("AFTER");
assertState(AlertType.kInfo, expectedEndState);
} finally {
SimHooks.resumeTiming();
}
}
@ResourceLock("timing")
@Test
void sortOrder() {
SimHooks.pauseTiming();
try (var a = makeAlert("A", AlertType.kInfo);
var b = makeAlert("B", AlertType.kInfo);
var c = makeAlert("C", AlertType.kInfo)) {
a.set(true);
assertState(AlertType.kInfo, List.of("A"));
SimHooks.stepTiming(1);
b.set(true);
assertState(AlertType.kInfo, List.of("B", "A"));
SimHooks.stepTiming(1);
c.set(true);
assertState(AlertType.kInfo, List.of("C", "B", "A"));
SimHooks.stepTiming(1);
c.set(false);
assertState(AlertType.kInfo, List.of("B", "A"));
SimHooks.stepTiming(1);
c.set(true);
assertState(AlertType.kInfo, List.of("C", "B", "A"));
SimHooks.stepTiming(1);
a.set(false);
assertState(AlertType.kInfo, List.of("C", "B"));
SimHooks.stepTiming(1);
b.set(false);
assertState(AlertType.kInfo, List.of("C"));
SimHooks.stepTiming(1);
b.set(true);
assertState(AlertType.kInfo, List.of("B", "C"));
SimHooks.stepTiming(1);
a.set(true);
assertState(AlertType.kInfo, List.of("A", "B", "C"));
} finally {
SimHooks.resumeTiming();
}
}
}

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.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
class Color8BitTest {
@Test
void testConstructDefault() {
var color = new Color8Bit();
assertEquals(0, color.red);
assertEquals(0, color.green);
assertEquals(0, color.blue);
}
@Test
void testConstructFromInts() {
var color = new Color8Bit(255, 128, 64);
assertEquals(255, color.red);
assertEquals(128, color.green);
assertEquals(64, color.blue);
}
@Test
void testConstructFromColor() {
var color = new Color8Bit(new Color(255, 128, 64));
assertEquals(255, color.red);
assertEquals(128, color.green);
assertEquals(64, color.blue);
}
@Test
void testConstructFromHexString() {
var color = new Color8Bit("#FF8040");
assertEquals(255, color.red);
assertEquals(128, color.green);
assertEquals(64, color.blue);
// No leading #
assertThrows(IllegalArgumentException.class, () -> new Color8Bit("112233"));
// Too long
assertThrows(IllegalArgumentException.class, () -> new Color8Bit("#11223344"));
// Invalid hex characters
assertThrows(IllegalArgumentException.class, () -> new Color8Bit("#$$$$$$"));
}
@Test
void testToHexString() {
var color = new Color8Bit(255, 128, 64);
assertEquals("#FF8040", color.toHexString());
assertEquals("#FF8040", color.toString());
}
}

View File

@@ -0,0 +1,126 @@
// 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.util;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.params.provider.Arguments.arguments;
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 ColorTest {
@Test
void testConstructDefault() {
var color = new Color();
assertEquals(0.0, color.red);
assertEquals(0.0, color.green);
assertEquals(0.0, color.blue);
}
@Test
void testConstructFromDoubles() {
{
var color = new Color(1.0, 0.5, 0.25);
assertEquals(1.0, color.red, 1e-2);
assertEquals(0.5, color.green, 1e-2);
assertEquals(0.25, color.blue, 1e-2);
}
{
var color = new Color(1.0, 0.0, 0.0);
// Check for exact match to ensure round-and-clamp is correct
assertEquals(1.0, color.red);
assertEquals(0.0, color.green);
assertEquals(0.0, color.blue);
}
}
@Test
void testConstructFromInts() {
var color = new Color(255, 128, 64);
assertEquals(1.0, color.red, 1e-2);
assertEquals(0.5, color.green, 1e-2);
assertEquals(0.25, color.blue, 1e-2);
}
@Test
void testConstructFromHexString() {
var color = new Color("#FF8040");
assertEquals(1.0, color.red, 1e-2);
assertEquals(0.5, color.green, 1e-2);
assertEquals(0.25, color.blue, 1e-2);
// No leading #
assertThrows(IllegalArgumentException.class, () -> new Color("112233"));
// Too long
assertThrows(IllegalArgumentException.class, () -> new Color("#11223344"));
// Invalid hex characters
assertThrows(IllegalArgumentException.class, () -> new Color("#$$$$$$"));
}
@Test
void testFromHSV() {
var color = Color.fromHSV(90, 128, 64);
assertEquals(0.125732421875, color.red);
assertEquals(0.251220703125, color.green);
assertEquals(0.251220703125, color.blue);
}
@Test
void testToHexString() {
var color = new Color(255, 128, 64);
assertEquals("#FF8040", color.toHexString());
assertEquals("#FF8040", color.toString());
}
@ParameterizedTest
@MethodSource("hsvToRgbProvider")
void hsvToRgb(int h, int s, int v, int r, int g, int b) {
int rgb = Color.hsvToRgb(h, s, v);
int R = Color.unpackRGB(rgb, Color.RGBChannel.kRed);
int G = Color.unpackRGB(rgb, Color.RGBChannel.kGreen);
int B = Color.unpackRGB(rgb, Color.RGBChannel.kBlue);
assertAll(
() -> assertEquals(r, R, "R value didn't match"),
() -> assertEquals(g, G, "G value didn't match"),
() -> assertEquals(b, B, "B value didn't match"));
}
private 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
);
}
}

View File

@@ -0,0 +1,208 @@
// 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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.Topic;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@Execution(SAME_THREAD)
class PreferencesTest {
private NetworkTableInstance m_inst;
private NetworkTable m_table;
private static final String kFilename = "networktables.json";
@BeforeEach
void setup(@TempDir Path tempDir) {
m_inst = NetworkTableInstance.create();
m_table = m_inst.getTable("Preferences");
Preferences.setNetworkTableInstance(m_inst);
Path filepath = tempDir.resolve(kFilename);
try (InputStream is = getClass().getResource("PreferencesTestDefault.json").openStream()) {
Files.copy(is, filepath);
} catch (IOException ex) {
fail(ex);
}
m_inst.startServer(filepath.toString(), "", 0);
try {
int count = 0;
while (m_inst.getNetworkMode().contains(NetworkTableInstance.NetworkMode.kStarting)) {
Thread.sleep(100);
count++;
if (count > 30) {
throw new InterruptedException();
}
}
} catch (InterruptedException ex) {
fail("interrupted while waiting for server to start");
}
}
@AfterEach
void cleanup() {
m_inst.close();
}
@Disabled("Fails often with 'Preferences was not empty!'")
@Test
void removeAllTest() {
Preferences.removeAll();
List<String> keys = new ArrayList<>();
boolean anyPersistent = false;
for (Topic topic : m_table.getTopics()) {
if (topic.isPersistent()) {
anyPersistent = true;
keys.add(topic.getName());
}
}
assertFalse(
anyPersistent,
"Preferences was not empty! Preferences in table: " + Arrays.toString(keys.toArray()));
}
@Test
void getNetworkTableTest() {
NetworkTable networkTable = Preferences.getNetworkTable();
assertEquals(m_table, networkTable);
}
@ParameterizedTest
@MethodSource("defaultKeyProvider")
void defaultKeysTest(String key) {
assertTrue(Preferences.containsKey(key));
}
@ParameterizedTest
@MethodSource("defaultKeyProvider")
void defaultKeysAllTest(String key) {
assertTrue(Preferences.getKeys().contains(key));
}
@Test
void defaultValueTest() {
assertAll(
() -> assertEquals(172L, Preferences.getLong("checkedValueLong", 0)),
() -> assertEquals(0.2, Preferences.getDouble("checkedValueDouble", 0), 1e-6),
() -> assertEquals("Hello. How are you?", Preferences.getString("checkedValueString", "")),
() -> assertEquals(2, Preferences.getInt("checkedValueInt", 0)),
() -> assertEquals(3.4, Preferences.getFloat("checkedValueFloat", 0), 1e-6),
() -> assertFalse(Preferences.getBoolean("checkedValueBoolean", true)));
}
@Nested
class PutGetTests {
@Test
void intTest() {
final String key = "test";
final int value = 123;
Preferences.setInt(key, value);
assertAll(
() -> assertEquals(value, Preferences.getInt(key, -1)),
() -> assertEquals(value, m_table.getEntry(key).getNumber(-1).intValue()));
}
@Test
void longTest() {
final String key = "test";
final long value = 190L;
Preferences.setLong(key, value);
assertAll(
() -> assertEquals(value, Preferences.getLong(key, -1)),
() -> assertEquals(value, m_table.getEntry(key).getNumber(-1).longValue()));
}
@Test
void floatTest() {
final String key = "test";
final float value = 9.42f;
Preferences.setFloat(key, value);
assertAll(
() -> assertEquals(value, Preferences.getFloat(key, -1), 1e-6),
() -> assertEquals(value, m_table.getEntry(key).getNumber(-1).floatValue(), 1e-6));
}
@Test
void doubleTest() {
final String key = "test";
final double value = 6.28;
Preferences.setDouble(key, value);
assertAll(
() -> assertEquals(value, Preferences.getDouble(key, -1), 1e-6),
() -> assertEquals(value, m_table.getEntry(key).getNumber(-1).doubleValue(), 1e-6));
}
@Test
void stringTest() {
final String key = "test";
final String value = "value";
Preferences.setString(key, value);
assertAll(
() -> assertEquals(value, Preferences.getString(key, "")),
() -> assertEquals(value, m_table.getEntry(key).getString("")));
}
@Test
void booleanTest() {
final String key = "test";
final boolean value = true;
Preferences.setBoolean(key, value);
assertAll(
() -> assertEquals(value, Preferences.getBoolean(key, false)),
() -> assertEquals(value, m_table.getEntry(key).getBoolean(false)));
}
}
static Stream<String> defaultKeyProvider() {
return Stream.of(
"checkedValueLong",
"checkedValueDouble",
"checkedValueString",
"checkedValueInt",
"checkedValueFloat",
"checkedValueBoolean");
}
}