Skip to content

Commit 8fbde68

Browse files
committed
Add tests to try to reproduce #191 (no luck yet)
1 parent a81cb06 commit 8fbde68

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

src/test/java/com/fasterxml/jackson/core/sym/TestSymbolTables.java

+76-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.io.IOException;
44
import java.nio.charset.Charset;
55

6-
import com.fasterxml.jackson.core.JsonFactory;
6+
import com.fasterxml.jackson.core.*;
77

88
public class TestSymbolTables extends com.fasterxml.jackson.core.BaseTest
99
{
@@ -133,4 +133,79 @@ public void testCollisionsWithChars187() throws IOException
133133
// as well as collision counts
134134
assertEquals(10, symbols.maxCollisionLength());
135135
}
136+
137+
// [core#191]: similarly, but for "short" symbols:
138+
public void testShortNameCollisionsViaParser() throws Exception
139+
{
140+
JsonFactory f = new JsonFactory();
141+
String json = _shortDoc191();
142+
JsonParser p;
143+
144+
// First: ensure that char-based is fine
145+
p = f.createParser(json);
146+
while (p.nextToken() != null) { }
147+
p.close();
148+
149+
// and then that byte-based
150+
p = f.createParser(json.getBytes("UTF-8"));
151+
while (p.nextToken() != null) { }
152+
p.close();
153+
}
154+
155+
// [core#191]
156+
public void testShortNameCollisionsDirect() throws IOException
157+
{
158+
final int COUNT = 400;
159+
160+
// First, char-based
161+
{
162+
CharsToNameCanonicalizer symbols = CharsToNameCanonicalizer.createRoot(1);
163+
for (int i = 0; i < COUNT; ++i) {
164+
String id = String.valueOf((char) i);
165+
char[] ch = id.toCharArray();
166+
symbols.findSymbol(ch, 0, ch.length, symbols.calcHash(id));
167+
}
168+
assertEquals(COUNT, symbols.size());
169+
assertEquals(1024, symbols.bucketCount());
170+
171+
assertEquals(0, symbols.collisionCount());
172+
assertEquals(0, symbols.maxCollisionLength());
173+
}
174+
175+
// then byte-based
176+
{
177+
BytesToNameCanonicalizer symbols =
178+
BytesToNameCanonicalizer.createRoot(1).makeChild(JsonFactory.Feature.collectDefaults());
179+
for (int i = 0; i < COUNT; ++i) {
180+
String id = String.valueOf((char) i);
181+
int[] quads = BytesToNameCanonicalizer.calcQuads(id.getBytes("UTF-8"));
182+
symbols.addName(id, quads, quads.length);
183+
}
184+
assertEquals(COUNT, symbols.size());
185+
assertEquals(1024, symbols.bucketCount());
186+
187+
assertEquals(15, symbols.collisionCount());
188+
assertEquals(1, symbols.maxCollisionLength());
189+
}
190+
}
191+
192+
private String _shortDoc191() {
193+
StringBuilder sb = new StringBuilder();
194+
sb.append("{\n");
195+
for (int i = 0; i < 400; ++i) {
196+
if (i > 0) {
197+
sb.append(",\n");
198+
}
199+
sb.append('"');
200+
char c = (char) i;
201+
if (Character.isLetterOrDigit(c)) {
202+
sb.append((char) i);
203+
} else {
204+
sb.append(String.format("\\u%04x", i));
205+
}
206+
sb.append("\" : "+i);
207+
}
208+
sb.append("}\n");
209+
return sb.toString();
210+
}
136211
}

0 commit comments

Comments
 (0)