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