Converts non hardware dependent tests to unit tests (#10)

This commit is contained in:
Jonathan Leitschuh
2016-05-20 15:15:14 -04:00
committed by Peter Johnson
parent 975568c774
commit 00b2902102
15 changed files with 157 additions and 83 deletions

View File

@@ -1,101 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. 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 org.junit.Test;
import java.util.logging.Logger;
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
import static org.junit.Assert.assertEquals;
public class CircularBufferTest extends AbstractComsSetup {
private static final Logger logger = Logger.getLogger(CircularBufferTest.class.getName());
private double[] m_values = {751.848, 766.366, 342.657, 234.252, 716.126,
132.344, 445.697, 22.727, 421.125, 799.913};
private double[] m_pushFrontOut = {799.913, 421.125, 22.727, 445.697, 132.344,
716.126, 234.252, 342.657};
private double[] m_pushBackOut = {342.657, 234.252, 716.126, 132.344, 445.697,
22.727, 421.125, 799.913};
@Test
public void pushFrontTest() {
CircularBuffer queue = new CircularBuffer(8);
for (double value : m_values) {
queue.pushFront(value);
}
for (int i = 0; i < m_pushFrontOut.length; i++) {
assertEquals(m_pushFrontOut[i], queue.get(i), 0.00005);
}
}
@Test
public void pushBackTest() {
CircularBuffer queue = new CircularBuffer(8);
for (double value : m_values) {
queue.pushBack(value);
}
for (int i = 0; i < m_pushBackOut.length; i++) {
assertEquals(m_pushBackOut[i], queue.get(i), 0.00005);
}
}
@Test
public void pushPopTest() {
CircularBuffer queue = new CircularBuffer(3);
// Insert three elements into the buffer
queue.pushBack(1.0);
queue.pushBack(2.0);
queue.pushBack(3.0);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
assertEquals(3.0, queue.get(2), 0.00005);
/*
* The buffer is full now, so pushing subsequent elements will overwrite the
* front-most elements.
*/
queue.pushBack(4.0); // Overwrite 1 with 4
// The buffer now contains 2, 3, and 4
assertEquals(2.0, queue.get(0), 0.00005);
assertEquals(3.0, queue.get(1), 0.00005);
assertEquals(4.0, queue.get(2), 0.00005);
queue.pushBack(5.0); // Overwrite 2 with 5
// The buffer now contains 3, 4, and 5
assertEquals(3.0, queue.get(0), 0.00005);
assertEquals(4.0, queue.get(1), 0.00005);
assertEquals(5.0, queue.get(2), 0.00005);
assertEquals(5.0, queue.popBack(), 0.00005); // 5 is removed
// The buffer now contains 3 and 4
assertEquals(3.0, queue.get(0), 0.00005);
assertEquals(4.0, queue.get(1), 0.00005);
assertEquals(3.0, queue.popFront(), 0.00005); // 3 is removed
// Leaving only one element with value == 4
assertEquals(4.0, queue.get(0), 0.00005);
}
@Override
protected Logger getClassLogger() {
return logger;
}
}

View File

@@ -1,105 +0,0 @@
package edu.wpi.first.wpilibj;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.logging.Logger;
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class PIDToleranceTest extends AbstractComsSetup {
private static final Logger logger = Logger.getLogger(PIDToleranceTest.class.getName());
private PIDController m_pid;
private final double m_setPoint = 50.0;
private final double m_tolerance = 10.0;
private final double m_range = 200;
private class FakeInput implements PIDSource {
public double m_val;
public FakeInput() {
m_val = 0;
}
@Override
public PIDSourceType getPIDSourceType() {
return PIDSourceType.kDisplacement;
}
@Override
public double pidGet() {
return m_val;
}
@Override
public void setPIDSourceType(PIDSourceType arg0) {
}
}
private FakeInput m_inp;
private PIDOutput m_out = new PIDOutput() {
@Override
public void pidWrite(double out) {
}
};
@Override
protected Logger getClassLogger() {
return logger;
}
@Before
public void setUp() throws Exception {
m_inp = new FakeInput();
m_pid = new PIDController(0.05, 0.0, 0.0, m_inp, m_out);
m_pid.setInputRange(-m_range / 2, m_range / 2);
}
@After
public void tearDown() throws Exception {
m_pid.free();
m_pid = null;
}
@Test
public void testAbsoluteTolerance() {
m_pid.setAbsoluteTolerance(m_tolerance);
m_pid.setSetpoint(m_setPoint);
m_pid.enable();
Timer.delay(1);
assertFalse("Error was in tolerance when it should not have been. Error was "
+ m_pid.getAvgError(), m_pid.onTarget());
m_inp.m_val = m_setPoint + m_tolerance / 2;
Timer.delay(1.0);
assertTrue("Error was not in tolerance when it should have been. Error was "
+ m_pid.getAvgError(), m_pid.onTarget());
m_inp.m_val = m_setPoint + 10 * m_tolerance;
Timer.delay(1.0);
assertFalse("Error was in tolerance when it should not have been. Error was "
+ m_pid.getAvgError(), m_pid.onTarget());
}
@Test
public void testPercentTolerance() {
m_pid.setPercentTolerance(m_tolerance);
m_pid.setSetpoint(m_setPoint);
m_pid.enable();
assertFalse("Error was in tolerance when it should not have been. Error was "
+ m_pid.getAvgError(), m_pid.onTarget());
//half of percent tolerance away from setPoint
m_inp.m_val = m_setPoint + (m_tolerance) / 200 * m_range;
Timer.delay(1.0);
assertTrue("Error was not in tolerance when it should have been. Error was "
+ m_pid.getAvgError(), m_pid.onTarget());
//double percent tolerance away from setPoint
m_inp.m_val = m_setPoint + (m_tolerance) / 50 * m_range;
Timer.delay(1.0);
assertFalse("Error was in tolerance when it should not have been. Error was "
+ m_pid.getAvgError(), m_pid.onTarget());
}
}

View File

@@ -1,62 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. 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.command;
import org.junit.Before;
import edu.wpi.first.wpilibj.mocks.MockCommand;
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* The basic test for all {@link Command} tests.
*
* @author jonathanleitschuh
*/
public abstract class AbstractCommandTest extends AbstractComsSetup {
@Before
public void commandSetup() {
Scheduler.getInstance().removeAll();
Scheduler.getInstance().enable();
}
public class ASubsystem extends Subsystem {
Command m_command;
protected void initDefaultCommand() {
if (m_command != null) {
setDefaultCommand(m_command);
}
}
public void init(Command command) {
m_command = command;
}
}
protected void assertCommandState(MockCommand command, int initialize, int execute,
int isFinished, int end, int interrupted) {
assertEquals(initialize, command.getInitializeCount());
assertEquals(execute, command.getExecuteCount());
assertEquals(isFinished, command.getIsFinishedCount());
assertEquals(end, command.getEndCount());
assertEquals(interrupted, command.getInterruptedCount());
}
protected void sleep(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException ex) {
fail("Sleep Interrupted!?!?!?!?");
}
}
}

View File

@@ -1,128 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. 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.command;
import org.junit.Before;
import org.junit.Test;
import java.util.logging.Logger;
import edu.wpi.first.wpilibj.buttons.InternalButton;
import edu.wpi.first.wpilibj.mocks.MockCommand;
/**
* Test that covers the {@link edu.wpi.first.wpilibj.buttons.Button} with the {@link Command}
* library.
*
* @author Mitchell
* @author jonathanleitschuh
*/
public class ButtonTest extends AbstractCommandTest {
private static final Logger logger = Logger.getLogger(ButtonTest.class.getName());
private InternalButton m_button1;
private InternalButton m_button2;
protected Logger getClassLogger() {
return logger;
}
@Before
public void setUp() throws Exception {
m_button1 = new InternalButton();
m_button2 = new InternalButton();
}
/**
* Simple Button Test.
*/
@Test
public void test() {
final MockCommand command1 = new MockCommand();
final MockCommand command2 = new MockCommand();
final MockCommand command3 = new MockCommand();
final MockCommand command4 = new MockCommand();
m_button1.whenPressed(command1);
m_button1.whenReleased(command2);
m_button1.whileHeld(command3);
m_button2.whileHeld(command4);
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
assertCommandState(command4, 0, 0, 0, 0, 0);
m_button1.setPressed(true);
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
assertCommandState(command4, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
assertCommandState(command4, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 1, 1, 1, 0, 0);
assertCommandState(command4, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 2, 2, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 1, 2, 2, 0, 0);
assertCommandState(command4, 0, 0, 0, 0, 0);
m_button2.setPressed(true);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 3, 3, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 1, 3, 3, 0, 0);
assertCommandState(command4, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 4, 4, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 1, 4, 4, 0, 0);
assertCommandState(command4, 1, 1, 1, 0, 0);
m_button1.setPressed(false);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 5, 5, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 1, 4, 4, 0, 1);
assertCommandState(command4, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 6, 6, 0, 0);
assertCommandState(command2, 1, 1, 1, 0, 0);
assertCommandState(command3, 1, 4, 4, 0, 1);
assertCommandState(command4, 1, 3, 3, 0, 0);
m_button2.setPressed(false);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 7, 7, 0, 0);
assertCommandState(command2, 1, 2, 2, 0, 0);
assertCommandState(command3, 1, 4, 4, 0, 1);
assertCommandState(command4, 1, 3, 3, 0, 1);
command1.cancel();
Scheduler.getInstance().run();
assertCommandState(command1, 1, 7, 7, 0, 1);
assertCommandState(command2, 1, 3, 3, 0, 0);
assertCommandState(command3, 1, 4, 4, 0, 1);
assertCommandState(command4, 1, 3, 3, 0, 1);
command2.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 7, 7, 0, 1);
assertCommandState(command2, 1, 4, 4, 1, 0);
assertCommandState(command3, 1, 4, 4, 0, 1);
assertCommandState(command4, 1, 3, 3, 0, 1);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 7, 7, 0, 1);
assertCommandState(command2, 1, 4, 4, 1, 0);
assertCommandState(command3, 1, 4, 4, 0, 1);
assertCommandState(command4, 1, 3, 3, 0, 1);
}
}

View File

@@ -1,69 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. 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.command;
import org.junit.Test;
import java.util.logging.Logger;
import edu.wpi.first.wpilibj.mocks.MockCommand;
/**
* Ported from the old CrioTest Classes.
*
* @author Mitchell
* @author Jonathan Leitschuh
*/
public class CommandParallelGroupTest extends AbstractCommandTest {
private static final Logger logger = Logger.getLogger(CommandParallelGroupTest.class.getName());
protected Logger getClassLogger() {
return logger;
}
/**
* Simple Parallel Command Group With 2 commands one command terminates first.
*/
@Test
public void testParallelCommandGroupWithTwoCommands() {
final MockCommand command1 = new MockCommand();
final MockCommand command2 = new MockCommand();
final CommandGroup commandGroup = new CommandGroup();
commandGroup.addParallel(command1);
commandGroup.addParallel(command2);
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
commandGroup.start();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 0);
assertCommandState(command2, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 2, 2, 0, 0);
assertCommandState(command2, 1, 2, 2, 0, 0);
command1.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 3, 3, 1, 0);
assertCommandState(command2, 1, 3, 3, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 3, 3, 1, 0);
assertCommandState(command2, 1, 4, 4, 0, 0);
command2.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 3, 3, 1, 0);
assertCommandState(command2, 1, 5, 5, 1, 0);
}
}

View File

@@ -1,74 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. 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.command;
import org.junit.Test;
import java.util.logging.Logger;
import edu.wpi.first.wpilibj.mocks.MockCommand;
/**
* Ported from the old CrioTest Classes.
*
* @author Mitchell
* @author Jonathan Leitschuh
*/
public class CommandScheduleTest extends AbstractCommandTest {
private static final Logger logger = Logger.getLogger(CommandScheduleTest.class.getName());
protected Logger getClassLogger() {
return logger;
}
/**
* Simple scheduling of a command and making sure the command is run and successfully terminates.
*/
@Test
public void testRunAndTerminate() {
final MockCommand command = new MockCommand();
command.start();
assertCommandState(command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 2, 2, 0, 0);
command.setHasFinished(true);
assertCommandState(command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 3, 3, 1, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 3, 3, 1, 0);
}
/**
* Simple scheduling of a command and making sure the command is run and cancels correctly.
*/
@Test
public void testRunAndCancel() {
final MockCommand command = new MockCommand();
command.start();
assertCommandState(command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 3, 3, 0, 0);
command.cancel();
assertCommandState(command, 1, 3, 3, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 3, 3, 0, 1);
Scheduler.getInstance().run();
assertCommandState(command, 1, 3, 3, 0, 1);
}
}

View File

@@ -1,114 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. 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.command;
import org.junit.Test;
import java.util.logging.Logger;
import edu.wpi.first.wpilibj.mocks.MockCommand;
/**
* Ported from the old CrioTest Classes.
*
* @author Mitchell
* @author Jonathan Leitschuh
*/
public class CommandSequentialGroupTest extends AbstractCommandTest {
private static final Logger logger = Logger.getLogger(CommandSequentialGroupTest.class.getName());
protected Logger getClassLogger() {
return logger;
}
/**
* Simple Command Group With 3 commands that all depend on a subsystem. Some commands have a
* timeout.
*/
@Test(timeout = 20000)
public void testThreeCommandOnSubSystem() {
logger.fine("Begining Test");
final ASubsystem subsystem = new ASubsystem();
logger.finest("Creating Mock Command1");
final MockCommand command1 = new MockCommand() {
{
requires(subsystem);
}
};
logger.finest("Creating Mock Command2");
final MockCommand command2 = new MockCommand() {
{
requires(subsystem);
}
};
logger.finest("Creating Mock Command3");
final MockCommand command3 = new MockCommand() {
{
requires(subsystem);
}
};
logger.finest("Creating Command Group");
final CommandGroup commandGroup = new CommandGroup();
commandGroup.addSequential(command1, 1.0);
commandGroup.addSequential(command2, 2.0);
commandGroup.addSequential(command3);
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
logger.finest("Starting Command group");
commandGroup.start();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
sleep(1000);// command 1 timeout
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 1, 1, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
sleep(2000);// command 2 timeout
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 1);
assertCommandState(command3, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 1);
assertCommandState(command3, 1, 2, 2, 0, 0);
command3.setHasFinished(true);
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 1);
assertCommandState(command3, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 1);
assertCommandState(command3, 1, 3, 3, 1, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 1);
assertCommandState(command3, 1, 3, 3, 1, 0);
}
}

View File

@@ -1,128 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. 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.command;
import org.junit.Test;
import java.util.logging.Logger;
import edu.wpi.first.wpilibj.mocks.MockCommand;
/**
* Ported from the old CrioTest Classes.
*
* @author Mitchell
* @author Jonathan Leitschuh
*/
public class CommandSupersedeTest extends AbstractCommandTest {
private static final Logger logger = Logger.getLogger(CommandSupersedeTest.class.getName());
protected Logger getClassLogger() {
return logger;
}
/**
* Testing one command superseding another because of dependencies.
*/
@Test
public void testOneCommandSupersedingAnotherBecauseOfDependencies() {
final ASubsystem subsystem = new ASubsystem();
final MockCommand command1 = new MockCommand() {
{
requires(subsystem);
}
};
final MockCommand command2 = new MockCommand() {
{
requires(subsystem);
}
};
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
command1.start();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 2, 2, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 3, 3, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
command2.start();
assertCommandState(command1, 1, 3, 3, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 4, 4, 0, 1);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 4, 4, 0, 1);
assertCommandState(command2, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 4, 4, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 4, 4, 0, 1);
assertCommandState(command2, 1, 3, 3, 0, 0);
}
/**
* Testing one command failing superseding another because of dependencies because the first
* command cannot be interrupted.
*/
@Test
public void testCommandFailingSupersedingBecauseFirstCanNotBeInterrupted() {
final ASubsystem subsystem = new ASubsystem();
final MockCommand command1 = new MockCommand() {
{
requires(subsystem);
setInterruptible(false);
}
};
final MockCommand command2 = new MockCommand() {
{
requires(subsystem);
}
};
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
command1.start();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 2, 2, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 3, 3, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
command2.start();
assertCommandState(command1, 1, 3, 3, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 4, 4, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
}
}

View File

@@ -1,65 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. 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.command;
import org.junit.Test;
import java.util.logging.Logger;
import edu.wpi.first.wpilibj.mocks.MockCommand;
/**
* Test a {@link Command} that times out.
*
* @author jonathanleitschuh
*/
public class CommandTimeoutTest extends AbstractCommandTest {
private static final Logger logger = Logger.getLogger(CommandTimeoutTest.class.getName());
protected Logger getClassLogger() {
return logger;
}
/**
* Command 2 second Timeout Test.
*/
@Test
public void testTwoSecondTimeout() {
final ASubsystem subsystem = new ASubsystem();
final MockCommand command = new MockCommand() {
{
requires(subsystem);
setTimeout(2);
}
@Override
public boolean isFinished() {
return super.isFinished() || isTimedOut();
}
};
command.start();
assertCommandState(command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 3, 3, 0, 0);
sleep(2000);
Scheduler.getInstance().run();
assertCommandState(command, 1, 4, 4, 1, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 4, 4, 1, 0);
}
}

View File

@@ -1,142 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. 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.command;
import org.junit.Test;
import java.util.logging.Logger;
import edu.wpi.first.wpilibj.mocks.MockCommand;
/**
* Tests the {@link Command} library.
*
* @author jonathanleitschuh
*/
public class DefaultCommandTest extends AbstractCommandTest {
private static final Logger logger = Logger.getLogger(DefaultCommandTest.class.getName());
protected Logger getClassLogger() {
return logger;
}
/**
* Testing of default commands where the interrupting command ends itself.
*/
@Test
public void testDefaultCommandWhereTheInteruptingCommandEndsItself() {
final ASubsystem subsystem = new ASubsystem();
final MockCommand defaultCommand = new MockCommand() {
{
requires(subsystem);
}
};
final MockCommand anotherCommand = new MockCommand() {
{
requires(subsystem);
}
};
assertCommandState(defaultCommand, 0, 0, 0, 0, 0);
subsystem.init(defaultCommand);
assertCommandState(defaultCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 2, 2, 0, 0);
anotherCommand.start();
assertCommandState(defaultCommand, 1, 2, 2, 0, 0);
assertCommandState(anotherCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 0);
anotherCommand.setHasFinished(true);
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 3, 3, 1, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 2, 4, 4, 0, 1);
assertCommandState(anotherCommand, 1, 3, 3, 1, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 2, 5, 5, 0, 1);
assertCommandState(anotherCommand, 1, 3, 3, 1, 0);
}
/**
* Testing of default commands where the interrupting command is canceled.
*/
@Test
public void testDefaultCommandsInterruptingCommandCanceled() {
final ASubsystem subsystem = new ASubsystem();
final MockCommand defaultCommand = new MockCommand() {
{
requires(subsystem);
}
};
final MockCommand anotherCommand = new MockCommand() {
{
requires(subsystem);
}
};
assertCommandState(defaultCommand, 0, 0, 0, 0, 0);
subsystem.init(defaultCommand);
subsystem.initDefaultCommand();
assertCommandState(defaultCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 2, 2, 0, 0);
anotherCommand.start();
assertCommandState(defaultCommand, 1, 2, 2, 0, 0);
assertCommandState(anotherCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 0);
anotherCommand.cancel();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 1);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 2, 4, 4, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 1);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 2, 5, 5, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 1);
}
}

View File

@@ -1,118 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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.mocks;
import edu.wpi.first.wpilibj.command.Command;
/**
* A class to simulate a simple command The command keeps track of how many times each method was
* called.
*
* @author mwills
*/
public class MockCommand extends Command {
private int m_initializeCount = 0;
private int m_executeCount = 0;
private int m_isFinishedCount = 0;
private boolean m_hasFinished = false;
private int m_endCount = 0;
private int m_interruptedCount = 0;
protected void initialize() {
++m_initializeCount;
}
protected void execute() {
++m_executeCount;
}
protected boolean isFinished() {
++m_isFinishedCount;
return isHasFinished();
}
protected void end() {
++m_endCount;
}
protected void interrupted() {
++m_interruptedCount;
}
/**
* How many times the initialize method has been called.
*/
public int getInitializeCount() {
return m_initializeCount;
}
/**
* If the initialize method has been called at least once.
*/
public boolean hasInitialized() {
return getInitializeCount() > 0;
}
/**
* How many time the execute method has been called.
*/
public int getExecuteCount() {
return m_executeCount;
}
/**
* How many times the isFinished method has been called.
*/
public int getIsFinishedCount() {
return m_isFinishedCount;
}
/**
* @return what value the isFinished method will return.
*/
public boolean isHasFinished() {
return m_hasFinished;
}
/**
* @param hasFinished set what value the isFinished method will return.
*/
public void setHasFinished(boolean hasFinished) {
m_hasFinished = hasFinished;
}
/**
* How many times the end method has been called.
*/
public int getEndCount() {
return m_endCount;
}
/**
* If the end method has been called at least once.
*/
public boolean hasEnd() {
return getEndCount() > 0;
}
/**
* How many times the interrupted method has been called.
*/
public int getInterruptedCount() {
return m_interruptedCount;
}
/**
* If the interrupted method has been called at least once.
*/
public boolean hasInterrupted() {
return getInterruptedCount() > 0;
}
}