diff --git a/shared/java/javacommon.gradle b/shared/java/javacommon.gradle index d0cbf96e2c..5dc248725e 100644 --- a/shared/java/javacommon.gradle +++ b/shared/java/javacommon.gradle @@ -107,9 +107,9 @@ tasks.withType(JavaCompile).configureEach { } dependencies { - testImplementation 'org.junit.jupiter:junit-jupiter-api:5.2.0' - testImplementation 'org.junit.jupiter:junit-jupiter-params:5.2.0' - testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.2.0' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2' + testImplementation 'org.junit.jupiter:junit-jupiter-params:5.4.2' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.2' devCompile sourceSets.main.output diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/LinearFilterNoiseTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/LinearFilterNoiseTest.java deleted file mode 100644 index 1bdf6f9367..0000000000 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/LinearFilterNoiseTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -package edu.wpi.first.wpilibj; - -import java.util.Random; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.EnumSource; - -import static org.junit.jupiter.api.Assertions.assertTrue; - - -public class LinearFilterNoiseTest { - public enum TestType { - kSinglePoleIIR, kMovAvg - } - - // Filter constants - public static final double kFilterStep = 0.005; - public static final double kFilterTime = 2.0; - public static final double kSinglePoleIIRTimeConstant = 0.015915; - public static final int kMovAvgTaps = 6; - - @SuppressWarnings("ParameterName") - public static double getData(double t) { - return 100.0 * Math.sin(2.0 * Math.PI * t); - } - - /** - * Test if the filter reduces the noise produced by a signal generator. - */ - @ParameterizedTest - @EnumSource(TestType.class) - public void testNoiseReduce(TestType type) { - final LinearFilter filter; - - if (type == TestType.kSinglePoleIIR) { - filter = LinearFilter.singlePoleIIR(kSinglePoleIIRTimeConstant, kFilterStep); - } else { - filter = LinearFilter.movingAverage(kMovAvgTaps); - } - - double noiseGenError = 0.0; - double filterError = 0.0; - - final Random gen = new Random(); - final double kStdDev = 10.0; - - for (double t = 0; t < kFilterTime; t += kFilterStep) { - final double theory = getData(t); - final double noise = gen.nextGaussian() * kStdDev; - filterError += Math.abs(filter.calculate(theory + noise) - theory); - noiseGenError += Math.abs(noise - theory); - } - - assertTrue(noiseGenError > filterError, - "Filter should have reduced noise accumulation from " + noiseGenError - + " but failed. The filter error was " + filterError); - } -} diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/LinearFilterOutputTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/LinearFilterOutputTest.java deleted file mode 100644 index 0e0d5c1997..0000000000 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/LinearFilterOutputTest.java +++ /dev/null @@ -1,85 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -package edu.wpi.first.wpilibj; - -import java.util.function.DoubleFunction; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.EnumSource; - -import static org.junit.jupiter.api.Assertions.assertEquals; - - -public class LinearFilterOutputTest { - public enum TestType { - kSinglePoleIIR, - kHighPass, - kMovAvg, - kPulse - } - - // Filter constants - public static final double kFilterStep = 0.005; - public static final double kFilterTime = 2.0; - public static final double kSinglePoleIIRTimeConstant = 0.015915; - public static final double kSinglePoleIIRExpectedOutput = -3.2172003; - public static final double kHighPassTimeConstant = 0.006631; - public static final double kHighPassExpectedOutput = 10.074717; - public static final int kMovAvgTaps = 6; - public static final double kMovAvgExpectedOutput = -10.191644; - - @SuppressWarnings("ParameterName") - public static double getData(double t) { - return 100.0 * Math.sin(2.0 * Math.PI * t) + 20.0 * Math.cos(50.0 * Math.PI * t); - } - - @SuppressWarnings("ParameterName") - public static double getPulseData(double t) { - if (Math.abs(t - 1.0) < 0.001) { - return 1.0; - } else { - return 0.0; - } - } - - /** - * Test if the linear filters produce consistent output for a given data set. - */ - @ParameterizedTest - @EnumSource(TestType.class) - public void testOutput(TestType type) { - final LinearFilter filter; - final DoubleFunction data; - final double expectedOutput; - - if (type == TestType.kSinglePoleIIR) { - filter = LinearFilter.singlePoleIIR(kSinglePoleIIRTimeConstant, kFilterStep); - data = (double t) -> getData(t); - expectedOutput = kSinglePoleIIRExpectedOutput; - } else if (type == TestType.kHighPass) { - filter = LinearFilter.highPass(kHighPassTimeConstant, kFilterStep); - data = (double t) -> getData(t); - expectedOutput = kHighPassExpectedOutput; - } else if (type == TestType.kMovAvg) { - filter = LinearFilter.movingAverage(kMovAvgTaps); - data = (double t) -> getData(t); - expectedOutput = kMovAvgExpectedOutput; - } else { - filter = LinearFilter.movingAverage(kMovAvgTaps); - data = (double t) -> getPulseData(t); - expectedOutput = 0.0; - } - - double filterOutput = 0.0; - for (double t = 0.0; t < kFilterTime; t += kFilterStep) { - filterOutput = filter.calculate(data.apply(t)); - } - - assertEquals(expectedOutput, filterOutput, 0.00005, "Filter output was incorrect."); - } -} diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/LinearFilterTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/LinearFilterTest.java new file mode 100644 index 0000000000..b7a53426ac --- /dev/null +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/LinearFilterTest.java @@ -0,0 +1,116 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.wpilibj; + +import java.util.Random; +import java.util.function.DoubleFunction; +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; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.params.provider.Arguments.arguments; + +class LinearFilterTest { + private static final double kFilterStep = 0.005; + private static final double kFilterTime = 2.0; + private static final double kSinglePoleIIRTimeConstant = 0.015915; + private static final double kHighPassTimeConstant = 0.006631; + private static final int kMovAvgTaps = 6; + + private static final double kSinglePoleIIRExpectedOutput = -3.2172003; + private static final double kHighPassExpectedOutput = 10.074717; + private static final double kMovAvgExpectedOutput = -10.191644; + + @SuppressWarnings("ParameterName") + private static double getData(double t) { + return 100.0 * Math.sin(2.0 * Math.PI * t) + 20.0 * Math.cos(50.0 * Math.PI * t); + } + + @SuppressWarnings("ParameterName") + private static double getPulseData(double t) { + if (Math.abs(t - 1.0) < 0.001) { + return 1.0; + } else { + return 0.0; + } + } + + @Test + void illegalTapNumberTest() { + assertThrows(IllegalArgumentException.class, () -> LinearFilter.movingAverage(0)); + } + + /** + * Test if the filter reduces the noise produced by a signal generator. + */ + @ParameterizedTest + @MethodSource("noiseFilterProvider") + void noiseReduceTest(final LinearFilter filter) { + double noiseGenError = 0.0; + double filterError = 0.0; + + final Random gen = new Random(); + final double kStdDev = 10.0; + + for (double t = 0; t < kFilterTime; t += kFilterStep) { + final double theory = getData(t); + final double noise = gen.nextGaussian() * kStdDev; + filterError += Math.abs(filter.calculate(theory + noise) - theory); + noiseGenError += Math.abs(noise - theory); + } + + assertTrue(noiseGenError > filterError, + "Filter should have reduced noise accumulation from " + noiseGenError + + " but failed. The filter error was " + filterError); + } + + static Stream noiseFilterProvider() { + return Stream.of( + LinearFilter.singlePoleIIR(kSinglePoleIIRTimeConstant, kFilterStep), + LinearFilter.movingAverage(kMovAvgTaps) + ); + } + + /** + * Test if the linear filters produce consistent output for a given data set. + */ + @ParameterizedTest + @MethodSource("outputFilterProvider") + void outputTest(final LinearFilter filter, final DoubleFunction data, + final double expectedOutput) { + double filterOutput = 0.0; + for (double t = 0.0; t < kFilterTime; t += kFilterStep) { + filterOutput = filter.calculate(data.apply(t)); + } + + assertEquals(expectedOutput, filterOutput, 5e-5, "Filter output was incorrect."); + } + + static Stream outputFilterProvider() { + return Stream.of( + arguments(LinearFilter.singlePoleIIR(kSinglePoleIIRTimeConstant, kFilterStep), + (DoubleFunction) LinearFilterTest::getData, + kSinglePoleIIRExpectedOutput), + arguments(LinearFilter.highPass(kHighPassTimeConstant, kFilterStep), + (DoubleFunction) LinearFilterTest::getData, + kHighPassExpectedOutput), + arguments(LinearFilter.movingAverage(kMovAvgTaps), + (DoubleFunction) LinearFilterTest::getData, + kMovAvgExpectedOutput), + arguments(LinearFilter.movingAverage(kMovAvgTaps), + (DoubleFunction) LinearFilterTest::getPulseData, + 0.0) + ); + } +} diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/PreferencesTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/PreferencesTest.java new file mode 100644 index 0000000000..7b3735ffa9 --- /dev/null +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/PreferencesTest.java @@ -0,0 +1,212 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.wpilibj; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Stream; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import edu.wpi.first.networktables.NetworkTable; +import edu.wpi.first.networktables.NetworkTableInstance; + +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; + +class PreferencesTest { + private final Preferences m_prefs = Preferences.getInstance(); + private final NetworkTable m_table = NetworkTableInstance.getDefault().getTable("Preferences"); + + private static final String kFilename = "networktables.ini"; + + @BeforeAll + static void setupAll() { + NetworkTableInstance.getDefault().stopServer(); + } + + @BeforeEach + void setup(@TempDir Path tempDir) { + m_table.getKeys().forEach(m_table::delete); + + Path filepath = tempDir.resolve(kFilename); + try (InputStream is = getClass().getResource("PreferencesTestDefault.ini").openStream()) { + Files.copy(is, filepath); + } catch (IOException ex) { + fail(ex); + } + + NetworkTableInstance.getDefault().startServer(filepath.toString()); + } + + @AfterEach + void cleanup() { + NetworkTableInstance.getDefault().stopServer(); + } + + @AfterAll + static void cleanupAll() { + NetworkTableInstance.getDefault().startServer(); + } + + @Test + void removeAllTest() { + m_prefs.removeAll(); + + Set keys = m_table.getKeys(); + keys.remove(".type"); + + assertTrue(keys.isEmpty(), "Preferences was not empty! Preferences in table: " + + Arrays.toString(keys.toArray())); + } + + @ParameterizedTest + @MethodSource("defaultKeyProvider") + void defaultKeysTest(String key) { + assertTrue(m_prefs.containsKey(key)); + } + + @ParameterizedTest + @MethodSource("defaultKeyProvider") + void defaultKeysAllTest(String key) { + assertTrue(m_prefs.getKeys().contains(key)); + } + + @Test + void defaultValueTest() { + assertAll( + () -> assertEquals(172L, m_prefs.getLong("checkedValueLong", 0)), + () -> assertEquals(0.2, m_prefs.getDouble("checkedValueDouble", 0), 1e-6), + () -> assertEquals("Hello. How are you?", m_prefs.getString("checkedValueString", "")), + () -> assertEquals(2, m_prefs.getInt("checkedValueInt", 0)), + () -> assertEquals(3.14, m_prefs.getFloat("checkedValueFloat", 0), 1e-6), + () -> assertFalse(m_prefs.getBoolean("checkedValueBoolean", true)) + ); + } + + @Test + void backupTest() { + m_prefs.removeAll(); + + assertAll( + () -> assertEquals(0, m_prefs.getLong("checkedValueLong", 0)), + () -> assertEquals(0, m_prefs.getDouble("checkedValueDouble", 0), 1e-6), + () -> assertEquals("", m_prefs.getString("checkedValueString", "")), + () -> assertEquals(0, m_prefs.getInt("checkedValueInt", 0)), + () -> assertEquals(0, m_prefs.getFloat("checkedValueFloat", 0), 1e-6), + () -> assertTrue(m_prefs.getBoolean("checkedValueBoolean", true)) + ); + } + + @Nested + class PutGetTests { + @Test + void intTest() { + final String key = "test"; + final int value = 123; + + m_prefs.putInt(key, value); + + assertAll( + () -> assertEquals(value, m_prefs.getInt(key, -1)), + () -> assertEquals(value, m_table.getEntry(key).getNumber(-1).intValue()) + ); + } + + @Test + void longTest() { + final String key = "test"; + final long value = 190L; + + m_prefs.putLong(key, value); + + assertAll( + () -> assertEquals(value, m_prefs.getLong(key, -1)), + () -> assertEquals(value, m_table.getEntry(key).getNumber(-1).longValue()) + ); + } + + @Test + void floatTest() { + final String key = "test"; + final float value = 9.42f; + + m_prefs.putFloat(key, value); + + assertAll( + () -> assertEquals(value, m_prefs.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; + + m_prefs.putDouble(key, value); + + assertAll( + () -> assertEquals(value, m_prefs.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"; + + m_prefs.putString(key, value); + + assertAll( + () -> assertEquals(value, m_prefs.getString(key, "")), + () -> assertEquals(value, m_table.getEntry(key).getString("")) + ); + } + + @Test + void booleanTest() { + final String key = "test"; + final boolean value = true; + + m_prefs.putBoolean(key, value); + + assertAll( + () -> assertEquals(value, m_prefs.getBoolean(key, false)), + () -> assertEquals(value, m_table.getEntry(key).getBoolean(false)) + ); + } + } + + static Stream defaultKeyProvider() { + return Stream.of( + "checkedValueLong", + "checkedValueDouble", + "checkedValueString", + "checkedValueInt", + "checkedValueFloat", + "checkedValueBoolean" + ); + } +} diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTest.java index 00dd00221c..b702278f2b 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -7,10 +7,113 @@ package edu.wpi.first.wpilibj.smartdashboard; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import edu.wpi.first.networktables.NetworkTable; +import edu.wpi.first.networktables.NetworkTableInstance; import edu.wpi.first.wpilibj.UtilityClassTest; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + class SmartDashboardTest extends UtilityClassTest { + private final NetworkTable m_table = NetworkTableInstance.getDefault().getTable("SmartDashboard"); + SmartDashboardTest() { super(SmartDashboard.class); } + + @BeforeEach + void beforeEach() { + m_table.getKeys().forEach(m_table::delete); + } + + @Test + void getBadValueTest() { + assertEquals("Expected", SmartDashboard.getString("KEY_SHOULD_NOT_BE_FOUND", "Expected")); + } + + @Test + void putStringTest() { + final String key = "putString"; + final String value = "thisIsAValue"; + + SmartDashboard.putString(key, value); + + assertEquals(value, m_table.getEntry(key).getString("")); + } + + @Test + void getStringTest() { + final String key = "getString"; + final String value = "thisIsAValue"; + + m_table.getEntry(key).setString(value); + + assertEquals(value, SmartDashboard.getString(key, "")); + } + + @Test + void putNumberTest() { + final String key = "PutNumber"; + final int value = 2147483647; + + SmartDashboard.putNumber(key, value); + + assertEquals(value, m_table.getEntry(key).getNumber(0).intValue()); + } + + @Test + void getNumberTest() { + final String key = "GetNumber"; + final int value = 2147483647; + + m_table.getEntry(key).setNumber(value); + + assertEquals(value, SmartDashboard.getNumber(key, 0), 0.01); + } + + @Test + void putBooleanTest() { + final String key = "PutBoolean"; + final boolean value = true; + + SmartDashboard.putBoolean(key, value); + + assertEquals(value, m_table.getEntry(key).getBoolean(!value)); + } + + @Test + void getBooleanTest() { + final String key = "GetBoolean"; + final boolean value = true; + + m_table.getEntry(key).setBoolean(value); + + assertEquals(value, SmartDashboard.getBoolean(key, !value)); + } + + @Test + void testReplaceString() { + final String key = "testReplaceString"; + final String valueNew = "newValue"; + + m_table.getEntry(key).setString("oldValue"); + SmartDashboard.putString(key, valueNew); + + assertEquals(valueNew, m_table.getEntry(key).getString("")); + } + + @Test + void putStringNullKeyTest() { + assertThrows(NullPointerException.class, + () -> SmartDashboard.putString(null, "This should not work")); + } + + @Test + void putStringNullValueTest() { + assertThrows(NullPointerException.class, + () -> SmartDashboard.putString("KEY_SHOULD_NOT_BE_STORED", null)); + } } diff --git a/wpilibj/src/test/resources/edu/wpi/first/wpilibj/PreferencesTestDefault.ini b/wpilibj/src/test/resources/edu/wpi/first/wpilibj/PreferencesTestDefault.ini new file mode 100644 index 0000000000..a685de0cad --- /dev/null +++ b/wpilibj/src/test/resources/edu/wpi/first/wpilibj/PreferencesTestDefault.ini @@ -0,0 +1,7 @@ +[NetworkTables Storage 3.0] +double "/Preferences/checkedValueInt"=2 +double "/Preferences/checkedValueDouble"=.2 +double "/Preferences/checkedValueFloat"=3.14 +double "/Preferences/checkedValueLong"=172 +string "/Preferences/checkedValueString"="Hello. How are you?" +boolean "/Preferences/checkedValueBoolean"=false diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PreferencesTest.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PreferencesTest.java deleted file mode 100644 index 41633ad842..0000000000 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PreferencesTest.java +++ /dev/null @@ -1,144 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -package edu.wpi.first.wpilibj; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.util.Arrays; -import java.util.logging.Logger; - -import org.junit.Before; -import org.junit.Test; - -import edu.wpi.first.wpilibj.networktables.NetworkTable; -import edu.wpi.first.wpilibj.test.AbstractComsSetup; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * Tests the {@link Preferences}. - */ -public class PreferencesTest extends AbstractComsSetup { - private static final Logger logger = Logger.getLogger(PreferencesTest.class.getName()); - - private NetworkTable m_prefTable; - private Preferences m_pref; - private long m_check; - - @Override - protected Logger getClassLogger() { - return logger; - } - - - @Before - public void setUp() throws Exception { - NetworkTable.shutdown(); - try { - File file = new File("networktables.ini"); - file.mkdirs(); - if (file.exists()) { - file.delete(); - } - file.createNewFile(); - OutputStream output = new FileOutputStream(file); - output - .write(("[NetworkTables Storage 3.0]\ndouble \"/Preferences/checkedValueInt\"=2\ndouble " - + "\"/Preferences/checkedValueDouble\"=.2\ndouble " - + "\"/Preferences/checkedValueFloat\"=3.14\ndouble " - + "\"/Preferences/checkedValueLong\"=172\nstring " - + "\"/Preferences/checkedValueString\"=\"hello \\nHow are you ?\"\nboolean " - + "\"/Preferences/checkedValueBoolean\"=false\n") - .getBytes()); - - } catch (IOException ex) { - ex.printStackTrace(); - } - NetworkTable.initialize(); - - m_pref = Preferences.getInstance(); - m_prefTable = NetworkTable.getTable("Preferences"); - m_check = System.currentTimeMillis(); - } - - - protected void remove() { - m_pref.remove("checkedValueLong"); - m_pref.remove("checkedValueDouble"); - m_pref.remove("checkedValueString"); - m_pref.remove("checkedValueInt"); - m_pref.remove("checkedValueFloat"); - m_pref.remove("checkedValueBoolean"); - } - - protected void addCheckedValue() { - m_pref.putLong("checkedValueLong", m_check); - m_pref.putDouble("checkedValueDouble", 1); - m_pref.putString("checkedValueString", "checked"); - m_pref.putInt("checkedValueInt", 1); - m_pref.putFloat("checkedValueFloat", 1); - m_pref.putBoolean("checkedValueBoolean", true); - } - - /** - * Just checking to make sure our helper method works. - */ - @Test - public void testRemove() { - remove(); - assertEquals("Preferences was not empty! Preferences in table: " - + Arrays.toString(m_pref.getKeys().toArray()), - 1, m_pref.getKeys().size()); - } - - @Test - public void testRemoveAll() { - m_pref.removeAll(); - assertEquals("Preferences was not empty! Preferences in table: " - + Arrays.toString(m_pref.getKeys().toArray()), - 1, m_pref.getKeys().size()); - } - - @Test - public void testAddRemoveSave() { - assertEquals(m_pref.getLong("checkedValueLong", 0), 172L); - assertEquals(m_pref.getDouble("checkedValueDouble", 0), .2, 0); - assertEquals(m_pref.getString("checkedValueString", ""), "hello \nHow are you ?"); - assertEquals(m_pref.getInt("checkedValueInt", 0), 2); - assertEquals(m_pref.getFloat("checkedValueFloat", 0), 3.14, .001); - assertFalse(m_pref.getBoolean("checkedValueBoolean", true)); - remove(); - assertEquals(m_pref.getLong("checkedValueLong", 0), 0); - assertEquals(m_pref.getDouble("checkedValueDouble", 0), 0, 0); - assertEquals(m_pref.getString("checkedValueString", ""), ""); - assertEquals(m_pref.getInt("checkedValueInt", 0), 0); - assertEquals(m_pref.getFloat("checkedValueFloat", 0), 0, 0); - assertFalse(m_pref.getBoolean("checkedValueBoolean", false)); - addCheckedValue(); - assertEquals(m_check, m_pref.getLong("checkedValueLong", 0)); - assertEquals(m_pref.getDouble("checkedValueDouble", 0), 1, 0); - assertEquals(m_pref.getString("checkedValueString", ""), "checked"); - assertEquals(m_pref.getInt("checkedValueInt", 0), 1); - assertEquals(m_pref.getFloat("checkedValueFloat", 0), 1, 0); - assertTrue(m_pref.getBoolean("checkedValueBoolean", false)); - } - - @Test - public void testPreferencesToNetworkTables() { - String networkedNumber = "networkCheckedValue"; - int networkNumberValue = 100; - m_pref.putInt(networkedNumber, networkNumberValue); - assertEquals(networkNumberValue, (int) (m_prefTable.getNumber(networkedNumber, 9999999))); - m_pref.remove(networkedNumber); - } - -} diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/WpiLibJTestSuite.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/WpiLibJTestSuite.java index 44f283753e..b323d78ae1 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/WpiLibJTestSuite.java +++ b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/WpiLibJTestSuite.java @@ -22,7 +22,7 @@ import edu.wpi.first.wpilibj.test.AbstractTestSuite; BuiltInAccelerometerTest.class, ConstantsPortsTest.class, CounterTest.class, DigitalGlitchFilterTest.class, DIOCrossConnectTest.class, DriveTest.class, DriverStationTest.class, EncoderTest.class, GyroTest.class, MotorEncoderTest.class, - MotorInvertingTest.class, PCMTest.class, PDPTest.class, PIDTest.class, PreferencesTest.class, + MotorInvertingTest.class, PCMTest.class, PDPTest.class, PIDTest.class, RelayCrossConnectTest.class, SampleTest.class, TimerTest.class}) public class WpiLibJTestSuite extends AbstractTestSuite { } diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTest.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTest.java deleted file mode 100644 index b7e98b1c7f..0000000000 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTest.java +++ /dev/null @@ -1,89 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -package edu.wpi.first.wpilibj.smartdashboard; - -import java.util.logging.Logger; - -import org.junit.Ignore; -import org.junit.Test; - -import edu.wpi.first.wpilibj.networktables.NetworkTable; -import edu.wpi.first.wpilibj.test.AbstractComsSetup; - -import static org.junit.Assert.assertEquals; - -/** - * Test that covers {@link SmartDashboard}. - */ -public class SmartDashboardTest extends AbstractComsSetup { - private static final Logger logger = Logger.getLogger(SmartDashboardTest.class.getName()); - private static final NetworkTable table = NetworkTable.getTable("SmartDashboard"); - - @Override - protected Logger getClassLogger() { - return logger; - } - - @Test - public void testGetBadValue() { - assertEquals("", SmartDashboard.getString("_404_STRING_KEY_SHOULD_NOT_BE_FOUND_", "")); - } - - @Test - public void testPutString() { - String key = "testPutString"; - String value = "thisIsAValue"; - SmartDashboard.putString(key, value); - assertEquals(value, SmartDashboard.getString(key, "")); - assertEquals(value, table.getString(key, "")); - } - - @Test - public void testPutNumber() { - String key = "testPutNumber"; - int value = 2147483647; - SmartDashboard.putNumber(key, value); - assertEquals(value, SmartDashboard.getNumber(key, 0), 0.01); - assertEquals(value, table.getNumber(key, 0), 0.01); - } - - @Test - public void testPutBoolean() { - String key = "testPutBoolean"; - boolean value = true; - SmartDashboard.putBoolean(key, value); - assertEquals(value, SmartDashboard.getBoolean(key, !value)); - assertEquals(value, table.getBoolean(key, false)); - } - - @Test - public void testReplaceString() { - String key = "testReplaceString"; - String valueOld = "oldValue"; - SmartDashboard.putString(key, valueOld); - assertEquals(valueOld, SmartDashboard.getString(key, "")); - assertEquals(valueOld, table.getString(key, "")); - - String valueNew = "newValue"; - SmartDashboard.putString(key, valueNew); - assertEquals(valueNew, SmartDashboard.getString(key, "")); - assertEquals(valueNew, table.getString(key, "")); - } - - @Ignore - @Test(expected = IllegalArgumentException.class) - public void testPutStringNullKey() { - SmartDashboard.putString(null, "This should not work"); - } - - @Ignore - @Test(expected = IllegalArgumentException.class) - public void testPutStringNullValue() { - SmartDashboard.putString("KEY_SHOULD_NOT_BE_STORED", null); - } -} diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTestSuite.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTestSuite.java deleted file mode 100644 index d3e475e374..0000000000 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTestSuite.java +++ /dev/null @@ -1,22 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -package edu.wpi.first.wpilibj.smartdashboard; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - -import edu.wpi.first.wpilibj.test.AbstractTestSuite; - -/** - * All tests pertaining to {@link SmartDashboard}. - */ -@RunWith(Suite.class) -@SuiteClasses({SmartDashboardTest.class}) -public class SmartDashboardTestSuite extends AbstractTestSuite { -} diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/TestSuite.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/TestSuite.java index 5422d8f1bc..31b3b0d34e 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/TestSuite.java +++ b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/TestSuite.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -28,7 +28,6 @@ import junit.framework.JUnit4TestAdapter; import junit.runner.Version; import edu.wpi.first.wpilibj.WpiLibJTestSuite; -import edu.wpi.first.wpilibj.smartdashboard.SmartDashboardTestSuite; /** * The WPILibJ Integeration Test Suite collects all of the tests to be run by junit. In order for a @@ -36,8 +35,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SmartDashboardTestSuite; * order they are listed in the suite classes annotation. */ @RunWith(Suite.class) -// These are listed on separate lines to prevent merge conflicts -@SuiteClasses({WpiLibJTestSuite.class, SmartDashboardTestSuite.class}) +@SuiteClasses(WpiLibJTestSuite.class) public class TestSuite extends AbstractTestSuite { static { // Sets up the logging output