firmware/libfirmware/util/test/test_arrays.cpp
| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | #include <rusefi/arrays.h> | |||
| 2 | ||||
| 3 | #include <gtest/gtest.h> | |||
| 4 | #include <gmock/gmock.h> | |||
| 5 | ||||
| 6 | using testing::ElementsAre; | |||
| 7 | ||||
| 8 | 4 | TEST(Util_Arrays, CopyArray) { | ||
| 9 | 1 | int arr[2]; | ||
| 10 | ||||
| 11 | // Copy from an initializer list | |||
| 12 | 1 | copyArray(arr, { 10, 20 }); | ||
| 13 |
5/10✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 12 taken 1 time.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 time.
✗ Branch 23 not taken.
✗ Branch 28 not taken.
✗ Branch 31 not taken.
✓ Branch 38 taken 1 time.
✗ Branch 39 not taken.
|
1 | ASSERT_THAT(arr, ElementsAre(10, 20)); | |
| 14 | ||||
| 15 | // Copy from another array | |||
| 16 | 1 | int arr2[] = { 30, 40 }; | ||
| 17 | 1 | copyArray(arr, arr2); | ||
| 18 |
5/10✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 12 taken 1 time.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 time.
✗ Branch 23 not taken.
✗ Branch 28 not taken.
✗ Branch 31 not taken.
✓ Branch 38 taken 1 time.
✗ Branch 39 not taken.
|
1 | ASSERT_THAT(arr, ElementsAre(30, 40)); | |
| 19 | ||||
| 20 | // Copy to a scaled channel | |||
| 21 | 1 | scaled_channel<int, 10, 1> arr3[2]; | ||
| 22 |
1/1✓ Branch 2 taken 1 time.
|
1 | copyArray(arr3, { 100, 200 }); | |
| 23 |
5/10✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 12 taken 1 time.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 time.
✗ Branch 23 not taken.
✗ Branch 28 not taken.
✗ Branch 31 not taken.
✓ Branch 38 taken 1 time.
✗ Branch 39 not taken.
|
1 | ASSERT_THAT(arr3, ElementsAre(100, 200)); | |
| 24 | } | |||
| 25 | ||||
| 26 | 4 | TEST(Util_Arrays, CopyArrayPartial) { | ||
| 27 | 1 | int arr[4]; | ||
| 28 | 1 | copyArray(arr, { 1, 2, 3, 4 }); | ||
| 29 |
5/10✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
✓ Branch 14 taken 1 time.
✗ Branch 23 not taken.
✓ Branch 24 taken 1 time.
✗ Branch 27 not taken.
✗ Branch 32 not taken.
✗ Branch 35 not taken.
✓ Branch 42 taken 1 time.
✗ Branch 43 not taken.
|
1 | ASSERT_THAT(arr, ElementsAre(1, 2, 3, 4)); | |
| 30 | ||||
| 31 | // Copy a smaller array over the beginning of arr | |||
| 32 | 1 | copyArrayPartial(arr, { 100, 200 }); | ||
| 33 |
5/10✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
✓ Branch 14 taken 1 time.
✗ Branch 23 not taken.
✓ Branch 24 taken 1 time.
✗ Branch 27 not taken.
✗ Branch 32 not taken.
✗ Branch 35 not taken.
✓ Branch 42 taken 1 time.
✗ Branch 43 not taken.
|
1 | ASSERT_THAT(arr, ElementsAre(100, 200, 3, 4)); | |
| 34 | } | |||
| 35 | ||||
| 36 | 4 | TEST(Util_Arrays, Size) { | ||
| 37 | 1 | int arr1[2]; | ||
| 38 | 1 | uint8_t arr2[17]; | ||
| 39 | 1 | scaled_channel<uint16_t, 3, 1> arr3[21]; | ||
| 40 | ||||
| 41 |
3/8✓ Branch 5 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✓ Branch 29 taken 1 time.
✗ Branch 30 not taken.
|
1 | ASSERT_EQ(2u, efi::size(arr1)); | |
| 42 |
3/8✓ Branch 5 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✓ Branch 29 taken 1 time.
✗ Branch 30 not taken.
|
1 | ASSERT_EQ(17u, efi::size(arr2)); | |
| 43 |
3/8✓ Branch 5 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✓ Branch 29 taken 1 time.
✗ Branch 30 not taken.
|
1 | ASSERT_EQ(21u, efi::size(arr3)); | |
| 44 | } | |||
| 45 | ||||
| 46 | #define ARRAY_INTERPOLATION_ERROR 0.01f | |||
| 47 | ||||
| 48 | 4 | TEST(Util_Arrays, copyArrayInterpolated){ | ||
| 49 | // direct copy | |||
| 50 | { | |||
| 51 | 1 | float src[] = { 1.0f, 2.0f, 3.0f }; | ||
| 52 | 1 | float dest[3]; | ||
| 53 | 1 | copyArrayInterpolated(dest, src); | ||
| 54 | 1 | ASSERT_THAT(dest, ElementsAre(1.0f, 2.0f, 3.0f)); | ||
| 55 | } | |||
| 56 | ||||
| 57 | // Test upscaling with interpolation (2 -> 5) | |||
| 58 | { | |||
| 59 | 1 | float src[] = { 0.0f, 10.0f }; | ||
| 60 | 1 | float dest[5]; | ||
| 61 | 1 | copyArrayInterpolated(dest, src); | ||
| 62 | 1 | ASSERT_NEAR(dest[0], 0.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 63 | 1 | ASSERT_NEAR(dest[1], 2.5f, ARRAY_INTERPOLATION_ERROR); | ||
| 64 | 1 | ASSERT_NEAR(dest[2], 5.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 65 | 1 | ASSERT_NEAR(dest[3], 7.5f, ARRAY_INTERPOLATION_ERROR); | ||
| 66 | 1 | ASSERT_NEAR(dest[4], 10.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 67 | } | |||
| 68 | ||||
| 69 | // Test upscaling with interpolation (3 -> 7) | |||
| 70 | { | |||
| 71 | 1 | float src[] = { 0.0f, 6.0f, 12.0f }; | ||
| 72 | 1 | float dest[7]; | ||
| 73 | 1 | copyArrayInterpolated(dest, src); | ||
| 74 | 1 | ASSERT_NEAR(dest[0], 0.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 75 | 1 | ASSERT_NEAR(dest[1], 2.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 76 | 1 | ASSERT_NEAR(dest[2], 4.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 77 | 1 | ASSERT_NEAR(dest[3], 6.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 78 | 1 | ASSERT_NEAR(dest[4], 8.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 79 | 1 | ASSERT_NEAR(dest[5], 10.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 80 | 1 | ASSERT_NEAR(dest[6], 12.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 81 | } | |||
| 82 | ||||
| 83 | // Test downscaling (5 -> 3) | |||
| 84 | { | |||
| 85 | 1 | float src[] = { 0.0f, 2.5f, 5.0f, 7.5f, 10.0f }; | ||
| 86 | 1 | float dest[3]; | ||
| 87 | 1 | copyArrayInterpolated(dest, src); | ||
| 88 | 1 | ASSERT_THAT(dest, ElementsAre(0.0f, 5.0f, 10.0f)); | ||
| 89 | } | |||
| 90 | ||||
| 91 | // Test rounding with default (2 digits) | |||
| 92 | { | |||
| 93 | 1 | float src[] = { 0.0f, 1.0f }; | ||
| 94 | 1 | float dest[3]; | ||
| 95 | 1 | copyArrayInterpolated(dest, src); | ||
| 96 | // Middle value should be 0.5, rounded to 2 digits | |||
| 97 | 1 | EXPECT_FLOAT_EQ(dest[0], 0.0f); | ||
| 98 | 1 | EXPECT_FLOAT_EQ(dest[1], 0.5f); | ||
| 99 | 1 | EXPECT_FLOAT_EQ(dest[2], 1.0f); | ||
| 100 | } | |||
| 101 | ||||
| 102 | // Test rounding with 1 digit | |||
| 103 | { | |||
| 104 | 1 | float src[] = { 0.0f, 1.0f }; | ||
| 105 | 1 | float dest[4]; | ||
| 106 | 1 | copyArrayInterpolated<float, float, 4, 2, 1>(dest, src); | ||
| 107 | 1 | EXPECT_FLOAT_EQ(dest[0], 0.0f); | ||
| 108 | 1 | EXPECT_NEAR(dest[1], 0.3f, 0.05f); | ||
| 109 | 1 | EXPECT_NEAR(dest[2], 0.7f, 0.05f); | ||
| 110 | 1 | EXPECT_FLOAT_EQ(dest[3], 1.0f); | ||
| 111 | } | |||
| 112 | ||||
| 113 | // Test with integer types | |||
| 114 | { | |||
| 115 | 1 | int src[] = { 0, 100 }; | ||
| 116 | 1 | int dest[5]; | ||
| 117 | 1 | copyArrayInterpolated(dest, src); | ||
| 118 | 1 | ASSERT_THAT(dest, ElementsAre(0, 25, 50, 75, 100)); | ||
| 119 | } | |||
| 120 | ||||
| 121 | // Test different src/dest types | |||
| 122 | { | |||
| 123 | 1 | int src[] = { 0, 10, 20 }; | ||
| 124 | 1 | float dest[5]; | ||
| 125 | 1 | copyArrayInterpolated(dest, src); | ||
| 126 | 1 | ASSERT_NEAR(dest[0], 0.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 127 | 1 | ASSERT_NEAR(dest[1], 5.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 128 | 1 | ASSERT_NEAR(dest[2], 10.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 129 | 1 | ASSERT_NEAR(dest[3], 15.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 130 | 1 | ASSERT_NEAR(dest[4], 20.0f, ARRAY_INTERPOLATION_ERROR); | ||
| 131 | } | |||
| 132 | } | |||
| 133 |