Add option to fast forward sleeping
This commit is contained in:
parent
45faf97cea
commit
f005187a72
@ -27,4 +27,5 @@ java {
|
|||||||
|
|
||||||
tasks.test {
|
tasks.test {
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
|
jvmArgs = listOf("-Dfast=true")
|
||||||
}
|
}
|
@ -21,4 +21,5 @@ java {
|
|||||||
|
|
||||||
tasks.test {
|
tasks.test {
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
|
jvmArgs = listOf("-Dfast=true")
|
||||||
}
|
}
|
@ -27,7 +27,7 @@ import static com.cleverthis.interview.padlock.Utils.ensureSleep;
|
|||||||
* After create, the input buffer is empty, you have to initialize.
|
* After create, the input buffer is empty, you have to initialize.
|
||||||
*/
|
*/
|
||||||
public class PadlockImpl {
|
public class PadlockImpl {
|
||||||
private final boolean debug;
|
private final boolean fast = Boolean.parseBoolean(System.getProperty("fast"));
|
||||||
private final int numpadSize;
|
private final int numpadSize;
|
||||||
private final Integer[] inputBuffer;
|
private final Integer[] inputBuffer;
|
||||||
private final Integer[] correctPasscode;
|
private final Integer[] correctPasscode;
|
||||||
@ -38,17 +38,6 @@ public class PadlockImpl {
|
|||||||
* @param numpadSize The number of buttons on the numpad of this lock.
|
* @param numpadSize The number of buttons on the numpad of this lock.
|
||||||
*/
|
*/
|
||||||
public PadlockImpl(int numpadSize) {
|
public PadlockImpl(int numpadSize) {
|
||||||
this(numpadSize, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a padlock instance.
|
|
||||||
*
|
|
||||||
* @param numpadSize The number of buttons on the numpad of this lock.
|
|
||||||
* @param debug Will skip sleep if is true
|
|
||||||
*/
|
|
||||||
PadlockImpl(int numpadSize, boolean debug) {
|
|
||||||
this.debug = debug;
|
|
||||||
if (numpadSize < 1) throw new IllegalArgumentException("numpadSize must be a positive number");
|
if (numpadSize < 1) throw new IllegalArgumentException("numpadSize must be a positive number");
|
||||||
this.numpadSize = numpadSize;
|
this.numpadSize = numpadSize;
|
||||||
this.inputBuffer = new Integer[numpadSize];
|
this.inputBuffer = new Integer[numpadSize];
|
||||||
@ -73,7 +62,7 @@ public class PadlockImpl {
|
|||||||
* @return The old value, null if not initialized.
|
* @return The old value, null if not initialized.
|
||||||
*/
|
*/
|
||||||
public synchronized Integer writeInputBuffer(int address, int keyIndex) {
|
public synchronized Integer writeInputBuffer(int address, int keyIndex) {
|
||||||
if (!debug) ensureSleep(1000);
|
if (!fast) ensureSleep(1000);
|
||||||
if (keyIndex < 0 || keyIndex >= numpadSize)
|
if (keyIndex < 0 || keyIndex >= numpadSize)
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"keyIndex out of range. Keypad size: " + numpadSize + ", keyIndex: " + keyIndex);
|
"keyIndex out of range. Keypad size: " + numpadSize + ", keyIndex: " + keyIndex);
|
||||||
|
@ -22,7 +22,7 @@ class PadlockTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testInstantiationRest() {
|
void testInstantiationRest() {
|
||||||
PadlockImpl padlock = new PadlockImpl(5, true);
|
PadlockImpl padlock = new PadlockImpl(5);
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
// ensure input buffer is uninitialized
|
// ensure input buffer is uninitialized
|
||||||
// should return null when first set
|
// should return null when first set
|
||||||
@ -34,7 +34,7 @@ class PadlockTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRejectInvalidInput() {
|
void testRejectInvalidInput() {
|
||||||
PadlockImpl padlock = new PadlockImpl(5, true);
|
PadlockImpl padlock = new PadlockImpl(5);
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
padlock.writeInputBuffer(i, i);
|
padlock.writeInputBuffer(i, i);
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ class PadlockTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRejectInvalidInputBufferAddressAndValue() {
|
void testRejectInvalidInputBufferAddressAndValue() {
|
||||||
PadlockImpl padlock = new PadlockImpl(5, true);
|
PadlockImpl padlock = new PadlockImpl(5);
|
||||||
// test address
|
// test address
|
||||||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> padlock.writeInputBuffer(-1, 1));
|
assertThrows(ArrayIndexOutOfBoundsException.class, () -> padlock.writeInputBuffer(-1, 1));
|
||||||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> padlock.writeInputBuffer(-10, 1));
|
assertThrows(ArrayIndexOutOfBoundsException.class, () -> padlock.writeInputBuffer(-10, 1));
|
||||||
|
Loading…
Reference in New Issue
Block a user