aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/test/java/com/yahoo/fsa/test/FSADataTestCase.java
blob: b0dc4b8b6b167ee0211c8409c5fd437c768fe819 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.fsa.test;

import com.yahoo.fsa.FSA;
import org.junit.Before;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.nio.BufferUnderflowException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * @author geirst
 */
public class FSADataTestCase {

    private static class Worker extends Thread {
        FSA.State state;
        String word;
        String data;
        long numRuns;
        long numExceptions;
        long numAsserts;
        public Worker(FSA fsa, String word, String data, long numRuns) {
            state = fsa.getState();
            this.word = word;
            this.data = data;
            this.numRuns = numRuns;
            this.numExceptions = 0;
            this.numAsserts = 0;
        }
        @Override
        public void run() {
            for (long i = 0; i < numRuns; ++i) {
                state.start();
                state.delta(word);
                try {
                    String data = state.dataString();
                    if (!this.data.equals(data)) {
                        ++numAsserts;
                    }
                } catch (BufferUnderflowException e) {
                    ++numExceptions;
                }
            }
            System.out.println("Worker(" + word + "): numExceptions(" + numExceptions + "), numAsserts(" + numAsserts + ")");
        }
    };

    private FSA fsa;

    @Before
    public void setUp() throws IOException {
        fsa = new FSA(new FileInputStream("src/test/fsa/test-data.fsa"));
    }

    @Test
    public void testClose() throws IOException {
        FSA fsa = new FSA(new FileInputStream("src/test/fsa/test-data.fsa"));
        fsa.close();
    }

    @Test
    public void testBasic() {
        FSA.State state = fsa.getState();
        state.delta("aa");
        assertTrue(state.isFinal());
        assertEquals("aa data", state.dataString());

        state.start();
        state.delta("bbbb");
        assertTrue(state.isFinal());
        assertEquals("bbbb data", state.dataString());

        state.start();
        state.delta("c");
        assertTrue(state.isFinal());
        assertEquals("c data", state.dataString());

        state.start();
        state.delta("dddddd");
        assertTrue(state.isFinal());
        assertEquals("dddddd data", state.dataString());
    }

    @Test
    public void testMultipleThreads() {
        long numRuns = 10000;
        List<Worker> workers = new ArrayList<Worker>();
        workers.add(new Worker(fsa, "aa", "aa data", numRuns));
        workers.add(new Worker(fsa, "bbbb", "bbbb data", numRuns));
        workers.add(new Worker(fsa, "c", "c data", numRuns));
        workers.add(new Worker(fsa, "dddddd", "dddddd data", numRuns));
        for (int i = 0; i < workers.size(); ++i) {
            workers.get(i).start();
        }
        try {
            for (int i = 0; i < workers.size(); ++i) {
                workers.get(i).join();
            }
        } catch (InterruptedException e) {
            assertTrue(false);
        }
        for (int i = 0; i < workers.size(); ++i) {
            assertEquals(0, workers.get(i).numExceptions);
            assertEquals(0, workers.get(i).numAsserts);
        }
    }

}