private static void assertEqualsInternal( String msg, StringBuilder expected, CharBlockArray actual) { assertEquals(msg, expected.length(), actual.length()); for (int i = 0; i < expected.length(); i++) { assertEquals(msg, expected.charAt(i), actual.charAt(i)); } }
public int addLabel(CategoryPath label, int hash, int cid) { int bucketIndex = indexFor(hash, this.capacity); for (Entry e = this.entries[bucketIndex]; e != null; e = e.next) { if (e.hash == hash && CategoryPathUtils.equalsToSerialized(label, labelRepository, e.offset)) { return e.cid; } } // new string; add to label repository int offset = labelRepository.length(); CategoryPathUtils.serialize(label, labelRepository); addEntry(offset, cid, hash, bucketIndex); return cid; }
@Test public void testArray() throws Exception { CharBlockArray array = new CharBlockArray(); StringBuilder builder = new StringBuilder(); final int n = 100 * 1000; byte[] buffer = new byte[50]; for (int i = 0; i < n; i++) { random().nextBytes(buffer); int size = 1 + random().nextInt(50); // This test is turning random bytes into a string, // this is asking for trouble. CharsetDecoder decoder = IOUtils.CHARSET_UTF_8 .newDecoder() .onUnmappableCharacter(CodingErrorAction.REPLACE) .onMalformedInput(CodingErrorAction.REPLACE); String s = decoder.decode(ByteBuffer.wrap(buffer, 0, size)).toString(); array.append(s); builder.append(s); } for (int i = 0; i < n; i++) { random().nextBytes(buffer); int size = 1 + random().nextInt(50); // This test is turning random bytes into a string, // this is asking for trouble. CharsetDecoder decoder = IOUtils.CHARSET_UTF_8 .newDecoder() .onUnmappableCharacter(CodingErrorAction.REPLACE) .onMalformedInput(CodingErrorAction.REPLACE); String s = decoder.decode(ByteBuffer.wrap(buffer, 0, size)).toString(); array.append((CharSequence) s); builder.append(s); } for (int i = 0; i < n; i++) { random().nextBytes(buffer); int size = 1 + random().nextInt(50); // This test is turning random bytes into a string, // this is asking for trouble. CharsetDecoder decoder = IOUtils.CHARSET_UTF_8 .newDecoder() .onUnmappableCharacter(CodingErrorAction.REPLACE) .onMalformedInput(CodingErrorAction.REPLACE); String s = decoder.decode(ByteBuffer.wrap(buffer, 0, size)).toString(); for (int j = 0; j < s.length(); j++) { array.append(s.charAt(j)); } builder.append(s); } assertEqualsInternal("GrowingCharArray<->StringBuilder mismatch.", builder, array); File tempDir = _TestUtil.getTempDir("growingchararray"); File f = new File(tempDir, "GrowingCharArrayTest.tmp"); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f)); array.flush(out); out.flush(); out.close(); BufferedInputStream in = new BufferedInputStream(new FileInputStream(f)); array = CharBlockArray.open(in); assertEqualsInternal( "GrowingCharArray<->StringBuilder mismatch after flush/load.", builder, array); in.close(); f.delete(); }