Debian Codegraph @

qt5/tests/check_goostring.cpp

raw
1 #include <QtCore/QScopedPointer>
2 #include <QtTest/QTest>
3
4 #include "goo/GooString.h"
5
6 class TestGooString : public QObject
7 {
8 Q_OBJECT
9 public:
10 explicit TestGooString(QObject *parent = nullptr) : QObject(parent) { }
11 private Q_SLOTS:
12 void testInsertData_data();
13 void testInsertData();
14 void testInsert();
15 void testFormat();
16 void testFromNullptr();
17 };
18
19 void TestGooString::testInsertData_data()
20 {
21 QTest::addColumn<QByteArray>("string");
22 QTest::addColumn<QByteArray>("addition");
23 QTest::addColumn<int>("position");
24 QTest::addColumn<QByteArray>("result");
25
26 QTest::newRow("foo") << QByteArray("foo") << QByteArray("bar") << 0 << QByteArray("barfoo");
27 QTest::newRow("<empty>") << QByteArray() << QByteArray("bar") << 0 << QByteArray("bar");
28 QTest::newRow("foo+bar #1") << QByteArray("f+bar") << QByteArray("oo") << 1 << QByteArray("foo+bar");
29 QTest::newRow("foo+bar #2") << QByteArray("fobar") << QByteArray("o+") << 2 << QByteArray("foo+bar");
30 QTest::newRow("foo+bar #last") << QByteArray("foo+r") << QByteArray("ba") << 4 << QByteArray("foo+bar");
31 QTest::newRow("foo+bar #end") << QByteArray("foo+") << QByteArray("bar") << 4 << QByteArray("foo+bar");
32 QTest::newRow("long #start") << QByteArray("very string") << QByteArray("long long long long long ") << 5 << QByteArray("very long long long long long string");
33 }
34
35 void TestGooString::testInsertData()
36 {
37 QFETCH(QByteArray, string);
38 QFETCH(QByteArray, addition);
39 QFETCH(int, position);
40 QFETCH(QByteArray, result);
41
42 GooString goo(string.constData());
43 QCOMPARE(goo.c_str(), string.constData());
44 goo.insert(position, addition.constData());
45 QCOMPARE(goo.c_str(), result.constData());
46 }
47
48 void TestGooString::testInsert()
49 {
50 {
51 GooString goo;
52 goo.insert(0, ".");
53 goo.insert(0, "This is a very long long test string");
54 QCOMPARE(goo.c_str(), "This is a very long long test string.");
55 }
56 {
57 GooString goo;
58 goo.insert(0, "second-part-third-part");
59 goo.insert(0, "first-part-");
60 QCOMPARE(goo.c_str(), "first-part-second-part-third-part");
61 }
62 }
63
64 void TestGooString::testFormat()
65 {
66 {
67 const std::string goo(GooString::format("{0:d},{1:x}", 1, 0xF));
68 QCOMPARE(goo.c_str(), "1,f");
69 }
70 {
71 const std::string goo(GooString::format("{0:d},{0:x},{0:X},{0:o},{0:b},{0:w}", 0xA));
72 QCOMPARE(goo.c_str(), "10,a,A,12,1010, ");
73 }
74 {
75 const std::string goo(GooString::format("{0:d},{0:x},{0:X},{0:o},{0:b}", -0xA));
76 QCOMPARE(goo.c_str(), "-10,-a,-A,-12,-1010");
77 }
78 {
79 const std::string goo(GooString::format("{0:c}{1:c}{2:c}{3:c}", 'T', (char)'E', (short)'S', (int)'T'));
80 QCOMPARE(goo.c_str(), "TEST");
81
82 GooString gooGoo(goo);
83 const std::string goo2(GooString::format("{0:s} {1:t}", "TEST", &gooGoo));
84 QCOMPARE(goo2.c_str(), "TEST TEST");
85 }
86 {
87 const std::string goo(GooString::format("{0:ud} {1:d} {2:d}", UINT_MAX, INT_MAX, INT_MIN));
88 const QByteArray expected = QStringLiteral("%1 %2 %3").arg(UINT_MAX).arg(INT_MAX).arg(INT_MIN).toLatin1();
89 QCOMPARE(goo.c_str(), expected.constData());
90 }
91 {
92 const std::string goo(GooString::format("{0:uld} {1:ld} {2:ld}", ULONG_MAX, LONG_MAX, LONG_MIN));
93 const QByteArray expected = QStringLiteral("%1 %2 %3").arg(ULONG_MAX).arg(LONG_MAX).arg(LONG_MIN).toLatin1();
94 QCOMPARE(goo.c_str(), expected.constData());
95 }
96 {
97 const std::string goo(GooString::format("{0:ulld} {1:lld} {2:lld}", ULLONG_MAX, LLONG_MAX, LLONG_MIN));
98 const QByteArray expected = QStringLiteral("%1 %2 %3").arg(ULLONG_MAX).arg(LLONG_MAX).arg(LLONG_MIN).toLatin1();
99 QCOMPARE(goo.c_str(), expected.constData());
100 }
101 {
102 const std::string gooD(GooString::format("{0:.1f} {0:.1g} {0:.1gs} | {1:.1f} {1:.1g} {1:.1gs}", 1., .012));
103 const std::string gooF(GooString::format("{0:.1f} {0:.1g} {0:.1gs} | {1:.1f} {1:.1g} {1:.1gs}", 1.f, .012f));
104 QCOMPARE(gooD.c_str(), "1.0 1 1 | 0.0 0 0.01");
105 QCOMPARE(gooF.c_str(), "1.0 1 1 | 0.0 0 0.01");
106 }
107 {
108 const std::string goo(GooString::format("{0:.4f} {0:.4g} {0:.4gs}", .012));
109 QCOMPARE(goo.c_str(), "0.0120 0.012 0.012");
110 }
111 {
112 const std::string goo(GooString::format("{{ SomeText {0:d} }}", 1));
113 QCOMPARE(goo.c_str(), "{ SomeText 1 }");
114 }
115 {
116 const std::string goo(GooString::format("{{{{ {{ SomeText {0:d}", 2));
117 QCOMPARE(goo.c_str(), "{{ { SomeText 2");
118 }
119 {
120 const std::string goo(GooString::format("SomeText {0:d} }} }}}}", 3));
121 QCOMPARE(goo.c_str(), "SomeText 3 } }}");
122 }
123 }
124
125 void TestGooString::testFromNullptr()
126 {
127 {
128 GooString str;
129 str.Set(static_cast<const GooString *>(nullptr));
130 QCOMPARE(str.size(), 0);
131 }
132
133 {
134 GooString str { static_cast<const char *>(nullptr) };
135 QCOMPARE(str.size(), 0);
136 }
137
138 {
139 GooString str { static_cast<const char *>(nullptr), 0 };
140 QCOMPARE(str.size(), 0);
141 }
142
143 {
144 GooString str;
145 str.Set(static_cast<const char *>(nullptr));
146 QCOMPARE(str.size(), 0);
147 }
148
149 {
150 GooString str;
151 str.Set(static_cast<const char *>(nullptr), 0);
152 QCOMPARE(str.size(), 0);
153 }
154 }
155
156 QTEST_GUILESS_MAIN(TestGooString)
157 #include "check_goostring.moc"