Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-os/TESTS/mbed_drivers/c_strings/main.cpp@3:6fe17b8a6d62, 2021-03-09 (annotated)
- Committer:
- boro
- Date:
- Tue Mar 09 13:10:40 2021 +0000
- Revision:
- 3:6fe17b8a6d62
- Parent:
- 0:4beb2ea291ec
SDBlockDevice added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
boro | 0:4beb2ea291ec | 1 | /* |
boro | 0:4beb2ea291ec | 2 | * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved |
boro | 0:4beb2ea291ec | 3 | * SPDX-License-Identifier: Apache-2.0 |
boro | 0:4beb2ea291ec | 4 | * |
boro | 0:4beb2ea291ec | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
boro | 0:4beb2ea291ec | 6 | * not use this file except in compliance with the License. |
boro | 0:4beb2ea291ec | 7 | * You may obtain a copy of the License at |
boro | 0:4beb2ea291ec | 8 | * |
boro | 0:4beb2ea291ec | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
boro | 0:4beb2ea291ec | 10 | * |
boro | 0:4beb2ea291ec | 11 | * Unless required by applicable law or agreed to in writing, software |
boro | 0:4beb2ea291ec | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
boro | 0:4beb2ea291ec | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
boro | 0:4beb2ea291ec | 14 | * See the License for the specific language governing permissions and |
boro | 0:4beb2ea291ec | 15 | * limitations under the License. |
boro | 0:4beb2ea291ec | 16 | */ |
boro | 0:4beb2ea291ec | 17 | #include <stdio.h> |
boro | 0:4beb2ea291ec | 18 | #include <string.h> |
boro | 0:4beb2ea291ec | 19 | #include "mbed.h" |
boro | 0:4beb2ea291ec | 20 | #include "greentea-client/test_env.h" |
boro | 0:4beb2ea291ec | 21 | #include "unity/unity.h" |
boro | 0:4beb2ea291ec | 22 | #include "utest/utest.h" |
boro | 0:4beb2ea291ec | 23 | |
boro | 0:4beb2ea291ec | 24 | namespace { |
boro | 0:4beb2ea291ec | 25 | static char buffer[256] = {0}; |
boro | 0:4beb2ea291ec | 26 | } |
boro | 0:4beb2ea291ec | 27 | |
boro | 0:4beb2ea291ec | 28 | #define CLEAN_BUFFER memset(::buffer, 0x00, sizeof(::buffer)) |
boro | 0:4beb2ea291ec | 29 | #define NEGATIVE_INTEGERS -32768,-3214,-999,-100,-1,0,-1,-4231,-999,-4123,-32760,-99999 |
boro | 0:4beb2ea291ec | 30 | #define POSITIVE_INTEGERS 32768,3214,999,100,1,0,1,4231,999,4123,32760,99999 |
boro | 0:4beb2ea291ec | 31 | #define FLOATS 0.002,0.92430,15.91320,791.77368,6208.2,25719.4952,426815.982588,6429271.046,42468024.93,212006462.910 |
boro | 0:4beb2ea291ec | 32 | |
boro | 0:4beb2ea291ec | 33 | using namespace utest::v1; |
boro | 0:4beb2ea291ec | 34 | |
boro | 0:4beb2ea291ec | 35 | |
boro | 0:4beb2ea291ec | 36 | void test_case_c_string_i_d() { |
boro | 0:4beb2ea291ec | 37 | CLEAN_BUFFER; |
boro | 0:4beb2ea291ec | 38 | sprintf(buffer, "%i %d %i %d %i %d %i %d %i %d %i %i", NEGATIVE_INTEGERS); |
boro | 0:4beb2ea291ec | 39 | TEST_ASSERT_EQUAL_STRING("-32768 -3214 -999 -100 -1 0 -1 -4231 -999 -4123 -32760 -99999", buffer); |
boro | 0:4beb2ea291ec | 40 | } |
boro | 0:4beb2ea291ec | 41 | |
boro | 0:4beb2ea291ec | 42 | void test_case_c_string_u_d() { |
boro | 0:4beb2ea291ec | 43 | CLEAN_BUFFER; |
boro | 0:4beb2ea291ec | 44 | sprintf(buffer, "%u %d %u %d %u %d %u %d %u %d %u %d", POSITIVE_INTEGERS); |
boro | 0:4beb2ea291ec | 45 | TEST_ASSERT_EQUAL_STRING("32768 3214 999 100 1 0 1 4231 999 4123 32760 99999", buffer); |
boro | 0:4beb2ea291ec | 46 | } |
boro | 0:4beb2ea291ec | 47 | |
boro | 0:4beb2ea291ec | 48 | void test_case_c_string_x_E() { |
boro | 0:4beb2ea291ec | 49 | CLEAN_BUFFER; |
boro | 0:4beb2ea291ec | 50 | sprintf(buffer, "%x %X %x %X %x %X %x %X %x %X %x %X", POSITIVE_INTEGERS); |
boro | 0:4beb2ea291ec | 51 | TEST_ASSERT_EQUAL_STRING("8000 C8E 3e7 64 1 0 1 1087 3e7 101B 7ff8 1869F", buffer); |
boro | 0:4beb2ea291ec | 52 | } |
boro | 0:4beb2ea291ec | 53 | |
boro | 0:4beb2ea291ec | 54 | void test_case_c_string_f_f() { |
boro | 0:4beb2ea291ec | 55 | CLEAN_BUFFER; |
boro | 0:4beb2ea291ec | 56 | sprintf(buffer, "%f %f %f %f %f %f %f %f %f %f", FLOATS); |
boro | 0:4beb2ea291ec | 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); |
boro | 0:4beb2ea291ec | 58 | } |
boro | 0:4beb2ea291ec | 59 | |
boro | 0:4beb2ea291ec | 60 | void test_case_c_string_g_g() { |
boro | 0:4beb2ea291ec | 61 | CLEAN_BUFFER; |
boro | 0:4beb2ea291ec | 62 | sprintf(buffer, "%g %g %g %g %g %g %g %g %g %g", FLOATS); |
boro | 0:4beb2ea291ec | 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); |
boro | 0:4beb2ea291ec | 64 | } |
boro | 0:4beb2ea291ec | 65 | |
boro | 0:4beb2ea291ec | 66 | void test_case_c_string_e_E() { |
boro | 0:4beb2ea291ec | 67 | CLEAN_BUFFER; |
boro | 0:4beb2ea291ec | 68 | sprintf(buffer, "%e %E %e %E %e %E %e %E %e %E", FLOATS); |
boro | 0:4beb2ea291ec | 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); |
boro | 0:4beb2ea291ec | 70 | } |
boro | 0:4beb2ea291ec | 71 | |
boro | 0:4beb2ea291ec | 72 | void test_case_c_string_strtok() { |
boro | 0:4beb2ea291ec | 73 | CLEAN_BUFFER; |
boro | 0:4beb2ea291ec | 74 | char str[] ="- This, a sample string."; |
boro | 0:4beb2ea291ec | 75 | char * pch = strtok (str," ,.-"); |
boro | 0:4beb2ea291ec | 76 | while (pch != NULL) { |
boro | 0:4beb2ea291ec | 77 | strcat(buffer, pch); |
boro | 0:4beb2ea291ec | 78 | pch = strtok (NULL, " ,.-"); |
boro | 0:4beb2ea291ec | 79 | } |
boro | 0:4beb2ea291ec | 80 | TEST_ASSERT_EQUAL_STRING("Thisasamplestring", buffer); |
boro | 0:4beb2ea291ec | 81 | } |
boro | 0:4beb2ea291ec | 82 | |
boro | 0:4beb2ea291ec | 83 | void test_case_c_string_strpbrk() { |
boro | 0:4beb2ea291ec | 84 | CLEAN_BUFFER; |
boro | 0:4beb2ea291ec | 85 | char str[] = "This is a sample string"; |
boro | 0:4beb2ea291ec | 86 | char key[] = "aeiou"; |
boro | 0:4beb2ea291ec | 87 | char *pch = strpbrk(str, key); |
boro | 0:4beb2ea291ec | 88 | while (pch != NULL) |
boro | 0:4beb2ea291ec | 89 | { |
boro | 0:4beb2ea291ec | 90 | char buf[2] = {*pch, '\0'}; |
boro | 0:4beb2ea291ec | 91 | strcat(buffer, buf); |
boro | 0:4beb2ea291ec | 92 | pch = strpbrk(pch + 1,key); |
boro | 0:4beb2ea291ec | 93 | } |
boro | 0:4beb2ea291ec | 94 | TEST_ASSERT_EQUAL_STRING("iiaaei", buffer); |
boro | 0:4beb2ea291ec | 95 | } |
boro | 0:4beb2ea291ec | 96 | |
boro | 0:4beb2ea291ec | 97 | utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { |
boro | 0:4beb2ea291ec | 98 | greentea_case_failure_abort_handler(source, reason); |
boro | 0:4beb2ea291ec | 99 | return STATUS_CONTINUE; |
boro | 0:4beb2ea291ec | 100 | } |
boro | 0:4beb2ea291ec | 101 | |
boro | 0:4beb2ea291ec | 102 | Case cases[] = { |
boro | 0:4beb2ea291ec | 103 | Case("C strings: strtok", test_case_c_string_strtok, greentea_failure_handler), |
boro | 0:4beb2ea291ec | 104 | Case("C strings: strpbrk", test_case_c_string_strpbrk, greentea_failure_handler), |
boro | 0:4beb2ea291ec | 105 | Case("C strings: %i %d integer formatting", test_case_c_string_i_d, greentea_failure_handler), |
boro | 0:4beb2ea291ec | 106 | Case("C strings: %u %d integer formatting", test_case_c_string_u_d, greentea_failure_handler), |
boro | 0:4beb2ea291ec | 107 | Case("C strings: %x %E integer formatting", test_case_c_string_x_E, greentea_failure_handler), |
boro | 0:4beb2ea291ec | 108 | Case("C strings: %f %f float formatting", test_case_c_string_f_f, greentea_failure_handler), |
boro | 0:4beb2ea291ec | 109 | Case("C strings: %e %E float formatting", test_case_c_string_e_E, greentea_failure_handler), |
boro | 0:4beb2ea291ec | 110 | Case("C strings: %g %g float formatting", test_case_c_string_g_g, greentea_failure_handler), |
boro | 0:4beb2ea291ec | 111 | }; |
boro | 0:4beb2ea291ec | 112 | |
boro | 0:4beb2ea291ec | 113 | utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { |
boro | 0:4beb2ea291ec | 114 | GREENTEA_SETUP(5, "default_auto"); |
boro | 0:4beb2ea291ec | 115 | return greentea_test_setup_handler(number_of_cases); |
boro | 0:4beb2ea291ec | 116 | } |
boro | 0:4beb2ea291ec | 117 | |
boro | 0:4beb2ea291ec | 118 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
boro | 0:4beb2ea291ec | 119 | |
boro | 0:4beb2ea291ec | 120 | int main() { |
boro | 0:4beb2ea291ec | 121 | Harness::run(specification); |
boro | 0:4beb2ea291ec | 122 | } |