Add option to fast forward sleeping

This commit is contained in:
Rui Hu 2024-03-13 17:34:11 +08:00
parent 45faf97cea
commit f005187a72
4 changed files with 7 additions and 16 deletions

View File

@ -27,4 +27,5 @@ java {
tasks.test {
useJUnitPlatform()
jvmArgs = listOf("-Dfast=true")
}

View File

@ -21,4 +21,5 @@ java {
tasks.test {
useJUnitPlatform()
jvmArgs = listOf("-Dfast=true")
}

View File

@ -27,7 +27,7 @@ import static com.cleverthis.interview.padlock.Utils.ensureSleep;
* After create, the input buffer is empty, you have to initialize.
*/
public class PadlockImpl {
private final boolean debug;
private final boolean fast = Boolean.parseBoolean(System.getProperty("fast"));
private final int numpadSize;
private final Integer[] inputBuffer;
private final Integer[] correctPasscode;
@ -38,17 +38,6 @@ public class PadlockImpl {
* @param numpadSize The number of buttons on the numpad of this lock.
*/
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");
this.numpadSize = numpadSize;
this.inputBuffer = new Integer[numpadSize];
@ -73,7 +62,7 @@ public class PadlockImpl {
* @return The old value, null if not initialized.
*/
public synchronized Integer writeInputBuffer(int address, int keyIndex) {
if (!debug) ensureSleep(1000);
if (!fast) ensureSleep(1000);
if (keyIndex < 0 || keyIndex >= numpadSize)
throw new IllegalArgumentException(
"keyIndex out of range. Keypad size: " + numpadSize + ", keyIndex: " + keyIndex);

View File

@ -22,7 +22,7 @@ class PadlockTest {
@Test
void testInstantiationRest() {
PadlockImpl padlock = new PadlockImpl(5, true);
PadlockImpl padlock = new PadlockImpl(5);
for (int i = 0; i < 5; i++) {
// ensure input buffer is uninitialized
// should return null when first set
@ -34,7 +34,7 @@ class PadlockTest {
@Test
void testRejectInvalidInput() {
PadlockImpl padlock = new PadlockImpl(5, true);
PadlockImpl padlock = new PadlockImpl(5);
for (int i = 0; i < 3; i++) {
padlock.writeInputBuffer(i, i);
}
@ -54,7 +54,7 @@ class PadlockTest {
@Test
void testRejectInvalidInputBufferAddressAndValue() {
PadlockImpl padlock = new PadlockImpl(5, true);
PadlockImpl padlock = new PadlockImpl(5);
// test address
assertThrows(ArrayIndexOutOfBoundsException.class, () -> padlock.writeInputBuffer(-1, 1));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> padlock.writeInputBuffer(-10, 1));