Preliminary main mbed library for nexpaq development
TESTS/mbed_drivers/stl_features/main.cpp@1:d96dbedaebdb, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:54:50 2016 +0000
- Revision:
- 1:d96dbedaebdb
- Parent:
- 0:6c56fb4bc5f0
Removed extra directories for other platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | /* |
nexpaq | 0:6c56fb4bc5f0 | 2 | * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved |
nexpaq | 0:6c56fb4bc5f0 | 3 | * SPDX-License-Identifier: Apache-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 4 | * |
nexpaq | 0:6c56fb4bc5f0 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
nexpaq | 0:6c56fb4bc5f0 | 6 | * not use this file except in compliance with the License. |
nexpaq | 0:6c56fb4bc5f0 | 7 | * You may obtain a copy of the License at |
nexpaq | 0:6c56fb4bc5f0 | 8 | * |
nexpaq | 0:6c56fb4bc5f0 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 10 | * |
nexpaq | 0:6c56fb4bc5f0 | 11 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 0:6c56fb4bc5f0 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
nexpaq | 0:6c56fb4bc5f0 | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 0:6c56fb4bc5f0 | 14 | * See the License for the specific language governing permissions and |
nexpaq | 0:6c56fb4bc5f0 | 15 | * limitations under the License. |
nexpaq | 0:6c56fb4bc5f0 | 16 | */ |
nexpaq | 0:6c56fb4bc5f0 | 17 | #include "mbed.h" |
nexpaq | 0:6c56fb4bc5f0 | 18 | #include "greentea-client/test_env.h" |
nexpaq | 0:6c56fb4bc5f0 | 19 | #include "unity/unity.h" |
nexpaq | 0:6c56fb4bc5f0 | 20 | #include "utest/utest.h" |
nexpaq | 0:6c56fb4bc5f0 | 21 | #include <stdio.h> |
nexpaq | 0:6c56fb4bc5f0 | 22 | #include <string.h> |
nexpaq | 0:6c56fb4bc5f0 | 23 | #include <algorithm> |
nexpaq | 0:6c56fb4bc5f0 | 24 | #include <string> |
nexpaq | 0:6c56fb4bc5f0 | 25 | #include <vector> |
nexpaq | 0:6c56fb4bc5f0 | 26 | #include <queue> |
nexpaq | 0:6c56fb4bc5f0 | 27 | #include <map> |
nexpaq | 0:6c56fb4bc5f0 | 28 | #include <math.h> |
nexpaq | 0:6c56fb4bc5f0 | 29 | |
nexpaq | 0:6c56fb4bc5f0 | 30 | using namespace utest::v1; |
nexpaq | 0:6c56fb4bc5f0 | 31 | |
nexpaq | 0:6c56fb4bc5f0 | 32 | #define TABLE_SIZE(TAB) (sizeof(TAB) / sizeof(TAB[0])) |
nexpaq | 0:6c56fb4bc5f0 | 33 | |
nexpaq | 0:6c56fb4bc5f0 | 34 | #define NEGATIVE_INTEGERS -32768,-3214,-999,-100,-1,0,1,4231,999,4123,32760,99999 |
nexpaq | 0:6c56fb4bc5f0 | 35 | #define POSITIVE_INTEGERS 32768,3214,999,100,1,0,1,4231,999,4123,32760,99999 |
nexpaq | 0:6c56fb4bc5f0 | 36 | #define FLOATS 0.002,0.92430,15.91320,791.77368,6208.2,25719.4952,426815.982588,6429271.046,42468024.93,212006462.910 |
nexpaq | 0:6c56fb4bc5f0 | 37 | #define FLOATS_STR "0.002","0.92430","15.91320","791.77368","6208.2","25719.4952","426815.982588","6429271.046","42468024.93","212006462.910" |
nexpaq | 0:6c56fb4bc5f0 | 38 | |
nexpaq | 0:6c56fb4bc5f0 | 39 | |
nexpaq | 0:6c56fb4bc5f0 | 40 | namespace { |
nexpaq | 0:6c56fb4bc5f0 | 41 | int p_integers[] = {POSITIVE_INTEGERS}; |
nexpaq | 0:6c56fb4bc5f0 | 42 | int n_integers[] = {NEGATIVE_INTEGERS}; |
nexpaq | 0:6c56fb4bc5f0 | 43 | float floats[] = {FLOATS}; |
nexpaq | 0:6c56fb4bc5f0 | 44 | |
nexpaq | 0:6c56fb4bc5f0 | 45 | template <class T, class F> |
nexpaq | 0:6c56fb4bc5f0 | 46 | void BubbleSort(T& _array, size_t array_size, F functor) { |
nexpaq | 0:6c56fb4bc5f0 | 47 | bool flag = true; |
nexpaq | 0:6c56fb4bc5f0 | 48 | size_t numLength = array_size; |
nexpaq | 0:6c56fb4bc5f0 | 49 | for(size_t i = 1; (i <= numLength) && flag; i++) { |
nexpaq | 0:6c56fb4bc5f0 | 50 | flag = false; |
nexpaq | 0:6c56fb4bc5f0 | 51 | for (size_t j = 0; j < (numLength - 1); j++) { |
nexpaq | 0:6c56fb4bc5f0 | 52 | if (functor(_array[j+1], _array[j])) { |
nexpaq | 0:6c56fb4bc5f0 | 53 | int temp = _array[j]; |
nexpaq | 0:6c56fb4bc5f0 | 54 | _array[j] = _array[j + 1]; |
nexpaq | 0:6c56fb4bc5f0 | 55 | _array[j+1] = temp; |
nexpaq | 0:6c56fb4bc5f0 | 56 | flag = true; |
nexpaq | 0:6c56fb4bc5f0 | 57 | } |
nexpaq | 0:6c56fb4bc5f0 | 58 | } |
nexpaq | 0:6c56fb4bc5f0 | 59 | } |
nexpaq | 0:6c56fb4bc5f0 | 60 | } |
nexpaq | 0:6c56fb4bc5f0 | 61 | |
nexpaq | 0:6c56fb4bc5f0 | 62 | struct printInt { |
nexpaq | 0:6c56fb4bc5f0 | 63 | void operator()(int i) { printf("%d ", i); } |
nexpaq | 0:6c56fb4bc5f0 | 64 | }; |
nexpaq | 0:6c56fb4bc5f0 | 65 | |
nexpaq | 0:6c56fb4bc5f0 | 66 | struct printFloat { |
nexpaq | 0:6c56fb4bc5f0 | 67 | void operator()(float f) { printf("%f ", f); } |
nexpaq | 0:6c56fb4bc5f0 | 68 | }; |
nexpaq | 0:6c56fb4bc5f0 | 69 | |
nexpaq | 0:6c56fb4bc5f0 | 70 | struct printString { |
nexpaq | 0:6c56fb4bc5f0 | 71 | void operator()(const char* s) { printf("%s ", s); } |
nexpaq | 0:6c56fb4bc5f0 | 72 | }; |
nexpaq | 0:6c56fb4bc5f0 | 73 | |
nexpaq | 0:6c56fb4bc5f0 | 74 | struct greaterAbs { |
nexpaq | 0:6c56fb4bc5f0 | 75 | bool operator()(int a, int b) { return abs(a) > abs(b); } |
nexpaq | 0:6c56fb4bc5f0 | 76 | }; |
nexpaq | 0:6c56fb4bc5f0 | 77 | |
nexpaq | 0:6c56fb4bc5f0 | 78 | } // namespace |
nexpaq | 0:6c56fb4bc5f0 | 79 | |
nexpaq | 0:6c56fb4bc5f0 | 80 | void test_case_stl_equal() { |
nexpaq | 0:6c56fb4bc5f0 | 81 | std::vector<int> v_pints(p_integers, p_integers + TABLE_SIZE(p_integers)); |
nexpaq | 0:6c56fb4bc5f0 | 82 | TEST_ASSERT_TRUE(std::equal(v_pints.begin(), v_pints.end(), p_integers)); |
nexpaq | 0:6c56fb4bc5f0 | 83 | } |
nexpaq | 0:6c56fb4bc5f0 | 84 | |
nexpaq | 0:6c56fb4bc5f0 | 85 | void test_case_stl_transform() { |
nexpaq | 0:6c56fb4bc5f0 | 86 | const char* floats_str[] = {FLOATS_STR}; |
nexpaq | 0:6c56fb4bc5f0 | 87 | float floats_transform[TABLE_SIZE(floats_str)] = {0.0}; |
nexpaq | 0:6c56fb4bc5f0 | 88 | std::transform(floats_str, floats_str + TABLE_SIZE(floats_str), floats_transform, atof); |
nexpaq | 0:6c56fb4bc5f0 | 89 | //printf("stl_transform::floats_str: "); |
nexpaq | 0:6c56fb4bc5f0 | 90 | //std::for_each(floats_str, floats_str + TABLE_SIZE(floats_str), printString()); |
nexpaq | 0:6c56fb4bc5f0 | 91 | //printf("stl_transform::floats_transform: "); |
nexpaq | 0:6c56fb4bc5f0 | 92 | //std::for_each(floats_transform, floats_transform + TABLE_SIZE(floats_transform), printFloat()); |
nexpaq | 0:6c56fb4bc5f0 | 93 | //printf("\n"); |
nexpaq | 0:6c56fb4bc5f0 | 94 | |
nexpaq | 0:6c56fb4bc5f0 | 95 | TEST_ASSERT_TRUE(std::equal(floats_transform, floats_transform + TABLE_SIZE(floats_transform), floats)); |
nexpaq | 0:6c56fb4bc5f0 | 96 | } |
nexpaq | 0:6c56fb4bc5f0 | 97 | |
nexpaq | 0:6c56fb4bc5f0 | 98 | void test_case_stl_sort_greater() { |
nexpaq | 0:6c56fb4bc5f0 | 99 | std::vector<int> v_nints_1(n_integers, n_integers + TABLE_SIZE(n_integers)); |
nexpaq | 0:6c56fb4bc5f0 | 100 | std::vector<int> v_nints_2(n_integers, n_integers + TABLE_SIZE(n_integers)); |
nexpaq | 0:6c56fb4bc5f0 | 101 | |
nexpaq | 0:6c56fb4bc5f0 | 102 | BubbleSort(v_nints_1, v_nints_1.size(), std::greater<int>()); |
nexpaq | 0:6c56fb4bc5f0 | 103 | std::sort(v_nints_2.begin(), v_nints_2.end(), std::greater<int>()); |
nexpaq | 0:6c56fb4bc5f0 | 104 | |
nexpaq | 0:6c56fb4bc5f0 | 105 | TEST_ASSERT_TRUE(std::equal(v_nints_1.begin(), v_nints_1.end(), v_nints_2.begin())); |
nexpaq | 0:6c56fb4bc5f0 | 106 | } |
nexpaq | 0:6c56fb4bc5f0 | 107 | |
nexpaq | 0:6c56fb4bc5f0 | 108 | void test_case_stl_sort_abs() { |
nexpaq | 0:6c56fb4bc5f0 | 109 | std::vector<int> v_nints_1(n_integers, n_integers + TABLE_SIZE(n_integers)); |
nexpaq | 0:6c56fb4bc5f0 | 110 | std::vector<int> v_nints_2(n_integers, n_integers + TABLE_SIZE(n_integers)); |
nexpaq | 0:6c56fb4bc5f0 | 111 | |
nexpaq | 0:6c56fb4bc5f0 | 112 | BubbleSort(v_nints_1, v_nints_1.size(), greaterAbs()); |
nexpaq | 0:6c56fb4bc5f0 | 113 | std::sort(v_nints_2.begin(), v_nints_2.end(), greaterAbs()); |
nexpaq | 0:6c56fb4bc5f0 | 114 | |
nexpaq | 0:6c56fb4bc5f0 | 115 | TEST_ASSERT_TRUE(std::equal(v_nints_1.begin(), v_nints_1.end(), v_nints_2.begin())); |
nexpaq | 0:6c56fb4bc5f0 | 116 | } |
nexpaq | 0:6c56fb4bc5f0 | 117 | |
nexpaq | 0:6c56fb4bc5f0 | 118 | utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { |
nexpaq | 0:6c56fb4bc5f0 | 119 | greentea_case_failure_abort_handler(source, reason); |
nexpaq | 0:6c56fb4bc5f0 | 120 | return STATUS_CONTINUE; |
nexpaq | 0:6c56fb4bc5f0 | 121 | } |
nexpaq | 0:6c56fb4bc5f0 | 122 | |
nexpaq | 0:6c56fb4bc5f0 | 123 | Case cases[] = { |
nexpaq | 0:6c56fb4bc5f0 | 124 | Case("STL std::equal", test_case_stl_equal, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 125 | Case("STL std::transform", test_case_stl_transform, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 126 | Case("STL std::sort greater", test_case_stl_sort_greater, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 127 | Case("STL std::sort abs", test_case_stl_sort_abs, greentea_failure_handler) |
nexpaq | 0:6c56fb4bc5f0 | 128 | }; |
nexpaq | 0:6c56fb4bc5f0 | 129 | |
nexpaq | 0:6c56fb4bc5f0 | 130 | utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { |
nexpaq | 0:6c56fb4bc5f0 | 131 | GREENTEA_SETUP(5, "default_auto"); |
nexpaq | 0:6c56fb4bc5f0 | 132 | return greentea_test_setup_handler(number_of_cases); |
nexpaq | 0:6c56fb4bc5f0 | 133 | } |
nexpaq | 0:6c56fb4bc5f0 | 134 | |
nexpaq | 0:6c56fb4bc5f0 | 135 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
nexpaq | 0:6c56fb4bc5f0 | 136 | |
nexpaq | 0:6c56fb4bc5f0 | 137 | int main() { |
nexpaq | 0:6c56fb4bc5f0 | 138 | Harness::run(specification); |
nexpaq | 0:6c56fb4bc5f0 | 139 | } |