| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 "libavutil/rational.c" | ||
| 23 | #include "libavutil/integer.h" | ||
| 24 | #include "libavutil/intfloat.h" | ||
| 25 | |||
| 26 | 1 | int main(void) | |
| 27 | { | ||
| 28 | AVRational a,b,r; | ||
| 29 | int i,j,k; | ||
| 30 | 1 | int ret = 0; | |
| 31 | static const int64_t numlist[] = { | ||
| 32 | INT64_MIN, INT64_MIN+1, INT64_MAX, INT32_MIN, INT32_MAX, 1,0,-1, | ||
| 33 | 123456789, INT32_MAX-1, INT32_MAX+1LL, UINT32_MAX-1, UINT32_MAX, UINT32_MAX+1LL | ||
| 34 | }; | ||
| 35 | |||
| 36 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for (a.num = -2; a.num <= 2; a.num++) { |
| 37 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 5 times.
|
30 | for (a.den = -2; a.den <= 2; a.den++) { |
| 38 |
2/2✓ Branch 0 taken 125 times.
✓ Branch 1 taken 25 times.
|
150 | for (b.num = -2; b.num <= 2; b.num++) { |
| 39 |
2/2✓ Branch 0 taken 625 times.
✓ Branch 1 taken 125 times.
|
750 | for (b.den = -2; b.den <= 2; b.den++) { |
| 40 | 625 | int c = av_cmp_q(a,b); | |
| 41 | 625 | double d = av_q2d(a) == av_q2d(b) ? | |
| 42 |
2/2✓ Branch 0 taken 553 times.
✓ Branch 1 taken 72 times.
|
625 | 0 : (av_q2d(a) - av_q2d(b)); |
| 43 |
2/2✓ Branch 0 taken 252 times.
✓ Branch 1 taken 373 times.
|
625 | if (d > 0) d = 1; |
| 44 |
2/2✓ Branch 0 taken 252 times.
✓ Branch 1 taken 121 times.
|
373 | else if (d < 0) d = -1; |
| 45 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 72 times.
|
121 | else if (d != d) d = INT_MIN; |
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 625 times.
|
625 | if (c != d) { |
| 47 | ✗ | av_log(NULL, AV_LOG_ERROR, "%d/%d %d/%d, %d %f\n", a.num, | |
| 48 | a.den, b.num, b.den, c,d); | ||
| 49 | ✗ | ret = 1; | |
| 50 | } | ||
| 51 | 625 | r = av_sub_q(av_add_q(b,a), b); | |
| 52 |
5/8✓ Branch 0 taken 500 times.
✓ Branch 1 taken 125 times.
✓ Branch 2 taken 500 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 500 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 500 times.
|
625 | if(b.den && (r.num*a.den != a.num*r.den || !r.num != !a.num || !r.den != !a.den)) { |
| 53 | ✗ | av_log(NULL, AV_LOG_ERROR, "%d/%d ", r.num, r.den); | |
| 54 | ✗ | ret = 1; | |
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | /* Check overflow behavior and edge cases */ | ||
| 62 | static const AVRational unit_mul_q[][3] = { | ||
| 63 | {{INT_MAX, 2}, { 2, 1}, { INT_MAX, 1}}, | ||
| 64 | {{INT_MAX, 2}, {-2, 1}, {-INT_MAX, 1}}, | ||
| 65 | {{INT_MAX, 2}, { 0, 1}, {0, 1}}, | ||
| 66 | {{INT_MIN, 2}, { 2, 1}, {-INT_MAX, 1}}, /* not INT_MIN */ | ||
| 67 | {{INT_MIN, 2}, {-2, 1}, { INT_MAX, 1}}, | ||
| 68 | {{INT_MIN, 2}, { 0, 1}, {0, 1}}, | ||
| 69 | {{INT_MAX >> 8, 1}, {INT_MAX >> 8, 1}, {INT_MAX, 1}}, | ||
| 70 | {{1, INT_MAX >> 8}, {1, INT_MAX >> 8}, {0, 1}}, | ||
| 71 | {{1, 1}, {0, 0}, {0, 0}}, | ||
| 72 | {{0, 1}, {0, 0}, {0, 0}}, | ||
| 73 | }; | ||
| 74 | |||
| 75 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
|
11 | for (i = 0; i < FF_ARRAY_ELEMS(unit_mul_q); i++) { |
| 76 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
|
30 | for (int c = 0; c < 2; c++) { /* test commutativity */ |
| 77 | 20 | AVRational a = unit_mul_q[i][c ? 1 : 0]; | |
| 78 | 20 | AVRational b = unit_mul_q[i][c ? 0 : 1]; | |
| 79 | 20 | AVRational c = unit_mul_q[i][2]; | |
| 80 | 20 | AVRational r = av_mul_q(a, b); | |
| 81 |
2/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
|
20 | if (r.num != c.num || r.den != c.den) { |
| 82 | ✗ | av_log(NULL, AV_LOG_ERROR, "%d/%d * %d/%d = %d/%d, expected %d/%d\n", | |
| 83 | a.num, a.den, b.num, b.den, r.num, r.den, c.num, c.den); | ||
| 84 | ✗ | ret = 1; | |
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | static const AVRational unit_add_q[][3] = { | ||
| 90 | {{INT_MAX, 1}, { 2, 2}, { INT_MAX, 1}}, | ||
| 91 | {{INT_MAX, 1}, {-2, 2}, { INT_MAX - 1, 1}}, | ||
| 92 | {{INT_MAX, 1}, { 0, 2}, { INT_MAX, 1}}, | ||
| 93 | {{INT_MIN, 1}, { 2, 2}, {-INT_MAX, 1}}, | ||
| 94 | {{INT_MIN, 1}, {-2, 2}, {-INT_MAX, 1}}, | ||
| 95 | {{INT_MIN, 1}, { 0, 2}, {-INT_MAX, 1}}, | ||
| 96 | {{INT_MAX - 10, 1}, {20, 1}, { INT_MAX, 1}}, | ||
| 97 | {{2, INT_MAX}, {2, INT_MAX}, {4, INT_MAX}}, | ||
| 98 | {{1, 1}, {0, 0}, {0, 0}}, | ||
| 99 | {{0, 1}, {0, 0}, {0, 0}}, | ||
| 100 | }; | ||
| 101 | |||
| 102 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
|
11 | for (i = 0; i < FF_ARRAY_ELEMS(unit_add_q); i++) { |
| 103 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
|
30 | for (int c = 0; c < 2; c++) { /* test commutativity */ |
| 104 | 20 | AVRational a = unit_add_q[i][c ? 1 : 0]; | |
| 105 | 20 | AVRational b = unit_add_q[i][c ? 0 : 1]; | |
| 106 | 20 | AVRational c = unit_add_q[i][2]; | |
| 107 | 20 | AVRational r = av_add_q(a, b); | |
| 108 |
2/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
|
20 | if (r.num != c.num || r.den != c.den) { |
| 109 | ✗ | av_log(NULL, AV_LOG_ERROR, "%d/%d + %d/%d = %d/%d, expected %d/%d\n", | |
| 110 | a.num, a.den, b.num, b.den, r.num, r.den, c.num, c.den); | ||
| 111 | ✗ | ret = 1; | |
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1 times.
|
15 | for (i = 0; i < FF_ARRAY_ELEMS(numlist); i++) { |
| 117 | 14 | int64_t a = numlist[i]; | |
| 118 | |||
| 119 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 14 times.
|
210 | for (j = 0; j < FF_ARRAY_ELEMS(numlist); j++) { |
| 120 | 196 | int64_t b = numlist[j]; | |
| 121 |
2/2✓ Branch 0 taken 70 times.
✓ Branch 1 taken 126 times.
|
196 | if (b<=0) |
| 122 | 70 | continue; | |
| 123 |
2/2✓ Branch 0 taken 1764 times.
✓ Branch 1 taken 126 times.
|
1890 | for (k = 0; k < FF_ARRAY_ELEMS(numlist); k++) { |
| 124 | 1764 | int64_t c = numlist[k]; | |
| 125 | int64_t res; | ||
| 126 | AVInteger ai; | ||
| 127 | |||
| 128 |
2/2✓ Branch 0 taken 630 times.
✓ Branch 1 taken 1134 times.
|
1764 | if (c<=0) |
| 129 | 1764 | continue; | |
| 130 | 1134 | res = av_rescale_rnd(a,b,c, AV_ROUND_ZERO); | |
| 131 | |||
| 132 | 1134 | ai = av_mul_i(av_int2i(a), av_int2i(b)); | |
| 133 | 1134 | ai = av_div_i(ai, av_int2i(c)); | |
| 134 | |||
| 135 |
3/4✓ Branch 0 taken 75 times.
✓ Branch 1 taken 1059 times.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
|
1134 | if (av_cmp_i(ai, av_int2i(INT64_MAX)) > 0 && res == INT64_MIN) |
| 136 | 75 | continue; | |
| 137 |
3/4✓ Branch 0 taken 76 times.
✓ Branch 1 taken 983 times.
✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
|
1059 | if (av_cmp_i(ai, av_int2i(INT64_MIN)) < 0 && res == INT64_MIN) |
| 138 | 76 | continue; | |
| 139 |
2/2✓ Branch 0 taken 957 times.
✓ Branch 1 taken 26 times.
|
983 | if (av_cmp_i(ai, av_int2i(res)) == 0) |
| 140 | 957 | continue; | |
| 141 | |||
| 142 | // Special exception for INT64_MIN, remove this in case INT64_MIN is handled without off by 1 error | ||
| 143 |
2/4✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
|
26 | if (av_cmp_i(ai, av_int2i(res-1)) == 0 && a == INT64_MIN) |
| 144 | 26 | continue; | |
| 145 | |||
| 146 | ✗ | av_log(NULL, AV_LOG_ERROR, "%"PRId64" * %"PRId64" / %"PRId64" = %"PRId64" or %"PRId64"\n", a,b,c, res, av_i2int(ai)); | |
| 147 | ✗ | ret = 1; | |
| 148 | } | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
|
11 | for (a.num = 1; a.num <= 10; a.num++) { |
| 153 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 10 times.
|
110 | for (a.den = 1; a.den <= 10; a.den++) { |
| 154 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 63 times.
|
100 | if (av_gcd(a.num, a.den) > 1) |
| 155 | 37 | continue; | |
| 156 |
2/2✓ Branch 0 taken 630 times.
✓ Branch 1 taken 63 times.
|
693 | for (b.num = 1; b.num <= 10; b.num++) { |
| 157 |
2/2✓ Branch 0 taken 6300 times.
✓ Branch 1 taken 630 times.
|
6930 | for (b.den = 1; b.den <= 10; b.den++) { |
| 158 | int start; | ||
| 159 |
2/2✓ Branch 0 taken 2331 times.
✓ Branch 1 taken 3969 times.
|
6300 | if (av_gcd(b.num, b.den) > 1) |
| 160 | 2331 | continue; | |
| 161 |
2/2✓ Branch 1 taken 1953 times.
✓ Branch 2 taken 2016 times.
|
3969 | if (av_cmp_q(b, a) < 0) |
| 162 | 1953 | continue; | |
| 163 |
2/2✓ Branch 0 taken 20160 times.
✓ Branch 1 taken 2016 times.
|
22176 | for (start = 0; start < 10 ; start++) { |
| 164 | 20160 | int acc= start; | |
| 165 | int i; | ||
| 166 | |||
| 167 |
2/2✓ Branch 0 taken 2016000 times.
✓ Branch 1 taken 20160 times.
|
2036160 | for (i = 0; i<100; i++) { |
| 168 | 2016000 | int exact = start + av_rescale_q(i+1, b, a); | |
| 169 | 2016000 | acc = av_add_stable(a, acc, b, 1); | |
| 170 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2016000 times.
|
2016000 | if (FFABS(acc - exact) > 2) { |
| 171 | ✗ | av_log(NULL, AV_LOG_ERROR, "%d/%d %d/%d, %d %d\n", a.num, | |
| 172 | a.den, b.num, b.den, acc, exact); | ||
| 173 | ✗ | return 1; | |
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
|
21 | for (a.den = 1; a.den < 0x100000000U/3; a.den*=3) { |
| 183 |
2/2✓ Branch 0 taken 29540 times.
✓ Branch 1 taken 20 times.
|
29560 | for (a.num = -1; a.num < (1<<27); a.num += 1 + a.num/100) { |
| 184 | 29540 | float f = av_int2float(av_q2intfloat(a)); | |
| 185 | 29540 | float f2 = av_q2d(a); | |
| 186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29540 times.
|
29540 | if (fabs(f - f2) > fabs(f)/5000000) { |
| 187 | ✗ | av_log(NULL, AV_LOG_ERROR, "%d/%d %f %f\n", a.num, | |
| 188 | a.den, f, f2); | ||
| 189 | ✗ | return 1; | |
| 190 | } | ||
| 191 | |||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | 1 | return ret; | |
| 196 | } | ||
| 197 |