Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "mbed.h"
00018 #include "greentea-client/test_env.h"
00019 #include "unity/unity.h"
00020 #include "utest/utest.h"
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <algorithm>
00024 #include <string>
00025 #include <vector>
00026 #include <queue>
00027 #include <map>
00028 #include <math.h>
00029 #include <functional>
00030 
00031 using namespace utest::v1;
00032 
00033 #define TABLE_SIZE(TAB) (sizeof(TAB) / sizeof(TAB[0]))
00034 
00035 #define NEGATIVE_INTEGERS -32768,-3214,-999,-100,-1,0,1,4231,999,4123,32760,99999
00036 #define FLOATS  0.002,0.92430,15.91320,791.77368,6208.2,25719.4952,426815.982588,6429271.046,42468024.93,212006462.910
00037 #define FLOATS_STR  "0.002","0.92430","15.91320","791.77368","6208.2","25719.4952","426815.982588","6429271.046","42468024.93","212006462.910"
00038 
00039 namespace {
00040 
00041 template <class T, class F>
00042 void BubbleSort(T& _array, size_t array_size, F functor) {
00043     bool flag = true;
00044     size_t numLength = array_size;
00045     for(size_t i = 1; (i <= numLength) && flag; i++) {
00046         flag = false;
00047         for (size_t j = 0; j < (numLength - 1); j++) {
00048             if (functor(_array[j+1], _array[j])) {
00049                 int temp = _array[j];
00050                 _array[j] = _array[j + 1];
00051                 _array[j+1] = temp;
00052                 flag = true;
00053             }
00054         }
00055     }
00056 }
00057 
00058 struct greaterAbs {
00059     bool operator()(int a, int b) { return abs(a) > abs(b); }
00060 };
00061 
00062 }   // namespace
00063 
00064 void test_case_stl_equal() {
00065     const int n_integers[] = {NEGATIVE_INTEGERS};
00066     std::vector<int> v_pints(n_integers, n_integers + TABLE_SIZE(n_integers));
00067     TEST_ASSERT_TRUE(std::equal(v_pints.begin(), v_pints.end(), n_integers));
00068 }
00069 
00070 void test_case_stl_transform() {
00071     const float floats[] = {FLOATS};
00072     const char* floats_str[] = {FLOATS_STR};
00073     float floats_transform[TABLE_SIZE(floats_str)] = {0.0};
00074     std::transform(floats_str, floats_str + TABLE_SIZE(floats_str), floats_transform, atof);
00075 
00076     TEST_ASSERT_TRUE(std::equal(floats_transform, floats_transform + TABLE_SIZE(floats_transform), floats));
00077 }
00078 
00079 void test_case_stl_sort_greater() {
00080     int n_integers[] = {NEGATIVE_INTEGERS};
00081     int n_integers2[] = {NEGATIVE_INTEGERS};
00082 
00083     BubbleSort(n_integers, TABLE_SIZE(n_integers), std::greater<int>());
00084     std::sort(n_integers2, n_integers2 + TABLE_SIZE(n_integers2), std::greater<int>());
00085 
00086     TEST_ASSERT_TRUE(std::equal(n_integers2, n_integers2 + TABLE_SIZE(n_integers2), n_integers));
00087 }
00088 
00089 
00090 void test_case_stl_sort_abs() {
00091     int n_integers[] = {NEGATIVE_INTEGERS};
00092     int n_integers2[] = {NEGATIVE_INTEGERS};
00093 
00094     BubbleSort(n_integers, TABLE_SIZE(n_integers), greaterAbs());
00095     std::sort(n_integers2, n_integers2 + TABLE_SIZE(n_integers2), greaterAbs());
00096 
00097     TEST_ASSERT_TRUE(std::equal(n_integers2, n_integers2 + TABLE_SIZE(n_integers2), n_integers));
00098 }
00099 
00100 
00101 utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
00102     greentea_case_failure_abort_handler(source, reason);
00103     return STATUS_CONTINUE;
00104 }
00105 
00106 Case cases[] = {
00107     Case("STL std::equal", test_case_stl_equal, greentea_failure_handler),
00108     Case("STL std::transform", test_case_stl_transform, greentea_failure_handler),
00109     Case("STL std::sort greater", test_case_stl_sort_greater, greentea_failure_handler),
00110     Case("STL std::sort abs", test_case_stl_sort_abs, greentea_failure_handler)
00111 };
00112 
00113 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
00114     GREENTEA_SETUP(5, "default_auto");
00115     return greentea_test_setup_handler(number_of_cases);
00116 }
00117 
00118 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
00119 
00120 int main() {
00121     Harness::run(specification);
00122 }