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.
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
test_m2mstring.cpp
00001 /* 00002 * Copyright (c) 2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "CppUTest/TestHarness.h" 00017 #include "test_m2mstring.h" 00018 #include <stdio.h> 00019 #include <string.h> 00020 00021 Test_M2MString::Test_M2MString() 00022 { 00023 str = new String("test"); 00024 } 00025 00026 Test_M2MString::~Test_M2MString() 00027 { 00028 delete str; 00029 } 00030 00031 void Test_M2MString::test_copy_constructor() 00032 { 00033 String s("name"); 00034 String s1(s); 00035 CHECK(s1.p[1] == 'a'); 00036 } 00037 00038 void Test_M2MString::test_operator_assign() 00039 { 00040 String s("name"); 00041 String s1 = s; 00042 const String s2("yes"); 00043 CHECK(s1.p[1] == 'a'); 00044 00045 s1 = "yeb"; 00046 CHECK(s1.p[1] == 'e'); 00047 s = s2; 00048 CHECK(s.p[1] == 'e'); 00049 } 00050 00051 void Test_M2MString::test_operator_add() 00052 { 00053 String s("name"); 00054 String s1("yeb"); 00055 00056 s += 'r'; 00057 CHECK(s.p[4] == 'r'); 00058 00059 s += s1; 00060 CHECK(s.p[5] == 'y'); 00061 00062 s += "hoi"; 00063 CHECK(s.p[9] == 'o'); 00064 00065 s1 += "somethingverylongggggggg"; 00066 00067 } 00068 00069 void Test_M2MString::test_push_back() 00070 { 00071 String s("name"); 00072 00073 s.push_back('r'); 00074 CHECK(s.p[4] == 'r'); 00075 } 00076 00077 void Test_M2MString::test_operator_equals() 00078 { 00079 String s("name"); 00080 String s1("yeb"); 00081 00082 CHECK( (s == s1) == false); 00083 CHECK( (s == "name") == true); 00084 const char c = NULL; 00085 CHECK( (s == c) == false); 00086 free(s.p); 00087 s.p = NULL; 00088 CHECK( (s == c) == true); 00089 } 00090 00091 void Test_M2MString::test_clear() 00092 { 00093 String s("name"); 00094 00095 s.clear(); 00096 CHECK(s.size_ == 0); 00097 } 00098 00099 void Test_M2MString::test_size() 00100 { 00101 String s("name"); 00102 String s1("yeb"); 00103 00104 CHECK(s.size() == 4); 00105 } 00106 00107 void Test_M2MString::test_length() 00108 { 00109 String s("name"); 00110 String s1("yeb"); 00111 00112 CHECK(s.length() == 4); 00113 } 00114 00115 void Test_M2MString::test_capacity() 00116 { 00117 String s("name"); 00118 String s1("yeb"); 00119 00120 CHECK(s.capacity() == 4); 00121 } 00122 00123 void Test_M2MString::test_empty() 00124 { 00125 String s("name"); 00126 String s1("yeb"); 00127 00128 CHECK(s.empty() == false); 00129 } 00130 00131 void Test_M2MString::test_c_str() 00132 { 00133 String s("name"); 00134 String s1("yeb"); 00135 00136 CHECK( s.c_str() != NULL); 00137 } 00138 00139 void Test_M2MString::test_reserve() 00140 { 00141 String s("name"); 00142 String s1("yeb"); 00143 00144 s.reserve(12); 00145 CHECK(s.allocated_ == 13); 00146 } 00147 00148 void Test_M2MString::test_resize() 00149 { 00150 String s("name"); 00151 String s1("yeb"); 00152 00153 s.resize(2); 00154 CHECK( s.size() == 2); 00155 00156 s.resize(12); 00157 CHECK( s.size() == 12); 00158 } 00159 00160 void Test_M2MString::test_swap() 00161 { 00162 String s("name"); 00163 String s1("yeb"); 00164 00165 s.swap(s1); 00166 CHECK(s1.p[1] == 'a'); 00167 00168 CHECK(s.p[1] == 'e'); 00169 } 00170 00171 void Test_M2MString::test_substr() 00172 { 00173 String s("name"); 00174 String s1("yeb"); 00175 00176 CHECK(s.substr(1, 1) == "a"); 00177 CHECK(s.substr(3, 4) == "e"); 00178 } 00179 00180 void Test_M2MString::test_operator_get() 00181 { 00182 String s("name"); 00183 const String s1("yeb"); 00184 00185 CHECK(s[1] == 'a'); 00186 CHECK(s1[1] == 'e'); 00187 } 00188 00189 void Test_M2MString::test_at() 00190 { 00191 String s("name"); 00192 const String s1("yeb"); 00193 00194 CHECK(s.at(1) == 'a'); 00195 CHECK(s.at(14) == '\0'); 00196 CHECK(s1.at(1) == 'e'); 00197 CHECK(s1.at(31) == '\0'); 00198 } 00199 00200 void Test_M2MString::test_erase() 00201 { 00202 String s("name"); 00203 String s1("yeb"); 00204 00205 s.erase(1,1); 00206 CHECK(s[1] == 'm'); 00207 } 00208 00209 void Test_M2MString::test_append() 00210 { 00211 String s("name"); 00212 String s1("yeb"); 00213 00214 s.append( s1.c_str(), 1 ); 00215 CHECK(s.size() == 5); 00216 00217 s.append( s1.c_str(), 15 ); 00218 CHECK(s.size() == 8); 00219 } 00220 00221 void Test_M2MString::test_append_raw() 00222 { 00223 String s("name"); 00224 const char test_source[] = "something"; 00225 String expected("namesomething"); 00226 00227 s.append_raw(test_source, 1); 00228 CHECK(s.size() == 5); 00229 00230 s.append_raw(test_source + 1, 8); 00231 CHECK(s.size() == 13); 00232 00233 CHECK(s == expected); 00234 } 00235 00236 void Test_M2MString::test_append_int() 00237 { 00238 String s("source"); 00239 String expected("source1234"); 00240 String expected2("source12342147483647"); 00241 00242 s.append_int(1234); 00243 CHECK(s.size() == 10); 00244 00245 CHECK(s == expected); 00246 00247 s.append_int(INT32_MAX); 00248 00249 CHECK(s == expected2); 00250 } 00251 00252 void Test_M2MString::test_compare() 00253 { 00254 String s("name"); 00255 String s1("yeb"); 00256 String s2("name"); 00257 String s3("nam"); 00258 00259 CHECK(s.compare(1,5, s1) < 0); 00260 CHECK(s1.compare(0,5, s2) > 0); 00261 CHECK(s.compare(0,4, s2) == 0); 00262 CHECK(s.compare(0,4, s3) > 0); 00263 00264 CHECK(s.compare(1,5, "yeb") < 0); 00265 CHECK(s1.compare(0,5, "name") > 0); 00266 CHECK(s.compare(0,4, "name") == 0); 00267 CHECK(s.compare(0,4, "nam") > 0); 00268 } 00269 00270 void Test_M2MString::test_find_last_of() 00271 { 00272 String s("namenamename"); 00273 String s1("yeb"); 00274 00275 CHECK(s.find_last_of('n') == 8); 00276 } 00277 00278 void Test_M2MString::test_operator_lt() 00279 { 00280 String s("name"); 00281 String s1("yeb"); 00282 String s2("yea"); 00283 00284 CHECK( (s < s1 ) == true); 00285 CHECK( (s1 < s2 ) == false); 00286 } 00287 void Test_M2MString::test_reverse() 00288 { 00289 char string1[] = "123"; 00290 char string2[] = "321"; 00291 m2m::reverse(string1, strlen(string1)); 00292 char string3[] = "9223372036854775807"; 00293 char string4[] = "7085774586302733229"; 00294 m2m::reverse(string3, strlen(string3)); 00295 00296 CHECK(strcmp(string1, string2) == 0); 00297 CHECK(strcmp(string3, string4) == 0); 00298 } 00299 void Test_M2MString::test_itoa_c() 00300 { 00301 int64_t value1 = 0; 00302 char* string1 = "0"; 00303 int64_t value2 = -10; 00304 char* string2 = "-10"; 00305 int64_t value3 = 10000; 00306 char* string3 = "10000"; 00307 int64_t value4 = 9223372036854775807; 00308 char* string4 = "9223372036854775807"; 00309 int64_t value5 = -9223372036854775807; 00310 char* string5 = "-9223372036854775807"; 00311 00312 char *buffer = (char*)malloc(21); 00313 00314 if(buffer) { 00315 m2m::itoa_c(value1, buffer); 00316 CHECK(strcmp(string1, buffer) == 0); 00317 m2m::itoa_c(value2, buffer); 00318 CHECK(strcmp(string2, buffer) == 0); 00319 m2m::itoa_c(value3, buffer); 00320 CHECK(strcmp(string3, buffer) == 0); 00321 m2m::itoa_c(value4, buffer); 00322 CHECK(strcmp(string4, buffer) == 0); 00323 m2m::itoa_c(value5, buffer); 00324 CHECK(strcmp(string5, buffer) == 0); 00325 free(buffer); 00326 } 00327 } 00328 00329 void Test_M2MString::test_convert_integer_to_array() 00330 { 00331 uint8_t *max_age_ptr = NULL; 00332 uint8_t max_age_len = 0; 00333 uint8_t *temp = NULL; 00334 uint8_t temp_len = 0; 00335 00336 int64_t val = 0; 00337 max_age_ptr = m2m::String::convert_integer_to_array(val,max_age_len); 00338 CHECK(max_age_ptr != NULL); 00339 CHECK(val == m2m::String::convert_array_to_integer(max_age_ptr, max_age_len)); 00340 temp = m2m::String::convert_integer_to_array(0,temp_len, max_age_ptr, max_age_len); 00341 CHECK(temp != NULL); 00342 CHECK(val == m2m::String::convert_array_to_integer(temp, temp_len)); 00343 free(temp); 00344 free(max_age_ptr); 00345 max_age_ptr = NULL; 00346 00347 val = 0xff; 00348 max_age_ptr = m2m::String::convert_integer_to_array(val,max_age_len); 00349 CHECK(max_age_ptr != NULL); 00350 CHECK(val == m2m::String::convert_array_to_integer(max_age_ptr, max_age_len)); 00351 temp = m2m::String::convert_integer_to_array(0,temp_len, max_age_ptr, max_age_len); 00352 CHECK(temp != NULL); 00353 CHECK(val == m2m::String::convert_array_to_integer(temp, temp_len)); 00354 free(temp); 00355 free(max_age_ptr); 00356 max_age_ptr = NULL; 00357 00358 val = 0xffff; 00359 max_age_ptr = m2m::String::convert_integer_to_array(val,max_age_len); 00360 CHECK(max_age_ptr != NULL); 00361 CHECK(val == m2m::String::convert_array_to_integer(max_age_ptr, max_age_len)); 00362 free(max_age_ptr); 00363 max_age_ptr = NULL; 00364 00365 val = 0xffffff; 00366 max_age_ptr = m2m::String::convert_integer_to_array(val,max_age_len); 00367 CHECK(max_age_ptr != NULL); 00368 CHECK(val == m2m::String::convert_array_to_integer(max_age_ptr, max_age_len)); 00369 free(max_age_ptr); 00370 max_age_ptr = NULL; 00371 00372 val = 0xffffffff; 00373 max_age_ptr = m2m::String::convert_integer_to_array(val,max_age_len); 00374 CHECK(max_age_ptr != NULL); 00375 CHECK(val == m2m::String::convert_array_to_integer(max_age_ptr, max_age_len)); 00376 free(max_age_ptr); 00377 max_age_ptr = NULL; 00378 00379 val = 0xffffffffff; 00380 max_age_ptr = m2m::String::convert_integer_to_array(val,max_age_len); 00381 CHECK(max_age_ptr != NULL); 00382 CHECK(val == m2m::String::convert_array_to_integer(max_age_ptr, max_age_len)); 00383 free(max_age_ptr); 00384 max_age_ptr = NULL; 00385 00386 val = 0xffffffffffff; 00387 max_age_ptr = m2m::String::convert_integer_to_array(val,max_age_len); 00388 CHECK(max_age_ptr != NULL); 00389 CHECK(val == m2m::String::convert_array_to_integer(max_age_ptr, max_age_len)); 00390 free(max_age_ptr); 00391 max_age_ptr = NULL; 00392 00393 val = 0xffffffffffffff; 00394 max_age_ptr = m2m::String::convert_integer_to_array(val,max_age_len); 00395 CHECK(max_age_ptr != NULL); 00396 CHECK(val == m2m::String::convert_array_to_integer(max_age_ptr, max_age_len)); 00397 free(max_age_ptr); 00398 max_age_ptr = NULL; 00399 00400 val = 0xffff; 00401 max_age_ptr = m2m::String::convert_integer_to_array(val,max_age_len); 00402 CHECK(max_age_ptr != NULL); 00403 CHECK(val == m2m::String::convert_array_to_integer(max_age_ptr, max_age_len)); 00404 free(max_age_ptr); 00405 max_age_ptr = NULL; 00406 00407 00408 00409 } 00410 00411 00412
Generated on Tue Jul 12 2022 12:28:54 by
1.7.2
