OLD | NEW |
(Empty) | |
| 1 #include "minitest.h" |
| 2 |
| 3 #include <wchar.h> |
| 4 #include <stdarg.h> |
| 5 #include <stdio.h> |
| 6 #include <stdlib.h> |
| 7 |
| 8 namespace { |
| 9 |
| 10 struct TestInfo { |
| 11 const char* test_name; |
| 12 const char* case_name; |
| 13 minitest::TestFunction* test_function; |
| 14 TestInfo* next; |
| 15 }; |
| 16 |
| 17 TestInfo* g_test_infos; |
| 18 TestInfo** g_test_infos_tail; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 namespace minitest { |
| 23 |
| 24 namespace internal { |
| 25 |
| 26 String::String(const char* str, size_t len) { |
| 27 Resize(len); |
| 28 ::memcpy(str_, str, len); |
| 29 size_ = len; |
| 30 } |
| 31 |
| 32 String& String::operator+=(const String& other) { |
| 33 size_t old_size = size_; |
| 34 Resize(old_size + other.size_); |
| 35 ::memcpy(str_ + old_size, other.str_, other.size_); |
| 36 return *this; |
| 37 } |
| 38 |
| 39 String& String::operator+=(const char* str) { |
| 40 size_t len = ::strlen(str); |
| 41 size_t old_size = size_; |
| 42 Resize(old_size + len); |
| 43 ::memcpy(str_ + old_size, str, len); |
| 44 return *this; |
| 45 } |
| 46 |
| 47 String& String::operator+=(char ch) { |
| 48 Resize(size_ + 1); |
| 49 str_[size_ - 1] = ch; |
| 50 return *this; |
| 51 } |
| 52 |
| 53 String& String::operator<<(const String& other) { |
| 54 (*this) += other; |
| 55 return *this; |
| 56 } |
| 57 |
| 58 String& String::operator<<(const char* str) { |
| 59 (*this) += str; |
| 60 return *this; |
| 61 } |
| 62 |
| 63 String& String::operator<<(char ch) { |
| 64 (*this) += ch; |
| 65 return *this; |
| 66 } |
| 67 |
| 68 String& String::operator<<(bool v) { |
| 69 (*this) += (v ? "true" : "false"); |
| 70 return *this; |
| 71 } |
| 72 |
| 73 #define MINITEST_STRING_OPERATOR_LL_(ParamType, Format) \ |
| 74 String& String::operator<<(ParamType v) { \ |
| 75 char buf[20]; \ |
| 76 ::snprintf(buf, sizeof(buf), Format, v); \ |
| 77 (*this) += buf; \ |
| 78 return *this; \ |
| 79 } |
| 80 |
| 81 MINITEST_STRING_OPERATOR_LL_(signed char, "%hhd") |
| 82 MINITEST_STRING_OPERATOR_LL_(unsigned char, "%hhu") |
| 83 MINITEST_STRING_OPERATOR_LL_(short, "%hd") |
| 84 MINITEST_STRING_OPERATOR_LL_(unsigned short, "%hu"); |
| 85 MINITEST_STRING_OPERATOR_LL_(int, "%d") |
| 86 MINITEST_STRING_OPERATOR_LL_(unsigned, "%u") |
| 87 MINITEST_STRING_OPERATOR_LL_(long, "%ld") |
| 88 MINITEST_STRING_OPERATOR_LL_(unsigned long, "%lu") |
| 89 MINITEST_STRING_OPERATOR_LL_(long long, "%lld") |
| 90 MINITEST_STRING_OPERATOR_LL_(unsigned long long, "%llu") |
| 91 MINITEST_STRING_OPERATOR_LL_(float, "%f") |
| 92 MINITEST_STRING_OPERATOR_LL_(double, "%f") |
| 93 MINITEST_STRING_OPERATOR_LL_(const void*, "%p") |
| 94 |
| 95 #undef MINITEST_STRING_OPERATOR_LL_ |
| 96 |
| 97 void String::Clear() { |
| 98 ::free(str_); |
| 99 str_ = NULL; |
| 100 size_ = 0; |
| 101 capacity_ = 0; |
| 102 } |
| 103 |
| 104 void String::Resize(size_t new_size) { |
| 105 if (new_size > capacity_) { |
| 106 size_t new_capacity = capacity_; |
| 107 while (new_capacity < new_size) |
| 108 new_capacity += (new_capacity >> 1) + 8; |
| 109 |
| 110 Reserve(new_capacity); |
| 111 } |
| 112 str_[new_size] = '\0'; |
| 113 size_ = new_size; |
| 114 } |
| 115 |
| 116 void String::Reserve(size_t new_capacity) { |
| 117 str_ = reinterpret_cast<char*>(::realloc(str_, new_capacity + 1)); |
| 118 if (new_capacity > capacity_) |
| 119 ::memset(str_ + capacity_, '\0', new_capacity - capacity_); |
| 120 capacity_ = new_capacity; |
| 121 } |
| 122 |
| 123 } // namespace internal |
| 124 |
| 125 internal::String Format(const char* format, ...) { |
| 126 internal::String result; |
| 127 va_list args, args2; |
| 128 va_start(args, format); |
| 129 // Note: Resize(n) allocates at least n+1 bytes. |
| 130 result.Resize(100); |
| 131 int len; |
| 132 for (;;) { |
| 133 va_copy(args2, args); |
| 134 len = snprintf(&result[0], result.size(), format, args2); |
| 135 va_end(args2); |
| 136 // On Windows, snprintf() returns -1 on truncation. On other |
| 137 // platforms, it returns the size of the string, without truncation. |
| 138 if (len >= 0 && static_cast<size_t>(len) <= result.size()) |
| 139 break; |
| 140 result.Resize(result.size()*2); |
| 141 } |
| 142 va_end(args); |
| 143 return result; |
| 144 } |
| 145 |
| 146 void TestCase::Failure() { |
| 147 if (result_ == PASS) |
| 148 result_ = FAIL; |
| 149 if (!text_.empty()) |
| 150 printf("%s\n", text_.c_str()); |
| 151 } |
| 152 |
| 153 void TestCase::FatalFailure() { |
| 154 result_ = FATAL; |
| 155 if (!text_.empty()) |
| 156 printf("%s\n", text_.c_str()); |
| 157 } |
| 158 |
| 159 internal::String& TestCase::GetText() { |
| 160 text_.Clear(); |
| 161 return text_; |
| 162 } |
| 163 |
| 164 void RegisterTest(const char* test_name, |
| 165 const char* case_name, |
| 166 TestFunction* test_function) { |
| 167 if (g_test_infos_tail == NULL) |
| 168 g_test_infos_tail = &g_test_infos; |
| 169 |
| 170 TestInfo* info = reinterpret_cast<TestInfo*>(::malloc(sizeof(*info))); |
| 171 |
| 172 info->test_name = test_name; |
| 173 info->case_name = case_name; |
| 174 info->test_function = test_function; |
| 175 |
| 176 *g_test_infos_tail = info; |
| 177 g_test_infos_tail = &info->next; |
| 178 } |
| 179 |
| 180 } // namespace minitest |
| 181 |
| 182 int main(void) { |
| 183 printf("--- TESTS STARTING ---\n"); |
| 184 TestInfo* info = g_test_infos; |
| 185 unsigned num_failures = 0; |
| 186 unsigned num_tests = 0; |
| 187 for ( ; info != NULL; info = info->next) { |
| 188 minitest::TestCase testcase; |
| 189 printf("[ RUNNING ] %s.%s\n", info->test_name, info->case_name); |
| 190 num_tests += 1; |
| 191 info->test_function(&testcase); |
| 192 const char* status; |
| 193 switch (testcase.result()) { |
| 194 case minitest::TestCase::PASS: |
| 195 status = "OK"; |
| 196 break; |
| 197 case minitest::TestCase::FAIL: |
| 198 case minitest::TestCase::FATAL: |
| 199 status = "FAIL"; |
| 200 num_failures += 1; |
| 201 break; |
| 202 } |
| 203 printf("[ %9s ] %s.%s\n", status, info->test_name, info->case_name); |
| 204 } |
| 205 printf("--- TESTS COMPLETED ---\n"); |
| 206 printf("tests completed: %d\n", num_tests); |
| 207 printf("tests passed: %d\n", num_tests - num_failures); |
| 208 printf("tests failed: %d\n", num_failures); |
| 209 |
| 210 return (num_failures > 0); |
| 211 } |
| 212 |
OLD | NEW |