@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 300000) public void testRemoveObserver() { // create some observers, add them to yoVariable int nObservers = 5; createVariableChangeListeners(nObservers); addAllListenersToYoVariable(); // let yoVariable notify observers. Assert that they got the event. yoVariable.notifyVariableChangedListeners(); for (TestVariableChangedListener observer : variableChangedListeners) { assertEquals(observer.getLastVariableChanged(), yoVariable); } // Remove and reset observers. Let yoVariable notify observers again. Assert that the observers // didn't notice anything. for (VariableChangedListener observer : variableChangedListeners) { yoVariable.removeVariableChangedListener(observer); } resetAllObservers(); yoVariable.notifyVariableChangedListeners(); for (TestVariableChangedListener observer : this.variableChangedListeners) { assertNull(observer.getLastVariableChanged()); } }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 300000) public void testNotifyVaribaleChangeListeners() { // create a bunch of Observers @SuppressWarnings("unused") int nObservers = 5; createVariableChangeListeners(5); // add them to the YoVariable yoVariable.removeAllVariableChangedListeners(); addAllListenersToYoVariable(); // create an observer that's not supposed to be notified. TestVariableChangedListener hearNoEvil = new TestVariableChangedListener(); // make sure there's no event stored in the observers before we set for (TestVariableChangedListener listener : variableChangedListeners) { assertNull(listener.getLastVariableChanged()); } assertNull(hearNoEvil.getLastVariableChanged()); // now notify and check if the observers catch on yoVariable.notifyVariableChangedListeners(); for (TestVariableChangedListener observer : variableChangedListeners) { assertEquals(observer.getLastVariableChanged(), yoVariable); } // make sure hearNoEvil is unaware assertNull(hearNoEvil.getLastVariableChanged()); }
@Override public void onNewData(ByteBuffer decompressed) { long timestamp = decompressed.getLong(); LongBuffer data = decompressed.asLongBuffer(); for (int i = 0; i < variables.size(); i++) { YoVariable<?> variable = variables.get(i); long previousValue = variable.getValueAsLongBits(); long newValue = data.get(); variable.setValueFromLongBits(newValue, false); if (previousValue != newValue) { ArrayList<VariableChangedListener> changedListeners = variable.getVariableChangedListeners(); if (changedListeners != null) { for (int listener = 0; listener < changedListeners.size(); listener++) { VariableChangedListener changedListener = changedListeners.get(listener); if (!(changedListener instanceof LogControlVariableChangeListener)) { changedListener.variableChanged(variable); } } } } } for (int i = 0; i < jointStates.size(); i++) { jointStates.get(i).update(data); } listener.receivedTimestampAndData(timestamp, decompressed); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 300000) public void testGetYoVariableRegistry() { YoVariableRegistry registry = yoVariable.getYoVariableRegistry(); assertNotNull(registry); assertEquals(registry, this.registry); assertEquals(registry.getVariable(yoVariable.getName()), yoVariable); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 300000) public void testAddVariableChangeListener() { // remove all observers, then add one new observer, and check if it can be removed without // exceptions. yoVariable.removeAllVariableChangedListeners(); TestVariableChangedListener listener = new TestVariableChangedListener(); yoVariable.addVariableChangedListener(listener); yoVariable.removeVariableChangedListener(listener); }
@DeployableTestMethod @Test(timeout = 5000) public void testWritingAndReadingADataFileWithLotsOfVariables() throws IOException, RepeatDataBufferEntryException { File fileOne = new File(TEST_DIRECTORY + "fileOne.data.gz"); if (fileOne.exists()) fileOne.delete(); long seed = 1776L; int numberOfVariables = 2000; // 12000 for when testing long files for efficiency; Random random = new Random(seed); ArrayList<YoVariable<?>> variables = createALargeNumberOfVariables(random, numberOfVariables); YoVariableList originalVarList = new YoVariableList("originalVarList"); originalVarList.addVariables(variables); int bufferSize = 50; DataBuffer dataBuffer = new DataBuffer(bufferSize); dataBuffer.addVariables(variables); for (int i = 0; i < bufferSize / 2; i++) { dataBuffer.setDataAtIndexToYoVariableValues(); dataBuffer.tick(1); } dataBuffer.setInOutPointFullBuffer(); Robot robot = new Robot("testWritingRobot"); writeALongDataFile(fileOne, dataBuffer, variables, robot); System.out.println("Wrote File. Now reading it."); DataFileReader dataFileReader = new DataFileReader(fileOne); YoVariableList newVarList = new YoVariableList("newVarList"); YoVariableRegistry registry = new YoVariableRegistry("rootRegistry"); DataBuffer newDataBuffer = new DataBuffer(); dataFileReader.readData(newVarList, registry, newDataBuffer); assertEquals(originalVarList.size(), newVarList.size()); for (int i = 0; i < originalVarList.size(); i++) { YoVariable<?> originalVariable = originalVarList.getVariable(i); YoVariable<?> newVariable = newVarList.getVariable(originalVariable.getName()); assertFalse(originalVariable == newVariable); assertEquals(originalVariable.getValueAsDouble(), newVariable.getValueAsDouble(), 1e-7); } fileOne.delete(); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 300000) public void testFullNameEndsWith() { assertTrue(yoVariable.fullNameEndsWithCaseInsensitive("robot.testRegistry.variableOne")); assertTrue(yoVariable.fullNameEndsWithCaseInsensitive("testRegistry.variableOne")); assertTrue(yoVariable.fullNameEndsWithCaseInsensitive("variableOne")); assertTrue(!yoVariable.fullNameEndsWithCaseInsensitive("bot.testRegistry.variableOne")); assertTrue(!yoVariable.fullNameEndsWithCaseInsensitive(".testRegistry.variableOne")); assertTrue(!yoVariable.fullNameEndsWithCaseInsensitive("gistry.variableOne")); assertTrue(!yoVariable.fullNameEndsWithCaseInsensitive("ableOne")); assertTrue(!yoVariable.fullNameEndsWithCaseInsensitive("robot.testRegistr")); assertTrue(yoVariable.fullNameEndsWithCaseInsensitive("robot.testRegistry.VARIABLEONE")); assertFalse(yoVariable.fullNameEndsWithCaseInsensitive("Robot.testRegistry.variableOne")); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 300000) public void testGetName1() { StringBuffer buffer = new StringBuffer(); yoVariable.getName(buffer); assertEquals(buffer.toString(), "variableOne"); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 300000) public void testGetDescription() { DoubleYoVariable descrVariable = new DoubleYoVariable("booleanVar", "Description", registry); assertEquals(descrVariable.getDescription(), "Description"); assertNotNull(yoVariable.getDescription()); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 300000) public void testRemoveObserverNonExistent1() { // make sure removing an observer that wasn't added throws an exception. try { yoVariable.removeVariableChangedListener(new TestVariableChangedListener()); fail(); } catch (NoSuchElementException e) { // pass. } }
@DeployableTestMethod @Test(timeout = 5000) public void testWritingAndReadingALongStateFile() throws IOException { File fileOne = new File(TEST_DIRECTORY + "fileOne.state"); if (fileOne.exists()) fileOne.delete(); long seed = 1776L; int numberOfVariables = 2000; // 12000 for when testing long files for efficiency; Random random = new Random(seed); ArrayList<YoVariable<?>> variables = createALargeNumberOfVariables(random, numberOfVariables); YoVariableList originalVarList = new YoVariableList("originalVarList"); originalVarList.addVariables(variables); writeALongStateFile(fileOne, variables); DataFileReader dataFileReader = new DataFileReader(fileOne); YoVariableList newVarList = new YoVariableList("newVarList"); boolean createMissingVariables = true; boolean printErrorForMissingVariables = false; YoVariableRegistry registry = new YoVariableRegistry("root"); dataFileReader.readState( newVarList, createMissingVariables, printErrorForMissingVariables, registry); assertEquals(originalVarList.size(), newVarList.size()); for (int i = 0; i < originalVarList.size(); i++) { YoVariable<?> originalVariable = originalVarList.getVariable(i); YoVariable<?> newVariable = newVarList.getVariable(originalVariable.getName()); assertFalse(originalVariable == newVariable); assertEquals(originalVariable.getValueAsDouble(), newVariable.getValueAsDouble(), 1e-7); } fileOne.delete(); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 300000, expected = NoSuchElementException.class) public void testRemoveObserverNonExistent2() { // make sure removing an observer that wasn't added throws an exception. // try { createVariableChangeListeners(5); addAllListenersToYoVariable(); yoVariable.removeVariableChangedListener(new TestVariableChangedListener()); // fail(); } // catch (NoSuchElementException e) { // pass. } }
private void addAllListenersToYoVariable() { for (TestVariableChangedListener observer : variableChangedListeners) { yoVariable.addVariableChangedListener(observer); } }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 300000) public void testGetName() { assertEquals(yoVariable.getName(), "variableOne"); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 300000) public void testGetFullNameWithNameSpace() { assertEquals(yoVariable.getFullNameWithNameSpace(), "robot.testRegistry.variableOne"); }
public boolean compare(SimulationConstructionSet scs1, SimulationConstructionSet scs2) { // compare variables YoVariable var0 = getRootJoint(scs1).getQx(); YoVariable var1 = getRootJoint(scs2).getQx(); return (MathTools.epsilonEquals(var0.getValueAsDouble(), var1.getValueAsDouble(), epsilon)); }