private void checkNextWithState(BytesTrie trie, StringAndValue[] data, int dataLength) { BytesTrie.State noState = new BytesTrie.State(), state = new BytesTrie.State(); for (int i = 0; i < dataLength; ++i) { if ((i & 1) == 0) { try { trie.ResetToState(noState); Errln("trie.ResetToState(noState) should throw an ArgumentException"); } catch (ArgumentException e) { // good } } byte[] expectedString = data[i].bytes; int stringLength = data[i].s.Length; int partialLength = stringLength / 3; for (int j = 0; j < partialLength; ++j) { if (!trie.Next(expectedString[j]).Matches()) { Errln("trie.Next()=BytesTrieResult.NO_MATCH for a prefix of " + data[i].s); return; } } trie.SaveState(state); Result resultAtState = trie.Current; Result result; int valueAtState = -99; if (resultAtState.HasValue()) { valueAtState = trie.GetValue(); } result = trie.Next(0); // mismatch if (result != Result.NoMatch || result != trie.Current) { Errln("trie.Next(0) matched after part of " + data[i].s); } if (resultAtState != trie.ResetToState(state).Current || (resultAtState.HasValue() && valueAtState != trie.GetValue()) ) { Errln("trie.Next(part of " + data[i].s + ") changes current()/getValue() after " + "saveState/next(0)/resetToState"); } else if (!(result = trie.Next(expectedString, partialLength, stringLength)).HasValue() || result != trie.Current) { Errln("trie.Next(rest of " + data[i].s + ") does not seem to contain " + data[i].s + " after " + "saveState/next(0)/resetToState"); } else if (!(result = trie.ResetToState(state). Next(expectedString, partialLength, stringLength)).HasValue() || result != trie.Current) { Errln("trie does not seem to contain " + data[i].s + " after saveState/next(rest)/resetToState"); } else if (trie.GetValue() != data[i].value) { Errln(String.Format("trie value for {0} is {1:d}=0x{2:x} instead of expected {3:d}=0x{4:x}", data[i].s, trie.GetValue(), trie.GetValue(), data[i].value, data[i].value)); } trie.Reset(); } }
private void checkNext(BytesTrie trie, StringAndValue[] data, int dataLength) { BytesTrie.State state = new BytesTrie.State(); for (int i = 0; i < dataLength; ++i) { int stringLength = data[i].s.Length; Result result; if (!(result = trie.Next(data[i].bytes, 0, stringLength)).HasValue() || result != trie.Current ) { Errln("trie does not seem to contain " + data[i].s); } else if (trie.GetValue() != data[i].value) { Errln(string.Format("trie value for {0} is {1:d}=0x{2:x} instead of expected {3:d}=0x{4:x}", data[i].s, trie.GetValue(), trie.GetValue(), data[i].value, data[i].value)); } else if (result != trie.Current || trie.GetValue() != data[i].value) { Errln("trie value for " + data[i].s + " changes when repeating current()/getValue()"); } trie.Reset(); result = trie.Current; for (int j = 0; j < stringLength; ++j) { if (!result.HasNext()) { Errln(String.Format("trie.Current!=hasNext before end of {0} (at index {1:d})", data[i].s, j)); break; } if (result == Result.IntermediateValue) { trie.GetValue(); if (trie.Current != Result.IntermediateValue) { Errln(String.Format("trie.GetValue().Current!=BytesTrieResult.INTERMEDIATE_VALUE " + "before end of {0} (at index {1:d})", data[i].s, j)); break; } } result = trie.Next(data[i].bytes[j]); if (!result.Matches()) { Errln(String.Format("trie.Next()=BytesTrieResult.NO_MATCH " + "before end of {0} (at index {1:d})", data[i].s, j)); break; } if (result != trie.Current) { Errln(String.Format("trie.Next()!=following current() " + "before end of {0} (at index {1:d})", data[i].s, j)); break; } } if (!result.HasValue()) { Errln("trie.Next()!=hasValue at the end of " + data[i].s); continue; } trie.GetValue(); if (result != trie.Current) { Errln("trie.Current != current()+getValue()+current() after end of " + data[i].s); } // Compare the final current() with whether next() can actually continue. trie.SaveState(state); bool nextContinues = false; for (int c = 0x20; c < 0x7f; ++c) { if (trie.ResetToState(state).Next(c).Matches()) { nextContinues = true; break; } } if ((result == Result.IntermediateValue) != nextContinues) { Errln("(trie.Current==BytesTrieResult.INTERMEDIATE_VALUE) contradicts " + "(trie.Next(some UChar)!=BytesTrieResult.NO_MATCH) after end of " + data[i].s); } trie.Reset(); } }