openMSX
strCat.cc
Go to the documentation of this file.
1#include "catch.hpp"
2#include "strCat.hh"
3
4#include <array>
5#include <iostream>
6
7static std::string rValue()
8{
9 return "bar";
10}
11
12struct MyType
13{
14 char c;
15};
16
17static std::ostream& operator<<(std::ostream& os, const MyType& m)
18{
19 os << "--|" << m.c << "|--";
20 return os;
21}
22
23TEST_CASE("strCat")
24{
25 std::string str = "abc";
26 std::string_view sr = "xyz";
27 const char* literal = "foo";
28 std::array<char, 100> buf; buf[0] = 'q'; buf[1] = 'u'; buf[2] = 'x'; buf[3] = '\0';
29 char c = '-';
30 unsigned char uc = 222;
31 int m = -31;
32 int i = 123456;
33 float f = 6.28f;
34 double d = -1.234;
35 MyType t = {'a'};
36
37 SECTION("zero") {
38 CHECK(strCat() == "");
39 }
40 SECTION("one") {
41 CHECK(strCat(rValue()) == "bar");
42 CHECK(strCat(str) == "abc");
43 CHECK(strCat(sr) == "xyz");
44 CHECK(strCat(literal) == "foo");
45 CHECK(strCat(buf.data()) == "qux");
46 CHECK(strCat(c) == "-");
47 CHECK(strCat(uc) == "222");
48 CHECK(strCat(m) == "-31");
49 CHECK(strCat(i) == "123456");
50 CHECK(strCat(f) == "6.28");
51 CHECK(strCat(d) == "-1.234");
52 CHECK(strCat(t) == "--|a|--");
53 CHECK(strCat(hex_string<8>(i)) == "0001e240");
54 CHECK(strCat(hex_string<4>(i)) == "e240");
55 CHECK(strCat(hex_string<4, HexCase::upper>(i)) == "E240");
56 CHECK(strCat(hex_string(Digits{5}, i)) == "1e240");
57 CHECK(strCat(hex_string<HexCase::upper>(Digits{5}, i)) == "1E240");
58 CHECK(strCat(hex_string(Digits{3}, i)) == "240");
59 CHECK(strCat(bin_string<16>(i)) == "1110001001000000");
60 CHECK(strCat(bin_string<8>(i)) == "01000000");
61 CHECK(strCat(dec_string<8>(i)) == " 123456");
62 CHECK(strCat(dec_string<6>(i)) == "123456");
63 CHECK(strCat(dec_string<4>(i)) == "3456");
64 CHECK(strCat(spaces(5)) == " ");
65 }
66 SECTION("two") {
67 CHECK(strCat(str, i) == "abc123456");
68 CHECK(strCat(str, m) == "abc-31");
69 CHECK(strCat(str, uc) == "abc222");
70 CHECK(strCat(i, str) == "123456abc");
71 CHECK(strCat(m, str) == "-31abc");
72 CHECK(strCat(uc, str) == "222abc");
73 CHECK(strCat(buf.data(), i) == "qux123456");
74 CHECK(strCat(literal, m) == "foo-31");
75 CHECK(strCat(i, m) == "123456-31");
76 CHECK(strCat(c, m) == "--31");
77 CHECK(strCat(i, rValue()) == "123456bar");
78 CHECK(strCat(spaces(4), i) == " 123456");
79 CHECK(strCat(hex_string<3>(i), i) == "240123456");
80 CHECK(strCat(c, hex_string<1>(i)) == "-0");
81 CHECK(strCat(rValue(), uc) == "bar222");
82
83 // these have special overloads
84 CHECK(strCat(str, str) == "abcabc");
85 CHECK(strCat(literal, str) == "fooabc");
86 CHECK(strCat(c, str) == "-abc");
87 CHECK(strCat(str, literal) == "abcfoo");
88 CHECK(strCat(str, c) == "abc-");
89 CHECK(strCat(rValue(), str) == "barabc");
90 CHECK(strCat(str, rValue()) == "abcbar");
91 CHECK(strCat(rValue(), rValue()) == "barbar");
92 CHECK(strCat(literal, rValue()) == "foobar");
93 CHECK(strCat(c, rValue()) == "-bar");
94 CHECK(strCat(rValue(), literal) == "barfoo");
95 CHECK(strCat(rValue(), c) == "bar-");
96 CHECK(strCat(rValue(), sr) == "barxyz");
97 }
98 SECTION("three") {
99 CHECK(strCat(str, str, str) == "abcabcabc");
100 CHECK(strCat(i, m, c) == "123456-31-");
101 CHECK(strCat(literal, buf.data(), rValue()) == "fooquxbar");
102
103 // 1st an r-value is a special case
104 CHECK(strCat(rValue(), i, literal) == "bar123456foo");
105 CHECK(strCat(rValue(), uc, buf.data()) == "bar222qux");
106 }
107 SECTION("many") {
108 CHECK(strCat(str, sr, literal, buf.data(), c, uc, m, i, rValue(), spaces(2), hex_string<2>(255)) ==
109 "abcxyzfooqux-222-31123456bar ff");
110 CHECK(strCat(rValue(), uc, buf.data(), c, spaces(2), str, i, hex_string<3>(9999), sr, literal, m) ==
111 "bar222qux- abc12345670fxyzfoo-31");
112 }
113}
114
115template<typename... Args>
116static void testAppend(const std::string& expected, Args&& ...args)
117{
118 std::string s1;
119 strAppend(s1, std::forward<Args>(args)...);
120 CHECK(s1 == expected);
121
122 std::string s2 = "abcdefghijklmnopqrstuvwxyz";
123 strAppend(s2, std::forward<Args>(args)...);
124 CHECK(s2 == ("abcdefghijklmnopqrstuvwxyz" + expected));
125}
126
127TEST_CASE("strAppend")
128{
129 std::string str = "mno";
130 std::string_view sr = "rst";
131 const char* literal = "ijklmn";
132 std::array<char, 100> buf; buf[0] = 'd'; buf[1] = 'e'; buf[2] = '\0'; buf[3] = 'f';
133 char c = '+';
134 unsigned u = 4294967295u;
135 long long ll = -876;
136
137 SECTION("zero") {
138 testAppend("");
139 }
140 SECTION("one") {
141 testAppend("bar", rValue());
142 testAppend("mno", str);
143 testAppend("rst", sr);
144 testAppend("ijklmn", literal);
145 testAppend("de", buf.data());
146 testAppend("+", c);
147 testAppend("4294967295", u);
148 testAppend("-876", ll);
149 testAppend(" ", spaces(10));
150 testAppend("fffffffc94", hex_string<10>(ll));
151 }
152 SECTION("many") {
153 std::string s = "bla";
154 strAppend(s, str, sr, literal, spaces(3));
155 strAppend(s, buf.data(), c, u, ll, rValue());
156 CHECK(s == "blamnorstijklmn de+4294967295-876bar");
157 }
158}
159
160
161#if 0
162
163// Not part of the actual unittest. Can be used to (manually) inspect the
164// quality of the generated code.
165
166auto test1(int i) { return strCat(i); }
167auto test1b(int i) { return std::to_string(i); }
168auto test2(const char* s) { return strCat(s); }
169auto test3(std::string_view s) { return strCat(s); }
170auto test4(const std::string& s) { return strCat(s); }
171auto test5() { return strCat("bla"); }
172auto test6() { return strCat('a'); }
173auto test7(char i) { return strCat('a', i, "bla"); }
174auto test8(int i, unsigned u) { return strCat(i, u); }
175
176auto testA1(const std::string& s1, const std::string& s2) { return strCat(s1, s2); }
177auto testA2(const std::string& s1, const std::string& s2) { return s1 + s2; }
178
179#endif
TclObject t
CHECK(m3==m3)
TEST_CASE("strCat")
Definition strCat.cc:23
auto spaces(size_t n)
Definition strCat.hh:801
std::string strCat()
Definition strCat.hh:703
strCatImpl::ConcatVariableWidthHexIntegral< Case, T > hex_string(Digits n, T t)
Definition strCat.hh:778
void strAppend(std::string &result, Ts &&...ts)
Definition strCat.hh:752
char c
Definition strCat.cc:14