Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* 00002 * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00006 * not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #include <stdio.h> 00018 #include <string.h> 00019 #include "mbed.h" 00020 #include "greentea-client/test_env.h" 00021 #include "unity/unity.h" 00022 #include "utest/utest.h" 00023 00024 namespace { 00025 static char buffer[256] = {0}; 00026 } 00027 00028 #define CLEAN_BUFFER memset(::buffer, 0x00, sizeof(::buffer)) 00029 #define NEGATIVE_INTEGERS -32768,-3214,-999,-100,-1,0,-1,-4231,-999,-4123,-32760,-99999 00030 #define POSITIVE_INTEGERS 32768,3214,999,100,1,0,1,4231,999,4123,32760,99999 00031 #define FLOATS 0.002,0.92430,15.91320,791.77368,6208.2,25719.4952,426815.982588,6429271.046,42468024.93,212006462.910 00032 00033 using namespace utest::v1; 00034 00035 00036 void test_case_c_string_i_d() { 00037 CLEAN_BUFFER; 00038 sprintf(buffer, "%i %d %i %d %i %d %i %d %i %d %i %i", NEGATIVE_INTEGERS); 00039 TEST_ASSERT_EQUAL_STRING("-32768 -3214 -999 -100 -1 0 -1 -4231 -999 -4123 -32760 -99999", buffer); 00040 } 00041 00042 void test_case_c_string_u_d() { 00043 CLEAN_BUFFER; 00044 sprintf(buffer, "%u %d %u %d %u %d %u %d %u %d %u %d", POSITIVE_INTEGERS); 00045 TEST_ASSERT_EQUAL_STRING("32768 3214 999 100 1 0 1 4231 999 4123 32760 99999", buffer); 00046 } 00047 00048 void test_case_c_string_x_E() { 00049 CLEAN_BUFFER; 00050 sprintf(buffer, "%x %X %x %X %x %X %x %X %x %X %x %X", POSITIVE_INTEGERS); 00051 TEST_ASSERT_EQUAL_STRING("8000 C8E 3e7 64 1 0 1 1087 3e7 101B 7ff8 1869F", buffer); 00052 } 00053 00054 void test_case_c_string_f_f() { 00055 CLEAN_BUFFER; 00056 sprintf(buffer, "%f %f %f %f %f %f %f %f %f %f", FLOATS); 00057 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); 00058 } 00059 00060 void test_case_c_string_g_g() { 00061 CLEAN_BUFFER; 00062 sprintf(buffer, "%g %g %g %g %g %g %g %g %g %g", FLOATS); 00063 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); 00064 } 00065 00066 void test_case_c_string_e_E() { 00067 CLEAN_BUFFER; 00068 sprintf(buffer, "%e %E %e %E %e %E %e %E %e %E", FLOATS); 00069 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); 00070 } 00071 00072 void test_case_c_string_strtok() { 00073 CLEAN_BUFFER; 00074 char str[] ="- This, a sample string."; 00075 char * pch = strtok (str," ,.-"); 00076 while (pch != NULL) { 00077 strcat(buffer, pch); 00078 pch = strtok (NULL, " ,.-"); 00079 } 00080 TEST_ASSERT_EQUAL_STRING("Thisasamplestring", buffer); 00081 } 00082 00083 void test_case_c_string_strpbrk() { 00084 CLEAN_BUFFER; 00085 char str[] = "This is a sample string"; 00086 char key[] = "aeiou"; 00087 char *pch = strpbrk(str, key); 00088 while (pch != NULL) 00089 { 00090 char buf[2] = {*pch, '\0'}; 00091 strcat(buffer, buf); 00092 pch = strpbrk(pch + 1,key); 00093 } 00094 TEST_ASSERT_EQUAL_STRING("iiaaei", buffer); 00095 } 00096 00097 utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { 00098 greentea_case_failure_abort_handler(source, reason); 00099 return STATUS_CONTINUE; 00100 } 00101 00102 Case cases[] = { 00103 Case("C strings: strtok", test_case_c_string_strtok, greentea_failure_handler), 00104 Case("C strings: strpbrk", test_case_c_string_strpbrk, greentea_failure_handler), 00105 Case("C strings: %i %d integer formatting", test_case_c_string_i_d, greentea_failure_handler), 00106 Case("C strings: %u %d integer formatting", test_case_c_string_u_d, greentea_failure_handler), 00107 Case("C strings: %x %E integer formatting", test_case_c_string_x_E, greentea_failure_handler), 00108 Case("C strings: %f %f float formatting", test_case_c_string_f_f, greentea_failure_handler), 00109 Case("C strings: %e %E float formatting", test_case_c_string_e_E, greentea_failure_handler), 00110 Case("C strings: %g %g float formatting", test_case_c_string_g_g, greentea_failure_handler), 00111 }; 00112 00113 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { 00114 GREENTEA_SETUP(5, "default_auto"); 00115 return greentea_test_setup_handler(number_of_cases); 00116 } 00117 00118 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); 00119 00120 int main() { 00121 Harness::run(specification); 00122 }
Generated on Sun Jul 17 2022 08:25:26 by 1.7.2