/*
* Copyright 2015-2016 Cel Skeggs
*
* This file is part of the CCRE, the Common Chicken Runtime Engine.
*
* The CCRE is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* The CCRE is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the CCRE. If not, see <http://www.gnu.org/licenses/>.
*/
package ccre.channel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Random;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import ccre.testing.CountingBooleanOutput;
import ccre.testing.CountingEventOutput;
@SuppressWarnings("javadoc")
public class BooleanCellTest {
private BooleanCell cell;
@Before
public void setUp() throws Exception {
this.cell = new BooleanCell();
}
@After
public void tearDown() throws Exception {
this.cell = null;
}
@Test
public void testBooleanCellBoolean() {
assertFalse(new BooleanCell(false).get());
assertTrue(new BooleanCell(true).get());
}
@Test
public void testBooleanCellBooleanOutput() {
CountingBooleanOutput cbo = new CountingBooleanOutput();
cbo.ifExpected = true;
cbo.valueExpected = false;
cell = new BooleanCell(cbo);
cbo.check();
assertTrue(cell.hasListeners());
Random random = new Random();
boolean lastValue = false;
for (int i = 0; i < 20; i++) {
boolean value = i % 3 == 1 ? random.nextBoolean() : i % 3 == 0;
cbo.ifExpected = (value != lastValue);
cbo.valueExpected = value;
cell.set(value);
cbo.check();
lastValue = value;
}
}
@Test(expected = NullPointerException.class)
public void testBooleanCellBooleanOutputNull() {
new BooleanCell((BooleanOutput) null);
}
@Test
public void testBooleanCellBooleanOutputArray() {
CountingBooleanOutput c1 = new CountingBooleanOutput();
CountingBooleanOutput c2 = new CountingBooleanOutput();
c1.ifExpected = c2.ifExpected = true;
c1.valueExpected = c2.valueExpected = false;
cell = new BooleanCell(c1, c2);
c1.check();
c2.check();
assertTrue(cell.hasListeners());
Random random = new Random();
boolean lastValue = false;
for (int i = 0; i < 20; i++) {
boolean value = i % 3 == 1 ? random.nextBoolean() : i % 3 == 0;
c1.ifExpected = c2.ifExpected = (value != lastValue);
c1.valueExpected = c2.valueExpected = value;
cell.set(value);
c1.check();
c2.check();
lastValue = value;
}
}
@Test(expected = NullPointerException.class)
public void testBooleanCellBooleanOutputArrayNull() {
new BooleanCell((BooleanOutput[]) null);
}
@Test(expected = NullPointerException.class)
public void testBooleanCellBooleanOutputArrayNulls() {
new BooleanCell(BooleanOutput.ignored, null);
}
@Test
public void testHasConsumers() {
assertFalse(cell.hasListeners());
for (int i = 0; i < 5; i++) {
CancelOutput eo = cell.send(BooleanOutput.ignored);
assertTrue(cell.hasListeners());
eo.cancel();
assertFalse(cell.hasListeners());
CancelOutput eo1 = cell.send(BooleanOutput.ignored);
assertTrue(cell.hasListeners());
CancelOutput eo2 = cell.send(BooleanOutput.ignored);
assertTrue(cell.hasListeners());
eo1.cancel();
assertTrue(cell.hasListeners());
eo2.cancel();
assertFalse(cell.hasListeners());
}
}
@Test
public void testSet() {
Random rand = new Random();
CountingBooleanOutput cbo = new CountingBooleanOutput();
cbo.ifExpected = true;
cbo.valueExpected = false;
cell.send(cbo);
boolean lastValue = false;
for (int i = 0; i < 20; i++) {
boolean value = i % 3 == 1 ? rand.nextBoolean() : i % 3 == 0;
cbo.ifExpected = (value != lastValue);
cbo.valueExpected = value;
assertEquals(lastValue, cell.get());
cell.set(value);
assertEquals(value, cell.get());
cbo.check();
lastValue = value;
}
}
@Test
public void testGet() {
CountingBooleanOutput ceo = new CountingBooleanOutput();
CountingBooleanOutput ceo2 = new CountingBooleanOutput();
ceo.ifExpected = ceo2.ifExpected = true;
ceo.valueExpected = ceo2.valueExpected = false;
cell.send((value) -> {
ceo.set(cell.get());
ceo2.set(value);
});
ceo.check();
ceo2.check();
for (int i = 0; i < 5; i++) {
boolean value = i % 2 == 0;
ceo.ifExpected = ceo2.ifExpected = true;
ceo.valueExpected = ceo2.valueExpected = value;
cell.set(value);
assertEquals(value, cell.get());
ceo.check();
ceo2.check();
}
}
@Test
public void testOnUpdate() {
CountingEventOutput ceo = new CountingEventOutput();
cell.onUpdate(ceo);
assertTrue(cell.hasListeners());
Random rand = new Random();
boolean lastValue = false;
for (int i = 0; i < 20; i++) {
boolean value = i % 3 == 1 ? rand.nextBoolean() : i % 3 == 0;
ceo.ifExpected = (value != lastValue);
cell.set(value);
ceo.check();
lastValue = value;
}
}
@Test(expected = NullPointerException.class)
public void testOnUpdateNull() {
cell.onUpdate(null);
}
@Test
public void testOnUpdateR() {
CountingEventOutput ceo = new CountingEventOutput();
CancelOutput unbind = cell.onUpdate(ceo);
assertTrue(cell.hasListeners());
Random rand = new Random();
boolean lastValue = false;
for (int i = 0; i < 20; i++) {
boolean value = i % 3 == 1 ? rand.nextBoolean() : i % 3 == 0;
ceo.ifExpected = (value != lastValue);
cell.set(value);
ceo.check();
lastValue = value;
}
assertTrue(cell.hasListeners());
unbind.cancel();
assertFalse(cell.hasListeners());
for (int i = 0; i < 20; i++) {
cell.set(i % 3 == 1 ? rand.nextBoolean() : i % 3 == 0);
ceo.check();
}
assertFalse(cell.hasListeners());
}
@Test
public void testOnUpdateAndOnUpdateR() {
assertFalse(cell.hasListeners());
CancelOutput unbind = cell.onUpdate(EventOutput.ignored);
assertTrue(cell.hasListeners());
cell.onUpdate(EventOutput.ignored);
assertTrue(cell.hasListeners());
unbind.cancel();
assertTrue(cell.hasListeners());
}
@Test
public void testOnUpdateRTwice() {
assertFalse(cell.hasListeners());
CancelOutput unbind1 = cell.onUpdate(EventOutput.ignored);
assertTrue(cell.hasListeners());
CancelOutput unbind2 = cell.onUpdate(EventOutput.ignored);
assertTrue(cell.hasListeners());
unbind1.cancel();
assertTrue(cell.hasListeners());
unbind2.cancel();
assertFalse(cell.hasListeners());
}
@Test(expected = NullPointerException.class)
public void testOnUpdateRNull() {
cell.onUpdate(null);
}
}