Skip to content

Commit 5f7ee2e

Browse files
committed
ICU-22995 templatize assert[Not]Equals of integral and enum types
1 parent 454ce01 commit 5f7ee2e

File tree

7 files changed

+459
-80
lines changed

7 files changed

+459
-80
lines changed

icu4c/source/test/intltest/Makefile.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ formattedvaluetest.o formatted_string_builder_test.o numbertest_permutation.o \
7777
units_data_test.o units_router_test.o units_test.o displayoptions_test.o \
7878
numbertest_simple.o \
7979
cplusplus_header_api_build_test.o uchar_type_build_test.o \
80-
ucolheaderonlytest.o usetheaderonlytest.o utfiteratortest.o
80+
ucolheaderonlytest.o usetheaderonlytest.o utfiteratortest.o \
81+
intltesttest.o
8182

8283
DEPS = $(OBJECTS:.o=.d)
8384

icu4c/source/test/intltest/intltest.cpp

Lines changed: 94 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdio.h>
1919
#include <stdlib.h>
2020
#include <string.h>
21+
#include <cinttypes>
2122
#include <cmath>
2223
#include <math.h>
2324
#include <string_view>
@@ -73,25 +74,25 @@ UCharToUnicodeString(char16_t c) { return {c}; }
7374
// [rtg] Just to get things working
7475
UnicodeString
7576
operator+(const UnicodeString& left,
76-
long num)
77+
int64_t num)
7778
{
7879
char buffer[64]; // nos changed from 10 to 64
7980
char danger = 'p'; // guard against overrunning the buffer (rtg)
8081

81-
snprintf(buffer, sizeof(buffer), "%ld", num);
82+
snprintf(buffer, sizeof(buffer), "%" PRId64, num);
8283
assert(danger == 'p');
8384

8485
return left + buffer;
8586
}
8687

8788
UnicodeString
8889
operator+(const UnicodeString& left,
89-
unsigned long num)
90+
uint64_t num)
9091
{
9192
char buffer[64]; // nos changed from 10 to 64
9293
char danger = 'p'; // guard against overrunning the buffer (rtg)
9394

94-
snprintf(buffer, sizeof(buffer), "%lu", num);
95+
snprintf(buffer, sizeof(buffer), "%" PRIu64, num);
9596
assert(danger == 'p');
9697

9798
return left + buffer;
@@ -103,11 +104,7 @@ Int64ToUnicodeString(int64_t num)
103104
char buffer[64]; // nos changed from 10 to 64
104105
char danger = 'p'; // guard against overrunning the buffer (rtg)
105106

106-
#if defined(_MSC_VER)
107-
snprintf(buffer, sizeof(buffer), "%I64d", num);
108-
#else
109-
snprintf(buffer, sizeof(buffer), "%lld", static_cast<long long>(num));
110-
#endif
107+
snprintf(buffer, sizeof(buffer), "%" PRId64, num);
111108
assert(danger == 'p');
112109

113110
return buffer;
@@ -143,14 +140,6 @@ operator+(const UnicodeString& left,
143140
return left + buffer;
144141
}
145142

146-
#if 0
147-
UnicodeString
148-
operator+(const UnicodeString& left,
149-
int64_t num) {
150-
return left + Int64ToUnicodeString(num);
151-
}
152-
#endif
153-
154143
#if !UCONFIG_NO_FORMATTING
155144

156145
/**
@@ -232,13 +221,18 @@ UnicodeString toString(const Formattable& f) {
232221

233222
// useful when operator+ won't cooperate
234223
UnicodeString toString(int32_t n) {
235-
return UnicodeString() + static_cast<long>(n);
224+
return UnicodeString() + static_cast<int64_t>(n);
236225
}
237226

238227

239228

240229
UnicodeString toString(UBool b) {
241-
return b ? UnicodeString("true"):UnicodeString("false");
230+
return b ? b != 1 ? UnicodeString("static_cast<UBool>(") + b + ")" : UnicodeString("true")
231+
: UnicodeString("false");
232+
}
233+
234+
UnicodeString toString(bool b) {
235+
return b ? UnicodeString("true") : UnicodeString("false");
242236
}
243237

244238
UnicodeString toString(const UnicodeSet& uniset, UErrorCode& status) {
@@ -249,17 +243,17 @@ UnicodeString toString(const UnicodeSet& uniset, UErrorCode& status) {
249243

250244
// stephen - cleaned up 05/05/99
251245
UnicodeString operator+(const UnicodeString& left, char num)
252-
{ return left + static_cast<long>(num); }
246+
{ return left + static_cast<int64_t>(num); }
253247
UnicodeString operator+(const UnicodeString& left, short num)
254-
{ return left + static_cast<long>(num); }
248+
{ return left + static_cast<int64_t>(num); }
255249
UnicodeString operator+(const UnicodeString& left, int num)
256-
{ return left + static_cast<long>(num); }
250+
{ return left + static_cast<int64_t>(num); }
257251
UnicodeString operator+(const UnicodeString& left, unsigned char num)
258-
{ return left + static_cast<unsigned long>(num); }
252+
{ return left + static_cast<uint64_t>(num); }
259253
UnicodeString operator+(const UnicodeString& left, unsigned short num)
260-
{ return left + static_cast<unsigned long>(num); }
254+
{ return left + static_cast<uint64_t>(num); }
261255
UnicodeString operator+(const UnicodeString& left, unsigned int num)
262-
{ return left + static_cast<unsigned long>(num); }
256+
{ return left + static_cast<uint64_t>(num); }
263257
UnicodeString operator+(const UnicodeString& left, float num)
264258
{ return left + static_cast<double>(num); }
265259

@@ -2091,12 +2085,22 @@ UBool IntlTest::assertEquals(const char* message, std::u16string_view expected,
20912085
possibleDataError);
20922086
}
20932087

2094-
UBool IntlTest::assertEquals(const char* message,
2095-
int32_t expected,
2096-
int32_t actual) {
2088+
bool IntlTest::assertSigned64Equals(const char *message, int64_t expected, int64_t actual) {
20972089
if (expected != actual) {
2098-
errln(UnicodeString("FAIL: ") + message + "; got " +
2099-
actual + "=0x" + toHex(actual) +
2090+
errln(UnicodeString("FAIL: ") + message + "; got " + actual + "; expected " + expected);
2091+
return false;
2092+
}
2093+
#ifdef VERBOSE_ASSERTIONS
2094+
else {
2095+
logln(UnicodeString("Ok: ") + message + "; got " + actual);
2096+
}
2097+
#endif
2098+
return true;
2099+
}
2100+
2101+
bool IntlTest::assertSigned32Equals(const char *message, int32_t expected, int32_t actual) {
2102+
if (expected != actual) {
2103+
errln(UnicodeString("FAIL: ") + message + "; got " + actual + "=0x" + toHex(actual) +
21002104
"; expected " + expected + "=0x" + toHex(expected));
21012105
return false;
21022106
}
@@ -2108,18 +2112,18 @@ UBool IntlTest::assertEquals(const char* message,
21082112
return true;
21092113
}
21102114

2111-
UBool IntlTest::assertEquals(const char* message,
2112-
int64_t expected,
2113-
int64_t actual) {
2115+
bool IntlTest::assertCodePointEquals(const char *message, char32_t expected, char32_t actual) {
21142116
if (expected != actual) {
2115-
errln(UnicodeString("FAIL: ") + message + "; got int64 " +
2116-
Int64ToUnicodeString(actual) +
2117-
"; expected " + Int64ToUnicodeString(expected) );
2117+
errln(UnicodeString("FAIL: ") + message + "; got U+" + toHex(actual, actual <= 0xFFFF ? 4 : -1) +
2118+
" " + UnicodeString(static_cast<UChar32>(actual)) + "; expected U+" +
2119+
toHex(expected, expected <= 0xFFFF ? 4 : -1) + +" " +
2120+
UnicodeString(static_cast<UChar32>(expected)));
21182121
return false;
21192122
}
21202123
#ifdef VERBOSE_ASSERTIONS
21212124
else {
2122-
logln(UnicodeString("Ok: ") + message + "; got int64 " + Int64ToUnicodeString(actual));
2125+
logln(UnicodeString("Ok: ") + message + "; got U+" + toHex(actual, actual <= 0xFFFF ? 4 : -1) +
2126+
" " + UnicodeString(static_cast<UChar32>(actual)));
21232127
}
21242128
#endif
21252129
return true;
@@ -2143,18 +2147,29 @@ UBool IntlTest::assertEquals(const char* message,
21432147
return true;
21442148
}
21452149

2146-
UBool IntlTest::assertEquals(const char* message,
2147-
UBool expected,
2148-
UBool actual) {
2150+
bool IntlTest::assertBooleanEquals(const char *message, int8_t expected, int8_t actual) {
21492151
if (expected != actual) {
2150-
errln(UnicodeString("FAIL: ") + message + "; got " +
2151-
toString(actual) +
2152-
"; expected " + toString(expected));
2152+
errln(UnicodeString("FAIL: ") + message + "; got " + toString(actual) + "; expected " +
2153+
toString(expected));
21532154
return false;
21542155
}
21552156
#ifdef VERBOSE_ASSERTIONS
21562157
else {
2157-
logln(UnicodeString("Ok: ") + message + "; got " + toString(actual));
2158+
logln(UnicodeString("Ok: ") + message + "; got " + toString(actual));
2159+
}
2160+
#endif
2161+
return true;
2162+
}
2163+
2164+
bool IntlTest::assertBooleanNotEquals(const char *message, int8_t expected, int8_t actual) {
2165+
if (expected == actual) {
2166+
errln(UnicodeString("FAIL: ") + message + "; got " + toString(actual) + "; expected != " +
2167+
toString(expected));
2168+
return false;
2169+
}
2170+
#ifdef VERBOSE_ASSERTIONS
2171+
else {
2172+
logln(UnicodeString("Ok: ") + message + "; got " + toString(actual));
21582173
}
21592174
#endif
21602175
return true;
@@ -2259,9 +2274,22 @@ UBool IntlTest::assertEquals(const char* message,
22592274
return true;
22602275
}
22612276

2262-
UBool IntlTest::assertNotEquals(const char* message,
2263-
int32_t expectedNot,
2264-
int32_t actual) {
2277+
bool IntlTest::assertSigned64NotEquals(const char *message, int64_t expected, int64_t actual) {
2278+
if (expected == actual) {
2279+
errln(UnicodeString("FAIL: ") + message + "; got " + actual + "; expected != " + expected);
2280+
return false;
2281+
}
2282+
#ifdef VERBOSE_ASSERTIONS
2283+
else {
2284+
logln(UnicodeString("Ok: ") + message + "; got " + actual);
2285+
}
2286+
#endif
2287+
return true;
2288+
}
2289+
2290+
bool IntlTest::assertSigned32NotEquals(const char* message,
2291+
int32_t expectedNot,
2292+
int32_t actual) {
22652293
if (expectedNot == actual) {
22662294
errln(UnicodeString("FAIL: ") + message + "; got " + actual + "=0x" + toHex(actual) +
22672295
"; expected != " + expectedNot);
@@ -2276,6 +2304,23 @@ UBool IntlTest::assertNotEquals(const char* message,
22762304
return true;
22772305
}
22782306

2307+
bool IntlTest::assertCodePointNotEquals(const char *message, char32_t expected, char32_t actual) {
2308+
if (expected == actual) {
2309+
errln(UnicodeString("FAIL: ") + message + "; got U+" + toHex(actual, actual <= 0xFFFF ? 4 : -1) +
2310+
" " + UnicodeString(static_cast<UChar32>(actual)) + "; expected != U+" +
2311+
toHex(expected, expected <= 0xFFFF ? 4 : -1) + +" " +
2312+
UnicodeString(static_cast<UChar32>(expected)));
2313+
return false;
2314+
}
2315+
#ifdef VERBOSE_ASSERTIONS
2316+
else {
2317+
logln(UnicodeString("Ok: ") + message + "; got U+" + toHex(actual, actual <= 0xFFFF ? 4 : -1) +
2318+
" " + UnicodeString(static_cast<UChar32>(actual)));
2319+
}
2320+
#endif
2321+
return true;
2322+
}
2323+
22792324
UBool IntlTest::assertEqualsNear(const char* message,
22802325
double expected,
22812326
double actual,
@@ -2308,7 +2353,7 @@ UBool IntlTest::assertEqualsNear(const char* message,
23082353

23092354
static char ASSERT_BUF[256];
23102355

2311-
static const char* extractToAssertBuf(std::u16string_view message) {
2356+
const char* IntlTest::extractToAssertBuf(std::u16string_view message) {
23122357
UnicodeString buf;
23132358
escape(message, buf);
23142359
buf.extract(0, 0x7FFFFFFF, ASSERT_BUF, sizeof(ASSERT_BUF) - 1, nullptr);
@@ -2340,21 +2385,7 @@ UBool IntlTest::assertEquals(std::u16string_view message,
23402385
const char* actual) {
23412386
return assertEquals(extractToAssertBuf(message), expected, actual);
23422387
}
2343-
UBool IntlTest::assertEquals(std::u16string_view message,
2344-
UBool expected,
2345-
UBool actual) {
2346-
return assertEquals(extractToAssertBuf(message), expected, actual);
2347-
}
2348-
UBool IntlTest::assertEquals(std::u16string_view message,
2349-
int32_t expected,
2350-
int32_t actual) {
2351-
return assertEquals(extractToAssertBuf(message), expected, actual);
2352-
}
2353-
UBool IntlTest::assertEquals(std::u16string_view message,
2354-
int64_t expected,
2355-
int64_t actual) {
2356-
return assertEquals(extractToAssertBuf(message), expected, actual);
2357-
}
2388+
23582389
UBool IntlTest::assertEquals(std::u16string_view message,
23592390
double expected,
23602391
double actual) {
@@ -2375,11 +2406,6 @@ UBool IntlTest::assertEquals(std::u16string_view message,
23752406
const std::vector<std::string>& actual) {
23762407
return assertEquals(extractToAssertBuf(message), expected, actual);
23772408
}
2378-
UBool IntlTest::assertNotEquals(std::u16string_view message,
2379-
int32_t expectedNot,
2380-
int32_t actual) {
2381-
return assertNotEquals(extractToAssertBuf(message), expectedNot, actual);
2382-
}
23832409
UBool IntlTest::assertEqualsNear(std::u16string_view message,
23842410
double expected,
23852411
double actual,

0 commit comments

Comments
 (0)