random clean-up and more explicit handing of 33 bits in a row

This commit is contained in:
rusefillc 2021-12-09 13:45:45 -05:00
parent 4d03ff9751
commit c0b30287e7
4 changed files with 33 additions and 24 deletions

View File

@ -17,7 +17,7 @@ public class BitState {
public void incrementBitIndex(ConfigField cf) { public void incrementBitIndex(ConfigField cf) {
if (bitIndex == 32) if (bitIndex == 32)
throw new IllegalStateException("Too many bits: " + cf.getName()); throw new TooManyBitsInARow("Too many bits in a row: " + cf.getName());
bitIndex++; bitIndex++;
} }
@ -28,4 +28,10 @@ public class BitState {
public int get() { public int get() {
return bitIndex; return bitIndex;
} }
public static class TooManyBitsInARow extends RuntimeException {
public TooManyBitsInARow(String s) {
super(s);
}
}
} }

View File

@ -1,5 +1,6 @@
package com.rusefi; package com.rusefi;
import com.rusefi.output.JavaFieldsConsumer;
import com.rusefi.util.SystemOut; import com.rusefi.util.SystemOut;
import com.rusefi.test.ConfigFieldParserTest; import com.rusefi.test.ConfigFieldParserTest;
@ -43,8 +44,6 @@ public class ConfigField {
private final ReaderState state; private final ReaderState state;
private final boolean fsioVisible; private final boolean fsioVisible;
private final boolean hasAutoscale; private final boolean hasAutoscale;
private final String individualName;
private final int indexWithinArray;
private final String trueName; private final String trueName;
private final String falseName; private final String falseName;
@ -62,11 +61,11 @@ public class ConfigField {
boolean fsioVisible, boolean fsioVisible,
boolean hasAutoscale, boolean hasAutoscale,
String individualName, String individualName,
int indexWithinArray, String trueName, String falseName) { int indexWithinArray,
String trueName,
String falseName) {
this.fsioVisible = fsioVisible; this.fsioVisible = fsioVisible;
this.hasAutoscale = hasAutoscale; this.hasAutoscale = hasAutoscale;
this.individualName = individualName;
this.indexWithinArray = indexWithinArray;
this.trueName = trueName == null ? "true" : trueName; this.trueName = trueName == null ? "true" : trueName;
this.falseName = falseName == null ? "false" : falseName; this.falseName = falseName == null ? "false" : falseName;
Objects.requireNonNull(name, comment + " " + type); Objects.requireNonNull(name, comment + " " + type);
@ -99,19 +98,6 @@ public class ConfigField {
return falseName; return falseName;
} }
public String getCFieldName() {
return getIndividualName() == null ? getName() : getIndividualName() + "["
+ (getIndexWithinArray() - 1) + "]";
}
public String getIndividualName() {
return individualName;
}
public int getIndexWithinArray() {
return indexWithinArray;
}
public boolean isBit() { public boolean isBit() {
return BOOLEAN_T.equalsIgnoreCase(type); return BOOLEAN_T.equalsIgnoreCase(type);
} }
@ -182,14 +168,16 @@ public class ConfigField {
return field; return field;
} }
public static boolean isPreprocessorDirective(ReaderState state, String line) { public static boolean isPreprocessorDirective(String line) {
Matcher matcher = DIRECTIVE.matcher(line); Matcher matcher = DIRECTIVE.matcher(line);
return matcher.matches(); return matcher.matches();
} }
public int getSize(ConfigField next) { public int getSize(ConfigField next) {
if (isBit() && next.isBit()) if (isBit() && next.isBit()) {
// we have a protection from 33+ bits in a row in BitState, see BitState.TooManyBitsInARow
return 0; return 0;
}
if (isBit()) if (isBit())
return 4; return 4;
int size = getElementSize(); int size = getElementSize();
@ -265,7 +253,7 @@ public class ConfigField {
} }
if (tsInfo == null) if (tsInfo == null)
throw new IllegalArgumentException("tsInfo expected with autoscale"); throw new IllegalArgumentException("tsInfo expected with autoscale");
String[] tokens = tsInfo.split("\\,"); String[] tokens = tsInfo.split(",");
if (tokens.length < 2) if (tokens.length < 2)
throw new IllegalArgumentException("Second comma-separated token expected in [" + tsInfo + "] for " + name); throw new IllegalArgumentException("Second comma-separated token expected in [" + tsInfo + "] for " + name);
@ -303,7 +291,7 @@ public class ConfigField {
private String[] getTokens() { private String[] getTokens() {
if (tsInfo == null) if (tsInfo == null)
return new String[0]; return new String[0];
return tsInfo.split("\\,"); return tsInfo.split(",");
} }
public String getUnits() { public String getUnits() {

View File

@ -240,7 +240,7 @@ public class ReaderState {
ConfigField cf = ConfigField.parse(state, line); ConfigField cf = ConfigField.parse(state, line);
if (cf == null) { if (cf == null) {
if (ConfigField.isPreprocessorDirective(state, line)) { if (ConfigField.isPreprocessorDirective(line)) {
cf = new ConfigField(state, "", line, null, cf = new ConfigField(state, "", line, null,
ConfigField.DIRECTIVE_T, new int[0], null, false, false, false, null, 0, ConfigField.DIRECTIVE_T, new int[0], null, false, false, false, null, 0,
null, null); null, null);

View File

@ -1,5 +1,6 @@
package com.rusefi.test; package com.rusefi.test;
import com.rusefi.BitState;
import com.rusefi.ReaderState; import com.rusefi.ReaderState;
import com.rusefi.output.DataLogConsumer; import com.rusefi.output.DataLogConsumer;
import com.rusefi.output.GaugeConsumer; import com.rusefi.output.GaugeConsumer;
@ -65,6 +66,20 @@ public class OutputsTest {
} }
@Test(expected = BitState.TooManyBitsInARow.class)
public void tooManyBits() throws IOException {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 40; i++)
sb.append("bit b" + i + "\n");
String test = "struct total\n" +
sb +
"end_struct\n";
ReaderState state = new ReaderState();
OutputsSectionConsumer tsProjectConsumer = new OutputsSectionConsumer(null, state);
state.readBufferedReader(test, Collections.singletonList(tsProjectConsumer));
}
@Test @Test
public void generateDataLog() throws IOException { public void generateDataLog() throws IOException {
String test = "struct total\n" + String test = "struct total\n" +