|
| 1 | +#include "mbed.h" |
| 2 | +#include "greentea-client/test_env.h" |
| 3 | +#include "unity.h" |
| 4 | +#include "utest.h" |
| 5 | + |
| 6 | +using namespace utest::v1; |
| 7 | + |
| 8 | + |
| 9 | +// static functions |
| 10 | +template <typename T> |
| 11 | +T static_func5(T a0, T a1, T a2, T a3, T a4) { return a0 | a1 | a2 | a3 | a4; } |
| 12 | +template <typename T> |
| 13 | +T static_func4(T a0, T a1, T a2, T a3) { return a0 | a1 | a2 | a3; } |
| 14 | +template <typename T> |
| 15 | +T static_func3(T a0, T a1, T a2) { return a0 | a1 | a2; } |
| 16 | +template <typename T> |
| 17 | +T static_func2(T a0, T a1) { return a0 | a1; } |
| 18 | +template <typename T> |
| 19 | +T static_func1(T a0) { return a0; } |
| 20 | +template <typename T> |
| 21 | +T static_func0() { return 0; } |
| 22 | + |
| 23 | +// class functions |
| 24 | +template <typename T> |
| 25 | +struct Thing { |
| 26 | + T t; |
| 27 | + Thing() : t(0x80) {} |
| 28 | + |
| 29 | + T member_func5(T a0, T a1, T a2, T a3, T a4) { return t | a0 | a1 | a2 | a3 | a4; } |
| 30 | + T member_func4(T a0, T a1, T a2, T a3) { return t | a0 | a1 | a2 | a3; } |
| 31 | + T member_func3(T a0, T a1, T a2) { return t | a0 | a1 | a2; } |
| 32 | + T member_func2(T a0, T a1) { return t | a0 | a1; } |
| 33 | + T member_func1(T a0) { return t | a0; } |
| 34 | + T member_func0() { return t; } |
| 35 | +}; |
| 36 | + |
| 37 | +// bound functions |
| 38 | +template <typename T> |
| 39 | +T bound_func5(Thing<T> *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } |
| 40 | +template <typename T> |
| 41 | +T bound_func4(Thing<T> *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } |
| 42 | +template <typename T> |
| 43 | +T bound_func3(Thing<T> *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } |
| 44 | +template <typename T> |
| 45 | +T bound_func2(Thing<T> *t, T a0, T a1) { return t->t | a0 | a1; } |
| 46 | +template <typename T> |
| 47 | +T bound_func1(Thing<T> *t, T a0) { return t->t | a0; } |
| 48 | +template <typename T> |
| 49 | +T bound_func0(Thing<T> *t) { return t->t; } |
| 50 | + |
| 51 | + |
| 52 | +// function call and result verification |
| 53 | +template <typename T> |
| 54 | +struct Verifier { |
| 55 | + static void verify5(Callback<T(T,T,T,T,T)> func) { |
| 56 | + T result = func(0x01, 0x02, 0x04, 0x08, 0x10); |
| 57 | + TEST_ASSERT_EQUAL(result, 0x1f); |
| 58 | + } |
| 59 | + |
| 60 | + template <typename O, typename M> |
| 61 | + static void verify5(O *obj, M method) { |
| 62 | + Callback<T(T,T,T,T,T)> func(obj, method); |
| 63 | + T result = func(0x01, 0x02, 0x04, 0x08, 0x10); |
| 64 | + TEST_ASSERT_EQUAL(result, 0x9f); |
| 65 | + } |
| 66 | + |
| 67 | + static void verify4(Callback<T(T,T,T,T)> func) { |
| 68 | + T result = func(0x01, 0x02, 0x04, 0x08); |
| 69 | + TEST_ASSERT_EQUAL(result, 0x0f); |
| 70 | + } |
| 71 | + |
| 72 | + template <typename O, typename M> |
| 73 | + static void verify4(O *obj, M method) { |
| 74 | + Callback<T(T,T,T,T)> func(obj, method); |
| 75 | + T result = func(0x01, 0x02, 0x04, 0x08); |
| 76 | + TEST_ASSERT_EQUAL(result, 0x8f); |
| 77 | + } |
| 78 | + |
| 79 | + static void verify3(Callback<T(T,T,T)> func) { |
| 80 | + T result = func(0x01, 0x02, 0x04); |
| 81 | + TEST_ASSERT_EQUAL(result, 0x07); |
| 82 | + } |
| 83 | + |
| 84 | + template <typename O, typename M> |
| 85 | + static void verify3(O *obj, M method) { |
| 86 | + Callback<T(T,T,T)> func(obj, method); |
| 87 | + T result = func(0x01, 0x02, 0x04); |
| 88 | + TEST_ASSERT_EQUAL(result, 0x87); |
| 89 | + } |
| 90 | + |
| 91 | + static void verify2(Callback<T(T,T)> func) { |
| 92 | + T result = func(0x01, 0x02); |
| 93 | + TEST_ASSERT_EQUAL(result, 0x03); |
| 94 | + } |
| 95 | + |
| 96 | + template <typename O, typename M> |
| 97 | + static void verify2(O *obj, M method) { |
| 98 | + Callback<T(T,T)> func(obj, method); |
| 99 | + T result = func(0x01, 0x02); |
| 100 | + TEST_ASSERT_EQUAL(result, 0x83); |
| 101 | + } |
| 102 | + |
| 103 | + static void verify1(Callback<T(T)> func) { |
| 104 | + T result = func(0x01); |
| 105 | + TEST_ASSERT_EQUAL(result, 0x01); |
| 106 | + } |
| 107 | + |
| 108 | + template <typename O, typename M> |
| 109 | + static void verify1(O *obj, M method) { |
| 110 | + Callback<T(T)> func(obj, method); |
| 111 | + T result = func(0x01); |
| 112 | + TEST_ASSERT_EQUAL(result, 0x81); |
| 113 | + } |
| 114 | + |
| 115 | + static void verify0(Callback<T()> func) { |
| 116 | + T result = func(); |
| 117 | + TEST_ASSERT_EQUAL(result, 0x00); |
| 118 | + } |
| 119 | + |
| 120 | + template <typename O, typename M> |
| 121 | + static void verify0(O *obj, M method) { |
| 122 | + Callback<T()> func(obj, method); |
| 123 | + T result = func(); |
| 124 | + TEST_ASSERT_EQUAL(result, 0x80); |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | + |
| 129 | +// test dispatch |
| 130 | +template <typename T> |
| 131 | +void test_dispatch5() { |
| 132 | + Thing<T> thing; |
| 133 | + Verifier<T>::verify5(static_func5<T>); |
| 134 | + Verifier<T>::verify5(&thing, &Thing<T>::member_func5); |
| 135 | + Verifier<T>::verify5(&thing, &bound_func5<T>); |
| 136 | + |
| 137 | + Callback<T(T,T,T,T,T)> callback(static_func5); |
| 138 | + Verifier<T>::verify5(callback); |
| 139 | + callback.attach(&thing, &bound_func5<T>); |
| 140 | + Verifier<T>::verify5(&callback, &Callback<T(T,T,T,T,T)>::call); |
| 141 | + Verifier<T>::verify5((void*)&callback, &Callback<T(T,T,T,T,T)>::thunk); |
| 142 | +} |
| 143 | + |
| 144 | +template <typename T> |
| 145 | +void test_dispatch4() { |
| 146 | + Thing<T> thing; |
| 147 | + Verifier<T>::verify4(static_func4<T>); |
| 148 | + Verifier<T>::verify4(&thing, &Thing<T>::member_func4); |
| 149 | + Verifier<T>::verify4(&thing, &bound_func4<T>); |
| 150 | + |
| 151 | + Callback<T(T,T,T,T)> callback(static_func4); |
| 152 | + Verifier<T>::verify4(callback); |
| 153 | + callback.attach(&thing, &bound_func4<T>); |
| 154 | + Verifier<T>::verify4(&callback, &Callback<T(T,T,T,T)>::call); |
| 155 | + Verifier<T>::verify4((void*)&callback, &Callback<T(T,T,T,T)>::thunk); |
| 156 | +} |
| 157 | + |
| 158 | +template <typename T> |
| 159 | +void test_dispatch3() { |
| 160 | + Thing<T> thing; |
| 161 | + Verifier<T>::verify3(static_func3<T>); |
| 162 | + Verifier<T>::verify3(&thing, &Thing<T>::member_func3); |
| 163 | + Verifier<T>::verify3(&thing, &bound_func3<T>); |
| 164 | + |
| 165 | + Callback<T(T,T,T)> callback(static_func3); |
| 166 | + Verifier<T>::verify3(callback); |
| 167 | + callback.attach(&thing, &bound_func3<T>); |
| 168 | + Verifier<T>::verify3(&callback, &Callback<T(T,T,T)>::call); |
| 169 | + Verifier<T>::verify3((void*)&callback, &Callback<T(T,T,T)>::thunk); |
| 170 | +} |
| 171 | + |
| 172 | +template <typename T> |
| 173 | +void test_dispatch2() { |
| 174 | + Thing<T> thing; |
| 175 | + Verifier<T>::verify2(static_func2<T>); |
| 176 | + Verifier<T>::verify2(&thing, &Thing<T>::member_func2); |
| 177 | + Verifier<T>::verify2(&thing, &bound_func2<T>); |
| 178 | + |
| 179 | + Callback<T(T,T)> callback(static_func2); |
| 180 | + Verifier<T>::verify2(callback); |
| 181 | + callback.attach(&thing, &bound_func2<T>); |
| 182 | + Verifier<T>::verify2(&callback, &Callback<T(T,T)>::call); |
| 183 | + Verifier<T>::verify2((void*)&callback, &Callback<T(T,T)>::thunk); |
| 184 | +} |
| 185 | + |
| 186 | +template <typename T> |
| 187 | +void test_dispatch1() { |
| 188 | + Thing<T> thing; |
| 189 | + Verifier<T>::verify1(static_func1<T>); |
| 190 | + Verifier<T>::verify1(&thing, &Thing<T>::member_func1); |
| 191 | + Verifier<T>::verify1(&thing, &bound_func1<T>); |
| 192 | + |
| 193 | + Callback<T(T)> callback(static_func1); |
| 194 | + Verifier<T>::verify1(callback); |
| 195 | + callback.attach(&thing, &bound_func1<T>); |
| 196 | + Verifier<T>::verify1(&callback, &Callback<T(T)>::call); |
| 197 | + Verifier<T>::verify1((void*)&callback, &Callback<T(T)>::thunk); |
| 198 | +} |
| 199 | + |
| 200 | +template <typename T> |
| 201 | +void test_dispatch0() { |
| 202 | + Thing<T> thing; |
| 203 | + Verifier<T>::verify0(static_func0<T>); |
| 204 | + Verifier<T>::verify0(&thing, &Thing<T>::member_func0); |
| 205 | + Verifier<T>::verify0(&thing, &bound_func0<T>); |
| 206 | + |
| 207 | + Callback<T()> callback(static_func0); |
| 208 | + Verifier<T>::verify0(callback); |
| 209 | + callback.attach(&thing, &bound_func0<T>); |
| 210 | + Verifier<T>::verify0(&callback, &Callback<T()>::call); |
| 211 | + Verifier<T>::verify0((void*)&callback, &Callback<T()>::thunk); |
| 212 | +} |
| 213 | + |
| 214 | +template <typename T> |
| 215 | +void test_fparg1() { |
| 216 | + Thing<T> thing; |
| 217 | + FunctionPointerArg1<T,T> fp(static_func1<T>); |
| 218 | + Verifier<T>::verify1(fp); |
| 219 | + Verifier<T>::verify1(fp.get_function()); |
| 220 | +} |
| 221 | + |
| 222 | +template <typename T> |
| 223 | +void test_fparg0() { |
| 224 | + Thing<T> thing; |
| 225 | + FunctionPointerArg1<T,void> fp(static_func0<T>); |
| 226 | + Verifier<T>::verify0(fp); |
| 227 | + Verifier<T>::verify0(fp.get_function()); |
| 228 | +} |
| 229 | + |
| 230 | + |
| 231 | +// Test setup |
| 232 | +utest::v1::status_t test_setup(const size_t number_of_cases) { |
| 233 | + GREENTEA_SETUP(5, "default_auto"); |
| 234 | + return verbose_test_setup_handler(number_of_cases); |
| 235 | +} |
| 236 | + |
| 237 | +Case cases[] = { |
| 238 | + Case("Testing callbacks with 5 ints", test_dispatch5<int>), |
| 239 | + Case("Testing callbacks with 4 ints", test_dispatch4<int>), |
| 240 | + Case("Testing callbacks with 3 ints", test_dispatch3<int>), |
| 241 | + Case("Testing callbacks with 2 ints", test_dispatch2<int>), |
| 242 | + Case("Testing callbacks with 1 ints", test_dispatch1<int>), |
| 243 | + Case("Testing callbacks with 0 ints", test_dispatch0<int>), |
| 244 | + |
| 245 | + Case("Testing callbacks with 5 uchars", test_dispatch5<unsigned char>), |
| 246 | + Case("Testing callbacks with 4 uchars", test_dispatch4<unsigned char>), |
| 247 | + Case("Testing callbacks with 3 uchars", test_dispatch3<unsigned char>), |
| 248 | + Case("Testing callbacks with 2 uchars", test_dispatch2<unsigned char>), |
| 249 | + Case("Testing callbacks with 1 uchars", test_dispatch1<unsigned char>), |
| 250 | + Case("Testing callbacks with 0 uchars", test_dispatch0<unsigned char>), |
| 251 | + |
| 252 | + Case("Testing callbacks with 5 uint64s", test_dispatch5<uint64_t>), |
| 253 | + Case("Testing callbacks with 4 uint64s", test_dispatch4<uint64_t>), |
| 254 | + Case("Testing callbacks with 3 uint64s", test_dispatch3<uint64_t>), |
| 255 | + Case("Testing callbacks with 2 uint64s", test_dispatch2<uint64_t>), |
| 256 | + Case("Testing callbacks with 1 uint64s", test_dispatch1<uint64_t>), |
| 257 | + Case("Testing callbacks with 0 uint64s", test_dispatch0<uint64_t>), |
| 258 | + |
| 259 | + Case("Testing FunctionPointerArg1 compatibility", test_fparg1<int>), |
| 260 | + Case("Testing FunctionPointer compatibility", test_fparg0<int>), |
| 261 | +}; |
| 262 | + |
| 263 | +Specification specification(test_setup, cases); |
| 264 | + |
| 265 | +int main() { |
| 266 | + return !Harness::run(specification); |
| 267 | +} |
0 commit comments