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