| 1 |
// Copyright (c) 2016 Ryan Prichard |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
// of this software and associated documentation files (the "Software"), to |
| 5 |
// deal in the Software without restriction, including without limitation the |
| 6 |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 |
// sell copies of the Software, and to permit persons to whom the Software is |
| 8 |
// furnished to do so, subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in |
| 11 |
// all copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 |
// IN THE SOFTWARE. |
| 20 |
|
| 21 |
#define STRING_BUILDER_TESTING |
| 22 |
|
| 23 |
#include "StringBuilder.h" |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#include <iomanip> |
| 29 |
#include <sstream> |
| 30 |
|
| 31 |
void display(const std::string &str) { fprintf(stderr, "%s", str.c_str()); } |
| 32 |
void display(const std::wstring &str) { fprintf(stderr, "%ls", str.c_str()); } |
| 33 |
|
| 34 |
#define CHECK_EQ(x, y) \ |
| 35 |
do { \ |
| 36 |
const auto xval = (x); \ |
| 37 |
const auto yval = (y); \ |
| 38 |
if (xval != yval) { \ |
| 39 |
fprintf(stderr, "error: %s:%d: %s != %s: ", \ |
| 40 |
__FILE__, __LINE__, #x, #y); \ |
| 41 |
display(xval); \ |
| 42 |
fprintf(stderr, " != "); \ |
| 43 |
display(yval); \ |
| 44 |
fprintf(stderr, "\n"); \ |
| 45 |
} \ |
| 46 |
} while(0) |
| 47 |
|
| 48 |
template <typename C, typename I> |
| 49 |
std::basic_string<C> decOfIntSS(const I value) { |
| 50 |
// std::to_string and std::to_wstring are missing in Cygwin as of this |
| 51 |
// writing (early 2016). |
| 52 |
std::basic_stringstream<C> ss; |
| 53 |
ss << +value; // We must promote char to print it as an integer. |
| 54 |
return ss.str(); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
template <typename C, bool leadingZeros=false, typename I> |
| 59 |
std::basic_string<C> hexOfIntSS(const I value) { |
| 60 |
typedef typename std::make_unsigned<I>::type U; |
| 61 |
const unsigned long long u64Value = value & static_cast<U>(~0); |
| 62 |
std::basic_stringstream<C> ss; |
| 63 |
if (leadingZeros) { |
| 64 |
ss << std::setfill(static_cast<C>('0')) << std::setw(sizeof(I) * 2); |
| 65 |
} |
| 66 |
ss << std::hex << u64Value; |
| 67 |
return ss.str(); |
| 68 |
} |
| 69 |
|
| 70 |
template <typename I> |
| 71 |
void testValue(I value) { |
| 72 |
CHECK_EQ(decOfInt(value).str(), (decOfIntSS<char>(value))); |
| 73 |
CHECK_EQ(wdecOfInt(value).str(), (decOfIntSS<wchar_t>(value))); |
| 74 |
CHECK_EQ((hexOfInt<false>(value).str()), (hexOfIntSS<char, false>(value))); |
| 75 |
CHECK_EQ((hexOfInt<true>(value).str()), (hexOfIntSS<char, true>(value))); |
| 76 |
CHECK_EQ((whexOfInt<false>(value).str()), (hexOfIntSS<wchar_t, false>(value))); |
| 77 |
CHECK_EQ((whexOfInt<true>(value).str()), (hexOfIntSS<wchar_t, true>(value))); |
| 78 |
} |
| 79 |
|
| 80 |
template <typename I> |
| 81 |
void testType() { |
| 82 |
typedef typename std::make_unsigned<I>::type U; |
| 83 |
const U quarter = static_cast<U>(1) << (sizeof(U) * 8 - 2); |
| 84 |
for (unsigned quarterIndex = 0; quarterIndex < 4; ++quarterIndex) { |
| 85 |
for (int offset = -18; offset <= 18; ++offset) { |
| 86 |
const I value = quarter * quarterIndex + static_cast<U>(offset); |
| 87 |
testValue(value); |
| 88 |
} |
| 89 |
} |
| 90 |
testValue(static_cast<I>(42)); |
| 91 |
testValue(static_cast<I>(123456)); |
| 92 |
testValue(static_cast<I>(0xdeadfacecafebeefull)); |
| 93 |
} |
| 94 |
|
| 95 |
int main() { |
| 96 |
testType<char>(); |
| 97 |
|
| 98 |
testType<signed char>(); |
| 99 |
testType<signed short>(); |
| 100 |
testType<signed int>(); |
| 101 |
testType<signed long>(); |
| 102 |
testType<signed long long>(); |
| 103 |
|
| 104 |
testType<unsigned char>(); |
| 105 |
testType<unsigned short>(); |
| 106 |
testType<unsigned int>(); |
| 107 |
testType<unsigned long>(); |
| 108 |
testType<unsigned long long>(); |
| 109 |
|
| 110 |
StringBuilder() << static_cast<const void*>("TEST"); |
| 111 |
WStringBuilder() << static_cast<const void*>("TEST"); |
| 112 |
|
| 113 |
fprintf(stderr, "All tests completed!\n"); |
| 114 |
} |