| 1 |
|
| 2 |
|
| 3 |
#include <commonlib/rational.h> |
| 4 |
#include <tests/test.h> |
| 5 |
|
| 6 |
struct rational_test_param { |
| 7 |
unsigned long num, den; |
| 8 |
unsigned long max_num, max_den; |
| 9 |
unsigned long exp_num, exp_den; |
| 10 |
}; |
| 11 |
|
| 12 |
static const struct rational_test_param test_params[] = { |
| 13 |
|
| 14 |
{ 1230, 10, 100, 20, 100, 1}, |
| 15 |
|
| 16 |
{ 34567, 100, 120, 20, 120, 1}, |
| 17 |
|
| 18 |
{ 1, 30, 100, 10, 0, 1}, |
| 19 |
|
| 20 |
{ 1, 19, 100, 10, 1, 10}, |
| 21 |
|
| 22 |
{ 1155, 7735, 255, 255, 33, 221}, |
| 23 |
|
| 24 |
{ 27, 32, 16, 16, 11, 13}, |
| 25 |
|
| 26 |
{ 67, 54, 17, 18, 5, 4}, |
| 27 |
|
| 28 |
{ 453, 182, 60, 60, 57, 23}, |
| 29 |
|
| 30 |
{ 87, 32, 70, 32, 68, 25}, |
| 31 |
|
| 32 |
{ 14533, 4626, 15000, 2400, 7433, 2366}, |
| 33 |
}; |
| 34 |
|
| 35 |
static void test_rational(void **state) |
| 36 |
{ |
| 37 |
int i; |
| 38 |
unsigned long num = 0, den = 0; |
| 39 |
|
| 40 |
for (i = 0; i < ARRAY_SIZE(test_params); i++) { |
| 41 |
rational_best_approximation(test_params[i].num, test_params[i].den, |
| 42 |
test_params[i].max_num, test_params[i].max_den, |
| 43 |
&num, &den); |
| 44 |
assert_int_equal(num, test_params[i].exp_num); |
| 45 |
assert_int_equal(den, test_params[i].exp_den); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
int main(void) |
| 50 |
{ |
| 51 |
const struct CMUnitTest tests[] = { |
| 52 |
cmocka_unit_test(test_rational), |
| 53 |
}; |
| 54 |
|
| 55 |
return cb_run_group_tests(tests, NULL, NULL); |
| 56 |
} |
| 57 |
|