Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "greentea-client/test_env.h"
00018 #include "unity.h"
00019 #include "utest.h"
00020 
00021 using namespace utest::v1;
00022 
00023 
00024 // static functions
00025 template <typename T>
00026 T static_func0()
00027     { return 0; }
00028 template <typename T>
00029 T static_func1(T a0)
00030     { return 0 | a0; }
00031 template <typename T>
00032 T static_func2(T a0, T a1)
00033     { return 0 | a0 | a1; }
00034 template <typename T>
00035 T static_func3(T a0, T a1, T a2)
00036     { return 0 | a0 | a1 | a2; }
00037 template <typename T>
00038 T static_func4(T a0, T a1, T a2, T a3)
00039     { return 0 | a0 | a1 | a2 | a3; }
00040 template <typename T>
00041 T static_func5(T a0, T a1, T a2, T a3, T a4)
00042     { return 0 | a0 | a1 | a2 | a3 | a4; }
00043 
00044 // class functions
00045 template <typename T>
00046 struct Thing {
00047     T t;
00048     Thing() : t(0x80) {}
00049 
00050     T member_func0()
00051         { return t; }
00052     T member_func1(T a0)
00053         { return t | a0; }
00054     T member_func2(T a0, T a1)
00055         { return t | a0 | a1; }
00056     T member_func3(T a0, T a1, T a2)
00057         { return t | a0 | a1 | a2; }
00058     T member_func4(T a0, T a1, T a2, T a3)
00059         { return t | a0 | a1 | a2 | a3; }
00060     T member_func5(T a0, T a1, T a2, T a3, T a4)
00061         { return t | a0 | a1 | a2 | a3 | a4; }
00062 
00063     T const_member_func0() const
00064         { return t; }
00065     T const_member_func1(T a0) const
00066         { return t | a0; }
00067     T const_member_func2(T a0, T a1) const
00068         { return t | a0 | a1; }
00069     T const_member_func3(T a0, T a1, T a2) const
00070         { return t | a0 | a1 | a2; }
00071     T const_member_func4(T a0, T a1, T a2, T a3) const
00072         { return t | a0 | a1 | a2 | a3; }
00073     T const_member_func5(T a0, T a1, T a2, T a3, T a4) const
00074         { return t | a0 | a1 | a2 | a3 | a4; }
00075 
00076     T volatile_member_func0() volatile
00077         { return t; }
00078     T volatile_member_func1(T a0) volatile
00079         { return t | a0; }
00080     T volatile_member_func2(T a0, T a1) volatile
00081         { return t | a0 | a1; }
00082     T volatile_member_func3(T a0, T a1, T a2) volatile
00083         { return t | a0 | a1 | a2; }
00084     T volatile_member_func4(T a0, T a1, T a2, T a3) volatile
00085         { return t | a0 | a1 | a2 | a3; }
00086     T volatile_member_func5(T a0, T a1, T a2, T a3, T a4) volatile
00087         { return t | a0 | a1 | a2 | a3 | a4; }
00088 
00089     T const_volatile_member_func0() const volatile
00090         { return t; }
00091     T const_volatile_member_func1(T a0) const volatile
00092         { return t | a0; }
00093     T const_volatile_member_func2(T a0, T a1) const volatile
00094         { return t | a0 | a1; }
00095     T const_volatile_member_func3(T a0, T a1, T a2) const volatile
00096         { return t | a0 | a1 | a2; }
00097     T const_volatile_member_func4(T a0, T a1, T a2, T a3) const volatile
00098         { return t | a0 | a1 | a2 | a3; }
00099     T const_volatile_member_func5(T a0, T a1, T a2, T a3, T a4) const volatile
00100         { return t | a0 | a1 | a2 | a3 | a4; }
00101 };
00102 
00103 // bound functions
00104 template <typename T>
00105 T bound_func0(Thing<T> *t)
00106     { return t->t; }
00107 template <typename T>
00108 T bound_func1(Thing<T> *t, T a0)
00109     { return t->t | a0; }
00110 template <typename T>
00111 T bound_func2(Thing<T> *t, T a0, T a1)
00112     { return t->t | a0 | a1; }
00113 template <typename T>
00114 T bound_func3(Thing<T> *t, T a0, T a1, T a2)
00115     { return t->t | a0 | a1 | a2; }
00116 template <typename T>
00117 T bound_func4(Thing<T> *t, T a0, T a1, T a2, T a3)
00118     { return t->t | a0 | a1 | a2 | a3; }
00119 template <typename T>
00120 T bound_func5(Thing<T> *t, T a0, T a1, T a2, T a3, T a4)
00121     { return t->t | a0 | a1 | a2 | a3 | a4; }
00122 template <typename T>
00123 T const_bound_func0(const Thing<T> *t)
00124     { return t->t; }
00125 template <typename T>
00126 T const_bound_func1(const Thing<T> *t, T a0)
00127     { return t->t | a0; }
00128 template <typename T>
00129 T const_bound_func2(const Thing<T> *t, T a0, T a1)
00130     { return t->t | a0 | a1; }
00131 template <typename T>
00132 T const_bound_func3(const Thing<T> *t, T a0, T a1, T a2)
00133     { return t->t | a0 | a1 | a2; }
00134 template <typename T>
00135 T const_bound_func4(const Thing<T> *t, T a0, T a1, T a2, T a3)
00136     { return t->t | a0 | a1 | a2 | a3; }
00137 template <typename T>
00138 T const_bound_func5(const Thing<T> *t, T a0, T a1, T a2, T a3, T a4)
00139     { return t->t | a0 | a1 | a2 | a3 | a4; }
00140 template <typename T>
00141 T volatile_bound_func0(volatile Thing<T> *t)
00142     { return t->t; }
00143 template <typename T>
00144 T volatile_bound_func1(volatile Thing<T> *t, T a0)
00145     { return t->t | a0; }
00146 template <typename T>
00147 T volatile_bound_func2(volatile Thing<T> *t, T a0, T a1)
00148     { return t->t | a0 | a1; }
00149 template <typename T>
00150 T volatile_bound_func3(volatile Thing<T> *t, T a0, T a1, T a2)
00151     { return t->t | a0 | a1 | a2; }
00152 template <typename T>
00153 T volatile_bound_func4(volatile Thing<T> *t, T a0, T a1, T a2, T a3)
00154     { return t->t | a0 | a1 | a2 | a3; }
00155 template <typename T>
00156 T volatile_bound_func5(volatile Thing<T> *t, T a0, T a1, T a2, T a3, T a4)
00157     { return t->t | a0 | a1 | a2 | a3 | a4; }
00158 template <typename T>
00159 T const_volatile_bound_func0(const volatile Thing<T> *t)
00160     { return t->t; }
00161 template <typename T>
00162 T const_volatile_bound_func1(const volatile Thing<T> *t, T a0)
00163     { return t->t | a0; }
00164 template <typename T>
00165 T const_volatile_bound_func2(const volatile Thing<T> *t, T a0, T a1)
00166     { return t->t | a0 | a1; }
00167 template <typename T>
00168 T const_volatile_bound_func3(const volatile Thing<T> *t, T a0, T a1, T a2)
00169     { return t->t | a0 | a1 | a2; }
00170 template <typename T>
00171 T const_volatile_bound_func4(const volatile Thing<T> *t, T a0, T a1, T a2, T a3)
00172     { return t->t | a0 | a1 | a2 | a3; }
00173 template <typename T>
00174 T const_volatile_bound_func5(const volatile Thing<T> *t, T a0, T a1, T a2, T a3, T a4)
00175     { return t->t | a0 | a1 | a2 | a3 | a4; }
00176 
00177 // void functions
00178 template <typename T>
00179 T void_func0(void *t)
00180     { return static_cast<Thing<T>*>(t)->t; }
00181 template <typename T>
00182 T void_func1(void *t, T a0)
00183     { return static_cast<Thing<T>*>(t)->t | a0; }
00184 template <typename T>
00185 T void_func2(void *t, T a0, T a1)
00186     { return static_cast<Thing<T>*>(t)->t | a0 | a1; }
00187 template <typename T>
00188 T void_func3(void *t, T a0, T a1, T a2)
00189     { return static_cast<Thing<T>*>(t)->t | a0 | a1 | a2; }
00190 template <typename T>
00191 T void_func4(void *t, T a0, T a1, T a2, T a3)
00192     { return static_cast<Thing<T>*>(t)->t | a0 | a1 | a2 | a3; }
00193 template <typename T>
00194 T void_func5(void *t, T a0, T a1, T a2, T a3, T a4)
00195     { return static_cast<Thing<T>*>(t)->t | a0 | a1 | a2 | a3 | a4; }
00196 template <typename T>
00197 T const_void_func0(const void *t)
00198     { return static_cast<const Thing<T>*>(t)->t; }
00199 template <typename T>
00200 T const_void_func1(const void *t, T a0)
00201     { return static_cast<const Thing<T>*>(t)->t | a0; }
00202 template <typename T>
00203 T const_void_func2(const void *t, T a0, T a1)
00204     { return static_cast<const Thing<T>*>(t)->t | a0 | a1; }
00205 template <typename T>
00206 T const_void_func3(const void *t, T a0, T a1, T a2)
00207     { return static_cast<const Thing<T>*>(t)->t | a0 | a1 | a2; }
00208 template <typename T>
00209 T const_void_func4(const void *t, T a0, T a1, T a2, T a3)
00210     { return static_cast<const Thing<T>*>(t)->t | a0 | a1 | a2 | a3; }
00211 template <typename T>
00212 T const_void_func5(const void *t, T a0, T a1, T a2, T a3, T a4)
00213     { return static_cast<const Thing<T>*>(t)->t | a0 | a1 | a2 | a3 | a4; }
00214 template <typename T>
00215 T volatile_void_func0(volatile void *t)
00216     { return static_cast<volatile Thing<T>*>(t)->t; }
00217 template <typename T>
00218 T volatile_void_func1(volatile void *t, T a0)
00219     { return static_cast<volatile Thing<T>*>(t)->t | a0; }
00220 template <typename T>
00221 T volatile_void_func2(volatile void *t, T a0, T a1)
00222     { return static_cast<volatile Thing<T>*>(t)->t | a0 | a1; }
00223 template <typename T>
00224 T volatile_void_func3(volatile void *t, T a0, T a1, T a2)
00225     { return static_cast<volatile Thing<T>*>(t)->t | a0 | a1 | a2; }
00226 template <typename T>
00227 T volatile_void_func4(volatile void *t, T a0, T a1, T a2, T a3)
00228     { return static_cast<volatile Thing<T>*>(t)->t | a0 | a1 | a2 | a3; }
00229 template <typename T>
00230 T volatile_void_func5(volatile void *t, T a0, T a1, T a2, T a3, T a4)
00231     { return static_cast<volatile Thing<T>*>(t)->t | a0 | a1 | a2 | a3 | a4; }
00232 template <typename T>
00233 T const_volatile_void_func0(const volatile void *t)
00234     { return static_cast<const volatile Thing<T>*>(t)->t; }
00235 template <typename T>
00236 T const_volatile_void_func1(const volatile void *t, T a0)
00237     { return static_cast<const volatile Thing<T>*>(t)->t | a0; }
00238 template <typename T>
00239 T const_volatile_void_func2(const volatile void *t, T a0, T a1)
00240     { return static_cast<const volatile Thing<T>*>(t)->t | a0 | a1; }
00241 template <typename T>
00242 T const_volatile_void_func3(const volatile void *t, T a0, T a1, T a2)
00243     { return static_cast<const volatile Thing<T>*>(t)->t | a0 | a1 | a2; }
00244 template <typename T>
00245 T const_volatile_void_func4(const volatile void *t, T a0, T a1, T a2, T a3)
00246     { return static_cast<const volatile Thing<T>*>(t)->t | a0 | a1 | a2 | a3; }
00247 template <typename T>
00248 T const_volatile_void_func5(const volatile void *t, T a0, T a1, T a2, T a3, T a4)
00249     { return static_cast<const volatile Thing<T>*>(t)->t | a0 | a1 | a2 | a3 | a4; }
00250 
00251 
00252 // function call and result verification
00253 template <typename T>
00254 struct Verifier {
00255     static void verify0(Callback<T()> func) {
00256         T result = func();
00257         TEST_ASSERT_EQUAL(result, 0x00);
00258     }
00259 
00260     template <typename O, typename M>
00261     static void verify0(O *obj, M method) {
00262         Callback<T()> func(obj, method);
00263         T result = func();
00264         TEST_ASSERT_EQUAL(result, 0x80);
00265     }
00266 
00267     static void verify1(Callback<T(T)> func) {
00268         T result = func((1 << 0));
00269         TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0));
00270     }
00271 
00272     template <typename O, typename M>
00273     static void verify1(O *obj, M method) {
00274         Callback<T(T)> func(obj, method);
00275         T result = func((1 << 0));
00276         TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0));
00277     }
00278 
00279     static void verify2(Callback<T(T, T)> func) {
00280         T result = func((1 << 0), (1 << 1));
00281         TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1));
00282     }
00283 
00284     template <typename O, typename M>
00285     static void verify2(O *obj, M method) {
00286         Callback<T(T, T)> func(obj, method);
00287         T result = func((1 << 0), (1 << 1));
00288         TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1));
00289     }
00290 
00291     static void verify3(Callback<T(T, T, T)> func) {
00292         T result = func((1 << 0), (1 << 1), (1 << 2));
00293         TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2));
00294     }
00295 
00296     template <typename O, typename M>
00297     static void verify3(O *obj, M method) {
00298         Callback<T(T, T, T)> func(obj, method);
00299         T result = func((1 << 0), (1 << 1), (1 << 2));
00300         TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2));
00301     }
00302 
00303     static void verify4(Callback<T(T, T, T, T)> func) {
00304         T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3));
00305         TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3));
00306     }
00307 
00308     template <typename O, typename M>
00309     static void verify4(O *obj, M method) {
00310         Callback<T(T, T, T, T)> func(obj, method);
00311         T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3));
00312         TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3));
00313     }
00314 
00315     static void verify5(Callback<T(T, T, T, T, T)> func) {
00316         T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4));
00317         TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4));
00318     }
00319 
00320     template <typename O, typename M>
00321     static void verify5(O *obj, M method) {
00322         Callback<T(T, T, T, T, T)> func(obj, method);
00323         T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4));
00324         TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4));
00325     }
00326 };
00327 
00328 
00329 // test dispatch
00330 template <typename T>
00331 void test_fparg1() {
00332     Thing<T> thing;
00333     FunctionPointerArg1<T,T> fp(static_func1<T>);
00334     Verifier<T>::verify1(fp);
00335     Verifier<T>::verify1(fp.get_function());
00336 }
00337 
00338 template <typename T>
00339 void test_fparg0() {
00340     Thing<T> thing;
00341     FunctionPointerArg1<T,void> fp(static_func0<T>);
00342     Verifier<T>::verify0(fp);
00343     Verifier<T>::verify0(fp.get_function());
00344 }
00345 
00346 
00347 // Test setup
00348 utest::v1::status_t test_setup(const size_t number_of_cases) {
00349     GREENTEA_SETUP(10, "default_auto");
00350     return verbose_test_setup_handler(number_of_cases);
00351 }
00352 
00353 Case cases[] = {
00354     Case("Testing FunctionPointerArg1 compatibility", test_fparg1<int>),
00355     Case("Testing FunctionPointer compatibility", test_fparg0<int>),
00356 };
00357 
00358 Specification specification(test_setup, cases);
00359 
00360 int main() {
00361     return !Harness::run(specification);
00362 }