1 // Copyright (c) 2016 Ryan Prichard
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
21 #define STRING_BUILDER_TESTING
23 #include "StringBuilder.h"
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()); }
34 #define CHECK_EQ(x, y) \
36 const auto xval = (x); \
37 const auto yval = (y); \
39 fprintf(stderr, "error: %s:%d: %s != %s: ", \
40 __FILE__, __LINE__, #x, #y); \
42 fprintf(stderr, " != "); \
44 fprintf(stderr, "\n"); \
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.
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;
64 ss << std::setfill(static_cast<C>('0')) << std::setw(sizeof(I) * 2);
66 ss << std::hex << u64Value;
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)));
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);
90 testValue(static_cast<I>(42));
91 testValue(static_cast<I>(123456));
92 testValue(static_cast<I>(0xdeadfacecafebeefull));
98 testType<signed char>();
99 testType<signed short>();
100 testType<signed int>();
101 testType<signed long>();
102 testType<signed long long>();
104 testType<unsigned char>();
105 testType<unsigned short>();
106 testType<unsigned int>();
107 testType<unsigned long>();
108 testType<unsigned long long>();
110 StringBuilder() << static_cast<const void*>("TEST");
111 WStringBuilder() << static_cast<const void*>("TEST");
113 fprintf(stderr, "All tests completed!\n");