Unit Testing framework based on http://cpputest.github.io/
CppUTest
Where to find more information
- The CppUTest manual can be found at CppUTest.org
- If you have any question, check out the Google Groups
- The sources from which this library was derived may be found at the main github page
Getting test reports on the console
You may need to tailor the file src/Platforms/mbed/UtestPlatform.cpp
to your needs. In particular, if you want console output, you might want to look at the function PlatformSpecificPutchar()
.
Quick introduction (some code!)
To write your first test, all you need is a new cpp file with a TEST_GROUP and a TEST, like:
#include "CppUTest/TestHarness.h" TEST_GROUP(FirstTestGroup) { }; TEST(FirstTestGroup, FirstTest) { FAIL("Fail me!"); }
This test will fail.
You can add new tests to the test group by just writing more tests in the file, like this:
TEST(FirstTestGroup, SecondTest) { STRCMP_EQUAL("hello", "world"); LONGS_EQUAL(1, 2); CHECK(false); }
You do need to trigger the tests from somewhere in your program. It could look something like:
#include "CppUTest/TestRegistry.h" #include "CppUTest/CommandLineTestRunner.h" int main(int ac, char** av) { .... unsigned failureCount = 0; { ConsoleTestOutput output; CommandLineTestRunner runner(ac, av, &output, TestRegistry::getCurrentRegistry()); failureCount = runner.runAllTestsMain(); } if (failureCount == 0) { console.printf("PASSED\r\n"); } ... }
For more information, We’d recommend to read the manual or, even better, check some existing tests such as SimpleStringTest or (a bit more complicated) MemoryLeakDetectorTest or the mocking tests or just check out the Cheat Sheet.
src/CppUTest/SimpleString.cpp@2:82161d9e7b36, 2014-06-19 (annotated)
- Committer:
- Rohit Grover
- Date:
- Thu Jun 19 08:24:31 2014 +0100
- Revision:
- 2:82161d9e7b36
- Parent:
- 1:4769360130ed
uncomment the code in CommandLineTestRunner::RunAllTests() having to do with MemoryLeakWarningPlugin
Bas Vodde, from the CppUTest development team has suggested an alternate way to run tests.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rohit Grover |
1:4769360130ed | 1 | /* |
Rohit Grover |
1:4769360130ed | 2 | * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde |
Rohit Grover |
1:4769360130ed | 3 | * All rights reserved. |
Rohit Grover |
1:4769360130ed | 4 | * |
Rohit Grover |
1:4769360130ed | 5 | * Redistribution and use in source and binary forms, with or without |
Rohit Grover |
1:4769360130ed | 6 | * modification, are permitted provided that the following conditions are met: |
Rohit Grover |
1:4769360130ed | 7 | * * Redistributions of source code must retain the above copyright |
Rohit Grover |
1:4769360130ed | 8 | * notice, this list of conditions and the following disclaimer. |
Rohit Grover |
1:4769360130ed | 9 | * * Redistributions in binary form must reproduce the above copyright |
Rohit Grover |
1:4769360130ed | 10 | * notice, this list of conditions and the following disclaimer in the |
Rohit Grover |
1:4769360130ed | 11 | * documentation and/or other materials provided with the distribution. |
Rohit Grover |
1:4769360130ed | 12 | * * Neither the name of the <organization> nor the |
Rohit Grover |
1:4769360130ed | 13 | * names of its contributors may be used to endorse or promote products |
Rohit Grover |
1:4769360130ed | 14 | * derived from this software without specific prior written permission. |
Rohit Grover |
1:4769360130ed | 15 | * |
Rohit Grover |
1:4769360130ed | 16 | * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY |
Rohit Grover |
1:4769360130ed | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Rohit Grover |
1:4769360130ed | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Rohit Grover |
1:4769360130ed | 19 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY |
Rohit Grover |
1:4769360130ed | 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Rohit Grover |
1:4769360130ed | 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Rohit Grover |
1:4769360130ed | 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
Rohit Grover |
1:4769360130ed | 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Rohit Grover |
1:4769360130ed | 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Rohit Grover |
1:4769360130ed | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Rohit Grover |
1:4769360130ed | 26 | */ |
Rohit Grover |
1:4769360130ed | 27 | |
Rohit Grover |
1:4769360130ed | 28 | #include "CppUTest/TestHarness.h" |
Rohit Grover |
1:4769360130ed | 29 | #include "CppUTest/SimpleString.h" |
Rohit Grover |
1:4769360130ed | 30 | #include "CppUTest/PlatformSpecificFunctions.h" |
Rohit Grover |
1:4769360130ed | 31 | #include "CppUTest/TestMemoryAllocator.h" |
Rohit Grover |
1:4769360130ed | 32 | |
Rohit Grover |
1:4769360130ed | 33 | |
Rohit Grover |
1:4769360130ed | 34 | TestMemoryAllocator* SimpleString::stringAllocator_ = NULL; |
Rohit Grover |
1:4769360130ed | 35 | |
Rohit Grover |
1:4769360130ed | 36 | TestMemoryAllocator* SimpleString::getStringAllocator() |
Rohit Grover |
1:4769360130ed | 37 | { |
Rohit Grover |
1:4769360130ed | 38 | if (stringAllocator_ == NULL) |
Rohit Grover |
1:4769360130ed | 39 | return defaultNewArrayAllocator(); |
Rohit Grover |
1:4769360130ed | 40 | return stringAllocator_; |
Rohit Grover |
1:4769360130ed | 41 | } |
Rohit Grover |
1:4769360130ed | 42 | |
Rohit Grover |
1:4769360130ed | 43 | void SimpleString::setStringAllocator(TestMemoryAllocator* allocator) |
Rohit Grover |
1:4769360130ed | 44 | { |
Rohit Grover |
1:4769360130ed | 45 | stringAllocator_ = allocator; |
Rohit Grover |
1:4769360130ed | 46 | } |
Rohit Grover |
1:4769360130ed | 47 | |
Rohit Grover |
1:4769360130ed | 48 | /* Avoid using the memory leak detector INSIDE SimpleString as its used inside the detector */ |
Rohit Grover |
1:4769360130ed | 49 | char* SimpleString::allocStringBuffer(size_t _size) |
Rohit Grover |
1:4769360130ed | 50 | { |
Rohit Grover |
1:4769360130ed | 51 | return getStringAllocator()->alloc_memory(_size, __FILE__, __LINE__); |
Rohit Grover |
1:4769360130ed | 52 | } |
Rohit Grover |
1:4769360130ed | 53 | |
Rohit Grover |
1:4769360130ed | 54 | void SimpleString::deallocStringBuffer(char* str) |
Rohit Grover |
1:4769360130ed | 55 | { |
Rohit Grover |
1:4769360130ed | 56 | getStringAllocator()->free_memory(str, __FILE__, __LINE__); |
Rohit Grover |
1:4769360130ed | 57 | } |
Rohit Grover |
1:4769360130ed | 58 | |
Rohit Grover |
1:4769360130ed | 59 | char* SimpleString::getEmptyString() const |
Rohit Grover |
1:4769360130ed | 60 | { |
Rohit Grover |
1:4769360130ed | 61 | char* empty = allocStringBuffer(1); |
Rohit Grover |
1:4769360130ed | 62 | empty[0] = '\0'; |
Rohit Grover |
1:4769360130ed | 63 | return empty; |
Rohit Grover |
1:4769360130ed | 64 | } |
Rohit Grover |
1:4769360130ed | 65 | |
Rohit Grover |
1:4769360130ed | 66 | char* SimpleString::StrNCpy(char* s1, const char* s2, size_t n) |
Rohit Grover |
1:4769360130ed | 67 | { |
Rohit Grover |
1:4769360130ed | 68 | char* result = s1; |
Rohit Grover |
1:4769360130ed | 69 | |
Rohit Grover |
1:4769360130ed | 70 | if((NULL == s1) || (0 == n)) return result; |
Rohit Grover |
1:4769360130ed | 71 | |
Rohit Grover |
1:4769360130ed | 72 | while ((*s1++ = *s2++) && --n != 0) |
Rohit Grover |
1:4769360130ed | 73 | ; |
Rohit Grover |
1:4769360130ed | 74 | return result; |
Rohit Grover |
1:4769360130ed | 75 | } |
Rohit Grover |
1:4769360130ed | 76 | |
Rohit Grover |
1:4769360130ed | 77 | SimpleString::SimpleString(const char *otherBuffer) |
Rohit Grover |
1:4769360130ed | 78 | { |
Rohit Grover |
1:4769360130ed | 79 | if (otherBuffer == 0) { |
Rohit Grover |
1:4769360130ed | 80 | buffer_ = getEmptyString(); |
Rohit Grover |
1:4769360130ed | 81 | } |
Rohit Grover |
1:4769360130ed | 82 | else { |
Rohit Grover |
1:4769360130ed | 83 | buffer_ = copyToNewBuffer(otherBuffer); |
Rohit Grover |
1:4769360130ed | 84 | } |
Rohit Grover |
1:4769360130ed | 85 | } |
Rohit Grover |
1:4769360130ed | 86 | |
Rohit Grover |
1:4769360130ed | 87 | SimpleString::SimpleString(const char *other, size_t repeatCount) |
Rohit Grover |
1:4769360130ed | 88 | { |
Rohit Grover |
1:4769360130ed | 89 | size_t otherStringLength = PlatformSpecificStrLen(other); |
Rohit Grover |
1:4769360130ed | 90 | size_t len = otherStringLength * repeatCount + 1; |
Rohit Grover |
1:4769360130ed | 91 | buffer_ = allocStringBuffer(len); |
Rohit Grover |
1:4769360130ed | 92 | char* next = buffer_; |
Rohit Grover |
1:4769360130ed | 93 | for (size_t i = 0; i < repeatCount; i++) { |
Rohit Grover |
1:4769360130ed | 94 | StrNCpy(next, other, otherStringLength + 1); |
Rohit Grover |
1:4769360130ed | 95 | next += otherStringLength; |
Rohit Grover |
1:4769360130ed | 96 | } |
Rohit Grover |
1:4769360130ed | 97 | *next = 0; |
Rohit Grover |
1:4769360130ed | 98 | } |
Rohit Grover |
1:4769360130ed | 99 | |
Rohit Grover |
1:4769360130ed | 100 | SimpleString::SimpleString(const SimpleString& other) |
Rohit Grover |
1:4769360130ed | 101 | { |
Rohit Grover |
1:4769360130ed | 102 | buffer_ = copyToNewBuffer(other.buffer_); |
Rohit Grover |
1:4769360130ed | 103 | } |
Rohit Grover |
1:4769360130ed | 104 | |
Rohit Grover |
1:4769360130ed | 105 | SimpleString& SimpleString::operator=(const SimpleString& other) |
Rohit Grover |
1:4769360130ed | 106 | { |
Rohit Grover |
1:4769360130ed | 107 | if (this != &other) { |
Rohit Grover |
1:4769360130ed | 108 | deallocStringBuffer(buffer_); |
Rohit Grover |
1:4769360130ed | 109 | buffer_ = copyToNewBuffer(other.buffer_); |
Rohit Grover |
1:4769360130ed | 110 | } |
Rohit Grover |
1:4769360130ed | 111 | return *this; |
Rohit Grover |
1:4769360130ed | 112 | } |
Rohit Grover |
1:4769360130ed | 113 | |
Rohit Grover |
1:4769360130ed | 114 | bool SimpleString::contains(const SimpleString& other) const |
Rohit Grover |
1:4769360130ed | 115 | { |
Rohit Grover |
1:4769360130ed | 116 | //strstr on some machines does not handle "" |
Rohit Grover |
1:4769360130ed | 117 | //the right way. "" should be found in any string |
Rohit Grover |
1:4769360130ed | 118 | if (PlatformSpecificStrLen(other.buffer_) == 0) return true; |
Rohit Grover |
1:4769360130ed | 119 | else if (PlatformSpecificStrLen(buffer_) == 0) return false; |
Rohit Grover |
1:4769360130ed | 120 | else return PlatformSpecificStrStr(buffer_, other.buffer_) != 0; |
Rohit Grover |
1:4769360130ed | 121 | } |
Rohit Grover |
1:4769360130ed | 122 | |
Rohit Grover |
1:4769360130ed | 123 | bool SimpleString::containsNoCase(const SimpleString& other) const |
Rohit Grover |
1:4769360130ed | 124 | { |
Rohit Grover |
1:4769360130ed | 125 | return toLower().contains(other.toLower()); |
Rohit Grover |
1:4769360130ed | 126 | } |
Rohit Grover |
1:4769360130ed | 127 | |
Rohit Grover |
1:4769360130ed | 128 | |
Rohit Grover |
1:4769360130ed | 129 | bool SimpleString::startsWith(const SimpleString& other) const |
Rohit Grover |
1:4769360130ed | 130 | { |
Rohit Grover |
1:4769360130ed | 131 | if (PlatformSpecificStrLen(other.buffer_) == 0) return true; |
Rohit Grover |
1:4769360130ed | 132 | else if (PlatformSpecificStrLen(buffer_) == 0) return false; |
Rohit Grover |
1:4769360130ed | 133 | else return PlatformSpecificStrStr(buffer_, other.buffer_) == buffer_; |
Rohit Grover |
1:4769360130ed | 134 | } |
Rohit Grover |
1:4769360130ed | 135 | |
Rohit Grover |
1:4769360130ed | 136 | bool SimpleString::endsWith(const SimpleString& other) const |
Rohit Grover |
1:4769360130ed | 137 | { |
Rohit Grover |
1:4769360130ed | 138 | size_t buffer_length = PlatformSpecificStrLen(buffer_); |
Rohit Grover |
1:4769360130ed | 139 | size_t other_buffer_length = PlatformSpecificStrLen(other.buffer_); |
Rohit Grover |
1:4769360130ed | 140 | if (other_buffer_length == 0) return true; |
Rohit Grover |
1:4769360130ed | 141 | if (buffer_length == 0) return false; |
Rohit Grover |
1:4769360130ed | 142 | if (buffer_length < other_buffer_length) return false; |
Rohit Grover |
1:4769360130ed | 143 | return PlatformSpecificStrCmp(buffer_ + buffer_length - other_buffer_length, other.buffer_) == 0; |
Rohit Grover |
1:4769360130ed | 144 | } |
Rohit Grover |
1:4769360130ed | 145 | |
Rohit Grover |
1:4769360130ed | 146 | size_t SimpleString::count(const SimpleString& substr) const |
Rohit Grover |
1:4769360130ed | 147 | { |
Rohit Grover |
1:4769360130ed | 148 | size_t num = 0; |
Rohit Grover |
1:4769360130ed | 149 | char* str = buffer_; |
Rohit Grover |
1:4769360130ed | 150 | while ((str = PlatformSpecificStrStr(str, substr.buffer_))) { |
Rohit Grover |
1:4769360130ed | 151 | num++; |
Rohit Grover |
1:4769360130ed | 152 | str++; |
Rohit Grover |
1:4769360130ed | 153 | } |
Rohit Grover |
1:4769360130ed | 154 | return num; |
Rohit Grover |
1:4769360130ed | 155 | } |
Rohit Grover |
1:4769360130ed | 156 | |
Rohit Grover |
1:4769360130ed | 157 | void SimpleString::split(const SimpleString& delimiter, SimpleStringCollection& col) const |
Rohit Grover |
1:4769360130ed | 158 | { |
Rohit Grover |
1:4769360130ed | 159 | size_t num = count(delimiter); |
Rohit Grover |
1:4769360130ed | 160 | size_t extraEndToken = (endsWith(delimiter)) ? 0 : 1U; |
Rohit Grover |
1:4769360130ed | 161 | col.allocate(num + extraEndToken); |
Rohit Grover |
1:4769360130ed | 162 | |
Rohit Grover |
1:4769360130ed | 163 | char* str = buffer_; |
Rohit Grover |
1:4769360130ed | 164 | char* prev; |
Rohit Grover |
1:4769360130ed | 165 | for (size_t i = 0; i < num; ++i) { |
Rohit Grover |
1:4769360130ed | 166 | prev = str; |
Rohit Grover |
1:4769360130ed | 167 | str = PlatformSpecificStrStr(str, delimiter.buffer_) + 1; |
Rohit Grover |
1:4769360130ed | 168 | size_t len = (size_t) (str - prev) + 1; |
Rohit Grover |
1:4769360130ed | 169 | col[i].buffer_ = copyToNewBuffer(prev, len); |
Rohit Grover |
1:4769360130ed | 170 | } |
Rohit Grover |
1:4769360130ed | 171 | if (extraEndToken) { |
Rohit Grover |
1:4769360130ed | 172 | col[num] = str; |
Rohit Grover |
1:4769360130ed | 173 | } |
Rohit Grover |
1:4769360130ed | 174 | } |
Rohit Grover |
1:4769360130ed | 175 | |
Rohit Grover |
1:4769360130ed | 176 | void SimpleString::replace(char to, char with) |
Rohit Grover |
1:4769360130ed | 177 | { |
Rohit Grover |
1:4769360130ed | 178 | size_t s = size(); |
Rohit Grover |
1:4769360130ed | 179 | for (size_t i = 0; i < s; i++) { |
Rohit Grover |
1:4769360130ed | 180 | if (buffer_[i] == to) buffer_[i] = with; |
Rohit Grover |
1:4769360130ed | 181 | } |
Rohit Grover |
1:4769360130ed | 182 | } |
Rohit Grover |
1:4769360130ed | 183 | |
Rohit Grover |
1:4769360130ed | 184 | void SimpleString::replace(const char* to, const char* with) |
Rohit Grover |
1:4769360130ed | 185 | { |
Rohit Grover |
1:4769360130ed | 186 | size_t c = count(to); |
Rohit Grover |
1:4769360130ed | 187 | size_t len = size(); |
Rohit Grover |
1:4769360130ed | 188 | size_t tolen = PlatformSpecificStrLen(to); |
Rohit Grover |
1:4769360130ed | 189 | size_t withlen = PlatformSpecificStrLen(with); |
Rohit Grover |
1:4769360130ed | 190 | |
Rohit Grover |
1:4769360130ed | 191 | size_t newsize = len + (withlen * c) - (tolen * c) + 1; |
Rohit Grover |
1:4769360130ed | 192 | |
Rohit Grover |
1:4769360130ed | 193 | if (newsize) { |
Rohit Grover |
1:4769360130ed | 194 | char* newbuf = allocStringBuffer(newsize); |
Rohit Grover |
1:4769360130ed | 195 | for (size_t i = 0, j = 0; i < len;) { |
Rohit Grover |
1:4769360130ed | 196 | if (PlatformSpecificStrNCmp(&buffer_[i], to, tolen) == 0) { |
Rohit Grover |
1:4769360130ed | 197 | StrNCpy(&newbuf[j], with, withlen + 1); |
Rohit Grover |
1:4769360130ed | 198 | j += withlen; |
Rohit Grover |
1:4769360130ed | 199 | i += tolen; |
Rohit Grover |
1:4769360130ed | 200 | } |
Rohit Grover |
1:4769360130ed | 201 | else { |
Rohit Grover |
1:4769360130ed | 202 | newbuf[j] = buffer_[i]; |
Rohit Grover |
1:4769360130ed | 203 | j++; |
Rohit Grover |
1:4769360130ed | 204 | i++; |
Rohit Grover |
1:4769360130ed | 205 | } |
Rohit Grover |
1:4769360130ed | 206 | } |
Rohit Grover |
1:4769360130ed | 207 | deallocStringBuffer(buffer_); |
Rohit Grover |
1:4769360130ed | 208 | buffer_ = newbuf; |
Rohit Grover |
1:4769360130ed | 209 | buffer_[newsize - 1] = '\0'; |
Rohit Grover |
1:4769360130ed | 210 | } |
Rohit Grover |
1:4769360130ed | 211 | else { |
Rohit Grover |
1:4769360130ed | 212 | buffer_ = getEmptyString(); |
Rohit Grover |
1:4769360130ed | 213 | buffer_[0] = '\0'; |
Rohit Grover |
1:4769360130ed | 214 | } |
Rohit Grover |
1:4769360130ed | 215 | } |
Rohit Grover |
1:4769360130ed | 216 | |
Rohit Grover |
1:4769360130ed | 217 | SimpleString SimpleString::toLower() const |
Rohit Grover |
1:4769360130ed | 218 | { |
Rohit Grover |
1:4769360130ed | 219 | SimpleString str(*this); |
Rohit Grover |
1:4769360130ed | 220 | |
Rohit Grover |
1:4769360130ed | 221 | size_t str_size = str.size(); |
Rohit Grover |
1:4769360130ed | 222 | for (size_t i = 0; i < str_size; i++) |
Rohit Grover |
1:4769360130ed | 223 | str.buffer_[i] = PlatformSpecificToLower(str.buffer_[i]); |
Rohit Grover |
1:4769360130ed | 224 | |
Rohit Grover |
1:4769360130ed | 225 | return str; |
Rohit Grover |
1:4769360130ed | 226 | } |
Rohit Grover |
1:4769360130ed | 227 | |
Rohit Grover |
1:4769360130ed | 228 | const char *SimpleString::asCharString() const |
Rohit Grover |
1:4769360130ed | 229 | { |
Rohit Grover |
1:4769360130ed | 230 | return buffer_; |
Rohit Grover |
1:4769360130ed | 231 | } |
Rohit Grover |
1:4769360130ed | 232 | |
Rohit Grover |
1:4769360130ed | 233 | size_t SimpleString::size() const |
Rohit Grover |
1:4769360130ed | 234 | { |
Rohit Grover |
1:4769360130ed | 235 | return PlatformSpecificStrLen(buffer_); |
Rohit Grover |
1:4769360130ed | 236 | } |
Rohit Grover |
1:4769360130ed | 237 | |
Rohit Grover |
1:4769360130ed | 238 | bool SimpleString::isEmpty() const |
Rohit Grover |
1:4769360130ed | 239 | { |
Rohit Grover |
1:4769360130ed | 240 | return size() == 0; |
Rohit Grover |
1:4769360130ed | 241 | } |
Rohit Grover |
1:4769360130ed | 242 | |
Rohit Grover |
1:4769360130ed | 243 | |
Rohit Grover |
1:4769360130ed | 244 | |
Rohit Grover |
1:4769360130ed | 245 | SimpleString::~SimpleString() |
Rohit Grover |
1:4769360130ed | 246 | { |
Rohit Grover |
1:4769360130ed | 247 | deallocStringBuffer(buffer_); |
Rohit Grover |
1:4769360130ed | 248 | } |
Rohit Grover |
1:4769360130ed | 249 | |
Rohit Grover |
1:4769360130ed | 250 | bool operator==(const SimpleString& left, const SimpleString& right) |
Rohit Grover |
1:4769360130ed | 251 | { |
Rohit Grover |
1:4769360130ed | 252 | return 0 == PlatformSpecificStrCmp(left.asCharString(), right.asCharString()); |
Rohit Grover |
1:4769360130ed | 253 | } |
Rohit Grover |
1:4769360130ed | 254 | |
Rohit Grover |
1:4769360130ed | 255 | bool SimpleString::equalsNoCase(const SimpleString& str) const |
Rohit Grover |
1:4769360130ed | 256 | { |
Rohit Grover |
1:4769360130ed | 257 | return toLower() == str.toLower(); |
Rohit Grover |
1:4769360130ed | 258 | } |
Rohit Grover |
1:4769360130ed | 259 | |
Rohit Grover |
1:4769360130ed | 260 | |
Rohit Grover |
1:4769360130ed | 261 | bool operator!=(const SimpleString& left, const SimpleString& right) |
Rohit Grover |
1:4769360130ed | 262 | { |
Rohit Grover |
1:4769360130ed | 263 | return !(left == right); |
Rohit Grover |
1:4769360130ed | 264 | } |
Rohit Grover |
1:4769360130ed | 265 | |
Rohit Grover |
1:4769360130ed | 266 | SimpleString SimpleString::operator+(const SimpleString& rhs) |
Rohit Grover |
1:4769360130ed | 267 | { |
Rohit Grover |
1:4769360130ed | 268 | SimpleString t(buffer_); |
Rohit Grover |
1:4769360130ed | 269 | t += rhs.buffer_; |
Rohit Grover |
1:4769360130ed | 270 | return t; |
Rohit Grover |
1:4769360130ed | 271 | } |
Rohit Grover |
1:4769360130ed | 272 | |
Rohit Grover |
1:4769360130ed | 273 | SimpleString& SimpleString::operator+=(const SimpleString& rhs) |
Rohit Grover |
1:4769360130ed | 274 | { |
Rohit Grover |
1:4769360130ed | 275 | return operator+=(rhs.buffer_); |
Rohit Grover |
1:4769360130ed | 276 | } |
Rohit Grover |
1:4769360130ed | 277 | |
Rohit Grover |
1:4769360130ed | 278 | SimpleString& SimpleString::operator+=(const char* rhs) |
Rohit Grover |
1:4769360130ed | 279 | { |
Rohit Grover |
1:4769360130ed | 280 | size_t originalSize = this->size(); |
Rohit Grover |
1:4769360130ed | 281 | size_t additionalStringSize = PlatformSpecificStrLen(rhs) + 1; |
Rohit Grover |
1:4769360130ed | 282 | size_t sizeOfNewString = originalSize + additionalStringSize; |
Rohit Grover |
1:4769360130ed | 283 | char* tbuffer = copyToNewBuffer(this->buffer_, sizeOfNewString); |
Rohit Grover |
1:4769360130ed | 284 | StrNCpy(tbuffer + originalSize, rhs, additionalStringSize); |
Rohit Grover |
1:4769360130ed | 285 | deallocStringBuffer(this->buffer_); |
Rohit Grover |
1:4769360130ed | 286 | this->buffer_ = tbuffer; |
Rohit Grover |
1:4769360130ed | 287 | return *this; |
Rohit Grover |
1:4769360130ed | 288 | } |
Rohit Grover |
1:4769360130ed | 289 | |
Rohit Grover |
1:4769360130ed | 290 | void SimpleString::padStringsToSameLength(SimpleString& str1, SimpleString& str2, char padCharacter) |
Rohit Grover |
1:4769360130ed | 291 | { |
Rohit Grover |
1:4769360130ed | 292 | if (str1.size() > str2.size()) { |
Rohit Grover |
1:4769360130ed | 293 | padStringsToSameLength(str2, str1, padCharacter); |
Rohit Grover |
1:4769360130ed | 294 | return; |
Rohit Grover |
1:4769360130ed | 295 | } |
Rohit Grover |
1:4769360130ed | 296 | |
Rohit Grover |
1:4769360130ed | 297 | char pad[2]; |
Rohit Grover |
1:4769360130ed | 298 | pad[0] = padCharacter; |
Rohit Grover |
1:4769360130ed | 299 | pad[1] = 0; |
Rohit Grover |
1:4769360130ed | 300 | str1 = SimpleString(pad, str2.size() - str1.size()) + str1; |
Rohit Grover |
1:4769360130ed | 301 | } |
Rohit Grover |
1:4769360130ed | 302 | |
Rohit Grover |
1:4769360130ed | 303 | SimpleString SimpleString::subString(size_t beginPos, size_t amount) const |
Rohit Grover |
1:4769360130ed | 304 | { |
Rohit Grover |
1:4769360130ed | 305 | if (beginPos > size()-1) return ""; |
Rohit Grover |
1:4769360130ed | 306 | |
Rohit Grover |
1:4769360130ed | 307 | SimpleString newString = buffer_ + beginPos; |
Rohit Grover |
1:4769360130ed | 308 | |
Rohit Grover |
1:4769360130ed | 309 | if (newString.size() > amount) |
Rohit Grover |
1:4769360130ed | 310 | newString.buffer_[amount] = '\0'; |
Rohit Grover |
1:4769360130ed | 311 | |
Rohit Grover |
1:4769360130ed | 312 | return newString; |
Rohit Grover |
1:4769360130ed | 313 | } |
Rohit Grover |
1:4769360130ed | 314 | |
Rohit Grover |
1:4769360130ed | 315 | char SimpleString::at(int pos) const |
Rohit Grover |
1:4769360130ed | 316 | { |
Rohit Grover |
1:4769360130ed | 317 | return buffer_[pos]; |
Rohit Grover |
1:4769360130ed | 318 | } |
Rohit Grover |
1:4769360130ed | 319 | |
Rohit Grover |
1:4769360130ed | 320 | int SimpleString::find(char ch) const |
Rohit Grover |
1:4769360130ed | 321 | { |
Rohit Grover |
1:4769360130ed | 322 | return findFrom(0, ch); |
Rohit Grover |
1:4769360130ed | 323 | } |
Rohit Grover |
1:4769360130ed | 324 | |
Rohit Grover |
1:4769360130ed | 325 | int SimpleString::findFrom(size_t starting_position, char ch) const |
Rohit Grover |
1:4769360130ed | 326 | { |
Rohit Grover |
1:4769360130ed | 327 | size_t length = size(); |
Rohit Grover |
1:4769360130ed | 328 | for (size_t i = starting_position; i < length; i++) |
Rohit Grover |
1:4769360130ed | 329 | if (buffer_[i] == ch) return (int) i; |
Rohit Grover |
1:4769360130ed | 330 | return -1; |
Rohit Grover |
1:4769360130ed | 331 | } |
Rohit Grover |
1:4769360130ed | 332 | |
Rohit Grover |
1:4769360130ed | 333 | SimpleString SimpleString::subStringFromTill(char startChar, char lastExcludedChar) const |
Rohit Grover |
1:4769360130ed | 334 | { |
Rohit Grover |
1:4769360130ed | 335 | int beginPos = find(startChar); |
Rohit Grover |
1:4769360130ed | 336 | if (beginPos < 0) return ""; |
Rohit Grover |
1:4769360130ed | 337 | |
Rohit Grover |
1:4769360130ed | 338 | int endPos = findFrom((size_t)beginPos, lastExcludedChar); |
Rohit Grover |
1:4769360130ed | 339 | if (endPos == -1) return subString((size_t)beginPos, size()); |
Rohit Grover |
1:4769360130ed | 340 | |
Rohit Grover |
1:4769360130ed | 341 | return subString((size_t)beginPos, (size_t) (endPos - beginPos)); |
Rohit Grover |
1:4769360130ed | 342 | } |
Rohit Grover |
1:4769360130ed | 343 | |
Rohit Grover |
1:4769360130ed | 344 | char* SimpleString::copyToNewBuffer(const char* bufferToCopy, size_t bufferSize) |
Rohit Grover |
1:4769360130ed | 345 | { |
Rohit Grover |
1:4769360130ed | 346 | if(bufferSize == 0) bufferSize = PlatformSpecificStrLen(bufferToCopy) + 1; |
Rohit Grover |
1:4769360130ed | 347 | |
Rohit Grover |
1:4769360130ed | 348 | char* newBuffer = allocStringBuffer(bufferSize); |
Rohit Grover |
1:4769360130ed | 349 | StrNCpy(newBuffer, bufferToCopy, bufferSize); |
Rohit Grover |
1:4769360130ed | 350 | newBuffer[bufferSize-1] = '\0'; |
Rohit Grover |
1:4769360130ed | 351 | return newBuffer; |
Rohit Grover |
1:4769360130ed | 352 | } |
Rohit Grover |
1:4769360130ed | 353 | |
Rohit Grover |
1:4769360130ed | 354 | void SimpleString::copyToBuffer(char* bufferToCopy, size_t bufferSize) const |
Rohit Grover |
1:4769360130ed | 355 | { |
Rohit Grover |
1:4769360130ed | 356 | if (bufferToCopy == NULL || bufferSize == 0) return; |
Rohit Grover |
1:4769360130ed | 357 | |
Rohit Grover |
1:4769360130ed | 358 | size_t sizeToCopy = (bufferSize-1 < size()) ? bufferSize : size(); |
Rohit Grover |
1:4769360130ed | 359 | |
Rohit Grover |
1:4769360130ed | 360 | StrNCpy(bufferToCopy, buffer_, sizeToCopy); |
Rohit Grover |
1:4769360130ed | 361 | bufferToCopy[sizeToCopy] = '\0'; |
Rohit Grover |
1:4769360130ed | 362 | } |
Rohit Grover |
1:4769360130ed | 363 | |
Rohit Grover |
1:4769360130ed | 364 | SimpleString StringFrom(bool value) |
Rohit Grover |
1:4769360130ed | 365 | { |
Rohit Grover |
1:4769360130ed | 366 | return SimpleString(StringFromFormat("%s", value ? "true" : "false")); |
Rohit Grover |
1:4769360130ed | 367 | } |
Rohit Grover |
1:4769360130ed | 368 | |
Rohit Grover |
1:4769360130ed | 369 | SimpleString StringFrom(const char *value) |
Rohit Grover |
1:4769360130ed | 370 | { |
Rohit Grover |
1:4769360130ed | 371 | return SimpleString(value); |
Rohit Grover |
1:4769360130ed | 372 | } |
Rohit Grover |
1:4769360130ed | 373 | |
Rohit Grover |
1:4769360130ed | 374 | SimpleString StringFromOrNull(const char * expected) |
Rohit Grover |
1:4769360130ed | 375 | { |
Rohit Grover |
1:4769360130ed | 376 | return (expected) ? StringFrom(expected) : "(null)"; |
Rohit Grover |
1:4769360130ed | 377 | } |
Rohit Grover |
1:4769360130ed | 378 | |
Rohit Grover |
1:4769360130ed | 379 | SimpleString StringFrom(int value) |
Rohit Grover |
1:4769360130ed | 380 | { |
Rohit Grover |
1:4769360130ed | 381 | return StringFromFormat("%d", value); |
Rohit Grover |
1:4769360130ed | 382 | } |
Rohit Grover |
1:4769360130ed | 383 | |
Rohit Grover |
1:4769360130ed | 384 | SimpleString StringFrom(long value) |
Rohit Grover |
1:4769360130ed | 385 | { |
Rohit Grover |
1:4769360130ed | 386 | return StringFromFormat("%ld", value); |
Rohit Grover |
1:4769360130ed | 387 | } |
Rohit Grover |
1:4769360130ed | 388 | |
Rohit Grover |
1:4769360130ed | 389 | SimpleString StringFrom(const void* value) |
Rohit Grover |
1:4769360130ed | 390 | { |
Rohit Grover |
1:4769360130ed | 391 | return SimpleString("0x") + HexStringFrom(value); |
Rohit Grover |
1:4769360130ed | 392 | } |
Rohit Grover |
1:4769360130ed | 393 | |
Rohit Grover |
1:4769360130ed | 394 | SimpleString HexStringFrom(long value) |
Rohit Grover |
1:4769360130ed | 395 | { |
Rohit Grover |
1:4769360130ed | 396 | return StringFromFormat("%lx", value); |
Rohit Grover |
1:4769360130ed | 397 | } |
Rohit Grover |
1:4769360130ed | 398 | |
Rohit Grover |
1:4769360130ed | 399 | SimpleString HexStringFrom(unsigned long value) |
Rohit Grover |
1:4769360130ed | 400 | { |
Rohit Grover |
1:4769360130ed | 401 | return StringFromFormat("%lx", value); |
Rohit Grover |
1:4769360130ed | 402 | } |
Rohit Grover |
1:4769360130ed | 403 | |
Rohit Grover |
1:4769360130ed | 404 | static long convertPointerToLongValue(const void* value) |
Rohit Grover |
1:4769360130ed | 405 | { |
Rohit Grover |
1:4769360130ed | 406 | /* |
Rohit Grover |
1:4769360130ed | 407 | * This way of converting also can convert a 64bit pointer in a 32bit integer by truncating. |
Rohit Grover |
1:4769360130ed | 408 | * This isn't the right way to convert pointers values and need to change by implementing a |
Rohit Grover |
1:4769360130ed | 409 | * proper portable way to convert pointers to strings. |
Rohit Grover |
1:4769360130ed | 410 | */ |
Rohit Grover |
1:4769360130ed | 411 | long* long_value = (long*) &value; |
Rohit Grover |
1:4769360130ed | 412 | return *long_value; |
Rohit Grover |
1:4769360130ed | 413 | } |
Rohit Grover |
1:4769360130ed | 414 | |
Rohit Grover |
1:4769360130ed | 415 | SimpleString HexStringFrom(const void* value) |
Rohit Grover |
1:4769360130ed | 416 | { |
Rohit Grover |
1:4769360130ed | 417 | return StringFromFormat("%lx", convertPointerToLongValue(value)); |
Rohit Grover |
1:4769360130ed | 418 | } |
Rohit Grover |
1:4769360130ed | 419 | |
Rohit Grover |
1:4769360130ed | 420 | SimpleString StringFrom(double value, int precision) |
Rohit Grover |
1:4769360130ed | 421 | { |
Rohit Grover |
1:4769360130ed | 422 | return StringFromFormat("%.*g", precision, value); |
Rohit Grover |
1:4769360130ed | 423 | } |
Rohit Grover |
1:4769360130ed | 424 | |
Rohit Grover |
1:4769360130ed | 425 | SimpleString StringFrom(char value) |
Rohit Grover |
1:4769360130ed | 426 | { |
Rohit Grover |
1:4769360130ed | 427 | return StringFromFormat("%c", value); |
Rohit Grover |
1:4769360130ed | 428 | } |
Rohit Grover |
1:4769360130ed | 429 | |
Rohit Grover |
1:4769360130ed | 430 | SimpleString StringFrom(const SimpleString& value) |
Rohit Grover |
1:4769360130ed | 431 | { |
Rohit Grover |
1:4769360130ed | 432 | return SimpleString(value); |
Rohit Grover |
1:4769360130ed | 433 | } |
Rohit Grover |
1:4769360130ed | 434 | |
Rohit Grover |
1:4769360130ed | 435 | SimpleString StringFromFormat(const char* format, ...) |
Rohit Grover |
1:4769360130ed | 436 | { |
Rohit Grover |
1:4769360130ed | 437 | SimpleString resultString; |
Rohit Grover |
1:4769360130ed | 438 | va_list arguments; |
Rohit Grover |
1:4769360130ed | 439 | va_start(arguments, format); |
Rohit Grover |
1:4769360130ed | 440 | |
Rohit Grover |
1:4769360130ed | 441 | resultString = VStringFromFormat(format, arguments); |
Rohit Grover |
1:4769360130ed | 442 | va_end(arguments); |
Rohit Grover |
1:4769360130ed | 443 | return resultString; |
Rohit Grover |
1:4769360130ed | 444 | } |
Rohit Grover |
1:4769360130ed | 445 | |
Rohit Grover |
1:4769360130ed | 446 | SimpleString StringFrom(unsigned int i) |
Rohit Grover |
1:4769360130ed | 447 | { |
Rohit Grover |
1:4769360130ed | 448 | return StringFromFormat("%10u (0x%08x)", i, i); |
Rohit Grover |
1:4769360130ed | 449 | } |
Rohit Grover |
1:4769360130ed | 450 | |
Rohit Grover |
1:4769360130ed | 451 | #if CPPUTEST_USE_STD_CPP_LIB |
Rohit Grover |
1:4769360130ed | 452 | |
Rohit Grover |
1:4769360130ed | 453 | #include <string> |
Rohit Grover |
1:4769360130ed | 454 | |
Rohit Grover |
1:4769360130ed | 455 | SimpleString StringFrom(const std::string& value) |
Rohit Grover |
1:4769360130ed | 456 | { |
Rohit Grover |
1:4769360130ed | 457 | return SimpleString(value.c_str()); |
Rohit Grover |
1:4769360130ed | 458 | } |
Rohit Grover |
1:4769360130ed | 459 | |
Rohit Grover |
1:4769360130ed | 460 | SimpleString StringFrom(unsigned long i) |
Rohit Grover |
1:4769360130ed | 461 | { |
Rohit Grover |
1:4769360130ed | 462 | return StringFromFormat("%lu (0x%lx)", i, i); |
Rohit Grover |
1:4769360130ed | 463 | } |
Rohit Grover |
1:4769360130ed | 464 | |
Rohit Grover |
1:4769360130ed | 465 | #else |
Rohit Grover |
1:4769360130ed | 466 | |
Rohit Grover |
1:4769360130ed | 467 | SimpleString StringFrom(unsigned long value) |
Rohit Grover |
1:4769360130ed | 468 | { |
Rohit Grover |
1:4769360130ed | 469 | return StringFromFormat("%lu", value); |
Rohit Grover |
1:4769360130ed | 470 | } |
Rohit Grover |
1:4769360130ed | 471 | |
Rohit Grover |
1:4769360130ed | 472 | #endif |
Rohit Grover |
1:4769360130ed | 473 | |
Rohit Grover |
1:4769360130ed | 474 | //Kludge to get a va_copy in VC++ V6 |
Rohit Grover |
1:4769360130ed | 475 | #ifndef va_copy |
Rohit Grover |
1:4769360130ed | 476 | #define va_copy(copy, original) copy = original; |
Rohit Grover |
1:4769360130ed | 477 | #endif |
Rohit Grover |
1:4769360130ed | 478 | |
Rohit Grover |
1:4769360130ed | 479 | SimpleString VStringFromFormat(const char* format, va_list args) |
Rohit Grover |
1:4769360130ed | 480 | { |
Rohit Grover |
1:4769360130ed | 481 | va_list argsCopy; |
Rohit Grover |
1:4769360130ed | 482 | va_copy(argsCopy, args); |
Rohit Grover |
1:4769360130ed | 483 | enum |
Rohit Grover |
1:4769360130ed | 484 | { |
Rohit Grover |
1:4769360130ed | 485 | sizeOfdefaultBuffer = 100 |
Rohit Grover |
1:4769360130ed | 486 | }; |
Rohit Grover |
1:4769360130ed | 487 | char defaultBuffer[sizeOfdefaultBuffer]; |
Rohit Grover |
1:4769360130ed | 488 | SimpleString resultString; |
Rohit Grover |
1:4769360130ed | 489 | |
Rohit Grover |
1:4769360130ed | 490 | size_t size = (size_t)PlatformSpecificVSNprintf(defaultBuffer, sizeOfdefaultBuffer, format, args); |
Rohit Grover |
1:4769360130ed | 491 | if (size < sizeOfdefaultBuffer) { |
Rohit Grover |
1:4769360130ed | 492 | resultString = SimpleString(defaultBuffer); |
Rohit Grover |
1:4769360130ed | 493 | } |
Rohit Grover |
1:4769360130ed | 494 | else { |
Rohit Grover |
1:4769360130ed | 495 | size_t newBufferSize = size + 1; |
Rohit Grover |
1:4769360130ed | 496 | char* newBuffer = SimpleString::allocStringBuffer(newBufferSize); |
Rohit Grover |
1:4769360130ed | 497 | PlatformSpecificVSNprintf(newBuffer, newBufferSize, format, argsCopy); |
Rohit Grover |
1:4769360130ed | 498 | resultString = SimpleString(newBuffer); |
Rohit Grover |
1:4769360130ed | 499 | |
Rohit Grover |
1:4769360130ed | 500 | SimpleString::deallocStringBuffer(newBuffer); |
Rohit Grover |
1:4769360130ed | 501 | } |
Rohit Grover |
1:4769360130ed | 502 | va_end(argsCopy); |
Rohit Grover |
1:4769360130ed | 503 | return resultString; |
Rohit Grover |
1:4769360130ed | 504 | } |
Rohit Grover |
1:4769360130ed | 505 | |
Rohit Grover |
1:4769360130ed | 506 | SimpleStringCollection::SimpleStringCollection() |
Rohit Grover |
1:4769360130ed | 507 | { |
Rohit Grover |
1:4769360130ed | 508 | collection_ = 0; |
Rohit Grover |
1:4769360130ed | 509 | size_ = 0; |
Rohit Grover |
1:4769360130ed | 510 | } |
Rohit Grover |
1:4769360130ed | 511 | |
Rohit Grover |
1:4769360130ed | 512 | void SimpleStringCollection::allocate(size_t _size) |
Rohit Grover |
1:4769360130ed | 513 | { |
Rohit Grover |
1:4769360130ed | 514 | delete[] collection_; |
Rohit Grover |
1:4769360130ed | 515 | |
Rohit Grover |
1:4769360130ed | 516 | size_ = _size; |
Rohit Grover |
1:4769360130ed | 517 | collection_ = new SimpleString[size_]; |
Rohit Grover |
1:4769360130ed | 518 | } |
Rohit Grover |
1:4769360130ed | 519 | |
Rohit Grover |
1:4769360130ed | 520 | SimpleStringCollection::~SimpleStringCollection() |
Rohit Grover |
1:4769360130ed | 521 | { |
Rohit Grover |
1:4769360130ed | 522 | delete[] (collection_); |
Rohit Grover |
1:4769360130ed | 523 | } |
Rohit Grover |
1:4769360130ed | 524 | |
Rohit Grover |
1:4769360130ed | 525 | size_t SimpleStringCollection::size() const |
Rohit Grover |
1:4769360130ed | 526 | { |
Rohit Grover |
1:4769360130ed | 527 | return size_; |
Rohit Grover |
1:4769360130ed | 528 | } |
Rohit Grover |
1:4769360130ed | 529 | |
Rohit Grover |
1:4769360130ed | 530 | SimpleString& SimpleStringCollection::operator[](size_t index) |
Rohit Grover |
1:4769360130ed | 531 | { |
Rohit Grover |
1:4769360130ed | 532 | if (index >= size_) { |
Rohit Grover |
1:4769360130ed | 533 | empty_ = ""; |
Rohit Grover |
1:4769360130ed | 534 | return empty_; |
Rohit Grover |
1:4769360130ed | 535 | } |
Rohit Grover |
1:4769360130ed | 536 | |
Rohit Grover |
1:4769360130ed | 537 | return collection_[index]; |
Rohit Grover |
1:4769360130ed | 538 | } |