|
13 | 13 | * @{
|
14 | 14 | */
|
15 | 15 |
|
| 16 | +ZTEST(sys_util, test_SCALE) |
| 17 | +{ |
| 18 | + /* Test for a few arbitrary values */ |
| 19 | + zassert_equal(SCALE(3, 0, 10, 0, 100), 30); |
| 20 | + zassert_equal(SCALE(-3, -10, 0, -100, 0), -30); |
| 21 | + zassert_equal(SCALE(10, -100, 100, -10, 10), 1); |
| 22 | + zassert_equal(SCALE(0, -10, 40, -50, 0), -40); |
| 23 | + zassert_equal(SCALE(0, -128, 127, 0, 2), 1); |
| 24 | + zassert_equal(SCALE(5, -50, 5000, -1000, 10), -989); |
| 25 | + |
| 26 | + /* Test for i = 1..60 and o = 60..1 */ |
| 27 | + for (int i = 1; i < 61; i++) { |
| 28 | + int o = 61 - i; |
| 29 | + int64_t imin = -(1ll << i); |
| 30 | + int64_t imax = +(1ll << i); |
| 31 | + int64_t omin = -(1ll << o); |
| 32 | + int64_t omax = +(1ll << o); |
| 33 | + |
| 34 | + /* Special case: the output range can be [0, 0] */ |
| 35 | + |
| 36 | + zassert_equal(SCALE(imin, imin, imax, 0, 0), 0); |
| 37 | + zassert_equal(SCALE(0, imin, imax, 0, 0), 0); |
| 38 | + zassert_equal(SCALE(imax, imin, imax, 0, 0), 0); |
| 39 | + |
| 40 | + zassert_equal(SCALE(0, 0, imax, 0, 0), 0); |
| 41 | + zassert_equal(SCALE(imax, 0, imax, 0, 0), 0); |
| 42 | + |
| 43 | + zassert_equal(SCALE(imin, imin, 0, 0, 0), 0); |
| 44 | + zassert_equal(SCALE(0, imin, 0, 0, 0), 0); |
| 45 | + |
| 46 | + /* Test the extreme of all known cases */ |
| 47 | + |
| 48 | + zassert_equal(SCALE(imin, imin, imax, omin, omax), omin); |
| 49 | + zassert_equal(SCALE(0, imin, imax, omin, omax), 0); |
| 50 | + zassert_equal(SCALE(imax, imin, imax, omin, omax), omax); |
| 51 | + |
| 52 | + zassert_equal(SCALE(0, 0, imax, omin, omax), omin); |
| 53 | + zassert_equal(SCALE(imax, 0, imax, omin, omax), omax); |
| 54 | + |
| 55 | + zassert_equal(SCALE(imin, imin, 0, omin, omax), omin); |
| 56 | + zassert_equal(SCALE(0, imin, 0, omin, omax), omax); |
| 57 | + |
| 58 | + zassert_equal(SCALE(imin, imin, imax, 0, omax), 0); |
| 59 | + zassert_equal(SCALE(0, imin, imax, 0, omax), omax / 2); |
| 60 | + zassert_equal(SCALE(imax, imin, imax, 0, omax), omax); |
| 61 | + |
| 62 | + zassert_equal(SCALE(imin, imin, imax, omin, 0), omin); |
| 63 | + zassert_equal(SCALE(0, imin, imax, omin, 0), omin / 2); |
| 64 | + zassert_equal(SCALE(imax, imin, imax, omin, 0), 0); |
| 65 | + |
| 66 | + zassert_equal(SCALE(0, 0, imax, 0, omax), 0); |
| 67 | + zassert_equal(SCALE(imax, 0, imax, 0, omax), omax); |
| 68 | + |
| 69 | + zassert_equal(SCALE(0, 0, imax, omin, 0), omin); |
| 70 | + zassert_equal(SCALE(imax, 0, imax, omin, 0), 0); |
| 71 | + |
| 72 | + zassert_equal(SCALE(imin, imin, 0, 0, omax), 0); |
| 73 | + zassert_equal(SCALE(0, imin, 0, 0, omax), omax); |
| 74 | + |
| 75 | + zassert_equal(SCALE(imin, imin, 0, omin, 0), omin); |
| 76 | + zassert_equal(SCALE(0, imin, 0, omin, 0), 0); |
| 77 | + |
| 78 | + zassert_equal(SCALE(0, 0, imax, 0, omax), 0); |
| 79 | + zassert_equal(SCALE(imax, 0, imax, 0, omax), omax); |
| 80 | + } |
| 81 | +} |
| 82 | + |
16 | 83 | /**
|
17 | 84 | * @brief Test wait_for works as expected with typical use cases
|
18 | 85 | *
|
@@ -71,7 +138,6 @@ ZTEST(sys_util, test_NUM_VA_ARGS_LESS_1)
|
71 | 138 | * @}
|
72 | 139 | */
|
73 | 140 |
|
74 |
| - |
75 | 141 | /**
|
76 | 142 | * @defgroup sys_util_tests Sys Util Tests
|
77 | 143 | * @ingroup all_tests
|
|
0 commit comments