Skip to content

Commit

Permalink
Add test for #213
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 8, 2015
1 parent 1649b25 commit 6f496e7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* merge back symbols to the root symbol table
*/
@SuppressWarnings("serial")
public class TestJsonParserSymbols
public class SymbolTableMergingTest
extends com.fasterxml.jackson.core.BaseTest
{
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.fasterxml.jackson.core.sym;

import java.io.IOException;
import java.util.HashSet;

import com.fasterxml.jackson.core.*;

/**
* Tests that use symbol table functionality through parser.
*/
public class SymbolsViaParserTest
extends com.fasterxml.jackson.core.BaseTest
{
// for [jackson-core#213]
public void test17CharSymbols() throws Exception
{
_test17Chars(false);
}

// for [jackson-core#213]
public void test17ByteSymbols() throws Exception
{
_test17Chars(true);
}

private void _test17Chars(boolean useBytes) throws IOException
{
String doc = _createDoc();
JsonFactory f = new JsonFactory();

JsonParser p;
if (useBytes) {
p = f.createParser(doc.getBytes("UTF-8"));
} else {
p = f.createParser(doc);
}
HashSet<String> syms = new HashSet<String>();
assertToken(JsonToken.START_OBJECT, p.nextToken());
for (int i = 0; i < 50; ++i) {
assertToken(JsonToken.FIELD_NAME, p.nextToken());
syms.add(p.getCurrentName());
assertToken(JsonToken.VALUE_TRUE, p.nextToken());
}
assertToken(JsonToken.END_OBJECT, p.nextToken());
assertEquals(50, syms.size());
p.close();
}

private String _createDoc() {
StringBuilder sb = new StringBuilder(1000);
sb.append("{\n");
for (int i = 1; i <= 50; ++i) {
if (i > 1) {
sb.append(",\n");
}
sb.append("\"lengthmatters")
.append(1000 + i)
.append("\": true");
}
sb.append("\n}");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
/**
* Some unit tests to try to exercise part of parser code that
* deals with symbol (table) management.
*<p>
* Note that the problem does not necessarily affect code at or
* after Jackson 2.6, since hash calculation algorithm has been
* completely changed. It may still be relevant for character-backed
* sources, however.
*/
public class TestHashCollisionChars
extends BaseTest
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/fasterxml/jackson/core/sym/TestSymbolTables.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

import com.fasterxml.jackson.core.*;

/**
* Tests that directly modify/access underlying low-level symbol tables
* (instead of indirectly using them via JsonParser).
*/
public class TestSymbolTables extends com.fasterxml.jackson.core.BaseTest
{
// Test for verifying stability of hashCode, wrt collisions, using
Expand Down Expand Up @@ -482,4 +486,23 @@ public void testShortNameCollisionsDirectNew() throws IOException
symbols.primaryCount() + symbols.secondaryCount() + symbols.tertiaryCount() + symbols.spilloverCount());
}
}

// to verify [jackson-core#213] -- did not fail, but ruled out low-level bug

public void testLongSymbols17Bytes() throws Exception
{
ByteQuadsCanonicalizer symbolsB =
ByteQuadsCanonicalizer.createRoot(3).makeChild(JsonFactory.Feature.collectDefaults());
CharsToNameCanonicalizer symbolsC = CharsToNameCanonicalizer.createRoot(3);

for (int i = 1001; i <= 1050; ++i) {
String id = "lengthmatters"+i;
int[] quads = calcQuads(id.getBytes("UTF-8"));
symbolsB.addName(id, quads, quads.length);
char[] idChars = id.toCharArray();
symbolsC.findSymbol(idChars, 0, idChars.length, symbolsC.calcHash(id));
}
assertEquals(50, symbolsB.size());
assertEquals(50, symbolsC.size());
}
}

0 comments on commit 6f496e7

Please sign in to comment.