File: intltesttest.cpp

package info (click to toggle)
icu 78.2-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 123,992 kB
  • sloc: cpp: 527,891; ansic: 112,789; sh: 4,983; makefile: 4,657; perl: 3,199; python: 2,933; xml: 749; sed: 36; lisp: 12
file content (258 lines) | stat: -rw-r--r-- 15,091 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Β© 2025 and later: Unicode, Inc. and others.
// License & terms of use: https://www.unicode.org/copyright.html

#include "intltest.h"

class IntlTestTest : public IntlTest {
  public:
    void runIndexedTest(int32_t index, UBool exec, const char*& name, char* /*par*/) override {
        if (exec) {
            logln("TestSuite IntlTestTest: ");
        }
        TESTCASE_AUTO_BEGIN;

        TESTCASE_AUTO(testAssertEquals);
        TESTCASE_AUTO(testAssertNotEquals);

        TESTCASE_AUTO_END;
    }

  protected:
    class TestableIntlTest : public IntlTest {
      public:
        TestableIntlTest(std::u16string* const lastLogLine) : lastLogLine(lastLogLine) {}

        size_t newErrors() {
            const size_t result = errors;
            errors = 0;
            return result;
        }

        virtual int32_t IncErrorCount() override {
          ++errors;
          return IntlTest::IncErrorCount();
        }

      protected:
        virtual void LL_message(std::u16string_view message, UBool newline) override {
            currentLogLine += message;
            if (newline) {
                *lastLogLine = currentLogLine;
                currentLogLine.clear();
            }
        }

      private:
        size_t errors = 0;
        std::u16string* lastLogLine;
        std::u16string currentLogLine;
    };

    void testAssertEquals() {
        std::u16string lastLogLine;
        TestableIntlTest metatest(&lastLogLine);
        // Booleans: Both UBool, mixed, both bool.
        metatest.assertEquals(WHERE "should fail", static_cast<UBool>(true), static_cast<UBool>(false));
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("boolean in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"true"));
        metatest.assertEquals(WHERE "should fail", static_cast<UBool>(true), false);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("boolean in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"true"));
        metatest.assertEquals(WHERE "should fail", true, false);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("boolean in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"true"));
        // The mysterious int32_t.
        metatest.assertEquals(WHERE "should fail", -1, 66);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal int32", std::u16string::npos, lastLogLine.find(u"-1"));
        assertNotEquals("hex int32 in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"0xFFFFFFFF"));
        metatest.assertEquals(WHERE "should fail", 65, 66);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal int32 in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"65"));
        assertNotEquals("hex int32 in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"0x41"));
        metatest.assertEquals(WHERE "should fail", UnicodeString("").char32At(0),
                              UnicodeString("B").char32At(0));
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal int32 in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"65535"));
        assertNotEquals("hex int32 in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"0xFFFF"));
        metatest.assertEquals(WHERE "should fail", UnicodeString("AB").char32At(0),
                              UnicodeString("AB").char32At(1));
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal int32 in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"65"));
        assertNotEquals("hex int32 in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"0x41"));
        // Definitely characters.
        // char32_t.
        metatest.assertEquals(WHERE "should fail", U'A', U'B');
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("char32_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"U+0041 A"));
        metatest.assertEquals(WHERE "should fail", UnicodeString("A").char32At(0), U'B');
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("char32_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"U+0041 A"));
        metatest.assertEquals(WHERE "should fail", U'A', 0x42);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("char32_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"U+0041 A"));
        // char16_t.
        metatest.assertEquals(WHERE "should fail", u'A', u'B');
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("char16_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"U+0041 A"));
        metatest.assertEquals(WHERE "should fail", 0x41, u'B');
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("char16_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"U+0041 A"));
        metatest.assertEquals(WHERE "should fail", UnicodeString("A").charAt(0), 0x42);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("char16_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"U+0041 A"));
        // Actual numeric comparisons.
        metatest.assertEquals(WHERE "should fail", static_cast<uint32_t>(65), 66);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal uint32 in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"65"));
        assertEquals("NO hex uint32 in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"41"));
        constexpr std::u16string_view s = u"π’Œ‰ 𒂍𒁾𒁀𒀀 π’Œ“ π’ŒŒπ’†·π’€€π’€­ π’ˆ¨π’‚  π’‰Œπ’Ίπ’‰ˆπ’‚—";
        metatest.assertEquals(WHERE "should fail", s.size(), 38);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal size_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"37"));
        assertEquals("NO hex size_t in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"25"));
        // This one is int32_t on 32-bit platforms.
        metatest.assertEquals(WHERE "should fail", s.end() - s.begin(), 38);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal ptrdiff_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"37"));
        if (sizeof(std::ptrdiff_t) == 4) {
            assertNotEquals("hex 32-bit ptrdiff_t in " + lastLogLine, std::u16string::npos,
                            lastLogLine.find(u"25"));
        } else {
            assertEquals("NO hex ptrdiff_t in " + lastLogLine, std::u16string::npos,
                         lastLogLine.find(u"25"));
        }
        // Making the enum inherit from int8_t, which is UBool, also allows us to check that we don’t
        // fall into the UBool overload (this would have been the case before this got templatized).
        enum Button : int8_t {
            Abort = 65,
            Retry = 66,
            Ignore = 67,
        };
        metatest.assertEquals(WHERE "should fail", Abort, Retry);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal enum in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"65"));
        assertEquals("NO hex enum in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"41"));
        assertEquals("NO UBool in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"UBool"));
    }

    void testAssertNotEquals() {
        std::u16string lastLogLine;
        TestableIntlTest metatest(&lastLogLine);
        // Booleans: Both UBool, mixed, both bool.
        metatest.assertNotEquals(WHERE "should fail", static_cast<UBool>(true), static_cast<UBool>(true));
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("boolean in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"true"));
        metatest.assertNotEquals(WHERE "should fail", static_cast<UBool>(true), true);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("boolean in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"true"));
        metatest.assertNotEquals(WHERE "should fail", true, true);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("boolean in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"true"));
        // The mysterious int32_t.
        metatest.assertNotEquals(WHERE "should fail", -1, -1);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal int32", std::u16string::npos, lastLogLine.find(u"-1"));
        assertNotEquals("hex int32 in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"0xFFFFFFFF"));
        metatest.assertNotEquals(WHERE "should fail", 65, 65);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal int32 in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"65"));
        assertNotEquals("hex int32 in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"0x41"));
        metatest.assertNotEquals(WHERE "should fail", UnicodeString("").char32At(0),
                              UnicodeString("B").char32At(1));
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal int32 in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"65535"));
        assertNotEquals("hex int32 in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"0xFFFF"));
        metatest.assertNotEquals(WHERE "should fail", UnicodeString("AB").char32At(0),
                              UnicodeString("BA").char32At(1));
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal int32 in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"65"));
        assertNotEquals("hex int32 in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"0x41"));
        // Definitely characters.
        // char32_t.
        metatest.assertNotEquals(WHERE "should fail", U'A', U'A');
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("char32_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"U+0041 A"));
        metatest.assertNotEquals(WHERE "should fail", UnicodeString("A").char32At(0), U'A');
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("char32_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"U+0041 A"));
        metatest.assertNotEquals(WHERE "should fail", U'A', 0x41);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("char32_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"U+0041 A"));
        // char16_t.
        metatest.assertNotEquals(WHERE "should fail", u'A', u'A');
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("char16_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"U+0041 A"));
        metatest.assertNotEquals(WHERE "should fail", 0x41, u'A');
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("char16_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"U+0041 A"));
        metatest.assertNotEquals(WHERE "should fail", UnicodeString("A").charAt(0), 0x41);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("char16_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"U+0041 A"));
        // Actual numeric comparisons.
        metatest.assertNotEquals(WHERE "should fail", static_cast<uint32_t>(65), 65);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal uint32 in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"65"));
        assertEquals("NO hex uint32 in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"41"));
        constexpr std::u16string_view s = u"π’Œ‰ 𒂍𒁾𒁀𒀀 π’Œ“ π’ŒŒπ’†·π’€€π’€­ π’ˆ¨π’‚  π’‰Œπ’Ίπ’‰ˆπ’‚—";
        metatest.assertNotEquals(WHERE "should fail", s.size(), 37);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal size_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"37"));
        assertEquals("NO hex size_t in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"25"));
        // This one is int32_t on 32-bit platforms.
        metatest.assertNotEquals(WHERE "should fail", s.end() - s.begin(), 37);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal ptrdiff_t in " + lastLogLine, std::u16string::npos,
                        lastLogLine.find(u"37"));
        if (sizeof(std::ptrdiff_t) == 4) {
            assertNotEquals("hex 32-bit ptrdiff_t in " + lastLogLine, std::u16string::npos,
                            lastLogLine.find(u"25"));
        } else {
            assertEquals("NO hex ptrdiff_t in " + lastLogLine, std::u16string::npos,
                         lastLogLine.find(u"25"));
        }
        // Making the enum inherit from int8_t, which is UBool, also allows us to check that we don’t
        // fall into the UBool overload (this would have been the case before this got templatized).
        enum Button : int8_t {
            Abort = 65,
            Retry = 66,
            Ignore = 67,
        };
        metatest.assertNotEquals(WHERE "should fail", Abort, Abort);
        assertEquals(WHERE "should have failed", 1, metatest.newErrors());
        assertNotEquals("decimal enum in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"65"));
        assertEquals("NO hex enum in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"41"));
        assertEquals("NO UBool in " + lastLogLine, std::u16string::npos, lastLogLine.find(u"UBool"));
    }
};

extern IntlTest *createIntlTestTest() {
    return new IntlTestTest();
}