| 1 | /* |
| 2 | * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of the <organization> nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY |
| 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY |
| 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "CppUTest/TestHarness.h" |
| 29 | #include "CppUTest/SimpleString.h" |
| 30 | #include "CppUTest/PlatformSpecificFunctions.h" |
| 31 | #include "CppUTest/TestMemoryAllocator.h" |
| 32 | #include "CppUTest/MemoryLeakDetector.h" |
| 33 | #include "CppUTest/TestTestingFixture.h" |
| 34 | |
| 35 | class JustUseNewStringAllocator : public TestMemoryAllocator |
| 36 | { |
| 37 | public: |
| 38 | virtual ~JustUseNewStringAllocator() _destructor_override {} |
| 39 | |
| 40 | char* alloc_memory(size_t size, const char* file, size_t line) _override |
| 41 | { |
| 42 | return MemoryLeakWarningPlugin::getGlobalDetector()->allocMemory(getCurrentNewArrayAllocator(), size, file, line); |
| 43 | } |
| 44 | void free_memory(char* str, size_t, const char* file, size_t line) _override |
| 45 | { |
| 46 | MemoryLeakWarningPlugin::getGlobalDetector()->deallocMemory(getCurrentNewArrayAllocator(), str, file, line); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | class GlobalSimpleStringMemoryAccountantExecFunction |
| 51 | : public ExecFunction |
| 52 | { |
| 53 | public: |
| 54 | void (*testFunction_)(GlobalSimpleStringMemoryAccountant*); |
| 55 | GlobalSimpleStringMemoryAccountant* parameter_; |
| 56 | |
| 57 | virtual void exec() _override |
| 58 | { |
| 59 | testFunction_(parameter_); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | TEST_GROUP(GlobalSimpleStringMemoryAccountant) |
| 64 | { |
| 65 | GlobalSimpleStringAllocatorStash stash; |
| 66 | GlobalSimpleStringMemoryAccountantExecFunction testFunction; |
| 67 | TestTestingFixture fixture; |
| 68 | GlobalSimpleStringMemoryAccountant accountant; |
| 69 | |
| 70 | void setup() |
| 71 | { |
| 72 | stash.save(); |
| 73 | testFunction.parameter_ = &accountant; |
| 74 | fixture.setTestFunction(&testFunction); |
| 75 | } |
| 76 | |
| 77 | void teardown() |
| 78 | { |
| 79 | stash.restore(); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | TEST(GlobalSimpleStringMemoryAccountant, start) |
| 84 | { |
| 85 | accountant.start(); |
| 86 | POINTERS_EQUAL(accountant.getAllocator(), SimpleString::getStringAllocator()); |
| 87 | } |
| 88 | |
| 89 | TEST(GlobalSimpleStringMemoryAccountant, startTwiceDoesNothing) |
| 90 | { |
| 91 | accountant.start(); |
| 92 | TestMemoryAllocator* memoryAccountantAllocator = SimpleString::getStringAllocator(); |
| 93 | accountant.start(); |
| 94 | |
| 95 | POINTERS_EQUAL(memoryAccountantAllocator, SimpleString::getStringAllocator()); |
| 96 | |
| 97 | accountant.stop(); |
| 98 | } |
| 99 | |
| 100 | TEST(GlobalSimpleStringMemoryAccountant, stop) |
| 101 | { |
| 102 | TestMemoryAllocator* originalAllocator = SimpleString::getStringAllocator(); |
| 103 | accountant.start(); |
| 104 | accountant.stop(); |
| 105 | POINTERS_EQUAL(originalAllocator, SimpleString::getStringAllocator()); |
| 106 | } |
| 107 | |
| 108 | static void _stopAccountant(GlobalSimpleStringMemoryAccountant* accountant) |
| 109 | { |
| 110 | accountant->stop(); |
| 111 | } |
| 112 | |
| 113 | TEST(GlobalSimpleStringMemoryAccountant, stopWithoutStartWillFail) |
| 114 | { |
| 115 | testFunction.testFunction_ = _stopAccountant; |
| 116 | fixture.runAllTests(); |
| 117 | fixture.assertPrintContains("Global SimpleString allocator stopped without starting"); |
| 118 | } |
| 119 | |
| 120 | static void _changeAllocatorBetweenStartAndStop(GlobalSimpleStringMemoryAccountant* accountant) |
| 121 | { |
| 122 | TestMemoryAllocator* originalAllocator = SimpleString::getStringAllocator(); |
| 123 | accountant->start(); |
| 124 | SimpleString::setStringAllocator(originalAllocator); |
| 125 | accountant->stop(); |
| 126 | } |
| 127 | |
| 128 | TEST(GlobalSimpleStringMemoryAccountant, stopFailsWhenAllocatorWasChangedInBetween) |
| 129 | { |
| 130 | testFunction.testFunction_ = _changeAllocatorBetweenStartAndStop; |
| 131 | fixture.runAllTests(); |
| 132 | fixture.assertPrintContains("GlobalStrimpleStringMemoryAccountant: allocator has changed between start and stop!"); |
| 133 | } |
| 134 | |
| 135 | TEST(GlobalSimpleStringMemoryAccountant, report) |
| 136 | { |
| 137 | SimpleString str; |
| 138 | accountant.start(); |
| 139 | str += "More"; |
| 140 | accountant.stop(); |
| 141 | STRCMP_CONTAINS(" 1 0 1", accountant.report().asCharString()); |
| 142 | } |
| 143 | |
| 144 | TEST(GlobalSimpleStringMemoryAccountant, reportUseCaches) |
| 145 | { |
| 146 | size_t caches[] = {32}; |
| 147 | accountant.useCacheSizes(caches, 1); |
| 148 | SimpleString str; |
| 149 | accountant.start(); |
| 150 | str += "More"; |
| 151 | accountant.stop(); |
| 152 | STRCMP_CONTAINS("32 1 1 1", accountant.report().asCharString()); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | TEST_GROUP(SimpleString) |
| 157 | { |
| 158 | JustUseNewStringAllocator justNewForSimpleStringTestAllocator; |
| 159 | GlobalSimpleStringAllocatorStash stash; |
| 160 | void setup() |
| 161 | { |
| 162 | stash.save(); |
| 163 | SimpleString::setStringAllocator(&justNewForSimpleStringTestAllocator); |
| 164 | } |
| 165 | void teardown() |
| 166 | { |
| 167 | stash.restore(); |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | TEST(SimpleString, defaultAllocatorIsNewArrayAllocator) |
| 172 | { |
| 173 | SimpleString::setStringAllocator(NULLPTR); |
| 174 | POINTERS_EQUAL(defaultNewArrayAllocator(), SimpleString::getStringAllocator()); |
| 175 | } |
| 176 | |
| 177 | class MyOwnStringAllocator : public TestMemoryAllocator |
| 178 | { |
| 179 | public: |
| 180 | MyOwnStringAllocator() : memoryWasAllocated(false) {} |
| 181 | virtual ~MyOwnStringAllocator() _destructor_override {} |
| 182 | |
| 183 | bool memoryWasAllocated; |
| 184 | char* alloc_memory(size_t size, const char* file, size_t line) _override |
| 185 | { |
| 186 | memoryWasAllocated = true; |
| 187 | return TestMemoryAllocator::alloc_memory(size, file, line); |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | TEST(SimpleString, allocatorForSimpleStringCanBeReplaced) |
| 192 | { |
| 193 | MyOwnStringAllocator myOwnAllocator; |
| 194 | SimpleString::setStringAllocator(&myOwnAllocator); |
| 195 | SimpleString simpleString; |
| 196 | CHECK(myOwnAllocator.memoryWasAllocated); |
| 197 | SimpleString::setStringAllocator(NULLPTR); |
| 198 | } |
| 199 | |
| 200 | TEST(SimpleString, CreateSequence) |
| 201 | { |
| 202 | SimpleString expected("hellohello"); |
| 203 | SimpleString actual("hello", 2); |
| 204 | |
| 205 | CHECK_EQUAL(expected, actual); |
| 206 | } |
| 207 | |
| 208 | TEST(SimpleString, CreateSequenceOfZero) |
| 209 | { |
| 210 | SimpleString expected(""); |
| 211 | SimpleString actual("hello", 0); |
| 212 | |
| 213 | CHECK_EQUAL(expected, actual); |
| 214 | } |
| 215 | |
| 216 | TEST(SimpleString, Copy) |
| 217 | { |
| 218 | SimpleString s1("hello"); |
| 219 | SimpleString s2(s1); |
| 220 | |
| 221 | CHECK_EQUAL(s1, s2); |
| 222 | } |
| 223 | |
| 224 | TEST(SimpleString, Assignment) |
| 225 | { |
| 226 | SimpleString s1("hello"); |
| 227 | SimpleString s2("goodbye"); |
| 228 | |
| 229 | s2 = s1; |
| 230 | |
| 231 | CHECK_EQUAL(s1, s2); |
| 232 | } |
| 233 | |
| 234 | TEST(SimpleString, Equality) |
| 235 | { |
| 236 | SimpleString s1("hello"); |
| 237 | SimpleString s2("hello"); |
| 238 | |
| 239 | CHECK(s1 == s2); |
| 240 | } |
| 241 | |
| 242 | TEST(SimpleString, InEquality) |
| 243 | { |
| 244 | SimpleString s1("hello"); |
| 245 | SimpleString s2("goodbye"); |
| 246 | |
| 247 | CHECK(s1 != s2); |
| 248 | } |
| 249 | |
| 250 | TEST(SimpleString, CompareNoCaseWithoutCase) |
| 251 | { |
| 252 | SimpleString s1("hello"); |
| 253 | SimpleString s2("hello"); |
| 254 | |
| 255 | CHECK(s1.equalsNoCase(s2)); |
| 256 | } |
| 257 | |
| 258 | TEST(SimpleString, CompareNoCaseWithCase) |
| 259 | { |
| 260 | SimpleString s1("hello"); |
| 261 | SimpleString s2("HELLO"); |
| 262 | |
| 263 | CHECK(s1.equalsNoCase(s2)); |
| 264 | } |
| 265 | |
| 266 | TEST(SimpleString, CompareNoCaseWithCaseNotEqual) |
| 267 | { |
| 268 | SimpleString s1("hello"); |
| 269 | SimpleString s2("WORLD"); |
| 270 | |
| 271 | CHECK(!s1.equalsNoCase(s2)); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | TEST(SimpleString, asCharString) |
| 276 | { |
| 277 | SimpleString s1("hello"); |
| 278 | |
| 279 | STRCMP_EQUAL("hello", s1.asCharString()); |
| 280 | } |
| 281 | |
| 282 | TEST(SimpleString, Size) |
| 283 | { |
| 284 | SimpleString s1("hello!"); |
| 285 | |
| 286 | LONGS_EQUAL(6, s1.size()); |
| 287 | } |
| 288 | |
| 289 | TEST(SimpleString, lowerCase) |
| 290 | { |
| 291 | SimpleString s1("AbCdEfG1234"); |
| 292 | SimpleString s2(s1.lowerCase()); |
| 293 | STRCMP_EQUAL("abcdefg1234", s2.asCharString()); |
| 294 | STRCMP_EQUAL("AbCdEfG1234", s1.asCharString()); |
| 295 | } |
| 296 | |
| 297 | TEST(SimpleString, Addition) |
| 298 | { |
| 299 | SimpleString s1("hello!"); |
| 300 | SimpleString s2("goodbye!"); |
| 301 | SimpleString s3("hello!goodbye!"); |
| 302 | SimpleString s4; |
| 303 | s4 = s1 + s2; |
| 304 | |
| 305 | CHECK_EQUAL(s3, s4); |
| 306 | } |
| 307 | |
| 308 | TEST(SimpleString, Concatenation) |
| 309 | { |
| 310 | SimpleString s1("hello!"); |
| 311 | SimpleString s2("goodbye!"); |
| 312 | SimpleString s3("hello!goodbye!"); |
| 313 | SimpleString s4; |
| 314 | s4 += s1; |
| 315 | s4 += s2; |
| 316 | |
| 317 | CHECK_EQUAL(s3, s4); |
| 318 | |
| 319 | SimpleString s5("hello!goodbye!hello!goodbye!"); |
| 320 | s4 += s4; |
| 321 | |
| 322 | CHECK_EQUAL(s5, s4); |
| 323 | } |
| 324 | |
| 325 | TEST(SimpleString, Contains) |
| 326 | { |
| 327 | SimpleString s("hello!"); |
| 328 | SimpleString empty(""); |
| 329 | SimpleString beginning("hello"); |
| 330 | SimpleString end("lo!"); |
| 331 | SimpleString mid("l"); |
| 332 | SimpleString notPartOfString("xxxx"); |
| 333 | |
| 334 | CHECK(s.contains(empty)); |
| 335 | CHECK(s.contains(beginning)); |
| 336 | CHECK(s.contains(end)); |
| 337 | CHECK(s.contains(mid)); |
| 338 | CHECK(!s.contains(notPartOfString)); |
| 339 | |
| 340 | CHECK(empty.contains(empty)); |
| 341 | CHECK(!empty.contains(s)); |
| 342 | } |
| 343 | |
| 344 | TEST(SimpleString, startsWith) |
| 345 | { |
| 346 | SimpleString hi("Hi you!"); |
| 347 | SimpleString part("Hi"); |
| 348 | SimpleString diff("Hrrm Hi you! ffdsfd"); |
| 349 | CHECK(hi.startsWith(part)); |
| 350 | CHECK(!part.startsWith(hi)); |
| 351 | CHECK(!diff.startsWith(hi)); |
| 352 | } |
| 353 | |
| 354 | TEST(SimpleString, split) |
| 355 | { |
| 356 | SimpleString hi("hello\nworld\nhow\ndo\nyou\ndo\n\n"); |
| 357 | |
| 358 | SimpleStringCollection collection; |
| 359 | hi.split("\n", collection); |
| 360 | |
| 361 | LONGS_EQUAL(7, collection.size()); |
| 362 | STRCMP_EQUAL("hello\n", collection[0].asCharString()); |
| 363 | STRCMP_EQUAL("world\n", collection[1].asCharString()); |
| 364 | STRCMP_EQUAL("how\n", collection[2].asCharString()); |
| 365 | STRCMP_EQUAL("do\n", collection[3].asCharString()); |
| 366 | STRCMP_EQUAL("you\n", collection[4].asCharString()); |
| 367 | STRCMP_EQUAL("do\n", collection[5].asCharString()); |
| 368 | STRCMP_EQUAL("\n", collection[6].asCharString()); |
| 369 | } |
| 370 | |
| 371 | TEST(SimpleString, splitNoTokenOnTheEnd) |
| 372 | { |
| 373 | SimpleString string("Bah Yah oops"); |
| 374 | SimpleStringCollection collection; |
| 375 | |
| 376 | string.split(" ", collection); |
| 377 | LONGS_EQUAL(3, collection.size()); |
| 378 | STRCMP_EQUAL("Bah ", collection[0].asCharString()); |
| 379 | STRCMP_EQUAL("Yah ", collection[1].asCharString()); |
| 380 | STRCMP_EQUAL("oops", collection[2].asCharString()); |
| 381 | } |
| 382 | |
| 383 | TEST(SimpleString, count) |
| 384 | { |
| 385 | SimpleString str("ha ha ha ha"); |
| 386 | LONGS_EQUAL(4, str.count("ha")); |
| 387 | } |
| 388 | |
| 389 | TEST(SimpleString, countTogether) |
| 390 | { |
| 391 | SimpleString str("hahahaha"); |
| 392 | LONGS_EQUAL(4, str.count("ha")); |
| 393 | } |
| 394 | |
| 395 | TEST(SimpleString, countEmptyString) |
| 396 | { |
| 397 | SimpleString str("hahahaha"); |
| 398 | LONGS_EQUAL(8, str.count("")); |
| 399 | } |
| 400 | |
| 401 | TEST(SimpleString, countEmptyStringInEmptyString) |
| 402 | { |
| 403 | SimpleString str; |
| 404 | LONGS_EQUAL(0, str.count("")); |
| 405 | } |
| 406 | |
| 407 | TEST(SimpleString, endsWith) |
| 408 | { |
| 409 | SimpleString str("Hello World"); |
| 410 | CHECK(str.endsWith("World")); |
| 411 | CHECK(!str.endsWith("Worl")); |
| 412 | CHECK(!str.endsWith("Hello")); |
| 413 | SimpleString str2("ah"); |
| 414 | CHECK(str2.endsWith("ah")); |
| 415 | CHECK(!str2.endsWith("baah")); |
| 416 | SimpleString str3(""); |
| 417 | CHECK(!str3.endsWith("baah")); |
| 418 | |
| 419 | SimpleString str4("ha ha ha ha"); |
| 420 | CHECK(str4.endsWith("ha")); |
| 421 | } |
| 422 | |
| 423 | TEST(SimpleString, replaceCharWithChar) |
| 424 | { |
| 425 | SimpleString str("abcabcabca"); |
| 426 | str.replace('a', 'b'); |
| 427 | STRCMP_EQUAL("bbcbbcbbcb", str.asCharString()); |
| 428 | } |
| 429 | |
| 430 | TEST(SimpleString, replaceEmptyStringWithEmptyString) |
| 431 | { |
| 432 | SimpleString str; |
| 433 | str.replace("", ""); |
| 434 | STRCMP_EQUAL("", str.asCharString()); |
| 435 | } |
| 436 | |
| 437 | TEST(SimpleString, replaceWholeString) |
| 438 | { |
| 439 | SimpleString str("boo"); |
| 440 | str.replace("boo", ""); |
| 441 | STRCMP_EQUAL("", str.asCharString()); |
| 442 | } |
| 443 | |
| 444 | TEST(SimpleString, replaceStringWithString) |
| 445 | { |
| 446 | SimpleString str("boo baa boo baa boo"); |
| 447 | str.replace("boo", "boohoo"); |
| 448 | STRCMP_EQUAL("boohoo baa boohoo baa boohoo", str.asCharString()); |
| 449 | } |
| 450 | |
| 451 | TEST(SimpleString, subStringFromEmptyString) |
| 452 | { |
| 453 | SimpleString str(""); |
| 454 | STRCMP_EQUAL("", str.subString(0, 1).asCharString()); |
| 455 | } |
| 456 | |
| 457 | TEST(SimpleString, subStringFromSmallString) |
| 458 | { |
| 459 | SimpleString str("H"); |
| 460 | STRCMP_EQUAL("H", str.subString(0, 1).asCharString()); |
| 461 | } |
| 462 | |
| 463 | TEST(SimpleString, subStringFromPos0) |
| 464 | { |
| 465 | SimpleString str("Hello World"); |
| 466 | STRCMP_EQUAL("Hello", str.subString(0, 5).asCharString()); |
| 467 | } |
| 468 | |
| 469 | TEST(SimpleString, subStringFromPos1) |
| 470 | { |
| 471 | SimpleString str("Hello World"); |
| 472 | STRCMP_EQUAL("ello ", str.subString(1, 5).asCharString()); |
| 473 | } |
| 474 | |
| 475 | TEST(SimpleString, subStringFromPos5WithAmountLargerThanString) |
| 476 | { |
| 477 | SimpleString str("Hello World"); |
| 478 | STRCMP_EQUAL("World", str.subString(6, 10).asCharString()); |
| 479 | } |
| 480 | |
| 481 | TEST(SimpleString, subStringFromPos6ToEndOfString) |
| 482 | { |
| 483 | SimpleString str("Hello World"); |
| 484 | STRCMP_EQUAL("World", str.subString(6).asCharString()); |
| 485 | } |
| 486 | |
| 487 | TEST(SimpleString, subStringBeginPosOutOfBounds) |
| 488 | { |
| 489 | SimpleString str("Hello World"); |
| 490 | STRCMP_EQUAL("", str.subString(13, 5).asCharString()); |
| 491 | } |
| 492 | |
| 493 | TEST(SimpleString, subStringFromTillNormal) |
| 494 | { |
| 495 | SimpleString str("Hello World"); |
| 496 | STRCMP_EQUAL("Hello", str.subStringFromTill('H', ' ').asCharString()); |
| 497 | } |
| 498 | |
| 499 | TEST(SimpleString, subStringFromTillOutOfBounds) |
| 500 | { |
| 501 | SimpleString str("Hello World"); |
| 502 | STRCMP_EQUAL("World", str.subStringFromTill('W', '!').asCharString()); |
| 503 | } |
| 504 | |
| 505 | TEST(SimpleString, subStringFromTillStartDoesntExist) |
| 506 | { |
| 507 | SimpleString str("Hello World"); |
| 508 | STRCMP_EQUAL("", str.subStringFromTill('!', ' ').asCharString()); |
| 509 | } |
| 510 | |
| 511 | TEST(SimpleString, subStringFromTillWhenTheEndAppearsBeforeTheStart) |
| 512 | { |
| 513 | SimpleString str("Hello World"); |
| 514 | STRCMP_EQUAL("World", str.subStringFromTill('W', 'H').asCharString()); |
| 515 | } |
| 516 | |
| 517 | TEST(SimpleString, findNormal) |
| 518 | { |
| 519 | SimpleString str("Hello World"); |
| 520 | LONGS_EQUAL(0, str.find('H')); |
| 521 | LONGS_EQUAL(1, str.find('e')); |
| 522 | LONGS_EQUAL(SimpleString::npos, str.find('!')); |
| 523 | } |
| 524 | |
| 525 | TEST(SimpleString, at) |
| 526 | { |
| 527 | SimpleString str("Hello World"); |
| 528 | BYTES_EQUAL('H', str.at(0)); |
| 529 | } |
| 530 | |
| 531 | TEST(SimpleString, copyInBufferNormal) |
| 532 | { |
| 533 | SimpleString str("Hello World"); |
| 534 | size_t bufferSize = str.size()+1; |
| 535 | char* buffer = (char*) PlatformSpecificMalloc(bufferSize); |
| 536 | str.copyToBuffer(buffer, bufferSize); |
| 537 | STRCMP_EQUAL(str.asCharString(), buffer); |
| 538 | PlatformSpecificFree(buffer); |
| 539 | } |
| 540 | |
| 541 | TEST(SimpleString, copyInBufferWithEmptyBuffer) |
| 542 | { |
| 543 | SimpleString str("Hello World"); |
| 544 | char* buffer= NULLPTR; |
| 545 | str.copyToBuffer(buffer, 0); |
| 546 | POINTERS_EQUAL(NULLPTR, buffer); |
| 547 | } |
| 548 | |
| 549 | TEST(SimpleString, copyInBufferWithBiggerBufferThanNeeded) |
| 550 | { |
| 551 | SimpleString str("Hello"); |
| 552 | size_t bufferSize = 20; |
| 553 | char* buffer= (char*) PlatformSpecificMalloc(bufferSize); |
| 554 | str.copyToBuffer(buffer, bufferSize); |
| 555 | STRCMP_EQUAL(str.asCharString(), buffer); |
| 556 | PlatformSpecificFree(buffer); |
| 557 | } |
| 558 | |
| 559 | TEST(SimpleString, copyInBufferWithSmallerBufferThanNeeded) |
| 560 | { |
| 561 | SimpleString str("Hello"); |
| 562 | size_t bufferSize = str.size(); |
| 563 | char* buffer= (char*) PlatformSpecificMalloc(bufferSize); |
| 564 | str.copyToBuffer(buffer, bufferSize); |
| 565 | STRNCMP_EQUAL(str.asCharString(), buffer, (bufferSize-1)); |
| 566 | LONGS_EQUAL(0, buffer[bufferSize-1]); |
| 567 | PlatformSpecificFree(buffer); |
| 568 | } |
| 569 | |
| 570 | TEST(SimpleString, ContainsNull) |
| 571 | { |
| 572 | SimpleString s(NULLPTR); |
| 573 | STRCMP_EQUAL("", s.asCharString()); |
| 574 | } |
| 575 | |
| 576 | TEST(SimpleString, NULLReportsNullString) |
| 577 | { |
| 578 | STRCMP_EQUAL("(null)", StringFromOrNull((char*) NULLPTR).asCharString()); |
| 579 | } |
| 580 | |
| 581 | TEST(SimpleString, Booleans) |
| 582 | { |
| 583 | SimpleString s1(StringFrom(true)); |
| 584 | SimpleString s2(StringFrom(false)); |
| 585 | CHECK(s1 == "true"); |
| 586 | CHECK(s2 == "false"); |
| 587 | } |
| 588 | |
| 589 | TEST(SimpleString, Pointers) |
| 590 | { |
| 591 | SimpleString s(StringFrom((void *)0x1234)); |
| 592 | STRCMP_EQUAL("0x1234", s.asCharString()); |
| 593 | } |
| 594 | |
| 595 | TEST(SimpleString, FunctionPointers) |
| 596 | { |
| 597 | SimpleString s(StringFrom((void (*)())0x1234)); |
| 598 | STRCMP_EQUAL("0x1234", s.asCharString()); |
| 599 | } |
| 600 | |
| 601 | TEST(SimpleString, Characters) |
| 602 | { |
| 603 | SimpleString s(StringFrom('a')); |
| 604 | STRCMP_EQUAL("a", s.asCharString()); |
| 605 | } |
| 606 | |
| 607 | TEST(SimpleString, NegativeSignedBytes) |
| 608 | { |
| 609 | STRCMP_EQUAL("-15", StringFrom((signed char)-15).asCharString()); |
| 610 | } |
| 611 | |
| 612 | TEST(SimpleString, PositiveSignedBytes) |
| 613 | { |
| 614 | STRCMP_EQUAL("4", StringFrom(4).asCharString()); |
| 615 | } |
| 616 | |
| 617 | TEST(SimpleString, LongInts) |
| 618 | { |
| 619 | SimpleString s(StringFrom((long)1)); |
| 620 | CHECK(s == "1"); |
| 621 | } |
| 622 | |
| 623 | TEST(SimpleString, UnsignedLongInts) |
| 624 | { |
| 625 | SimpleString s(StringFrom((unsigned long)1)); |
| 626 | SimpleString s2(StringFrom((unsigned long)1)); |
| 627 | CHECK(s == s2); |
| 628 | } |
| 629 | |
| 630 | #ifdef CPPUTEST_USE_LONG_LONG |
| 631 | |
| 632 | TEST(SimpleString, LongLongInts) |
| 633 | { |
| 634 | SimpleString s(StringFrom((long long)1)); |
| 635 | CHECK(s == "1"); |
| 636 | } |
| 637 | |
| 638 | TEST(SimpleString, UnsignedLongLongInts) |
| 639 | { |
| 640 | SimpleString s(StringFrom((unsigned long long)1)); |
| 641 | SimpleString s2(StringFrom((unsigned long long)1)); |
| 642 | CHECK(s == s2); |
| 643 | } |
| 644 | |
| 645 | #endif /* CPPUTEST_USE_LONG_LONG */ |
| 646 | |
| 647 | TEST(SimpleString, Doubles) |
| 648 | { |
| 649 | SimpleString s(StringFrom(1.2)); |
| 650 | STRCMP_EQUAL("1.2", s.asCharString()); |
| 651 | } |
| 652 | |
| 653 | extern "C" { |
| 654 | static int alwaysTrue(double) { return true; } |
| 655 | } |
| 656 | |
| 657 | TEST(SimpleString, NaN) |
| 658 | { |
| 659 | UT_PTR_SET(PlatformSpecificIsNan, alwaysTrue); |
| 660 | SimpleString s(StringFrom(0.0)); |
| 661 | STRCMP_EQUAL("Nan - Not a number", s.asCharString()); |
| 662 | } |
| 663 | |
| 664 | TEST(SimpleString, Inf) |
| 665 | { |
| 666 | UT_PTR_SET(PlatformSpecificIsInf, alwaysTrue); |
| 667 | SimpleString s(StringFrom(0.0)); |
| 668 | STRCMP_EQUAL("Inf - Infinity", s.asCharString()); |
| 669 | } |
| 670 | |
| 671 | TEST(SimpleString, SmallDoubles) |
| 672 | { |
| 673 | SimpleString s(StringFrom(1.2e-10)); |
| 674 | STRCMP_CONTAINS("1.2e", s.asCharString()); |
| 675 | } |
| 676 | |
| 677 | TEST(SimpleString, Sizes) |
| 678 | { |
| 679 | size_t size = 10; |
| 680 | STRCMP_EQUAL("10", StringFrom((int) size).asCharString()); |
| 681 | } |
| 682 | |
| 683 | #if __cplusplus > 199711L && !defined __arm__ && CPPUTEST_USE_STD_CPP_LIB |
| 684 | |
| 685 | TEST(SimpleString, nullptr_type) |
| 686 | { |
| 687 | SimpleString s(StringFrom(nullptr)); |
| 688 | STRCMP_EQUAL("(null)", s.asCharString()); |
| 689 | } |
| 690 | |
| 691 | #endif |
| 692 | |
| 693 | TEST(SimpleString, HexStrings) |
| 694 | { |
| 695 | STRCMP_EQUAL("f3", HexStringFrom((signed char)-13).asCharString()); |
| 696 | |
| 697 | SimpleString h1 = HexStringFrom(0xffffL); |
| 698 | STRCMP_EQUAL("ffff", h1.asCharString()); |
| 699 | |
| 700 | #ifdef CPPUTEST_USE_LONG_LONG |
| 701 | SimpleString h15 = HexStringFrom(0xffffLL); |
| 702 | STRCMP_EQUAL("ffff", h15.asCharString()); |
| 703 | #endif |
| 704 | |
| 705 | SimpleString h2 = HexStringFrom((void *)0xfffeL); |
| 706 | STRCMP_EQUAL("fffe", h2.asCharString()); |
| 707 | |
| 708 | SimpleString h3 = HexStringFrom((void (*)())0xfffdL); |
| 709 | STRCMP_EQUAL("fffd", h3.asCharString()); |
| 710 | } |
| 711 | |
| 712 | TEST(SimpleString, StringFromFormat) |
| 713 | { |
| 714 | SimpleString h1 = StringFromFormat("%s %s! %d", "Hello", "World", 2009); |
| 715 | STRCMP_EQUAL("Hello World! 2009", h1.asCharString()); |
| 716 | } |
| 717 | |
| 718 | TEST(SimpleString, StringFromFormatpointer) |
| 719 | { |
| 720 | //this is not a great test. but %p is odd on mingw and even more odd on Solaris. |
| 721 | SimpleString h1 = StringFromFormat("%p", (void*) 1); |
| 722 | if (h1.size() == 3) |
| 723 | STRCMP_EQUAL("0x1", h1.asCharString()); |
| 724 | else if (h1.size() == 8) |
| 725 | STRCMP_EQUAL("00000001", h1.asCharString()); |
| 726 | else if (h1.size() == 9) |
| 727 | STRCMP_EQUAL("0000:0001", h1.asCharString()); |
| 728 | else if (h1.size() == 16) |
| 729 | STRCMP_EQUAL("0000000000000001", h1.asCharString()); |
| 730 | else if (h1.size() == 1) |
| 731 | STRCMP_EQUAL("1", h1.asCharString()); |
| 732 | else |
| 733 | FAIL("Off %p behavior"); |
| 734 | } |
| 735 | |
| 736 | TEST(SimpleString, StringFromFormatLarge) |
| 737 | { |
| 738 | const char* s = "ThisIsAPrettyLargeStringAndIfWeAddThisManyTimesToABufferItWillbeFull"; |
| 739 | SimpleString h1 = StringFromFormat("%s%s%s%s%s%s%s%s%s%s", s, s, s, s, s, s, s, s, s, s); |
| 740 | LONGS_EQUAL(10, h1.count(s)); |
| 741 | } |
| 742 | |
| 743 | TEST(SimpleString, StringFromConstSimpleString) |
| 744 | { |
| 745 | STRCMP_EQUAL("bla", StringFrom(SimpleString("bla")).asCharString()); |
| 746 | } |
| 747 | |
| 748 | static int WrappedUpVSNPrintf(char* buf, size_t n, const char* format, ...) |
| 749 | { |
| 750 | va_list arguments; |
| 751 | va_start(arguments, format); |
| 752 | |
| 753 | int result = PlatformSpecificVSNprintf(buf, n, format, arguments); |
| 754 | va_end(arguments); |
| 755 | return result; |
| 756 | } |
| 757 | |
| 758 | TEST(SimpleString, PlatformSpecificSprintf_fits) |
| 759 | { |
| 760 | char buf[10]; |
| 761 | |
| 762 | int count = WrappedUpVSNPrintf(buf, sizeof(buf), "%s", "12345"); |
| 763 | STRCMP_EQUAL("12345", buf); |
| 764 | LONGS_EQUAL(5, count); |
| 765 | } |
| 766 | |
| 767 | TEST(SimpleString, PlatformSpecificSprintf_doesNotFit) |
| 768 | { |
| 769 | char buf[10]; |
| 770 | |
| 771 | int count = WrappedUpVSNPrintf(buf, sizeof(buf), "%s", "12345678901"); |
| 772 | STRCMP_EQUAL("123456789", buf); |
| 773 | LONGS_EQUAL(11, count); |
| 774 | } |
| 775 | |
| 776 | TEST(SimpleString, PadStringsToSameLengthString1Larger) |
| 777 | { |
| 778 | SimpleString str1("1"); |
| 779 | SimpleString str2("222"); |
| 780 | |
| 781 | SimpleString::padStringsToSameLength(str1, str2, '4'); |
| 782 | STRCMP_EQUAL("441", str1.asCharString()); |
| 783 | STRCMP_EQUAL("222", str2.asCharString()); |
| 784 | } |
| 785 | |
| 786 | TEST(SimpleString, PadStringsToSameLengthString2Larger) |
| 787 | { |
| 788 | SimpleString str1(" "); |
| 789 | SimpleString str2(""); |
| 790 | |
| 791 | SimpleString::padStringsToSameLength(str1, str2, ' '); |
| 792 | STRCMP_EQUAL(" ", str1.asCharString()); |
| 793 | STRCMP_EQUAL(" ", str2.asCharString()); |
| 794 | } |
| 795 | |
| 796 | TEST(SimpleString, PadStringsToSameLengthWithSameLengthStrings) |
| 797 | { |
| 798 | SimpleString str1("123"); |
| 799 | SimpleString str2("123"); |
| 800 | |
| 801 | SimpleString::padStringsToSameLength(str1, str2, ' '); |
| 802 | STRCMP_EQUAL("123", str1.asCharString()); |
| 803 | STRCMP_EQUAL("123", str2.asCharString()); |
| 804 | } |
| 805 | |
| 806 | TEST(SimpleString, NullParameters2) |
| 807 | { |
| 808 | SimpleString* arr = new SimpleString[100]; |
| 809 | delete[] arr; |
| 810 | } |
| 811 | |
| 812 | TEST(SimpleString, CollectionMultipleAllocateNoLeaksMemory) |
| 813 | { |
| 814 | SimpleStringCollection col; |
| 815 | col.allocate(5); |
| 816 | col.allocate(5); |
| 817 | // CHECK no memory leak |
| 818 | } |
| 819 | |
| 820 | TEST(SimpleString, CollectionReadOutOfBoundsReturnsEmptyString) |
| 821 | { |
| 822 | SimpleStringCollection col; |
| 823 | col.allocate(3); |
| 824 | STRCMP_EQUAL("", col[3].asCharString()); |
| 825 | } |
| 826 | |
| 827 | TEST(SimpleString, CollectionWritingToEmptyString) |
| 828 | { |
| 829 | SimpleStringCollection col; |
| 830 | col.allocate(3); |
| 831 | col[3] = SimpleString("HAH"); |
| 832 | STRCMP_EQUAL("", col[3].asCharString()); |
| 833 | } |
| 834 | |
| 835 | #ifdef CPPUTEST_64BIT |
| 836 | |
| 837 | TEST(SimpleString, _64BitAddressPrintsCorrectly) |
| 838 | { |
| 839 | char* p = (char*) 0x0012345678901234; |
| 840 | SimpleString expected("0x12345678901234"); |
| 841 | SimpleString actual = StringFrom((void*)p); |
| 842 | STRCMP_EQUAL(expected.asCharString(), actual.asCharString()); |
| 843 | } |
| 844 | |
| 845 | #ifndef CPPUTEST_64BIT_32BIT_LONGS |
| 846 | |
| 847 | TEST(SimpleString, BracketsFormattedHexStringFromForLongOnDifferentPlatform) |
| 848 | { |
| 849 | long value = -1; |
| 850 | |
| 851 | STRCMP_EQUAL("(0xffffffffffffffff)", BracketsFormattedHexStringFrom(value).asCharString()); |
| 852 | } |
| 853 | |
| 854 | #else |
| 855 | |
| 856 | TEST(SimpleString, BracketsFormattedHexStringFromForLongOnDifferentPlatform) |
| 857 | { |
| 858 | long value = -1; |
| 859 | |
| 860 | STRCMP_EQUAL("(0xffffffff)", BracketsFormattedHexStringFrom(value).asCharString()); |
| 861 | } |
| 862 | |
| 863 | #endif |
| 864 | #else |
| 865 | /* |
| 866 | * This test case cannot pass on 32 bit systems. |
| 867 | */ |
| 868 | IGNORE_TEST(SimpleString, _64BitAddressPrintsCorrectly) |
| 869 | { |
| 870 | } |
| 871 | |
| 872 | TEST(SimpleString, BracketsFormattedHexStringFromForLongOnDifferentPlatform) |
| 873 | { |
| 874 | long value = -1; |
| 875 | |
| 876 | STRCMP_EQUAL("(0xffffffff)", BracketsFormattedHexStringFrom(value).asCharString()); |
| 877 | } |
| 878 | #endif |
| 879 | |
| 880 | TEST(SimpleString, BuildStringFromUnsignedLongInteger) |
| 881 | { |
| 882 | unsigned long int i = 0xffffffff; |
| 883 | |
| 884 | SimpleString result = StringFrom(i); |
| 885 | const char* expected_string = "4294967295"; |
| 886 | CHECK_EQUAL(expected_string, result); |
| 887 | } |
| 888 | |
| 889 | TEST(SimpleString, BuildStringFromUnsignedInteger) |
| 890 | { |
| 891 | unsigned int i = 0xff; |
| 892 | |
| 893 | SimpleString result = StringFrom(i); |
| 894 | const char* expected_string = "255"; |
| 895 | CHECK_EQUAL(expected_string, result); |
| 896 | } |
| 897 | |
| 898 | #if CPPUTEST_USE_STD_CPP_LIB |
| 899 | |
| 900 | TEST(SimpleString, fromStdString) |
| 901 | { |
| 902 | std::string s("hello"); |
| 903 | SimpleString s1(StringFrom(s)); |
| 904 | |
| 905 | STRCMP_EQUAL("hello", s1.asCharString()); |
| 906 | } |
| 907 | |
| 908 | TEST(SimpleString, CHECK_EQUAL_unsigned_long) |
| 909 | { |
| 910 | unsigned long i = 0xffffffffUL; |
| 911 | CHECK_EQUAL(i, i); |
| 912 | } |
| 913 | |
| 914 | TEST(SimpleString, unsigned_long) |
| 915 | { |
| 916 | unsigned long i = 0xffffffffUL; |
| 917 | |
| 918 | SimpleString result = StringFrom(i); |
| 919 | const char* expected_string = "4294967295"; |
| 920 | CHECK_EQUAL(expected_string, result); |
| 921 | } |
| 922 | |
| 923 | #endif |
| 924 | |
| 925 | TEST(SimpleString, StrCmp) |
| 926 | { |
| 927 | char empty[] = ""; |
| 928 | char blabla[] = "blabla"; |
| 929 | char bla[] = "bla"; |
| 930 | CHECK(SimpleString::StrCmp(empty, empty) == 0); |
| 931 | CHECK(SimpleString::StrCmp(bla, blabla) == -(int)'b'); |
| 932 | CHECK(SimpleString::StrCmp(blabla, bla) == 'b'); |
| 933 | CHECK(SimpleString::StrCmp(bla, empty) == 'b'); |
| 934 | CHECK(SimpleString::StrCmp(empty, bla) == -(int)'b'); |
| 935 | CHECK(SimpleString::StrCmp(bla, bla) == 0); |
| 936 | } |
| 937 | |
| 938 | TEST(SimpleString, StrNCpy_no_zero_termination) |
| 939 | { |
| 940 | char str[] = "XXXXXXXXXX"; |
| 941 | STRCMP_EQUAL("womanXXXXX", SimpleString::StrNCpy(str, "woman", 5)); |
| 942 | } |
| 943 | |
| 944 | TEST(SimpleString, StrNCpy_zero_termination) |
| 945 | { |
| 946 | char str[] = "XXXXXXXXXX"; |
| 947 | STRCMP_EQUAL("woman", SimpleString::StrNCpy(str, "woman", 6)); |
| 948 | } |
| 949 | |
| 950 | TEST(SimpleString, StrNCpy_null_proof) |
| 951 | { |
| 952 | POINTERS_EQUAL(NULLPTR, SimpleString::StrNCpy(NULLPTR, "woman", 6)); |
| 953 | } |
| 954 | |
| 955 | TEST(SimpleString, StrNCpy_stops_at_end_of_string) |
| 956 | { |
| 957 | char str[] = "XXXXXXXXXX"; |
| 958 | STRCMP_EQUAL("woman", SimpleString::StrNCpy(str, "woman", 8)); |
| 959 | } |
| 960 | |
| 961 | TEST(SimpleString, StrNCpy_nothing_to_do) |
| 962 | { |
| 963 | char str[] = "XXXXXXXXXX"; |
| 964 | STRCMP_EQUAL("XXXXXXXXXX", SimpleString::StrNCpy(str, "woman", 0)); |
| 965 | } |
| 966 | |
| 967 | TEST(SimpleString, StrNCpy_write_into_the_middle) |
| 968 | { |
| 969 | char str[] = "womanXXXXX"; |
| 970 | SimpleString::StrNCpy(str+3, "e", 1); |
| 971 | STRCMP_EQUAL("womenXXXXX", str); |
| 972 | } |
| 973 | |
| 974 | TEST(SimpleString, StrNCmp_equal) |
| 975 | { |
| 976 | int result = SimpleString::StrNCmp("teststring", "tests", 5); |
| 977 | LONGS_EQUAL(0, result); |
| 978 | } |
| 979 | |
| 980 | TEST(SimpleString, StrNCmp_should_always_return_0_when_n_is_0) |
| 981 | { |
| 982 | int result = SimpleString::StrNCmp("a", "b", 0); |
| 983 | LONGS_EQUAL(0, result); |
| 984 | } |
| 985 | |
| 986 | TEST(SimpleString, StrNCmp_s1_smaller) |
| 987 | { |
| 988 | int result = SimpleString::StrNCmp("testing", "tests", 7); |
| 989 | LONGS_EQUAL('i' - 's', result); |
| 990 | } |
| 991 | |
| 992 | TEST(SimpleString, StrNCmp_s1_larger) |
| 993 | { |
| 994 | int result = SimpleString::StrNCmp("teststring", "tester", 7); |
| 995 | LONGS_EQUAL('s' - 'e', result); |
| 996 | } |
| 997 | |
| 998 | TEST(SimpleString, StrNCmp_n_too_large) |
| 999 | { |
| 1000 | int result = SimpleString::StrNCmp("teststring", "teststring", 20); |
| 1001 | LONGS_EQUAL(0, result); |
| 1002 | } |
| 1003 | |
| 1004 | TEST(SimpleString, StrNCmp_s1_empty) |
| 1005 | { |
| 1006 | int result = SimpleString::StrNCmp("", "foo", 2); |
| 1007 | LONGS_EQUAL(0 - 'f', result); |
| 1008 | } |
| 1009 | |
| 1010 | TEST(SimpleString, StrNCmp_s2_empty) |
| 1011 | { |
| 1012 | int result = SimpleString::StrNCmp("foo", "", 2); |
| 1013 | LONGS_EQUAL('f', result); |
| 1014 | } |
| 1015 | |
| 1016 | TEST(SimpleString, StrNCmp_s1_and_s2_empty) |
| 1017 | { |
| 1018 | int result = SimpleString::StrNCmp("", "", 2); |
| 1019 | LONGS_EQUAL(0, result); |
| 1020 | } |
| 1021 | |
| 1022 | TEST(SimpleString, StrStr) |
| 1023 | { |
| 1024 | char foo[] = "foo"; |
| 1025 | char empty[] = ""; |
| 1026 | char foobarfoo[] = "foobarfoo"; |
| 1027 | char barf[] = "barf"; |
| 1028 | CHECK(SimpleString::StrStr(foo, empty) == foo); |
| 1029 | CHECK(SimpleString::StrStr(empty, foo) == NULLPTR); |
| 1030 | CHECK(SimpleString::StrStr(foobarfoo, barf) == foobarfoo+3); |
| 1031 | CHECK(SimpleString::StrStr(barf, foobarfoo) == NULLPTR); |
| 1032 | CHECK(SimpleString::StrStr(foo, foo) == foo); |
| 1033 | } |
| 1034 | |
| 1035 | TEST(SimpleString, AtoI) |
| 1036 | { |
| 1037 | char max_short_str[] = "32767"; |
| 1038 | char min_short_str[] = "-32768"; |
| 1039 | |
| 1040 | CHECK(12345 == SimpleString::AtoI("012345")); |
| 1041 | CHECK(6789 == SimpleString::AtoI("6789")); |
| 1042 | CHECK(12345 == SimpleString::AtoI("12345/")); |
| 1043 | CHECK(12345 == SimpleString::AtoI("12345:")); |
| 1044 | CHECK(-12345 == SimpleString::AtoI("-12345")); |
| 1045 | CHECK(123 == SimpleString::AtoI("\t \r\n123")); |
| 1046 | CHECK(123 == SimpleString::AtoI("123-foo")); |
| 1047 | CHECK(0 == SimpleString::AtoI("-foo")); |
| 1048 | CHECK(-32768 == SimpleString::AtoI(min_short_str)); |
| 1049 | CHECK(32767 == SimpleString::AtoI(max_short_str)); |
| 1050 | } |
| 1051 | |
| 1052 | TEST(SimpleString, AtoU) |
| 1053 | { |
| 1054 | char max_short_str[] = "65535"; |
| 1055 | CHECK(12345 == SimpleString::AtoU("012345")); |
| 1056 | CHECK(6789 == SimpleString::AtoU("6789")); |
| 1057 | CHECK(12345 == SimpleString::AtoU("12345/")); |
| 1058 | CHECK(12345 == SimpleString::AtoU("12345:")); |
| 1059 | CHECK(123 == SimpleString::AtoU("\t \r\n123")); |
| 1060 | CHECK(123 == SimpleString::AtoU("123-foo")); |
| 1061 | CHECK(65535 == SimpleString::AtoU(max_short_str)); |
| 1062 | CHECK(0 == SimpleString::AtoU("foo")); |
| 1063 | CHECK(0 == SimpleString::AtoU("-foo")); |
| 1064 | CHECK(0 == SimpleString::AtoU("+1")); |
| 1065 | CHECK(0 == SimpleString::AtoU("-1")); |
| 1066 | CHECK(0 == SimpleString::AtoU("0")); |
| 1067 | } |
| 1068 | |
| 1069 | TEST(SimpleString, Binary) |
| 1070 | { |
| 1071 | const unsigned char value[] = { 0x00, 0x01, 0x2A, 0xFF }; |
| 1072 | const char expectedString[] = "00 01 2A FF"; |
| 1073 | |
| 1074 | STRCMP_EQUAL(expectedString, StringFromBinary(value, sizeof(value)).asCharString()); |
| 1075 | STRCMP_EQUAL(expectedString, StringFromBinaryOrNull(value, sizeof(value)).asCharString()); |
| 1076 | STRCMP_EQUAL("", StringFromBinary(value, 0).asCharString()); |
| 1077 | STRCMP_EQUAL("(null)", StringFromBinaryOrNull(NULLPTR, 0).asCharString()); |
| 1078 | } |
| 1079 | |
| 1080 | TEST(SimpleString, BinaryWithSize) |
| 1081 | { |
| 1082 | const unsigned char value[] = { 0x12, 0xFE, 0xA1 }; |
| 1083 | const char expectedString[] = "Size = 3 | HexContents = 12 FE A1"; |
| 1084 | |
| 1085 | STRCMP_EQUAL(expectedString, StringFromBinaryWithSize(value, sizeof(value)).asCharString()); |
| 1086 | STRCMP_EQUAL(expectedString, StringFromBinaryWithSizeOrNull(value, sizeof(value)).asCharString()); |
| 1087 | STRCMP_EQUAL("Size = 0 | HexContents = ", StringFromBinaryWithSize(value, 0).asCharString()); |
| 1088 | STRCMP_EQUAL("(null)", StringFromBinaryWithSizeOrNull(NULLPTR, 0).asCharString()); |
| 1089 | } |
| 1090 | |
| 1091 | TEST(SimpleString, BinaryWithSizeLargerThan128) |
| 1092 | { |
| 1093 | unsigned char value[129]; |
| 1094 | value[127] = 0x00; |
| 1095 | value[128] = 0xff; |
| 1096 | |
| 1097 | STRCMP_CONTAINS("00 ...", StringFromBinaryWithSize(value, sizeof(value)).asCharString()); |
| 1098 | } |
| 1099 | |
| 1100 | TEST(SimpleString, MemCmp) |
| 1101 | { |
| 1102 | unsigned char smaller[] = { 0x00, 0x01, 0x2A, 0xFF }; |
| 1103 | unsigned char greater[] = { 0x00, 0x01, 0xFF, 0xFF }; |
| 1104 | |
| 1105 | LONGS_EQUAL(0, SimpleString::MemCmp(smaller, smaller, sizeof(smaller))); |
| 1106 | CHECK(SimpleString::MemCmp(smaller, greater, sizeof(smaller)) < 0); |
| 1107 | CHECK(SimpleString::MemCmp(greater, smaller, sizeof(smaller)) > 0); |
| 1108 | LONGS_EQUAL(0, SimpleString::MemCmp(NULLPTR, NULLPTR, 0)); |
| 1109 | } |
| 1110 | |
| 1111 | TEST(SimpleString, MemCmpFirstLastNotMatching) |
| 1112 | { |
| 1113 | unsigned char base[] = { 0x00, 0x01, 0x2A, 0xFF }; |
| 1114 | unsigned char firstNotMatching[] = { 0x01, 0x01, 0x2A, 0xFF }; |
| 1115 | unsigned char lastNotMatching[] = { 0x00, 0x01, 0x2A, 0x00 }; |
| 1116 | CHECK(0 != SimpleString::MemCmp(base, firstNotMatching, sizeof(base))); |
| 1117 | CHECK(0 != SimpleString::MemCmp(base, lastNotMatching, sizeof(base))); |
| 1118 | } |
| 1119 | |
| 1120 | #if (CPPUTEST_CHAR_BIT == 16) |
| 1121 | TEST(SimpleString, MaskedBitsChar) |
| 1122 | { |
| 1123 | STRCMP_EQUAL("xxxxxxxx xxxxxxxx", StringFromMaskedBits(0x00, 0x00, 1).asCharString()); |
| 1124 | STRCMP_EQUAL("xxxxxxxx 00000000", StringFromMaskedBits(0x00, 0xFF, 1).asCharString()); |
| 1125 | STRCMP_EQUAL("xxxxxxxx 11111111", StringFromMaskedBits(0xFF, 0xFF, 1).asCharString()); |
| 1126 | STRCMP_EQUAL("xxxxxxxx 1xxxxxxx", StringFromMaskedBits(0x80, 0x80, 1).asCharString()); |
| 1127 | STRCMP_EQUAL("xxxxxxxx xxxxxxx1", StringFromMaskedBits(0x01, 0x01, 1).asCharString()); |
| 1128 | STRCMP_EQUAL("xxxxxxxx 11xx11xx", StringFromMaskedBits(0xFF, 0xCC, 1).asCharString()); |
| 1129 | } |
| 1130 | #else |
| 1131 | TEST(SimpleString, MaskedBitsChar) |
| 1132 | { |
| 1133 | STRCMP_EQUAL("xxxxxxxx", StringFromMaskedBits(0x00, 0x00, 1).asCharString()); |
| 1134 | STRCMP_EQUAL("00000000", StringFromMaskedBits(0x00, 0xFF, 1).asCharString()); |
| 1135 | STRCMP_EQUAL("11111111", StringFromMaskedBits(0xFF, 0xFF, 1).asCharString()); |
| 1136 | STRCMP_EQUAL("1xxxxxxx", StringFromMaskedBits(0x80, 0x80, 1).asCharString()); |
| 1137 | STRCMP_EQUAL("xxxxxxx1", StringFromMaskedBits(0x01, 0x01, 1).asCharString()); |
| 1138 | STRCMP_EQUAL("11xx11xx", StringFromMaskedBits(0xFF, 0xCC, 1).asCharString()); |
| 1139 | } |
| 1140 | #endif |
| 1141 | |
| 1142 | TEST(SimpleString, MaskedBits16Bit) |
| 1143 | { |
| 1144 | STRCMP_EQUAL("xxxxxxxx xxxxxxxx", StringFromMaskedBits(0x0000, 0x0000, 2*8/CPPUTEST_CHAR_BIT).asCharString()); |
| 1145 | STRCMP_EQUAL("00000000 00000000", StringFromMaskedBits(0x0000, 0xFFFF, 2*8/CPPUTEST_CHAR_BIT).asCharString()); |
| 1146 | STRCMP_EQUAL("11111111 11111111", StringFromMaskedBits(0xFFFF, 0xFFFF, 2*8/CPPUTEST_CHAR_BIT).asCharString()); |
| 1147 | STRCMP_EQUAL("1xxxxxxx xxxxxxxx", StringFromMaskedBits(0x8000, 0x8000, 2*8/CPPUTEST_CHAR_BIT).asCharString()); |
| 1148 | STRCMP_EQUAL("xxxxxxxx xxxxxxx1", StringFromMaskedBits(0x0001, 0x0001, 2*8/CPPUTEST_CHAR_BIT).asCharString()); |
| 1149 | STRCMP_EQUAL("11xx11xx 11xx11xx", StringFromMaskedBits(0xFFFF, 0xCCCC, 2*8/CPPUTEST_CHAR_BIT).asCharString()); |
| 1150 | } |
| 1151 | |
| 1152 | TEST(SimpleString, MaskedBits32Bit) |
| 1153 | { |
| 1154 | STRCMP_EQUAL("xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx", StringFromMaskedBits(0x00000000, 0x00000000, 4*8/CPPUTEST_CHAR_BIT).asCharString()); |
| 1155 | STRCMP_EQUAL("00000000 00000000 00000000 00000000", StringFromMaskedBits(0x00000000, 0xFFFFFFFF, 4*8/CPPUTEST_CHAR_BIT).asCharString()); |
| 1156 | STRCMP_EQUAL("11111111 11111111 11111111 11111111", StringFromMaskedBits(0xFFFFFFFF, 0xFFFFFFFF, 4*8/CPPUTEST_CHAR_BIT).asCharString()); |
| 1157 | STRCMP_EQUAL("1xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx", StringFromMaskedBits(0x80000000, 0x80000000, 4*8/CPPUTEST_CHAR_BIT).asCharString()); |
| 1158 | STRCMP_EQUAL("xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxx1", StringFromMaskedBits(0x00000001, 0x00000001, 4*8/CPPUTEST_CHAR_BIT).asCharString()); |
| 1159 | STRCMP_EQUAL("11xx11xx 11xx11xx 11xx11xx 11xx11xx", StringFromMaskedBits(0xFFFFFFFF, 0xCCCCCCCC, 4*8/CPPUTEST_CHAR_BIT).asCharString()); |
| 1160 | } |
| 1161 | |
| 1162 | TEST(SimpleString, StringFromOrdinalNumberOnes) |
| 1163 | { |
| 1164 | STRCMP_EQUAL("2nd", StringFromOrdinalNumber(2).asCharString()); |
| 1165 | STRCMP_EQUAL("3rd", StringFromOrdinalNumber(3).asCharString()); |
| 1166 | STRCMP_EQUAL("4th", StringFromOrdinalNumber(4).asCharString()); |
| 1167 | STRCMP_EQUAL("5th", StringFromOrdinalNumber(5).asCharString()); |
| 1168 | STRCMP_EQUAL("6th", StringFromOrdinalNumber(6).asCharString()); |
| 1169 | STRCMP_EQUAL("7th", StringFromOrdinalNumber(7).asCharString()); |
| 1170 | } |
| 1171 | |
| 1172 | TEST(SimpleString, StringFromOrdinalNumberTens) |
| 1173 | { |
| 1174 | STRCMP_EQUAL("10th", StringFromOrdinalNumber(10).asCharString()); |
| 1175 | STRCMP_EQUAL("11th", StringFromOrdinalNumber(11).asCharString()); |
| 1176 | STRCMP_EQUAL("12th", StringFromOrdinalNumber(12).asCharString()); |
| 1177 | STRCMP_EQUAL("13th", StringFromOrdinalNumber(13).asCharString()); |
| 1178 | STRCMP_EQUAL("14th", StringFromOrdinalNumber(14).asCharString()); |
| 1179 | STRCMP_EQUAL("18th", StringFromOrdinalNumber(18).asCharString()); |
| 1180 | } |
| 1181 | |
| 1182 | TEST(SimpleString, StringFromOrdinalNumberOthers) |
| 1183 | { |
| 1184 | STRCMP_EQUAL("21st", StringFromOrdinalNumber(21).asCharString()); |
| 1185 | STRCMP_EQUAL("22nd", StringFromOrdinalNumber(22).asCharString()); |
| 1186 | STRCMP_EQUAL("23rd", StringFromOrdinalNumber(23).asCharString()); |
| 1187 | STRCMP_EQUAL("24th", StringFromOrdinalNumber(24).asCharString()); |
| 1188 | STRCMP_EQUAL("32nd", StringFromOrdinalNumber(32).asCharString()); |
| 1189 | STRCMP_EQUAL("100th", StringFromOrdinalNumber(100).asCharString()); |
| 1190 | STRCMP_EQUAL("101st", StringFromOrdinalNumber(101).asCharString()); |
| 1191 | } |
| 1192 | |
| 1193 | TEST(SimpleString, BracketsFormattedHexStringFromForSignedChar) |
| 1194 | { |
| 1195 | signed char value = 'c'; |
| 1196 | |
| 1197 | STRCMP_EQUAL("(0x63)", BracketsFormattedHexStringFrom(value).asCharString()); |
| 1198 | } |
| 1199 | |
| 1200 | |
| 1201 | TEST(SimpleString, BracketsFormattedHexStringFromForUnsignedInt) |
| 1202 | { |
| 1203 | unsigned int value = 1; |
| 1204 | |
| 1205 | STRCMP_EQUAL("(0x1)", BracketsFormattedHexStringFrom(value).asCharString()); |
| 1206 | } |
| 1207 | |
| 1208 | TEST(SimpleString, BracketsFormattedHexStringFromForUnsignedLong) |
| 1209 | { |
| 1210 | unsigned long value = 1; |
| 1211 | |
| 1212 | STRCMP_EQUAL("(0x1)", BracketsFormattedHexStringFrom(value).asCharString()); |
| 1213 | } |
| 1214 | |
| 1215 | #ifdef CPPUTEST_16BIT_INTS |
| 1216 | TEST(SimpleString, BracketsFormattedHexStringFromForInt) |
| 1217 | { |
| 1218 | int value = -1; |
| 1219 | |
| 1220 | STRCMP_EQUAL("(0xffff)", BracketsFormattedHexStringFrom(value).asCharString()); |
| 1221 | } |
| 1222 | #else |
| 1223 | TEST(SimpleString, BracketsFormattedHexStringFromForInt) |
| 1224 | { |
| 1225 | int value = -1; |
| 1226 | STRCMP_EQUAL("(0xffffffff)", BracketsFormattedHexStringFrom(value).asCharString()); |
| 1227 | } |
| 1228 | #endif |
| 1229 | |
| 1230 | TEST(SimpleString, BracketsFormattedHexStringFromForLong) |
| 1231 | { |
| 1232 | long value = 1; |
| 1233 | |
| 1234 | STRCMP_EQUAL("(0x1)", BracketsFormattedHexStringFrom(value).asCharString()); |
| 1235 | } |
| 1236 | #ifdef CPPUTEST_USE_LONG_LONG |
| 1237 | |
| 1238 | TEST(SimpleString, BracketsFormattedHexStringFromForLongLong) |
| 1239 | { |
| 1240 | cpputest_longlong value = 1; |
| 1241 | |
| 1242 | STRCMP_EQUAL("(0x1)", BracketsFormattedHexStringFrom(value).asCharString()); |
| 1243 | } |
| 1244 | TEST(SimpleString, BracketsFormattedHexStringFromForULongLong) |
| 1245 | { |
| 1246 | cpputest_ulonglong value = 1; |
| 1247 | |
| 1248 | STRCMP_EQUAL("(0x1)", BracketsFormattedHexStringFrom(value).asCharString()); |
| 1249 | } |
| 1250 | #else |
| 1251 | TEST(SimpleString, BracketsFormattedHexStringFromForLongLong) |
| 1252 | { |
| 1253 | cpputest_longlong value = 1; |
| 1254 | |
| 1255 | STRCMP_EQUAL("", BracketsFormattedHexStringFrom(value).asCharString()); |
| 1256 | } |
| 1257 | TEST(SimpleString, BracketsFormattedHexStringFromForULongLong) |
| 1258 | { |
| 1259 | cpputest_ulonglong value = 1; |
| 1260 | |
| 1261 | STRCMP_EQUAL("", BracketsFormattedHexStringFrom(value).asCharString()); |
| 1262 | } |
| 1263 | |
| 1264 | #endif |
| 1265 |