/*
* Copyright 2014 Takao Nakaguchi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.trie4j.louds;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.junit.Assert;
import org.junit.Test;
import org.trie4j.AbstractTermIdTrieTest;
import org.trie4j.Node;
import org.trie4j.Trie;
import org.trie4j.patricia.PatriciaTrie;
import org.trie4j.tail.SuffixTrieTailArray;
public class TailLOUDSTrieWithSuffixTrieTailArrayTest
extends AbstractTermIdTrieTest<TailLOUDSTrie>{
@Override
protected TailLOUDSTrie buildSecond(Trie firstTrie) {
return new TailLOUDSTrie(firstTrie, new SuffixTrieTailArray(firstTrie.size()));
}
@Test
public void test() throws Exception{
String[] words = {"こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき"};
TailLOUDSTrie lt = trieWithWords(words);
for(String w : words){
Assert.assertTrue(w, 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());
}
@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());
}
@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());
}
}