b777e874dfbe03c17bddead928900563c7fc82e4
[platform/upstream/libvorbis.git] / test / test.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007             *
9  * by the Xiph.Org Foundation http://www.xiph.org/                  *
10  *                                                                  *
11  ********************************************************************
12
13  function: vorbis coded test suite using vorbisfile
14  last mod: $Id: test.c 13293 2007-07-24 00:09:47Z erikd $
15
16  ********************************************************************/
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <math.h>
21
22 #include "util.h"
23 #include "write_read.h"
24
25 #define DATA_LEN        2048
26
27 #define MAX(a,b)        ((a) > (b) ? (a) : (b))
28
29
30 static int check_output (const float * data_in, unsigned len, float allowable);
31
32 int
33 main(void){
34   static float data_out [DATA_LEN] ;
35   static float data_in [DATA_LEN] ;
36
37   /* Do safest and most used sample rates first. */
38   int sample_rates [] = { 44100, 48000, 32000, 22050, 16000, 96000 } ;
39   unsigned k ;
40   int errors = 0 ;
41   float q=-.05;
42
43   gen_windowed_sine (data_out, ARRAY_LEN (data_out), 0.95);
44
45   while(q<1.){
46     for (k = 0 ; k < ARRAY_LEN (sample_rates); k ++) {
47       char filename [64] ;
48       snprintf (filename, sizeof (filename), "vorbis_q%.1f_%u.ogg", q*10,sample_rates [k]);
49
50       printf ("    %-20s : ", filename);
51       fflush (stdout);
52
53       /* Set to know value. */
54       set_data_in (data_in, ARRAY_LEN (data_in), 3.141);
55
56       write_vorbis_data_or_die (filename, sample_rates [k], q, data_out, ARRAY_LEN (data_out));
57       read_vorbis_data_or_die (filename, sample_rates [k], data_in, ARRAY_LEN (data_in));
58
59       if (check_output (data_in, ARRAY_LEN (data_in), (.15f - .1f*q)) != 0)
60         errors ++ ;
61       else {
62         puts ("ok");
63         remove (filename);
64       }
65     }
66     q+=.1;
67   }
68
69   if (errors)
70     exit (1);
71
72   return 0;
73 }
74
75 static int
76 check_output (const float * data_in, unsigned len, float allowable)
77 {
78   float max_abs = 0.0 ;
79   unsigned k ;
80
81   for (k = 0 ; k < len ; k++) {
82     float temp = fabs (data_in [k]);
83     max_abs = MAX (max_abs, temp);
84   }
85
86   if (max_abs < 0.95-allowable) {
87     printf ("Error : max_abs (%f) too small.\n", max_abs);
88     return 1 ;
89   } else if (max_abs > .95+allowable) {
90     printf ("Error : max_abs (%f) too big.\n", max_abs);
91     return 1 ;
92   }
93
94   return 0 ;
95 }
96