added stb, more binaryout changes"
[henge/apc.git] / stb / tests / vorbseek / vorbseek.c
1 #include <assert.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 #define STB_VORBIS_HEADER_ONLY
7 #include "stb_vorbis.c"
8
9 #define SAMPLES_TO_TEST 3000
10
11 int test_count [5] = { 5000, 3000, 2000, 50000, 50000 };
12 int test_spacing[5] = { 1, 111, 3337, 7779, 72717 };
13
14 int try_seeking(stb_vorbis *v, unsigned int pos, short *output, unsigned int num_samples)
15 {
16 int count;
17 short samples[SAMPLES_TO_TEST*2];
18 assert(pos <= num_samples);
19
20 if (!stb_vorbis_seek(v, pos)) {
21 fprintf(stderr, "Seek to %u returned error from stb_vorbis\n", pos);
22 return 0;
23 }
24
25 count = stb_vorbis_get_samples_short_interleaved(v, 2, samples, SAMPLES_TO_TEST*2);
26
27 if (count > (int) (num_samples - pos)) {
28 fprintf(stderr, "Seek to %u allowed decoding %d samples when only %d should have been valid.\n",
29 pos, count, (int) (num_samples - pos));
30 return 0;
31 }
32
33 if (count < SAMPLES_TO_TEST && count < (int) (num_samples - pos)) {
34 fprintf(stderr, "Seek to %u only decoded %d samples of %d attempted when at least %d should have been valid.\n",
35 pos, count, SAMPLES_TO_TEST, num_samples - pos);
36 return 0;
37 }
38
39 if (0 != memcmp(samples, output + pos*2, count*2)) {
40 int k;
41 for (k=0; k < SAMPLES_TO_TEST*2; ++k) {
42 if (samples[k] != output[k]) {
43 fprintf(stderr, "Seek to %u produced incorrect samples starting at sample %u (short #%d in buffer).\n",
44 pos, pos + (k/2), k);
45 break;
46 }
47 }
48 assert(k != SAMPLES_TO_TEST*2);
49 return 0;
50 }
51
52 return 1;
53 }
54
55 int main(int argc, char **argv)
56 {
57 int num_chan, samprate;
58 int i, j, test, phase;
59 short *output;
60
61 if (argc == 1) {
62 fprintf(stderr, "Usage: vorbseek {vorbisfile} [{vorbisfile]*]\n");
63 fprintf(stderr, "Tests various seek offsets to make sure they're sample exact.\n");
64 return 0;
65 }
66
67 #if 0
68 {
69 // check that outofmem occurs correctly
70 stb_vorbis_alloc va;
71 va.alloc_buffer = malloc(1024*1024);
72 for (i=0; i < 1024*1024; i += 10) {
73 int error=0;
74 stb_vorbis *v;
75 va.alloc_buffer_length_in_bytes = i;
76 v = stb_vorbis_open_filename(argv[1], &error, &va);
77 if (v != NULL)
78 break;
79 printf("Error %d at %d\n", error, i);
80 }
81 }
82 #endif
83
84 for (j=1; j < argc; ++j) {
85 unsigned int successes=0, attempts = 0;
86 unsigned int num_samples = stb_vorbis_decode_filename(argv[j], &num_chan, &samprate, &output);
87
88 break;
89
90 if (num_samples == 0xffffffff) {
91 fprintf(stderr, "Error: couldn't open file or not vorbis file: %s\n", argv[j]);
92 goto fail;
93 }
94
95 if (num_chan != 2) {
96 fprintf(stderr, "vorbseek testing only works with files with 2 channels, %s has %d\n", argv[j], num_chan);
97 goto fail;
98 }
99
100 for (test=0; test < 5; ++test) {
101 int error;
102 stb_vorbis *v = stb_vorbis_open_filename(argv[j], &error, NULL);
103 if (v == NULL) {
104 fprintf(stderr, "Couldn't re-open %s for test #%d\n", argv[j], test);
105 goto fail;
106 }
107 for (phase=0; phase < 3; ++phase) {
108 unsigned int base = phase == 0 ? 0 : phase == 1 ? num_samples - test_count[test]*test_spacing[test] : num_samples/3;
109 for (i=0; i < test_count[test]; ++i) {
110 unsigned int pos = base + i*test_spacing[test];
111 if (pos > num_samples) // this also catches underflows
112 continue;
113 successes += try_seeking(v, pos, output, num_samples);
114 attempts += 1;
115 }
116 }
117 stb_vorbis_close(v);
118 }
119 printf("%d of %d seeks failed in %s (%d samples)\n", attempts-successes, attempts, argv[j], num_samples);
120 free(output);
121 }
122 return 0;
123 fail:
124 return 1;
125 }