summaryrefslogtreecommitdiffstats
path: root/test/src/com/jogamp/openal/ALTest.java
blob: 9eb07a0c5dfc7a24c0154ad1d2581dff629e94f7 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * Created on Jun 3, 2003
 *
 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.jogamp.openal;

import java.io.IOException;
import java.io.InputStream;

import javax.sound.sampled.UnsupportedAudioFileException;

import com.jogamp.openal.util.*;
import java.io.FileNotFoundException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * @author Athomas Goldberg
 * @author Michael Bien
 */
public class ALTest {

    private static AL al;
    private static ALC alc;
    private static ALCcontext context;
    private static ALCdevice device;
    private final static String TEST_FILE = "lewiscarroll.wav";

    @BeforeClass
    public static void setUp() {
        al = ALFactory.getAL();
        alc = ALFactory.getALC();
        device = alc.alcOpenDevice(null);
        context = alc.alcCreateContext(device, null);
        alc.alcMakeContextCurrent(context);
    }

    @AfterClass
    public static void tearDown() {
        alc.alcMakeContextCurrent(null);
        alc.alcDestroyContext(context);
        alc.alcCloseDevice(device);
    }


    /*
     * Test for void alGenBuffers(int, IntBuffer)
     *//*
    @Test public void testAlGenBuffersintIntBuffer() {
    System.out.println("begin testAlGenBuffersintintBuffer");
    // try basic case
    try {
    IntBuffer buffers = BufferUtils.newIntBuffer(7);
    al.alGenBuffers(7,buffers);
    for(int i = 0; i < 7; i++) {
    assertFalse(buffers.get(i) == 0);
    assertTrue(al.alIsBuffer(buffers.get(i)));
    }
    } catch (Exception e) {
    fail(e.getMessage());
    }

    Exception ex = null;
    // buffers == null
    try {
    IntBuffer buffers = null;
    al.alGenBuffers(7,buffers);


    } catch(IllegalArgumentException e) {
    ex = e;
    }
    assertNotNull(ex);

    ex = null;
    // buffer too small
    try {
    IntBuffer buffers = BufferUtils.newIntBuffer(5);
    al.alGenBuffers(7,buffers);
    } catch(IllegalArgumentException e) {
    ex = e;
    }
    assertNotNull(ex);

    ex = null;
    // buffer not direct
    try {
    IntBuffer buffers = IntBuffer.allocate(7);
    al.alGenBuffers(7,buffers);
    } catch(IllegalArgumentException e) {
    ex = e;
    }
    assertNotNull(ex);
    System.out.println("end testAlGenBuffersintintBuffer");
    }
     */

    /*
     * Test for void alGenBuffers(int, int[])
     */
    @Test
    public void testAlGenBuffersintintArray() {

        System.out.println("begin testAlGenBuffersintintArray");
        // try basic case
        int[] buffers = new int[7];
        al.alGenBuffers(7, buffers, 0);
        for (int i = 0; i < 7; i++) {
            assertFalse(buffers[i] == 0);
            assertTrue(al.alIsBuffer(buffers[i]));
        }

        Exception ex = null;
        // try exceptions
        try {
            buffers = null;
            al.alGenBuffers(7, buffers, 0);
        } catch (IllegalArgumentException e) {
            ex = e;
        }
        assertNotNull(ex);

        ex = null;
        try {
            buffers = new int[5];
            al.alGenBuffers(7, buffers, 0);
        } catch (IllegalArgumentException e) {
            ex = e;
        }
        assertNotNull(ex);

        System.out.println("end testAlGenBuffersintintArray");

    }

    /*
     * Test for void alDeleteBuffers(int, IntBuffer)
     */
//    @Test
    public void testAlDeleteBuffersintIntBuffer() {
        System.out.println("begin testAlDeleteBuffersintintArray");
        // try basic case
        int[] buffers = new int[7];
        al.alGenBuffers(7, buffers, 0);
        for (int i = 0; i < 7; i++) {
            assertFalse(buffers[i] == 0);
            assertTrue(al.alIsBuffer(buffers[i]));
        }
        al.alDeleteBuffers(7, buffers, 0);
        for (int i = 0; i < 7; i++) {
            assertFalse(al.alIsBuffer(buffers[i]));
        }

        Exception ex = null;
        // try exceptions
        try {
            al.alDeleteBuffers(7, (int[]) null, 0);
        } catch (IllegalArgumentException e) {
            ex = e;
        }
        assertNotNull(ex);

        ex = null;
        try {
            buffers = new int[5];
            al.alGenBuffers(5, buffers, 0);
            al.alDeleteBuffers(7, buffers, 0);
        } catch (IllegalArgumentException e) {
            ex = e;
        }
        assertNotNull(ex);

        try {
            buffers = new int[7];
            al.alDeleteBuffers(7, buffers, 0);
            assertTrue(al.alGetError() != 0);
        } catch (Exception e) {
            fail("deleting an unfilled buffer list should generate an ALError but not an exception");
        }

        System.out.println("end testAlDeleteBuffersintintArray");
    }

    /*
     * Test for void alDeleteBuffers(int, int[])
     */
//    @Test
    public void testAlDeleteBuffersintintArray() {
        System.out.println("begin testAlDeleteBuffersintIntBuffer");
        // try basic case
        int[] buffers = new int[7];
        al.alGenBuffers(7, buffers, 0);
        for (int i = 0; i < 7; i++) {
            assertFalse(buffers[i] == 0);
            assertTrue(al.alIsBuffer(buffers[i]));
        }
        al.alDeleteBuffers(7, buffers, 0);
        for (int i = 0; i < 7; i++) {
            assertFalse(al.alIsBuffer(buffers[i]));
        }

        Exception ex = null;
        // try exceptions
        try {
            al.alDeleteBuffers(7, (int[]) null, 0);
        } catch (IllegalArgumentException e) {
            ex = e;
        }
        assertNotNull(ex);
        ex = null;
        try {
            buffers = new int[5];
            al.alGenBuffers(5, buffers, 0);
            al.alDeleteBuffers(7, buffers, 0);
        } catch (IllegalArgumentException e) {
            ex = e;
        }
        assertNotNull(ex);

        ex = null;
        try {
            buffers = new int[5];
            al.alDeleteBuffers(7, buffers, 0);
        } catch (IllegalArgumentException e) {
            ex = e;
        }
        assertNotNull(ex);

        try {
            buffers = new int[7];
            al.alDeleteBuffers(7, buffers, 0);
            assertTrue(al.alGetError() != 0);
        } catch (Exception e) {
            fail("deleting an unfilled buffer list should generate an ALError but not an exception");
        }

        System.out.println("end testAlDeleteBuffersintintArray");
    }

    @Test
    public void testAlIsBuffer() {
        System.out.println("begin testALIsBuffer");
        // check a bufferlist with known bad values
        int[] buffers = new int[7];
        for (int i = 0; i < 7; i++) {
            buffers[i] = -1;
            assertFalse(al.alIsBuffer(buffers[i]));
        }
        // created
        al.alGenBuffers(7, buffers, 0);
        for (int i = 0; i < 7; i++) {
            assertTrue(al.alIsBuffer(buffers[i]));
        }
        // deleted
        al.alDeleteBuffers(7, buffers, 0);
        for (int i = 0; i < 7; i++) {
            assertFalse(al.alIsBuffer(buffers[i]));
        }
        System.out.println("end testALisBuffer");
    }

    /*
     * Test for void alBufferData(int, int, Buffer, int, int)
     */
    @Test
    public void testAlBufferDataintintByteBufferintint() throws IOException, UnsupportedAudioFileException {
        System.out.println("begin testAlBufferDataintintByteBufferintint");
        int[] buffers = new int[1];
        al.alGenBuffers(1, buffers, 0);
        WAVData wd = loadTestWAV();

        al.alBufferData(buffers[0], wd.format, wd.data, wd.size, wd.freq);
        int[] tmp = new int[1];
        al.alGetBufferi(buffers[0], AL.AL_SIZE, tmp, 0);
        assertFalse(tmp[0] == 0);

        Exception ex = null;
        try {
            buffers = new int[1];
            al.alGenBuffers(1, buffers, 0);

            al.alBufferData(buffers[0], AL.AL_FORMAT_STEREO16, null, 0, 0);
        } catch (IllegalArgumentException e) {
            ex = e;
        }
        assertNotNull(ex);

        System.out.println("end testAlBufferDataintintByteBufferintint");
    }

    /*
     * Test for void alGetBufferi(int, int, int[])
     */
    @Test
    public void testAlGetBufferiintintintArray() throws UnsupportedAudioFileException, IOException {
        System.out.println("begin testAlGetBufferiintintintArray");
        int[] buffers = new int[1];
        al.alGenBuffers(1, buffers, 0);
        WAVData wd = loadTestWAV();
        al.alBufferData(buffers[0], wd.format, wd.data, wd.size, wd.freq);
        int[] size = new int[1];
        int[] freq = new int[1];
        al.alGetBufferi(buffers[0], AL.AL_SIZE, size, 0);
        al.alGetBufferi(buffers[0], AL.AL_FREQUENCY, freq, 0);
//        assertEquals(wd.size, size[0]);
        assertEquals(wd.freq, freq[0]);

        Exception ex = null;
        try {
            buffers = new int[1];
            al.alGenBuffers(1, buffers, 0);
            wd = loadTestWAV();
            al.alBufferData(buffers[0], wd.format, wd.data, wd.size, wd.freq);
            size = null;
            al.alGetBufferi(buffers[0], AL.AL_SIZE, size, 0);

        } catch (IllegalArgumentException e) {
            ex = e;
        }

        assertNotNull(ex);

        System.out.println("end testAlGetBufferiintintintArray");
    }

    /*
     * Test for void alGetBufferi(int, int, IntBuffer)
     */
    @Test
    public void testAlGetBufferiintintIntBuffer() throws UnsupportedAudioFileException, IOException {
        int[] buffers = new int[1];
        al.alGenBuffers(1, buffers, 0);
        WAVData wd = loadTestWAV();
        al.alBufferData(buffers[0], wd.format, wd.data, wd.size, wd.freq);

        int[] size = new int[1];
        int[] freq = new int[1];
        al.alGetBufferi(buffers[0], AL.AL_SIZE, size, 0);
        al.alGetBufferi(buffers[0], AL.AL_FREQUENCY, freq, 0);
//        assertEquals(wd.size, size[0]);
        assertEquals(wd.freq, freq[0]);

        Exception ex = null;
        try {
            buffers = new int[1];
            al.alGenBuffers(1, buffers, 0);
            wd = loadTestWAV();
            al.alBufferData(buffers[0], wd.format, wd.data, wd.size, wd.freq);
            size = null;
            al.alGetBufferi(buffers[0], AL.AL_SIZE, size, 0);

        } catch (IllegalArgumentException e) {
            ex = e;
        }

        assertNotNull(ex);
        ex = null;
        try {
            buffers = new int[1];
            al.alGenBuffers(1, buffers, 0);
            wd = loadTestWAV();
            al.alBufferData(buffers[0], wd.format, wd.data, wd.size, wd.freq);
            size = new int[1];
            al.alGetBufferi(buffers[0], AL.AL_SIZE, size, 0);

        } catch (IllegalArgumentException e) {
            ex = e;
        }

//         assertNotNull(ex);
    }

    private WAVData loadTestWAV() throws IOException, UnsupportedAudioFileException {
        InputStream resource = getClass().getResourceAsStream(TEST_FILE);
        if(resource == null) {
            throw new FileNotFoundException(TEST_FILE+" not found");
        }
        return WAVLoader.loadFromStream(resource);
    }
}