aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/jsyn/engine/TestFifo.java
blob: cc539d1423949727f3aa8f7feb530d32c82edb23 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * Copyright 2009 Phil Burk, Mobileer Inc
 *
 * 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 com.jsyn.engine;

import junit.framework.TestCase;

import com.jsyn.io.AudioFifo;

public class TestFifo extends TestCase {

    public void testBasic() {
        Thread watchdog = startWatchdog(600);

        AudioFifo fifo = new AudioFifo();
        fifo.setReadWaitEnabled(false);
        fifo.allocate(8);
        assertEquals("start empty", 0, fifo.available());

        assertEquals("read back Nan when emopty", Double.NaN, fifo.read());

        fifo.write(1.0);
        assertEquals("added one value", 1, fifo.available());
        assertEquals("read back same value", 1.0, fifo.read());
        assertEquals("back to empty", 0, fifo.available());

        for (int i = 0; i < fifo.size(); i++) {
            assertEquals("adding data", i, fifo.available());
            fifo.write(100.0 + i);
        }
        for (int i = 0; i < fifo.size(); i++) {
            assertEquals("removing data", fifo.size() - i, fifo.available());
            assertEquals("reading back data", 100.0 + i, fifo.read());
        }
        watchdog.interrupt();
    }

    /**
     * Wrap around several times to test masking.
     */
    public void testWrapping() {

        final int chunk = 5;
        AudioFifo fifo = new AudioFifo();
        fifo.allocate(8);
        double value = 1000.0;
        for (int i = 0; i < (fifo.size() * chunk); i++) {
            value = checkFifoChunk(fifo, value, chunk);
        }

    }

    private double checkFifoChunk(AudioFifo fifo, double value, int chunk) {
        for (int i = 0; i < chunk; i++) {
            assertEquals("adding data", i, fifo.available());
            fifo.write(value + i);
        }
        for (int i = 0; i < chunk; i++) {
            assertEquals("removing data", chunk - i, fifo.available());
            assertEquals("reading back data", value + i, fifo.read());
        }
        return value + chunk;
    }

    public void testBadSize() {
        boolean caught = false;
        try {
            AudioFifo fifo = new AudioFifo();
            fifo.allocate(20); // not power of 2
            assertTrue("should not get here", false);
        } catch (IllegalArgumentException e) {
            caught = true;
        }
        assertTrue("should have caught size exception", caught);
    }

    public void testSingleReadWait() {
        final int chunk = 5;
        final AudioFifo fifo = new AudioFifo();
        fifo.allocate(8);

        fifo.setWriteWaitEnabled(false);
        fifo.setReadWaitEnabled(true);
        final double value = 50.0;

        // Schedule a delayed write in another thread.
        new Thread() {
            @Override
            public void run() {
                try {
                    sleep(200);
                    for (int i = 0; i < chunk; i++) {
                        fifo.write(value + i);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        Thread watchdog = startWatchdog(500);
        for (int i = 0; i < chunk; i++) {
            assertEquals("reading back data", value + i, fifo.read());
        }
        watchdog.interrupt();
    }

    private Thread startWatchdog(final int msec) {
        Thread watchdog = new Thread() {
            @Override
            public void run() {
                try {
                    sleep(msec);
                    assertTrue("test must still be waiting", false);
                } catch (InterruptedException e) {
                }
            }
        };
        watchdog.start();
        return watchdog;
    }

    public void testSingleWriteWait() {
        final int chunk = 13;
        final AudioFifo fifo = new AudioFifo();
        fifo.allocate(8);

        fifo.setWriteWaitEnabled(true);
        fifo.setReadWaitEnabled(true);
        final double value = 50.0;

        // Schedule a delayed read in another thread.
        Thread readThread = new Thread() {
            @Override
            public void run() {
                try {
                    sleep(200);
                    for (int i = 0; i < chunk; i++) {
                        // System.out.println( "testSingleWriteWait: try to read" );
                        double got = fifo.read();
                        assertEquals("adding data", value + i, got);
                        // System.out.println( "testSingleWriteWait: read " + got );
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        readThread.start();

        Thread watchdog = startWatchdog(500);
        // Try to write more than will fit so we will hang.
        for (int i = 0; i < chunk; i++) {
            fifo.write(value + i);
        }
        watchdog.interrupt();

        try {
            readThread.join(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        assertEquals("readThread should be done.", false, readThread.isAlive());
    }

    public void testBlockReadWait() {
        final int chunk = 50;
        final AudioFifo fifo = new AudioFifo();
        fifo.allocate(8);

        fifo.setWriteWaitEnabled(false);
        fifo.setReadWaitEnabled(true);
        final double value = 300.0;
        double[] readBuffer = new double[chunk];

        // Schedule delayed writes in another thread.
        new Thread() {
            @Override
            public void run() {
                int numWritten = 0;
                double[] writeBuffer = new double[4];
                try {
                    while (numWritten < chunk) {
                        sleep(30);
                        for (int i = 0; i < writeBuffer.length; i++) {
                            writeBuffer[i] = value + numWritten;
                            numWritten += 1;
                        }

                        fifo.write(writeBuffer);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        Thread watchdog = startWatchdog(600);
        fifo.read(readBuffer);
        for (int i = 0; i < chunk; i++) {
            assertEquals("reading back data", value + i, readBuffer[i]);
        }
        watchdog.interrupt();

    }
    
    public void testBlockReadAndWriteWaitStress() {
        final int chunk = 10000000; // 10 Megabytes
        final AudioFifo fifo = new AudioFifo();
        fifo.allocate(8);

        fifo.setWriteWaitEnabled(true);
        fifo.setReadWaitEnabled(true);
        final double value = 50.0;

        // Schedule a delayed write in another thread.
        new Thread() {
            @Override
            public void run() {
                try {
                    sleep(200);
                    for (int i = 0; i < chunk; i++) {
                        fifo.write(value + i);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        Thread watchdog = startWatchdog(10000);
        for (int i = 0; i < chunk; i++) {
            assertEquals("reading back data", value + i, fifo.read());
        }
        watchdog.interrupt();
    }
}