blob: 293dd5586442fa73ea34a65fba2a1e79dd682e58 [file] [log] [blame]
Fredrik Roubert0596fae2017-04-18 21:34:02 +02001// © 2016 and later: Unicode, Inc. and others.
Fredrik Roubert64339d32016-10-21 19:43:16 +02002// License & terms of use: http://www.unicode.org/copyright.html
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -07003/********************************************************************
4 * COPYRIGHT:
Fredrik Roubertc14898b2015-09-28 19:31:03 +02005 * Copyright (c) 1997-2015, International Business Machines Corporation and
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -07006 * others. All Rights Reserved.
7 ********************************************************************/
8/********************************************************************************
9*
10* File CINTLTST.C
11*
12* Modification History:
13* Name Description
14* Madhu Katragadda Creation
15*********************************************************************************
16*/
17
18/*The main root for C API tests*/
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include "unicode/utypes.h"
24#include "unicode/putil.h"
25#include "cstring.h"
26#include "cintltst.h"
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070027#include "uassert.h"
28#include "cmemory.h"
29#include "unicode/uchar.h"
30#include "unicode/ustring.h"
31#include "unicode/ucnv.h"
32#include "unicode/ures.h"
33#include "unicode/uclean.h"
34#include "unicode/ucal.h"
35#include "uoptions.h"
claireho27f65472011-06-09 11:11:49 -070036#include "putilimp.h" /* for uprv_getRawUTCtime() */
claireho50294ea2010-05-03 15:44:48 -070037#ifdef URES_DEBUG
38#include "uresimp.h" /* for ures_dumpCacheContents() */
39#endif
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070040
41#ifdef XP_MAC_CONSOLE
42# include <console.h>
43#endif
44
Craig Cornelius54dcd9b2013-02-15 14:03:14 -080045#define CTST_MAX_ALLOC 8192
46/* Array used as a queue */
47static void * ctst_allocated_stuff[CTST_MAX_ALLOC] = {0};
48static int ctst_allocated = 0;
49static UBool ctst_free = FALSE;
50static int ctst_allocated_total = 0;
51
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070052#define CTST_LEAK_CHECK 1
Craig Cornelius54dcd9b2013-02-15 14:03:14 -080053
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070054#ifdef CTST_LEAK_CHECK
Craig Cornelius54dcd9b2013-02-15 14:03:14 -080055static void ctst_freeAll(void);
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070056#endif
57
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070058/*
59 * Forward Declarations
60 */
61void ctest_setICU_DATA(void);
62
63
64
65#if UCONFIG_NO_LEGACY_CONVERSION
66# define TRY_CNV_1 "iso-8859-1"
67# define TRY_CNV_2 "ibm-1208"
68#else
69# define TRY_CNV_1 "iso-8859-7"
70# define TRY_CNV_2 "sjis"
71#endif
72
Claire Ho85bf2e22009-11-24 14:23:02 -080073static int gOrigArgc;
74static const char* const * gOrigArgv;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070075
Fredrik Roubertc14898b2015-09-28 19:31:03 +020076#ifdef UNISTR_COUNT_FINAL_STRING_LENGTHS
77U_CAPI void unistr_printLengths();
78#endif
79
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070080int main(int argc, const char* const argv[])
81{
82 int nerrors = 0;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070083 UBool defaultDataFound;
84 TestNode *root;
85 const char *warnOrErr = "Failure";
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070086 UDate startTime, endTime;
87 int32_t diffTime;
88
89 /* initial check for the default converter */
90 UErrorCode errorCode = U_ZERO_ERROR;
91 UResourceBundle *rb;
92 UConverter *cnv;
93
94 U_MAIN_INIT_ARGS(argc, argv);
95
claireho27f65472011-06-09 11:11:49 -070096 startTime = uprv_getRawUTCtime();
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070097
Claire Ho85bf2e22009-11-24 14:23:02 -080098 gOrigArgc = argc;
99 gOrigArgv = argv;
100 if (!initArgs(argc, argv, NULL, NULL)) {
101 /* Error already displayed. */
102 return -1;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700103 }
Claire Ho85bf2e22009-11-24 14:23:02 -0800104
105 /* Check whether ICU will initialize without forcing the build data directory into
106 * the ICU_DATA path. Success here means either the data dll contains data, or that
107 * this test program was run with ICU_DATA set externally. Failure of this check
108 * is normal when ICU data is not packaged into a shared library.
109 *
110 * Whether or not this test succeeds, we want to cleanup and reinitialize
111 * with a data path so that data loading from individual files can be tested.
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700112 */
Claire Ho85bf2e22009-11-24 14:23:02 -0800113 defaultDataFound = TRUE;
114 u_init(&errorCode);
115 if (U_FAILURE(errorCode)) {
116 fprintf(stderr,
117 "#### Note: ICU Init without build-specific setDataDirectory() failed. %s\n", u_errorName(errorCode));
118 defaultDataFound = FALSE;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700119 }
Claire Ho85bf2e22009-11-24 14:23:02 -0800120 u_cleanup();
claireho50294ea2010-05-03 15:44:48 -0700121#ifdef URES_DEBUG
122 fprintf(stderr, "After initial u_cleanup: RB cache %s empty.\n", ures_dumpCacheContents()?"WAS NOT":"was");
123#endif
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700124
claireho27f65472011-06-09 11:11:49 -0700125 while (getTestOption(REPEAT_TESTS_OPTION) > 0) { /* Loop runs once per complete execution of the tests
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700126 * used for -r (repeat) test option. */
Claire Ho85bf2e22009-11-24 14:23:02 -0800127 if (!initArgs(argc, argv, NULL, NULL)) {
128 /* Error already displayed. */
129 return -1;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700130 }
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700131 errorCode = U_ZERO_ERROR;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700132
133 /* Initialize ICU */
134 if (!defaultDataFound) {
135 ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */
136 }
137 u_init(&errorCode);
138 if (U_FAILURE(errorCode)) {
139 fprintf(stderr,
140 "#### ERROR! %s: u_init() failed with status = \"%s\".\n"
141 "*** Check the ICU_DATA environment variable and \n"
142 "*** check that the data files are present.\n", argv[0], u_errorName(errorCode));
claireho27f65472011-06-09 11:11:49 -0700143 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700144 fprintf(stderr, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
145 u_cleanup();
146 return 1;
147 }
148 }
149
150
151
152 /* try more data */
153 cnv = ucnv_open(TRY_CNV_2, &errorCode);
154 if(cnv != 0) {
155 /* ok */
156 ucnv_close(cnv);
157 } else {
158 fprintf(stderr,
159 "*** %s! The converter for " TRY_CNV_2 " cannot be opened.\n"
160 "*** Check the ICU_DATA environment variable and \n"
161 "*** check that the data files are present.\n", warnOrErr);
claireho27f65472011-06-09 11:11:49 -0700162 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700163 fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
164 u_cleanup();
165 return 1;
166 }
167 }
168
169 rb = ures_open(NULL, "en", &errorCode);
170 if(U_SUCCESS(errorCode)) {
171 /* ok */
172 ures_close(rb);
173 } else {
174 fprintf(stderr,
175 "*** %s! The \"en\" locale resource bundle cannot be opened.\n"
176 "*** Check the ICU_DATA environment variable and \n"
177 "*** check that the data files are present.\n", warnOrErr);
claireho27f65472011-06-09 11:11:49 -0700178 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700179 fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
180 u_cleanup();
181 return 1;
182 }
183 }
184
185 errorCode = U_ZERO_ERROR;
186 rb = ures_open(NULL, NULL, &errorCode);
187 if(U_SUCCESS(errorCode)) {
188 /* ok */
189 if (errorCode == U_USING_DEFAULT_WARNING || errorCode == U_USING_FALLBACK_WARNING) {
190 fprintf(stderr,
191 "#### Note: The default locale %s is not available\n", uloc_getDefault());
192 }
193 ures_close(rb);
194 } else {
195 fprintf(stderr,
196 "*** %s! Can not open a resource bundle for the default locale %s\n", warnOrErr, uloc_getDefault());
claireho27f65472011-06-09 11:11:49 -0700197 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700198 fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n"
199 "*** purposely removed, to continue test anyway.\n");
200 u_cleanup();
201 return 1;
202 }
203 }
204 fprintf(stdout, "Default locale for this run is %s\n", uloc_getDefault());
205
206 /* Build a tree of all tests.
207 * Subsequently will be used to find / iterate the tests to run */
208 root = NULL;
209 addAllTests(&root);
210
211 /* Tests acutally run HERE. TODO: separate command line option parsing & setting from test execution!! */
Claire Ho85bf2e22009-11-24 14:23:02 -0800212 nerrors = runTestRequest(root, argc, argv);
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700213
claireho27f65472011-06-09 11:11:49 -0700214 setTestOption(REPEAT_TESTS_OPTION, DECREMENT_OPTION_VALUE);
215 if (getTestOption(REPEAT_TESTS_OPTION) > 0) {
216 printf("Repeating tests %d more time(s)\n", getTestOption(REPEAT_TESTS_OPTION));
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700217 }
218 cleanUpTestTree(root);
219
220#ifdef CTST_LEAK_CHECK
221 ctst_freeAll();
222 /* To check for leaks */
223 u_cleanup(); /* nuke the hashtable.. so that any still-open cnvs are leaked */
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800224
225 if(getTestOption(VERBOSITY_OPTION) && ctst_allocated_total>0) {
226 fprintf(stderr,"ctst_freeAll(): cleaned up after %d allocations (queue of %d)\n", ctst_allocated_total, CTST_MAX_ALLOC);
227 }
claireho50294ea2010-05-03 15:44:48 -0700228#ifdef URES_DEBUG
229 if(ures_dumpCacheContents()) {
230 fprintf(stderr, "Error: After final u_cleanup, RB cache was not empty.\n");
231 nerrors++;
232 } else {
233 fprintf(stderr,"OK: After final u_cleanup, RB cache was empty.\n");
234 }
235#endif
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700236#endif
237
238 } /* End of loop that repeats the entire test, if requested. (Normally doesn't loop) */
239
Fredrik Roubertc14898b2015-09-28 19:31:03 +0200240#ifdef UNISTR_COUNT_FINAL_STRING_LENGTHS
241 unistr_printLengths();
242#endif
243
claireho27f65472011-06-09 11:11:49 -0700244 endTime = uprv_getRawUTCtime();
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700245 diffTime = (int32_t)(endTime - startTime);
246 printf("Elapsed Time: %02d:%02d:%02d.%03d\n",
247 (int)((diffTime%U_MILLIS_PER_DAY)/U_MILLIS_PER_HOUR),
248 (int)((diffTime%U_MILLIS_PER_HOUR)/U_MILLIS_PER_MINUTE),
249 (int)((diffTime%U_MILLIS_PER_MINUTE)/U_MILLIS_PER_SECOND),
250 (int)(diffTime%U_MILLIS_PER_SECOND));
251
Victor Chang99512882021-01-19 10:18:14 +0000252#ifdef ZERO_EXIT_CODE_FOR_FAILURES
253 return 0;
254#else
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700255 return nerrors ? 1 : 0;
Victor Chang99512882021-01-19 10:18:14 +0000256#endif
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700257}
258
259/*
260static void ctest_appendToDataDirectory(const char *toAppend)
261{
262 const char *oldPath ="";
263 char newBuf [1024];
264 char *newPath = newBuf;
265 int32_t oldLen;
266 int32_t newLen;
267
268 if((toAppend == NULL) || (*toAppend == 0)) {
269 return;
270 }
271
272 oldPath = u_getDataDirectory();
273 if( (oldPath==NULL) || (*oldPath == 0)) {
274 u_setDataDirectory(toAppend);
275 } else {
276 oldLen = strlen(oldPath);
277 newLen = strlen(toAppend)+1+oldLen;
278
279 if(newLen > 1022)
280 {
281 newPath = (char *)ctst_malloc(newLen);
282 }
283
284 strcpy(newPath, oldPath);
285 strcpy(newPath+oldLen, U_PATH_SEP_STRING);
286 strcpy(newPath+oldLen+1, toAppend);
287
288 u_setDataDirectory(newPath);
289
290 if(newPath != newBuf)
291 {
292 free(newPath);
293 }
294 }
295}
296*/
297
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700298/* ctest_setICU_DATA - if the ICU_DATA environment variable is not already
299 * set, try to deduce the directory in which ICU was built,
300 * and set ICU_DATA to "icu/source/data" in that location.
301 * The intent is to allow the tests to have a good chance
302 * of running without requiring that the user manually set
303 * ICU_DATA. Common data isn't a problem, since it is
304 * picked up via a static (build time) reference, but the
305 * tests dynamically load some data.
306 */
307void ctest_setICU_DATA() {
Victor Chang0901f442021-01-19 10:18:55 +0000308 // Android-changed: Do not u_setDataDirectory because libicuuc.so initializes itself.
309 #if !defined(ANDROID_USE_ICU_REG)
Victor Chang4b69c8d2021-01-19 10:17:56 +0000310 u_setDataDirectory(ctest_dataOutDir());
Victor Chang99512882021-01-19 10:18:14 +0000311 #endif
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700312}
313
Claire Ho85bf2e22009-11-24 14:23:02 -0800314/* These tests do cleanup and reinitialize ICU in the course of their operation.
315 * The ICU data directory must be preserved across these operations.
316 * Here is a helper function to assist with that.
317 */
318static char *safeGetICUDataDirectory() {
319 const char *dataDir = u_getDataDirectory(); /* Returned string vanashes with u_cleanup */
320 char *retStr = NULL;
321 if (dataDir != NULL) {
322 retStr = (char *)malloc(strlen(dataDir)+1);
323 strcpy(retStr, dataDir);
324 }
325 return retStr;
326}
327
328UBool ctest_resetICU() {
329 UErrorCode status = U_ZERO_ERROR;
330 char *dataDir = safeGetICUDataDirectory();
331
332 u_cleanup();
333 if (!initArgs(gOrigArgc, gOrigArgv, NULL, NULL)) {
334 /* Error already displayed. */
335 return FALSE;
336 }
Victor Chang99512882021-01-19 10:18:14 +0000337 ctest_setICU_DATA();
Claire Ho85bf2e22009-11-24 14:23:02 -0800338 free(dataDir);
339 u_init(&status);
340 if (U_FAILURE(status)) {
341 log_err_status(status, "u_init failed with %s\n", u_errorName(status));
342 return FALSE;
343 }
344 return TRUE;
345}
346
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700347UChar* CharsToUChars(const char* str) {
348 /* Might be faster to just use uprv_strlen() as the preflight len - liu */
349 int32_t len = u_unescape(str, 0, 0); /* preflight */
350 /* Do NOT use malloc() - we are supposed to be acting like user code! */
351 UChar *buf = (UChar*) malloc(sizeof(UChar) * (len + 1));
352 u_unescape(str, buf, len + 1);
353 return buf;
354}
355
356char *austrdup(const UChar* unichars)
357{
358 int length;
359 char *newString;
360
361 length = u_strlen ( unichars );
362 /*newString = (char*)malloc ( sizeof( char ) * 4 * ( length + 1 ) );*/ /* this leaks for now */
363 newString = (char*)ctst_malloc ( sizeof( char ) * 4 * ( length + 1 ) ); /* this shouldn't */
364
365 if ( newString == NULL )
366 return NULL;
367
368 u_austrcpy ( newString, unichars );
369
370 return newString;
371}
372
373char *aescstrdup(const UChar* unichars,int32_t length){
374 char *newString,*targetLimit,*target;
375 UConverterFromUCallback cb;
376 const void *p;
377 UErrorCode errorCode = U_ZERO_ERROR;
378#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700379# if U_PLATFORM == U_PF_OS390
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700380 static const char convName[] = "ibm-1047";
381# else
382 static const char convName[] = "ibm-37";
383# endif
384#else
385 static const char convName[] = "US-ASCII";
386#endif
387 UConverter* conv = ucnv_open(convName, &errorCode);
388 if(length==-1){
389 length = u_strlen( unichars);
390 }
391 newString = (char*)ctst_malloc ( sizeof(char) * 8 * (length +1));
392 target = newString;
393 targetLimit = newString+sizeof(char) * 8 * (length +1);
claireho50294ea2010-05-03 15:44:48 -0700394 ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, &cb, &p, &errorCode);
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700395 ucnv_fromUnicode(conv,&target,targetLimit, &unichars, (UChar*)(unichars+length),NULL,TRUE,&errorCode);
396 ucnv_close(conv);
397 *target = '\0';
398 return newString;
399}
400
Victor Chang4b69c8d2021-01-19 10:17:56 +0000401const char* loadTestData(UErrorCode* err) {
402 return ctest_loadTestData(err);
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700403}
404
405#define CTEST_MAX_TIMEZONE_SIZE 256
406static UChar gOriginalTimeZone[CTEST_MAX_TIMEZONE_SIZE] = {0};
407
408/**
409 * Call this once to get a consistent timezone. Use ctest_resetTimeZone to set it back to the original value.
410 * @param optionalTimeZone Set this to a requested timezone.
411 * Set to NULL to use the standard test timezone (Pacific Time)
412 */
413U_CFUNC void ctest_setTimeZone(const char *optionalTimeZone, UErrorCode *status) {
414#if !UCONFIG_NO_FORMATTING
415 UChar zoneID[CTEST_MAX_TIMEZONE_SIZE];
416
417 if (optionalTimeZone == NULL) {
418 optionalTimeZone = "America/Los_Angeles";
419 }
420 if (gOriginalTimeZone[0]) {
Claire Ho85bf2e22009-11-24 14:23:02 -0800421 log_data_err("*** Error: time zone saved twice. New value will be %s (Are you missing data?)\n",
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700422 optionalTimeZone);
423 }
424 ucal_getDefaultTimeZone(gOriginalTimeZone, CTEST_MAX_TIMEZONE_SIZE, status);
425 if (U_FAILURE(*status)) {
426 log_err("*** Error: Failed to save default time zone: %s\n",
427 u_errorName(*status));
428 *status = U_ZERO_ERROR;
429 }
430
431 u_uastrncpy(zoneID, optionalTimeZone, CTEST_MAX_TIMEZONE_SIZE-1);
432 zoneID[CTEST_MAX_TIMEZONE_SIZE-1] = 0;
433 ucal_setDefaultTimeZone(zoneID, status);
434 if (U_FAILURE(*status)) {
435 log_err("*** Error: Failed to set default time zone to \"%s\": %s\n",
436 optionalTimeZone, u_errorName(*status));
437 }
438#endif
439}
440
441/**
442 * Call this once get back the original timezone
443 */
444U_CFUNC void ctest_resetTimeZone(void) {
445#if !UCONFIG_NO_FORMATTING
446 UErrorCode status = U_ZERO_ERROR;
447
448 ucal_setDefaultTimeZone(gOriginalTimeZone, &status);
449 if (U_FAILURE(status)) {
450 log_err("*** Error: Failed to reset default time zone: %s\n",
451 u_errorName(status));
452 }
453 /* Set to an empty state */
454 gOriginalTimeZone[0] = 0;
455#endif
456}
457
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700458
459void *ctst_malloc(size_t size) {
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800460 ctst_allocated_total++;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700461 if(ctst_allocated >= CTST_MAX_ALLOC - 1) {
462 ctst_allocated = 0;
463 ctst_free = TRUE;
464 }
465 if(ctst_allocated_stuff[ctst_allocated]) {
466 free(ctst_allocated_stuff[ctst_allocated]);
467 }
468 return ctst_allocated_stuff[ctst_allocated++] = malloc(size);
469}
470
471#ifdef CTST_LEAK_CHECK
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800472static void ctst_freeAll() {
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700473 int i;
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800474 if(ctst_free == FALSE) { /* only free up to the allocated mark */
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700475 for(i=0; i<ctst_allocated; i++) {
476 free(ctst_allocated_stuff[i]);
477 ctst_allocated_stuff[i] = NULL;
478 }
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800479 } else { /* free all */
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700480 for(i=0; i<CTST_MAX_ALLOC; i++) {
481 free(ctst_allocated_stuff[i]);
482 ctst_allocated_stuff[i] = NULL;
483 }
484 }
485 ctst_allocated = 0;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700486}
487
488#define VERBOSE_ASSERTIONS
489
ccornelius59d709d2014-02-20 10:29:46 -0800490U_CFUNC UBool assertSuccessCheck(const char* msg, UErrorCode* ec, UBool possibleDataError) {
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700491 U_ASSERT(ec!=NULL);
492 if (U_FAILURE(*ec)) {
ccornelius59d709d2014-02-20 10:29:46 -0800493 if (possibleDataError) {
494 log_data_err("FAIL: %s (%s)\n", msg, u_errorName(*ec));
495 } else {
496 log_err_status(*ec, "FAIL: %s (%s)\n", msg, u_errorName(*ec));
497 }
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700498 return FALSE;
499 }
500 return TRUE;
501}
502
ccornelius59d709d2014-02-20 10:29:46 -0800503U_CFUNC UBool assertSuccess(const char* msg, UErrorCode* ec) {
504 U_ASSERT(ec!=NULL);
505 return assertSuccessCheck(msg, ec, FALSE);
506}
507
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700508/* if 'condition' is a UBool, the compiler complains bitterly about
509 expressions like 'a > 0' which it evaluates as int */
510U_CFUNC UBool assertTrue(const char* msg, int /*not UBool*/ condition) {
511 if (!condition) {
512 log_err("FAIL: assertTrue() failed: %s\n", msg);
513 }
514#ifdef VERBOSE_ASSERTIONS
515 else {
516 log_verbose("Ok: %s\n", msg);
517 }
518#endif
519 return (UBool)condition;
520}
521
522U_CFUNC UBool assertEquals(const char* message, const char* expected,
523 const char* actual) {
Victor Chang978167a2021-01-18 17:56:33 +0000524 if (expected == NULL) {
525 expected = "(null)";
526 }
527 if (actual == NULL) {
528 actual = "(null)";
529 }
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700530 if (uprv_strcmp(expected, actual) != 0) {
531 log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
532 message, actual, expected);
533 return FALSE;
534 }
535#ifdef VERBOSE_ASSERTIONS
536 else {
537 log_verbose("Ok: %s; got \"%s\"\n", message, actual);
538 }
539#endif
540 return TRUE;
541}
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700542
Fredrik Roubert57b72de2018-06-21 14:29:02 +0200543U_CFUNC UBool assertUEquals(const char* message, const UChar* expected,
544 const UChar* actual) {
Victor Chang978167a2021-01-18 17:56:33 +0000545 if (expected == NULL) {
546 expected = u"(null)";
547 }
548 if (actual == NULL) {
549 actual = u"(null)";
550 }
Fredrik Roubert57b72de2018-06-21 14:29:02 +0200551 for (int32_t i=0;; i++) {
552 if (expected[i] != actual[i]) {
553 log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
554 message, austrdup(actual), austrdup(expected));
555 return FALSE;
556 }
557 UChar curr = expected[i];
558 U_ASSERT(curr == actual[i]);
559 if (curr == 0) {
560 break;
561 }
562 }
563#ifdef VERBOSE_ASSERTIONS
564 log_verbose("Ok: %s; got \"%s\"\n", message, austrdup(actual));
565#endif
566 return TRUE;
567}
568
569U_CFUNC UBool assertIntEquals(const char* message, int64_t expected, int64_t actual) {
570 if (expected != actual) {
571 log_err("FAIL: %s; got \"%d\"; expected \"%d\"\n",
572 message, actual, expected);
573 return FALSE;
574 }
575#ifdef VERBOSE_ASSERTIONS
576 else {
577 log_verbose("Ok: %s; got \"%d\"\n", message, actual);
578 }
579#endif
580 return TRUE;
581}
582
Nikita Iashchenko4c0e2862019-11-05 16:38:00 +0000583U_CFUNC UBool assertPtrEquals(const char* message, const void* expected, const void* actual) {
584 if (expected != actual) {
585 log_err("FAIL: %s; got 0x%llx; expected 0x%llx\n",
586 message, actual, expected);
587 return FALSE;
588 }
589#ifdef VERBOSE_ASSERTIONS
590 else {
591 log_verbose("Ok: %s; got 0x%llx\n", message, actual);
592 }
593#endif
594 return TRUE;
595}
596
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700597#endif