mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
TESTS/mbed_drivers/c_strings/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 <stdio.h> |
elessair | 0:f269e3021894 | 18 | #include <string.h> |
elessair | 0:f269e3021894 | 19 | #include "mbed.h" |
elessair | 0:f269e3021894 | 20 | #include "greentea-client/test_env.h" |
elessair | 0:f269e3021894 | 21 | #include "unity/unity.h" |
elessair | 0:f269e3021894 | 22 | #include "utest/utest.h" |
elessair | 0:f269e3021894 | 23 | |
elessair | 0:f269e3021894 | 24 | namespace { |
elessair | 0:f269e3021894 | 25 | static char buffer[256] = {0}; |
elessair | 0:f269e3021894 | 26 | } |
elessair | 0:f269e3021894 | 27 | |
elessair | 0:f269e3021894 | 28 | #define CLEAN_BUFFER memset(::buffer, 0x00, sizeof(::buffer)) |
elessair | 0:f269e3021894 | 29 | #define NEGATIVE_INTEGERS -32768,-3214,-999,-100,-1,0,-1,-4231,-999,-4123,-32760,-99999 |
elessair | 0:f269e3021894 | 30 | #define POSITIVE_INTEGERS 32768,3214,999,100,1,0,1,4231,999,4123,32760,99999 |
elessair | 0:f269e3021894 | 31 | #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 | 32 | |
elessair | 0:f269e3021894 | 33 | using namespace utest::v1; |
elessair | 0:f269e3021894 | 34 | |
elessair | 0:f269e3021894 | 35 | |
elessair | 0:f269e3021894 | 36 | void test_case_c_string_i_d() { |
elessair | 0:f269e3021894 | 37 | CLEAN_BUFFER; |
elessair | 0:f269e3021894 | 38 | sprintf(buffer, "%i %d %i %d %i %d %i %d %i %d %i %i", NEGATIVE_INTEGERS); |
elessair | 0:f269e3021894 | 39 | TEST_ASSERT_EQUAL_STRING("-32768 -3214 -999 -100 -1 0 -1 -4231 -999 -4123 -32760 -99999", buffer); |
elessair | 0:f269e3021894 | 40 | } |
elessair | 0:f269e3021894 | 41 | |
elessair | 0:f269e3021894 | 42 | void test_case_c_string_u_d() { |
elessair | 0:f269e3021894 | 43 | CLEAN_BUFFER; |
elessair | 0:f269e3021894 | 44 | sprintf(buffer, "%u %d %u %d %u %d %u %d %u %d %u %d", POSITIVE_INTEGERS); |
elessair | 0:f269e3021894 | 45 | TEST_ASSERT_EQUAL_STRING("32768 3214 999 100 1 0 1 4231 999 4123 32760 99999", buffer); |
elessair | 0:f269e3021894 | 46 | } |
elessair | 0:f269e3021894 | 47 | |
elessair | 0:f269e3021894 | 48 | void test_case_c_string_x_E() { |
elessair | 0:f269e3021894 | 49 | CLEAN_BUFFER; |
elessair | 0:f269e3021894 | 50 | sprintf(buffer, "%x %X %x %X %x %X %x %X %x %X %x %X", POSITIVE_INTEGERS); |
elessair | 0:f269e3021894 | 51 | TEST_ASSERT_EQUAL_STRING("8000 C8E 3e7 64 1 0 1 1087 3e7 101B 7ff8 1869F", buffer); |
elessair | 0:f269e3021894 | 52 | } |
elessair | 0:f269e3021894 | 53 | |
elessair | 0:f269e3021894 | 54 | void test_case_c_string_f_f() { |
elessair | 0:f269e3021894 | 55 | CLEAN_BUFFER; |
elessair | 0:f269e3021894 | 56 | sprintf(buffer, "%f %f %f %f %f %f %f %f %f %f", FLOATS); |
elessair | 0:f269e3021894 | 57 | TEST_ASSERT_EQUAL_STRING("0.002000 0.924300 15.913200 791.773680 6208.200000 25719.495200 426815.982588 6429271.046000 42468024.930000 212006462.910000", buffer); |
elessair | 0:f269e3021894 | 58 | } |
elessair | 0:f269e3021894 | 59 | |
elessair | 0:f269e3021894 | 60 | void test_case_c_string_g_g() { |
elessair | 0:f269e3021894 | 61 | CLEAN_BUFFER; |
elessair | 0:f269e3021894 | 62 | sprintf(buffer, "%g %g %g %g %g %g %g %g %g %g", FLOATS); |
elessair | 0:f269e3021894 | 63 | TEST_ASSERT_EQUAL_STRING("0.002 0.9243 15.9132 791.774 6208.2 25719.5 426816 6.42927e+06 4.2468e+07 2.12006e+08", buffer); |
elessair | 0:f269e3021894 | 64 | } |
elessair | 0:f269e3021894 | 65 | |
elessair | 0:f269e3021894 | 66 | void test_case_c_string_e_E() { |
elessair | 0:f269e3021894 | 67 | CLEAN_BUFFER; |
elessair | 0:f269e3021894 | 68 | sprintf(buffer, "%e %E %e %E %e %E %e %E %e %E", FLOATS); |
elessair | 0:f269e3021894 | 69 | TEST_ASSERT_EQUAL_STRING("2.000000e-03 9.243000E-01 1.591320e+01 7.917737E+02 6.208200e+03 2.571950E+04 4.268160e+05 6.429271E+06 4.246802e+07 2.120065E+08", buffer); |
elessair | 0:f269e3021894 | 70 | } |
elessair | 0:f269e3021894 | 71 | |
elessair | 0:f269e3021894 | 72 | void test_case_c_string_strtok() { |
elessair | 0:f269e3021894 | 73 | CLEAN_BUFFER; |
elessair | 0:f269e3021894 | 74 | char str[] ="- This, a sample string."; |
elessair | 0:f269e3021894 | 75 | char * pch = strtok (str," ,.-"); |
elessair | 0:f269e3021894 | 76 | while (pch != NULL) { |
elessair | 0:f269e3021894 | 77 | strcat(buffer, pch); |
elessair | 0:f269e3021894 | 78 | pch = strtok (NULL, " ,.-"); |
elessair | 0:f269e3021894 | 79 | } |
elessair | 0:f269e3021894 | 80 | TEST_ASSERT_EQUAL_STRING("Thisasamplestring", buffer); |
elessair | 0:f269e3021894 | 81 | } |
elessair | 0:f269e3021894 | 82 | |
elessair | 0:f269e3021894 | 83 | void test_case_c_string_strpbrk() { |
elessair | 0:f269e3021894 | 84 | CLEAN_BUFFER; |
elessair | 0:f269e3021894 | 85 | char str[] = "This is a sample string"; |
elessair | 0:f269e3021894 | 86 | char key[] = "aeiou"; |
elessair | 0:f269e3021894 | 87 | char *pch = strpbrk(str, key); |
elessair | 0:f269e3021894 | 88 | while (pch != NULL) |
elessair | 0:f269e3021894 | 89 | { |
elessair | 0:f269e3021894 | 90 | char buf[2] = {*pch, '\0'}; |
elessair | 0:f269e3021894 | 91 | strcat(buffer, buf); |
elessair | 0:f269e3021894 | 92 | pch = strpbrk(pch + 1,key); |
elessair | 0:f269e3021894 | 93 | } |
elessair | 0:f269e3021894 | 94 | TEST_ASSERT_EQUAL_STRING("iiaaei", buffer); |
elessair | 0:f269e3021894 | 95 | } |
elessair | 0:f269e3021894 | 96 | |
elessair | 0:f269e3021894 | 97 | utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { |
elessair | 0:f269e3021894 | 98 | greentea_case_failure_abort_handler(source, reason); |
elessair | 0:f269e3021894 | 99 | return STATUS_CONTINUE; |
elessair | 0:f269e3021894 | 100 | } |
elessair | 0:f269e3021894 | 101 | |
elessair | 0:f269e3021894 | 102 | Case cases[] = { |
elessair | 0:f269e3021894 | 103 | Case("C strings: strtok", test_case_c_string_strtok, greentea_failure_handler), |
elessair | 0:f269e3021894 | 104 | Case("C strings: strpbrk", test_case_c_string_strpbrk, greentea_failure_handler), |
elessair | 0:f269e3021894 | 105 | Case("C strings: %i %d integer formatting", test_case_c_string_i_d, greentea_failure_handler), |
elessair | 0:f269e3021894 | 106 | Case("C strings: %u %d integer formatting", test_case_c_string_u_d, greentea_failure_handler), |
elessair | 0:f269e3021894 | 107 | Case("C strings: %x %E integer formatting", test_case_c_string_x_E, greentea_failure_handler), |
elessair | 0:f269e3021894 | 108 | Case("C strings: %f %f float formatting", test_case_c_string_f_f, greentea_failure_handler), |
elessair | 0:f269e3021894 | 109 | Case("C strings: %e %E float formatting", test_case_c_string_e_E, greentea_failure_handler), |
elessair | 0:f269e3021894 | 110 | Case("C strings: %g %g float formatting", test_case_c_string_g_g, greentea_failure_handler), |
elessair | 0:f269e3021894 | 111 | }; |
elessair | 0:f269e3021894 | 112 | |
elessair | 0:f269e3021894 | 113 | utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { |
elessair | 0:f269e3021894 | 114 | GREENTEA_SETUP(5, "default_auto"); |
elessair | 0:f269e3021894 | 115 | return greentea_test_setup_handler(number_of_cases); |
elessair | 0:f269e3021894 | 116 | } |
elessair | 0:f269e3021894 | 117 | |
elessair | 0:f269e3021894 | 118 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
elessair | 0:f269e3021894 | 119 | |
elessair | 0:f269e3021894 | 120 | int main() { |
elessair | 0:f269e3021894 | 121 | Harness::run(specification); |
elessair | 0:f269e3021894 | 122 | } |