| 1 |
package morfologik.fsa.builders; |
| 2 |
|
| 3 |
import static morfologik.fsa.FSAFlags.*; |
| 4 |
|
| 5 |
import java.io.IOException; |
| 6 |
import java.nio.ByteBuffer; |
| 7 |
import java.util.ArrayList; |
| 8 |
import java.util.Arrays; |
| 9 |
import java.util.Collections; |
| 10 |
import java.util.List; |
| 11 |
|
| 12 |
import morfologik.fsa.FSA; |
| 13 |
import morfologik.fsa.FSA5; |
| 14 |
import morfologik.fsa.FSAFlags; |
| 15 |
|
| 16 |
import org.junit.Test; |
| 17 |
import static org.junit.Assert.*; |
| 18 |
|
| 19 |
/** |
| 20 |
* Additional tests for {@link FSA5}. |
| 21 |
*/ |
| 22 |
public final class FSA5Test extends TestBase { |
| 23 |
public List<String> expected = Arrays.asList("a", "aba", "ac", "b", "ba", "c"); |
| 24 |
|
| 25 |
@Test |
| 26 |
public void testVersion5() throws IOException { |
| 27 |
final FSA fsa = FSA.read(this.getClass().getResourceAsStream("abc.fsa")); |
| 28 |
assertFalse(fsa.getFlags().contains(FSAFlags.NUMBERS)); |
| 29 |
verifyContent(expected, fsa); |
| 30 |
} |
| 31 |
|
| 32 |
@Test |
| 33 |
public void testVersion5WithNumbers() throws IOException { |
| 34 |
final FSA fsa = FSA.read(this.getClass().getResourceAsStream("abc-numbers.fsa")); |
| 35 |
|
| 36 |
verifyContent(expected, fsa); |
| 37 |
assertTrue(fsa.getFlags().contains(FSAFlags.NUMBERS)); |
| 38 |
} |
| 39 |
|
| 40 |
@Test |
| 41 |
public void testArcsAndNodes() throws IOException { |
| 42 |
final FSA fsa1 = FSA.read(this.getClass().getResourceAsStream("abc.fsa")); |
| 43 |
final FSA fsa2 = FSA.read(this.getClass().getResourceAsStream("abc-numbers.fsa")); |
| 44 |
|
| 45 |
FSAInfo info1 = new FSAInfo(fsa1); |
| 46 |
FSAInfo info2 = new FSAInfo(fsa2); |
| 47 |
|
| 48 |
assertEquals(info1.arcsCount, info2.arcsCount); |
| 49 |
assertEquals(info1.nodeCount, info2.nodeCount); |
| 50 |
|
| 51 |
assertEquals(4, info2.nodeCount); |
| 52 |
assertEquals(7, info2.arcsCount); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void testNumbers() throws IOException { |
| 57 |
final FSA fsa = FSA.read(this.getClass().getResourceAsStream("abc-numbers.fsa")); |
| 58 |
|
| 59 |
assertTrue(fsa.getFlags().contains(NEXTBIT)); |
| 60 |
|
| 61 |
// Get all numbers for nodes. |
| 62 |
byte[] buffer = new byte[128]; |
| 63 |
final ArrayList<String> result = new ArrayList<String>(); |
| 64 |
walkNode(buffer, 0, fsa, fsa.getRootNode(), 0, result); |
| 65 |
|
| 66 |
Collections.sort(result); |
| 67 |
assertEquals(Arrays.asList("0 c", "1 b", "2 ba", "3 a", "4 ac", "5 aba"), result); |
| 68 |
} |
| 69 |
|
| 70 |
public static void walkNode(byte[] buffer, int depth, FSA fsa, int node, int cnt, List<String> result) |
| 71 |
throws IOException { |
| 72 |
for (int arc = fsa.getFirstArc(node); arc != 0; arc = fsa.getNextArc(arc)) { |
| 73 |
buffer[depth] = fsa.getArcLabel(arc); |
| 74 |
|
| 75 |
if (fsa.isArcFinal(arc) || fsa.isArcTerminal(arc)) { |
| 76 |
result.add(cnt + " " + new String(buffer, 0, depth + 1, "UTF-8")); |
| 77 |
} |
| 78 |
|
| 79 |
if (fsa.isArcFinal(arc)) { |
| 80 |
cnt++; |
| 81 |
} |
| 82 |
|
| 83 |
if (!fsa.isArcTerminal(arc)) { |
| 84 |
walkNode(buffer, depth + 1, fsa, fsa.getEndNode(arc), cnt, result); |
| 85 |
cnt += fsa.getRightLanguageCount(fsa.getEndNode(arc)); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
private static void verifyContent(List<String> expected, FSA fsa) throws IOException { |
| 91 |
final ArrayList<String> actual = new ArrayList<String>(); |
| 92 |
|
| 93 |
int count = 0; |
| 94 |
for (ByteBuffer bb : fsa.getSequences()) { |
| 95 |
assertEquals(0, bb.arrayOffset()); |
| 96 |
assertEquals(0, bb.position()); |
| 97 |
actual.add(new String(bb.array(), 0, bb.remaining(), "UTF-8")); |
| 98 |
count++; |
| 99 |
} |
| 100 |
assertEquals(expected.size(), count); |
| 101 |
Collections.sort(actual); |
| 102 |
assertEquals(expected, actual); |
| 103 |
} |
| 104 |
} |