use of java.io.ByteArrayInputStream in project commons by twitter.
the class TokenStreamSerializerTest method testVInt.
@Test
public void testVInt() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TokenStreamSerializer.AttributeOutputStream out = new TokenStreamSerializer.AttributeOutputStream(baos);
final int N = 10000;
final int[] expected = new int[N];
Random rnd = new Random();
for (int i = 0; i < N; i++) {
expected[i] = rnd.nextInt();
out.writeVInt(expected[i]);
}
out.flush();
byte[] data = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(data);
TokenStreamSerializer.AttributeInputStream in = new TokenStreamSerializer.AttributeInputStream(bais);
for (int i = 0; i < N; i++) {
assertEquals(expected[i], in.readVInt());
}
}
use of java.io.ByteArrayInputStream in project trie4j by takawitter.
the class LongsTailLOUDSTrieWithConcatTailArrayTest method test_save_load.
@Test
public void test_save_load() throws Exception {
String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
TailLOUDSTrie lt = trieWithWords(words);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
lt.writeExternal(oos);
oos.flush();
lt = new TailLOUDSTrie();
lt.readExternal(new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())));
for (String w : words) {
Assert.assertTrue(lt.contains(w));
}
Assert.assertFalse(lt.contains("おやすみなさい"));
StringBuilder b = new StringBuilder();
Node[] children = lt.getRoot().getChildren();
for (Node n : children) {
char[] letters = n.getLetters();
b.append(letters[0]);
}
Assert.assertEquals("おこさ", b.toString());
}
use of java.io.ByteArrayInputStream in project trie4j by takawitter.
the class TailLOUDSTrieWithConcatTailArrayTest method test_save_load.
@Test
public void test_save_load() throws Exception {
String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
TailLOUDSTrie lt = trieWithWords(words);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
lt.writeExternal(oos);
oos.flush();
lt = new TailLOUDSTrie();
lt.readExternal(new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())));
for (String w : words) {
Assert.assertTrue(lt.contains(w));
}
Assert.assertFalse(lt.contains("おやすみなさい"));
StringBuilder b = new StringBuilder();
Node[] children = lt.getRoot().getChildren();
for (Node n : children) {
char[] letters = n.getLetters();
b.append(letters[0]);
}
Assert.assertEquals("おこさ", b.toString());
}
use of java.io.ByteArrayInputStream in project trie4j by takawitter.
the class TailLOUDSTrieWithSuffixTrieTailArrayTest method test_save_load2.
@Test
public void test_save_load2() throws Exception {
String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
Trie trie = new PatriciaTrie();
for (String w : words) trie.insert(w);
TailLOUDSTrie lt = new TailLOUDSTrie(trie);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(lt);
oos.flush();
lt = (TailLOUDSTrie) new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
for (String w : words) {
Assert.assertTrue(lt.contains(w));
}
Assert.assertFalse(lt.contains("おやすみなさい"));
StringBuilder b = new StringBuilder();
Node[] children = lt.getRoot().getChildren();
for (Node n : children) {
char[] letters = n.getLetters();
b.append(letters[0]);
}
Assert.assertEquals("おこさ", b.toString());
}
use of java.io.ByteArrayInputStream in project trie4j by takawitter.
the class TailLOUDSTrieWithSuffixTrieTailArrayTest method test_save_load.
@Test
public void test_save_load() throws Exception {
String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
TailLOUDSTrie lt = trieWithWords(words);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
lt.writeExternal(oos);
oos.flush();
lt = new TailLOUDSTrie();
lt.readExternal(new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())));
for (String w : words) {
Assert.assertTrue(lt.contains(w));
}
Assert.assertFalse(lt.contains("おやすみなさい"));
StringBuilder b = new StringBuilder();
Node[] children = lt.getRoot().getChildren();
for (Node n : children) {
char[] letters = n.getLetters();
b.append(letters[0]);
}
Assert.assertEquals("おこさ", b.toString());
}
Aggregations