]> git.sesse.net Git - ffmpeg/blob - libavutil/rational-test.c
avformat/au: Write MetaData in AU Sun audio file header
[ffmpeg] / libavutil / rational-test.c
1 /*
2  * rational numbers
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "rational.c"
23
24 #include "integer.h"
25
26 int main(void)
27 {
28     AVRational a,b,r;
29     int i,j,k;
30     static const int64_t numlist[] = {
31         INT64_MIN, INT64_MIN+1, INT64_MAX, INT32_MIN, INT32_MAX, 1,0,-1,
32         123456789, INT32_MAX-1, INT32_MAX+1LL, UINT32_MAX-1, UINT32_MAX, UINT32_MAX+1LL
33     };
34
35     for (a.num = -2; a.num <= 2; a.num++) {
36         for (a.den = -2; a.den <= 2; a.den++) {
37             for (b.num = -2; b.num <= 2; b.num++) {
38                 for (b.den = -2; b.den <= 2; b.den++) {
39                     int c = av_cmp_q(a,b);
40                     double d = av_q2d(a) == av_q2d(b) ?
41                                0 : (av_q2d(a) - av_q2d(b));
42                     if (d > 0)       d = 1;
43                     else if (d < 0)  d = -1;
44                     else if (d != d) d = INT_MIN;
45                     if (c != d)
46                         av_log(NULL, AV_LOG_ERROR, "%d/%d %d/%d, %d %f\n", a.num,
47                                a.den, b.num, b.den, c,d);
48                     r = av_sub_q(av_add_q(b,a), b);
49                     if(b.den && (r.num*a.den != a.num*r.den || !r.num != !a.num || !r.den != !a.den))
50                         av_log(NULL, AV_LOG_ERROR, "%d/%d ", r.num, r.den);
51                 }
52             }
53         }
54     }
55
56     for (i = 0; i < FF_ARRAY_ELEMS(numlist); i++) {
57         int64_t a = numlist[i];
58
59         for (j = 0; j < FF_ARRAY_ELEMS(numlist); j++) {
60             int64_t b = numlist[j];
61             if (b<=0)
62                 continue;
63             for (k = 0; k < FF_ARRAY_ELEMS(numlist); k++) {
64                 int64_t c = numlist[k];
65                 int64_t res;
66                 AVInteger ai;
67
68                 if (c<=0)
69                     continue;
70                 res = av_rescale_rnd(a,b,c, AV_ROUND_ZERO);
71
72                 ai = av_mul_i(av_int2i(a), av_int2i(b));
73                 ai = av_div_i(ai, av_int2i(c));
74
75                 if (av_cmp_i(ai, av_int2i(INT64_MAX)) > 0 && res == INT64_MIN)
76                     continue;
77                 if (av_cmp_i(ai, av_int2i(INT64_MIN)) < 0 && res == INT64_MIN)
78                     continue;
79                 if (av_cmp_i(ai, av_int2i(res)) == 0)
80                     continue;
81
82                 // Special exception for INT64_MIN, remove this in case INT64_MIN is handled without off by 1 error
83                 if (av_cmp_i(ai, av_int2i(res-1)) == 0 && a == INT64_MIN)
84                     continue;
85
86                 av_log(NULL, AV_LOG_ERROR, "%"PRId64" * %"PRId64" / %"PRId64" = %"PRId64" or %"PRId64"\n", a,b,c, res, av_i2int(ai));
87             }
88         }
89     }
90
91     for (a.num = 1; a.num <= 10; a.num++) {
92         for (a.den = 1; a.den <= 10; a.den++) {
93             if (av_gcd(a.num, a.den) > 1)
94                 continue;
95             for (b.num = 1; b.num <= 10; b.num++) {
96                 for (b.den = 1; b.den <= 10; b.den++) {
97                     int start;
98                     if (av_gcd(b.num, b.den) > 1)
99                         continue;
100                     if (av_cmp_q(b, a) < 0)
101                         continue;
102                     for (start = 0; start < 10 ; start++) {
103                         int acc= start;
104                         int i;
105
106                         for (i = 0; i<100; i++) {
107                             int exact = start + av_rescale_q(i+1, b, a);
108                             acc = av_add_stable(a, acc, b, 1);
109                             if (FFABS(acc - exact) > 2) {
110                                 av_log(NULL, AV_LOG_ERROR, "%d/%d %d/%d, %d %d\n", a.num,
111                                        a.den, b.num, b.den, acc, exact);
112                                 return 1;
113                             }
114                         }
115                     }
116                 }
117             }
118         }
119     }
120
121     for (a.den = 1; a.den < 0x100000000U/3; a.den*=3) {
122         for (a.num = -1; a.num < (1<<27); a.num += 1 + a.num/100) {
123             float f  = av_int2float(av_q2intfloat(a));
124             float f2 = av_q2d(a);
125             if (fabs(f - f2) > fabs(f)/5000000) {
126                 av_log(NULL, AV_LOG_ERROR, "%d/%d %f %f\n", a.num,
127                        a.den, f, f2);
128                 return 1;
129             }
130
131         }
132     }
133
134     return 0;
135 }