CounterScanner v2

This commit is contained in:
rusefillc 2022-11-12 23:34:27 -05:00
parent 445acda316
commit 9d8fdc97a1
2 changed files with 76 additions and 20 deletions

View File

@ -29,11 +29,12 @@ public class CounterScanner {
for (Map.Entry<BitStateKey, BitState> e : bitStates.entrySet()) { for (Map.Entry<BitStateKey, BitState> e : bitStates.entrySet()) {
if (e.getValue().couldBeCounter) BitState bitState = e.getValue();
System.out.println("Looks like counter " + e.getKey()); if (bitState.couldBeCounter()) {
BitStateKey key = e.getKey();
System.out.println("Looks like counter " + key + " " + bitState.cycleLength);
}
} }
} }
static class BitStateKey implements Comparable { static class BitStateKey implements Comparable {
@ -94,23 +95,55 @@ public class CounterScanner {
} }
static class BitState { static class BitState {
boolean isFirst = true; int index;
boolean couldBeCounter = true; int cycleLength;
public boolean couldBeCounter() {
return state == StateMachine.HAPPY_COUNTER;
}
enum StateMachine {
FIRST_VALUE,
LOOKING_FOR_FIRST_SWITCHOVER,
FOUND_FIRST_SWITCHOVER,
HAPPY_COUNTER,
NOT_GOOD
}
StateMachine state = StateMachine.FIRST_VALUE;
boolean previousBitValue; boolean previousBitValue;
public void handle(boolean bitValue) { public void handle(boolean bitValue) {
if (isFirst) { if (state == StateMachine.NOT_GOOD) {
isFirst = false; return;
} else if (state == StateMachine.FIRST_VALUE) {
previousBitValue = bitValue; previousBitValue = bitValue;
return; state = StateMachine.LOOKING_FOR_FIRST_SWITCHOVER;
} } else if (state == StateMachine.LOOKING_FOR_FIRST_SWITCHOVER) {
if (!couldBeCounter) if (previousBitValue == bitValue)
return; return;
if (previousBitValue == bitValue) { previousBitValue = bitValue;
couldBeCounter = false; state = StateMachine.FOUND_FIRST_SWITCHOVER;
} } else if (state == StateMachine.FOUND_FIRST_SWITCHOVER) {
previousBitValue = bitValue; index++;
if (previousBitValue != bitValue) {
state = StateMachine.HAPPY_COUNTER;
cycleLength = index;
previousBitValue = bitValue;
index = 0;
}
} else if (state == StateMachine.HAPPY_COUNTER) {
index++;
if (previousBitValue != bitValue) {
if (index != cycleLength)
state = StateMachine.NOT_GOOD;
previousBitValue = bitValue;
index = 0;
}
} else
throw new IllegalStateException(state.toString());
} }
} }
} }

View File

@ -2,21 +2,28 @@ package com.rusefi.can.analysis;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
public class CounterScannerTest { public class CounterScannerTest {
@Test @Test
public void testNotCounter() { public void testNotCounter() {
CounterScanner.BitState state = new CounterScanner.BitState(); CounterScanner.BitState state = new CounterScanner.BitState();
state.handle(true); state.handle(true);
assertEquals(state.state, CounterScanner.BitState.StateMachine.LOOKING_FOR_FIRST_SWITCHOVER);
state.handle(false); state.handle(false);
assertEquals(state.state, CounterScanner.BitState.StateMachine.FOUND_FIRST_SWITCHOVER);
state.handle(false); state.handle(false);
assertEquals(CounterScanner.BitState.StateMachine.FOUND_FIRST_SWITCHOVER, state.state);
state.handle(true); state.handle(true);
assertEquals(CounterScanner.BitState.StateMachine.HAPPY_COUNTER, state.state);
state.handle(false); state.handle(false);
state.handle(false); state.handle(false);
assertFalse(state.couldBeCounter); assertFalse(state.couldBeCounter());
} }
@Test @Test
@ -27,6 +34,22 @@ public class CounterScannerTest {
state.handle(true); state.handle(true);
state.handle(false); state.handle(false);
assertTrue(state.couldBeCounter); assertTrue(state.couldBeCounter());
assertEquals(1, state.cycleLength);
}
@Test
public void testCounterLen2() {
CounterScanner.BitState state = new CounterScanner.BitState();
state.handle(true);
state.handle(false);
state.handle(false);
state.handle(true);
state.handle(true);
state.handle(false);
assertTrue(state.couldBeCounter());
assertEquals(2, state.cycleLength);
} }
} }