ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "greentea-client/test_env.h"
00003 #include "unity.h"
00004 #include "utest.h"
00005 
00006 using namespace utest::v1;
00007 
00008 
00009 // static functions
00010 template <typename T>
00011 T static_func0() { return 0; }
00012 template <typename T>
00013 T static_func1(T a0) { return 0 | a0; }
00014 template <typename T>
00015 T static_func2(T a0, T a1) { return 0 | a0 | a1; }
00016 template <typename T>
00017 T static_func3(T a0, T a1, T a2) { return 0 | a0 | a1 | a2; }
00018 template <typename T>
00019 T static_func4(T a0, T a1, T a2, T a3) { return 0 | a0 | a1 | a2 | a3; }
00020 template <typename T>
00021 T static_func5(T a0, T a1, T a2, T a3, T a4) { return 0 | a0 | a1 | a2 | a3 | a4; }
00022 
00023 // class functions
00024 template <typename T>
00025 struct Thing {
00026     T t;
00027     Thing() : t(0x80) {}
00028 
00029     T member_func0() { return t; }
00030     T member_func1(T a0) { return t | a0; }
00031     T member_func2(T a0, T a1) { return t | a0 | a1; }
00032     T member_func3(T a0, T a1, T a2) { return t | a0 | a1 | a2; }
00033     T member_func4(T a0, T a1, T a2, T a3) { return t | a0 | a1 | a2 | a3; }
00034     T member_func5(T a0, T a1, T a2, T a3, T a4) { return t | a0 | a1 | a2 | a3 | a4; }
00035 
00036     T const_member_func0() const { return t; }
00037     T const_member_func1(T a0) const { return t | a0; }
00038     T const_member_func2(T a0, T a1) const { return t | a0 | a1; }
00039     T const_member_func3(T a0, T a1, T a2) const { return t | a0 | a1 | a2; }
00040     T const_member_func4(T a0, T a1, T a2, T a3) const { return t | a0 | a1 | a2 | a3; }
00041     T const_member_func5(T a0, T a1, T a2, T a3, T a4) const { return t | a0 | a1 | a2 | a3 | a4; }
00042 
00043     T volatile_member_func0() volatile { return t; }
00044     T volatile_member_func1(T a0) volatile { return t | a0; }
00045     T volatile_member_func2(T a0, T a1) volatile { return t | a0 | a1; }
00046     T volatile_member_func3(T a0, T a1, T a2) volatile { return t | a0 | a1 | a2; }
00047     T volatile_member_func4(T a0, T a1, T a2, T a3) volatile { return t | a0 | a1 | a2 | a3; }
00048     T volatile_member_func5(T a0, T a1, T a2, T a3, T a4) volatile { return t | a0 | a1 | a2 | a3 | a4; }
00049 
00050     T const_volatile_member_func0() const volatile { return t; }
00051     T const_volatile_member_func1(T a0) const volatile { return t | a0; }
00052     T const_volatile_member_func2(T a0, T a1) const volatile { return t | a0 | a1; }
00053     T const_volatile_member_func3(T a0, T a1, T a2) const volatile { return t | a0 | a1 | a2; }
00054     T const_volatile_member_func4(T a0, T a1, T a2, T a3) const volatile { return t | a0 | a1 | a2 | a3; }
00055     T const_volatile_member_func5(T a0, T a1, T a2, T a3, T a4) const volatile { return t | a0 | a1 | a2 | a3 | a4; }
00056 };
00057 
00058 // bound functions
00059 template <typename T>
00060 T bound_func0(Thing<T> *t) { return t->t; }
00061 template <typename T>
00062 T bound_func1(Thing<T> *t, T a0) { return t->t | a0; }
00063 template <typename T>
00064 T bound_func2(Thing<T> *t, T a0, T a1) { return t->t | a0 | a1; }
00065 template <typename T>
00066 T bound_func3(Thing<T> *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; }
00067 template <typename T>
00068 T bound_func4(Thing<T> *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; }
00069 template <typename T>
00070 T bound_func5(Thing<T> *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; }
00071 
00072 // const bound functions
00073 template <typename T>
00074 T const_func0(const Thing<T> *t) { return t->t; }
00075 template <typename T>
00076 T const_func1(const Thing<T> *t, T a0) { return t->t | a0; }
00077 template <typename T>
00078 T const_func2(const Thing<T> *t, T a0, T a1) { return t->t | a0 | a1; }
00079 template <typename T>
00080 T const_func3(const Thing<T> *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; }
00081 template <typename T>
00082 T const_func4(const Thing<T> *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; }
00083 template <typename T>
00084 T const_func5(const Thing<T> *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; }
00085 
00086 // volatile bound functions
00087 template <typename T>
00088 T volatile_func0(volatile Thing<T> *t) { return t->t; }
00089 template <typename T>
00090 T volatile_func1(volatile Thing<T> *t, T a0) { return t->t | a0; }
00091 template <typename T>
00092 T volatile_func2(volatile Thing<T> *t, T a0, T a1) { return t->t | a0 | a1; }
00093 template <typename T>
00094 T volatile_func3(volatile Thing<T> *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; }
00095 template <typename T>
00096 T volatile_func4(volatile Thing<T> *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; }
00097 template <typename T>
00098 T volatile_func5(volatile Thing<T> *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; }
00099 
00100 // const volatile bound functions
00101 template <typename T>
00102 T const_volatile_func0(const volatile Thing<T> *t) { return t->t; }
00103 template <typename T>
00104 T const_volatile_func1(const volatile Thing<T> *t, T a0) { return t->t | a0; }
00105 template <typename T>
00106 T const_volatile_func2(const volatile Thing<T> *t, T a0, T a1) { return t->t | a0 | a1; }
00107 template <typename T>
00108 T const_volatile_func3(const volatile Thing<T> *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; }
00109 template <typename T>
00110 T const_volatile_func4(const volatile Thing<T> *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; }
00111 template <typename T>
00112 T const_volatile_func5(const volatile Thing<T> *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; }
00113 
00114 
00115 // function call and result verification
00116 template <typename T>
00117 struct Verifier {
00118     static void verify0(Callback<T()> func) {
00119         T result = func();
00120         TEST_ASSERT_EQUAL(result, 0x00);
00121     }
00122 
00123     template <typename O, typename M>
00124     static void verify0(O *obj, M method) {
00125         Callback<T()> func(obj, method);
00126         T result = func();
00127         TEST_ASSERT_EQUAL(result, 0x80);
00128     }
00129 
00130     static void verify1(Callback<T(T)> func) {
00131         T result = func((1 << 0));
00132         TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0));
00133     }
00134 
00135     template <typename O, typename M>
00136     static void verify1(O *obj, M method) {
00137         Callback<T(T)> func(obj, method);
00138         T result = func((1 << 0));
00139         TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0));
00140     }
00141 
00142     static void verify2(Callback<T(T, T)> func) {
00143         T result = func((1 << 0), (1 << 1));
00144         TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1));
00145     }
00146 
00147     template <typename O, typename M>
00148     static void verify2(O *obj, M method) {
00149         Callback<T(T, T)> func(obj, method);
00150         T result = func((1 << 0), (1 << 1));
00151         TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1));
00152     }
00153 
00154     static void verify3(Callback<T(T, T, T)> func) {
00155         T result = func((1 << 0), (1 << 1), (1 << 2));
00156         TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2));
00157     }
00158 
00159     template <typename O, typename M>
00160     static void verify3(O *obj, M method) {
00161         Callback<T(T, T, T)> func(obj, method);
00162         T result = func((1 << 0), (1 << 1), (1 << 2));
00163         TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2));
00164     }
00165 
00166     static void verify4(Callback<T(T, T, T, T)> func) {
00167         T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3));
00168         TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3));
00169     }
00170 
00171     template <typename O, typename M>
00172     static void verify4(O *obj, M method) {
00173         Callback<T(T, T, T, T)> func(obj, method);
00174         T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3));
00175         TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3));
00176     }
00177 
00178     static void verify5(Callback<T(T, T, T, T, T)> func) {
00179         T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4));
00180         TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4));
00181     }
00182 
00183     template <typename O, typename M>
00184     static void verify5(O *obj, M method) {
00185         Callback<T(T, T, T, T, T)> func(obj, method);
00186         T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4));
00187         TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4));
00188     }
00189 };
00190 
00191 
00192 // test dispatch
00193 template <typename T>
00194 void test_dispatch0() {
00195     Thing<T> thing;
00196     Verifier<T>::verify0(static_func0<T>);
00197     Verifier<T>::verify0(&thing, &Thing<T>::member_func0);
00198     Verifier<T>::verify0((const Thing<T>*)&thing, &Thing<T>::const_member_func0);
00199     Verifier<T>::verify0((volatile Thing<T>*)&thing, &Thing<T>::volatile_member_func0);
00200     Verifier<T>::verify0((const volatile Thing<T>*)&thing, &Thing<T>::const_volatile_member_func0);
00201     Verifier<T>::verify0(&bound_func0<T>, &thing);
00202     Verifier<T>::verify0(&const_func0<T>, (const Thing<T>*)&thing);
00203     Verifier<T>::verify0(&volatile_func0<T>, (volatile Thing<T>*)&thing);
00204     Verifier<T>::verify0(&const_volatile_func0<T>, (const volatile Thing<T>*)&thing);
00205     Verifier<T>::verify0(callback(static_func0<T>));
00206 
00207     Callback<T()> cb(static_func0);
00208     Verifier<T>::verify0(cb);
00209     cb = static_func0;
00210     Verifier<T>::verify0(cb);
00211     cb.attach(&bound_func0<T>, &thing);
00212     Verifier<T>::verify0(&cb, &Callback<T()>::call);
00213     Verifier<T>::verify0(&Callback<T()>::thunk, (void*)&cb);
00214 }
00215 
00216 template <typename T>
00217 void test_dispatch1() {
00218     Thing<T> thing;
00219     Verifier<T>::verify1(static_func1<T>);
00220     Verifier<T>::verify1(&thing, &Thing<T>::member_func1);
00221     Verifier<T>::verify1((const Thing<T>*)&thing, &Thing<T>::const_member_func1);
00222     Verifier<T>::verify1((volatile Thing<T>*)&thing, &Thing<T>::volatile_member_func1);
00223     Verifier<T>::verify1((const volatile Thing<T>*)&thing, &Thing<T>::const_volatile_member_func1);
00224     Verifier<T>::verify1(&bound_func1<T>, &thing);
00225     Verifier<T>::verify1(&const_func1<T>, (const Thing<T>*)&thing);
00226     Verifier<T>::verify1(&volatile_func1<T>, (volatile Thing<T>*)&thing);
00227     Verifier<T>::verify1(&const_volatile_func1<T>, (const volatile Thing<T>*)&thing);
00228     Verifier<T>::verify1(callback(static_func1<T>));
00229 
00230     Callback<T(T)> cb(static_func1);
00231     Verifier<T>::verify1(cb);
00232     cb = static_func1;
00233     Verifier<T>::verify1(cb);
00234     cb.attach(&bound_func1<T>, &thing);
00235     Verifier<T>::verify1(&cb, &Callback<T(T)>::call);
00236     Verifier<T>::verify1(&Callback<T(T)>::thunk, (void*)&cb);
00237 }
00238 
00239 template <typename T>
00240 void test_dispatch2() {
00241     Thing<T> thing;
00242     Verifier<T>::verify2(static_func2<T>);
00243     Verifier<T>::verify2(&thing, &Thing<T>::member_func2);
00244     Verifier<T>::verify2((const Thing<T>*)&thing, &Thing<T>::const_member_func2);
00245     Verifier<T>::verify2((volatile Thing<T>*)&thing, &Thing<T>::volatile_member_func2);
00246     Verifier<T>::verify2((const volatile Thing<T>*)&thing, &Thing<T>::const_volatile_member_func2);
00247     Verifier<T>::verify2(&bound_func2<T>, &thing);
00248     Verifier<T>::verify2(&const_func2<T>, (const Thing<T>*)&thing);
00249     Verifier<T>::verify2(&volatile_func2<T>, (volatile Thing<T>*)&thing);
00250     Verifier<T>::verify2(&const_volatile_func2<T>, (const volatile Thing<T>*)&thing);
00251     Verifier<T>::verify2(callback(static_func2<T>));
00252 
00253     Callback<T(T, T)> cb(static_func2);
00254     Verifier<T>::verify2(cb);
00255     cb = static_func2;
00256     Verifier<T>::verify2(cb);
00257     cb.attach(&bound_func2<T>, &thing);
00258     Verifier<T>::verify2(&cb, &Callback<T(T, T)>::call);
00259     Verifier<T>::verify2(&Callback<T(T, T)>::thunk, (void*)&cb);
00260 }
00261 
00262 template <typename T>
00263 void test_dispatch3() {
00264     Thing<T> thing;
00265     Verifier<T>::verify3(static_func3<T>);
00266     Verifier<T>::verify3(&thing, &Thing<T>::member_func3);
00267     Verifier<T>::verify3((const Thing<T>*)&thing, &Thing<T>::const_member_func3);
00268     Verifier<T>::verify3((volatile Thing<T>*)&thing, &Thing<T>::volatile_member_func3);
00269     Verifier<T>::verify3((const volatile Thing<T>*)&thing, &Thing<T>::const_volatile_member_func3);
00270     Verifier<T>::verify3(&bound_func3<T>, &thing);
00271     Verifier<T>::verify3(&const_func3<T>, (const Thing<T>*)&thing);
00272     Verifier<T>::verify3(&volatile_func3<T>, (volatile Thing<T>*)&thing);
00273     Verifier<T>::verify3(&const_volatile_func3<T>, (const volatile Thing<T>*)&thing);
00274     Verifier<T>::verify3(callback(static_func3<T>));
00275 
00276     Callback<T(T, T, T)> cb(static_func3);
00277     Verifier<T>::verify3(cb);
00278     cb = static_func3;
00279     Verifier<T>::verify3(cb);
00280     cb.attach(&bound_func3<T>, &thing);
00281     Verifier<T>::verify3(&cb, &Callback<T(T, T, T)>::call);
00282     Verifier<T>::verify3(&Callback<T(T, T, T)>::thunk, (void*)&cb);
00283 }
00284 
00285 template <typename T>
00286 void test_dispatch4() {
00287     Thing<T> thing;
00288     Verifier<T>::verify4(static_func4<T>);
00289     Verifier<T>::verify4(&thing, &Thing<T>::member_func4);
00290     Verifier<T>::verify4((const Thing<T>*)&thing, &Thing<T>::const_member_func4);
00291     Verifier<T>::verify4((volatile Thing<T>*)&thing, &Thing<T>::volatile_member_func4);
00292     Verifier<T>::verify4((const volatile Thing<T>*)&thing, &Thing<T>::const_volatile_member_func4);
00293     Verifier<T>::verify4(&bound_func4<T>, &thing);
00294     Verifier<T>::verify4(&const_func4<T>, (const Thing<T>*)&thing);
00295     Verifier<T>::verify4(&volatile_func4<T>, (volatile Thing<T>*)&thing);
00296     Verifier<T>::verify4(&const_volatile_func4<T>, (const volatile Thing<T>*)&thing);
00297     Verifier<T>::verify4(callback(static_func4<T>));
00298 
00299     Callback<T(T, T, T, T)> cb(static_func4);
00300     Verifier<T>::verify4(cb);
00301     cb = static_func4;
00302     Verifier<T>::verify4(cb);
00303     cb.attach(&bound_func4<T>, &thing);
00304     Verifier<T>::verify4(&cb, &Callback<T(T, T, T, T)>::call);
00305     Verifier<T>::verify4(&Callback<T(T, T, T, T)>::thunk, (void*)&cb);
00306 }
00307 
00308 template <typename T>
00309 void test_dispatch5() {
00310     Thing<T> thing;
00311     Verifier<T>::verify5(static_func5<T>);
00312     Verifier<T>::verify5(&thing, &Thing<T>::member_func5);
00313     Verifier<T>::verify5((const Thing<T>*)&thing, &Thing<T>::const_member_func5);
00314     Verifier<T>::verify5((volatile Thing<T>*)&thing, &Thing<T>::volatile_member_func5);
00315     Verifier<T>::verify5((const volatile Thing<T>*)&thing, &Thing<T>::const_volatile_member_func5);
00316     Verifier<T>::verify5(&bound_func5<T>, &thing);
00317     Verifier<T>::verify5(&const_func5<T>, (const Thing<T>*)&thing);
00318     Verifier<T>::verify5(&volatile_func5<T>, (volatile Thing<T>*)&thing);
00319     Verifier<T>::verify5(&const_volatile_func5<T>, (const volatile Thing<T>*)&thing);
00320     Verifier<T>::verify5(callback(static_func5<T>));
00321 
00322     Callback<T(T, T, T, T, T)> cb(static_func5);
00323     Verifier<T>::verify5(cb);
00324     cb = static_func5;
00325     Verifier<T>::verify5(cb);
00326     cb.attach(&bound_func5<T>, &thing);
00327     Verifier<T>::verify5(&cb, &Callback<T(T, T, T, T, T)>::call);
00328     Verifier<T>::verify5(&Callback<T(T, T, T, T, T)>::thunk, (void*)&cb);
00329 }
00330 
00331 
00332 // Test setup
00333 utest::v1::status_t  test_setup(const size_t number_of_cases) {
00334     GREENTEA_SETUP(10, "default_auto");
00335     return verbose_test_setup_handler(number_of_cases);
00336 }
00337 
00338 Case cases[] = {
00339     Case("Testing callbacks with 0 uchars", test_dispatch0<unsigned char>),
00340     Case("Testing callbacks with 1 uchars", test_dispatch1<unsigned char>),
00341     Case("Testing callbacks with 2 uchars", test_dispatch2<unsigned char>),
00342     Case("Testing callbacks with 3 uchars", test_dispatch3<unsigned char>),
00343     Case("Testing callbacks with 4 uchars", test_dispatch4<unsigned char>),
00344     Case("Testing callbacks with 5 uchars", test_dispatch5<unsigned char>),
00345 };
00346 
00347 Specification specification(test_setup, cases);
00348 
00349 int main() {
00350     return !Harness::run(specification);
00351 }