blob: aaa187f6bdb14c22c5fb68c17e677f8d5c9fb8c3 [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
58static char* _testDataPath=NULL;
59
60/*
61 * Forward Declarations
62 */
63void ctest_setICU_DATA(void);
64
65
66
67#if UCONFIG_NO_LEGACY_CONVERSION
68# define TRY_CNV_1 "iso-8859-1"
69# define TRY_CNV_2 "ibm-1208"
70#else
71# define TRY_CNV_1 "iso-8859-7"
72# define TRY_CNV_2 "sjis"
73#endif
74
Claire Ho85bf2e22009-11-24 14:23:02 -080075static int gOrigArgc;
76static const char* const * gOrigArgv;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070077
Fredrik Roubertc14898b2015-09-28 19:31:03 +020078#ifdef UNISTR_COUNT_FINAL_STRING_LENGTHS
79U_CAPI void unistr_printLengths();
80#endif
81
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070082int main(int argc, const char* const argv[])
83{
84 int nerrors = 0;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070085 UBool defaultDataFound;
86 TestNode *root;
87 const char *warnOrErr = "Failure";
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070088 UDate startTime, endTime;
89 int32_t diffTime;
90
91 /* initial check for the default converter */
92 UErrorCode errorCode = U_ZERO_ERROR;
93 UResourceBundle *rb;
94 UConverter *cnv;
95
96 U_MAIN_INIT_ARGS(argc, argv);
97
claireho27f65472011-06-09 11:11:49 -070098 startTime = uprv_getRawUTCtime();
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070099
Claire Ho85bf2e22009-11-24 14:23:02 -0800100 gOrigArgc = argc;
101 gOrigArgv = argv;
102 if (!initArgs(argc, argv, NULL, NULL)) {
103 /* Error already displayed. */
104 return -1;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700105 }
Claire Ho85bf2e22009-11-24 14:23:02 -0800106
107 /* Check whether ICU will initialize without forcing the build data directory into
108 * the ICU_DATA path. Success here means either the data dll contains data, or that
109 * this test program was run with ICU_DATA set externally. Failure of this check
110 * is normal when ICU data is not packaged into a shared library.
111 *
112 * Whether or not this test succeeds, we want to cleanup and reinitialize
113 * with a data path so that data loading from individual files can be tested.
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700114 */
Claire Ho85bf2e22009-11-24 14:23:02 -0800115 defaultDataFound = TRUE;
116 u_init(&errorCode);
117 if (U_FAILURE(errorCode)) {
118 fprintf(stderr,
119 "#### Note: ICU Init without build-specific setDataDirectory() failed. %s\n", u_errorName(errorCode));
120 defaultDataFound = FALSE;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700121 }
Claire Ho85bf2e22009-11-24 14:23:02 -0800122 u_cleanup();
claireho50294ea2010-05-03 15:44:48 -0700123#ifdef URES_DEBUG
124 fprintf(stderr, "After initial u_cleanup: RB cache %s empty.\n", ures_dumpCacheContents()?"WAS NOT":"was");
125#endif
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700126
claireho27f65472011-06-09 11:11:49 -0700127 while (getTestOption(REPEAT_TESTS_OPTION) > 0) { /* Loop runs once per complete execution of the tests
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700128 * used for -r (repeat) test option. */
Claire Ho85bf2e22009-11-24 14:23:02 -0800129 if (!initArgs(argc, argv, NULL, NULL)) {
130 /* Error already displayed. */
131 return -1;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700132 }
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700133 errorCode = U_ZERO_ERROR;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700134
135 /* Initialize ICU */
136 if (!defaultDataFound) {
137 ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */
138 }
139 u_init(&errorCode);
140 if (U_FAILURE(errorCode)) {
141 fprintf(stderr,
142 "#### ERROR! %s: u_init() failed with status = \"%s\".\n"
143 "*** Check the ICU_DATA environment variable and \n"
144 "*** check that the data files are present.\n", argv[0], u_errorName(errorCode));
claireho27f65472011-06-09 11:11:49 -0700145 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700146 fprintf(stderr, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
147 u_cleanup();
148 return 1;
149 }
150 }
151
152
153
154 /* try more data */
155 cnv = ucnv_open(TRY_CNV_2, &errorCode);
156 if(cnv != 0) {
157 /* ok */
158 ucnv_close(cnv);
159 } else {
160 fprintf(stderr,
161 "*** %s! The converter for " TRY_CNV_2 " cannot be opened.\n"
162 "*** Check the ICU_DATA environment variable and \n"
163 "*** check that the data files are present.\n", warnOrErr);
claireho27f65472011-06-09 11:11:49 -0700164 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700165 fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
166 u_cleanup();
167 return 1;
168 }
169 }
170
171 rb = ures_open(NULL, "en", &errorCode);
172 if(U_SUCCESS(errorCode)) {
173 /* ok */
174 ures_close(rb);
175 } else {
176 fprintf(stderr,
177 "*** %s! The \"en\" locale resource bundle cannot be opened.\n"
178 "*** Check the ICU_DATA environment variable and \n"
179 "*** check that the data files are present.\n", warnOrErr);
claireho27f65472011-06-09 11:11:49 -0700180 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700181 fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
182 u_cleanup();
183 return 1;
184 }
185 }
186
187 errorCode = U_ZERO_ERROR;
188 rb = ures_open(NULL, NULL, &errorCode);
189 if(U_SUCCESS(errorCode)) {
190 /* ok */
191 if (errorCode == U_USING_DEFAULT_WARNING || errorCode == U_USING_FALLBACK_WARNING) {
192 fprintf(stderr,
193 "#### Note: The default locale %s is not available\n", uloc_getDefault());
194 }
195 ures_close(rb);
196 } else {
197 fprintf(stderr,
198 "*** %s! Can not open a resource bundle for the default locale %s\n", warnOrErr, uloc_getDefault());
claireho27f65472011-06-09 11:11:49 -0700199 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700200 fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n"
201 "*** purposely removed, to continue test anyway.\n");
202 u_cleanup();
203 return 1;
204 }
205 }
206 fprintf(stdout, "Default locale for this run is %s\n", uloc_getDefault());
207
208 /* Build a tree of all tests.
209 * Subsequently will be used to find / iterate the tests to run */
210 root = NULL;
211 addAllTests(&root);
212
213 /* Tests acutally run HERE. TODO: separate command line option parsing & setting from test execution!! */
Claire Ho85bf2e22009-11-24 14:23:02 -0800214 nerrors = runTestRequest(root, argc, argv);
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700215
claireho27f65472011-06-09 11:11:49 -0700216 setTestOption(REPEAT_TESTS_OPTION, DECREMENT_OPTION_VALUE);
217 if (getTestOption(REPEAT_TESTS_OPTION) > 0) {
218 printf("Repeating tests %d more time(s)\n", getTestOption(REPEAT_TESTS_OPTION));
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700219 }
220 cleanUpTestTree(root);
221
222#ifdef CTST_LEAK_CHECK
223 ctst_freeAll();
224 /* To check for leaks */
225 u_cleanup(); /* nuke the hashtable.. so that any still-open cnvs are leaked */
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800226
227 if(getTestOption(VERBOSITY_OPTION) && ctst_allocated_total>0) {
228 fprintf(stderr,"ctst_freeAll(): cleaned up after %d allocations (queue of %d)\n", ctst_allocated_total, CTST_MAX_ALLOC);
229 }
claireho50294ea2010-05-03 15:44:48 -0700230#ifdef URES_DEBUG
231 if(ures_dumpCacheContents()) {
232 fprintf(stderr, "Error: After final u_cleanup, RB cache was not empty.\n");
233 nerrors++;
234 } else {
235 fprintf(stderr,"OK: After final u_cleanup, RB cache was empty.\n");
236 }
237#endif
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700238#endif
239
240 } /* End of loop that repeats the entire test, if requested. (Normally doesn't loop) */
241
Fredrik Roubertc14898b2015-09-28 19:31:03 +0200242#ifdef UNISTR_COUNT_FINAL_STRING_LENGTHS
243 unistr_printLengths();
244#endif
245
claireho27f65472011-06-09 11:11:49 -0700246 endTime = uprv_getRawUTCtime();
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700247 diffTime = (int32_t)(endTime - startTime);
248 printf("Elapsed Time: %02d:%02d:%02d.%03d\n",
249 (int)((diffTime%U_MILLIS_PER_DAY)/U_MILLIS_PER_HOUR),
250 (int)((diffTime%U_MILLIS_PER_HOUR)/U_MILLIS_PER_MINUTE),
251 (int)((diffTime%U_MILLIS_PER_MINUTE)/U_MILLIS_PER_SECOND),
252 (int)(diffTime%U_MILLIS_PER_SECOND));
253
254 return nerrors ? 1 : 0;
255}
256
257/*
258static void ctest_appendToDataDirectory(const char *toAppend)
259{
260 const char *oldPath ="";
261 char newBuf [1024];
262 char *newPath = newBuf;
263 int32_t oldLen;
264 int32_t newLen;
265
266 if((toAppend == NULL) || (*toAppend == 0)) {
267 return;
268 }
269
270 oldPath = u_getDataDirectory();
271 if( (oldPath==NULL) || (*oldPath == 0)) {
272 u_setDataDirectory(toAppend);
273 } else {
274 oldLen = strlen(oldPath);
275 newLen = strlen(toAppend)+1+oldLen;
276
277 if(newLen > 1022)
278 {
279 newPath = (char *)ctst_malloc(newLen);
280 }
281
282 strcpy(newPath, oldPath);
283 strcpy(newPath+oldLen, U_PATH_SEP_STRING);
284 strcpy(newPath+oldLen+1, toAppend);
285
286 u_setDataDirectory(newPath);
287
288 if(newPath != newBuf)
289 {
290 free(newPath);
291 }
292 }
293}
294*/
295
296/* returns the path to icu/source/data */
297const char * ctest_dataSrcDir()
298{
299 static const char *dataSrcDir = NULL;
300
301 if(dataSrcDir) {
302 return dataSrcDir;
303 }
304
305 /* U_TOPSRCDIR is set by the makefiles on UNIXes when building cintltst and intltst
306 // to point to the top of the build hierarchy, which may or
307 // may not be the same as the source directory, depending on
308 // the configure options used. At any rate,
309 // set the data path to the built data from this directory.
310 // The value is complete with quotes, so it can be used
311 // as-is as a string constant.
312 */
313#if defined (U_TOPSRCDIR)
314 {
315 dataSrcDir = U_TOPSRCDIR U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
316 }
317#else
318
319 /* On Windows, the file name obtained from __FILE__ includes a full path.
320 * This file is "wherever\icu\source\test\cintltst\cintltst.c"
321 * Change to "wherever\icu\source\data"
322 */
323 {
324 static char p[sizeof(__FILE__) + 20];
325 char *pBackSlash;
326 int i;
327
328 strcpy(p, __FILE__);
329 /* We want to back over three '\' chars. */
330 /* Only Windows should end up here, so looking for '\' is safe. */
331 for (i=1; i<=3; i++) {
332 pBackSlash = strrchr(p, U_FILE_SEP_CHAR);
333 if (pBackSlash != NULL) {
334 *pBackSlash = 0; /* Truncate the string at the '\' */
335 }
336 }
337
338 if (pBackSlash != NULL) {
339 /* We found and truncated three names from the path.
340 * Now append "source\data" and set the environment
341 */
342 strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING );
343 dataSrcDir = p;
344 }
345 else {
346 /* __FILE__ on MSVC7 does not contain the directory */
347 FILE *file = fopen(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r");
348 if (file) {
349 fclose(file);
350 dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
351 }
352 else {
Claire Ho85bf2e22009-11-24 14:23:02 -0800353 dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700354 }
355 }
356 }
357#endif
358
359 return dataSrcDir;
360
361}
362
363/* returns the path to icu/source/data/out */
364const char *ctest_dataOutDir()
365{
366 static const char *dataOutDir = NULL;
367
368 if(dataOutDir) {
369 return dataOutDir;
370 }
371
372 /* U_TOPBUILDDIR is set by the makefiles on UNIXes when building cintltst and intltst
373 // to point to the top of the build hierarchy, which may or
374 // may not be the same as the source directory, depending on
375 // the configure options used. At any rate,
376 // set the data path to the built data from this directory.
377 // The value is complete with quotes, so it can be used
378 // as-is as a string constant.
379 */
380#if defined (U_TOPBUILDDIR)
381 {
382 dataOutDir = U_TOPBUILDDIR "data"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
383 }
384#else
385
386 /* On Windows, the file name obtained from __FILE__ includes a full path.
387 * This file is "wherever\icu\source\test\cintltst\cintltst.c"
388 * Change to "wherever\icu\source\data"
389 */
390 {
391 static char p[sizeof(__FILE__) + 20];
392 char *pBackSlash;
393 int i;
394
395 strcpy(p, __FILE__);
396 /* We want to back over three '\' chars. */
397 /* Only Windows should end up here, so looking for '\' is safe. */
398 for (i=1; i<=3; i++) {
399 pBackSlash = strrchr(p, U_FILE_SEP_CHAR);
400 if (pBackSlash != NULL) {
401 *pBackSlash = 0; /* Truncate the string at the '\' */
402 }
403 }
404
405 if (pBackSlash != NULL) {
406 /* We found and truncated three names from the path.
407 * Now append "source\data" and set the environment
408 */
409 strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING);
410 dataOutDir = p;
411 }
412 else {
413 /* __FILE__ on MSVC7 does not contain the directory */
414 FILE *file = fopen(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r");
415 if (file) {
416 fclose(file);
417 dataOutDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING;
418 }
419 else {
Claire Ho85bf2e22009-11-24 14:23:02 -0800420 dataOutDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700421 }
422 }
423 }
424#endif
425
426 return dataOutDir;
427}
428
429/* ctest_setICU_DATA - if the ICU_DATA environment variable is not already
430 * set, try to deduce the directory in which ICU was built,
431 * and set ICU_DATA to "icu/source/data" in that location.
432 * The intent is to allow the tests to have a good chance
433 * of running without requiring that the user manually set
434 * ICU_DATA. Common data isn't a problem, since it is
435 * picked up via a static (build time) reference, but the
436 * tests dynamically load some data.
437 */
438void ctest_setICU_DATA() {
439
440 /* No location for the data dir was identifiable.
441 * Add other fallbacks for the test data location here if the need arises
442 */
443 if (getenv("ICU_DATA") == NULL) {
444 /* If ICU_DATA isn't set, set it to the usual location */
445 u_setDataDirectory(ctest_dataOutDir());
446 }
447}
448
Claire Ho85bf2e22009-11-24 14:23:02 -0800449/* These tests do cleanup and reinitialize ICU in the course of their operation.
450 * The ICU data directory must be preserved across these operations.
451 * Here is a helper function to assist with that.
452 */
453static char *safeGetICUDataDirectory() {
454 const char *dataDir = u_getDataDirectory(); /* Returned string vanashes with u_cleanup */
455 char *retStr = NULL;
456 if (dataDir != NULL) {
457 retStr = (char *)malloc(strlen(dataDir)+1);
458 strcpy(retStr, dataDir);
459 }
460 return retStr;
461}
462
463UBool ctest_resetICU() {
464 UErrorCode status = U_ZERO_ERROR;
465 char *dataDir = safeGetICUDataDirectory();
466
467 u_cleanup();
468 if (!initArgs(gOrigArgc, gOrigArgv, NULL, NULL)) {
469 /* Error already displayed. */
470 return FALSE;
471 }
472 u_setDataDirectory(dataDir);
473 free(dataDir);
474 u_init(&status);
475 if (U_FAILURE(status)) {
476 log_err_status(status, "u_init failed with %s\n", u_errorName(status));
477 return FALSE;
478 }
479 return TRUE;
480}
481
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700482UChar* CharsToUChars(const char* str) {
483 /* Might be faster to just use uprv_strlen() as the preflight len - liu */
484 int32_t len = u_unescape(str, 0, 0); /* preflight */
485 /* Do NOT use malloc() - we are supposed to be acting like user code! */
486 UChar *buf = (UChar*) malloc(sizeof(UChar) * (len + 1));
487 u_unescape(str, buf, len + 1);
488 return buf;
489}
490
491char *austrdup(const UChar* unichars)
492{
493 int length;
494 char *newString;
495
496 length = u_strlen ( unichars );
497 /*newString = (char*)malloc ( sizeof( char ) * 4 * ( length + 1 ) );*/ /* this leaks for now */
498 newString = (char*)ctst_malloc ( sizeof( char ) * 4 * ( length + 1 ) ); /* this shouldn't */
499
500 if ( newString == NULL )
501 return NULL;
502
503 u_austrcpy ( newString, unichars );
504
505 return newString;
506}
507
508char *aescstrdup(const UChar* unichars,int32_t length){
509 char *newString,*targetLimit,*target;
510 UConverterFromUCallback cb;
511 const void *p;
512 UErrorCode errorCode = U_ZERO_ERROR;
513#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700514# if U_PLATFORM == U_PF_OS390
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700515 static const char convName[] = "ibm-1047";
516# else
517 static const char convName[] = "ibm-37";
518# endif
519#else
520 static const char convName[] = "US-ASCII";
521#endif
522 UConverter* conv = ucnv_open(convName, &errorCode);
523 if(length==-1){
524 length = u_strlen( unichars);
525 }
526 newString = (char*)ctst_malloc ( sizeof(char) * 8 * (length +1));
527 target = newString;
528 targetLimit = newString+sizeof(char) * 8 * (length +1);
claireho50294ea2010-05-03 15:44:48 -0700529 ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, &cb, &p, &errorCode);
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700530 ucnv_fromUnicode(conv,&target,targetLimit, &unichars, (UChar*)(unichars+length),NULL,TRUE,&errorCode);
531 ucnv_close(conv);
532 *target = '\0';
533 return newString;
534}
535
536const char* loadTestData(UErrorCode* err){
537 if( _testDataPath == NULL){
538 const char* directory=NULL;
539 UResourceBundle* test =NULL;
540 char* tdpath=NULL;
541 const char* tdrelativepath;
542#if defined (U_TOPBUILDDIR)
543 tdrelativepath = "test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
544 directory = U_TOPBUILDDIR;
545#else
546 tdrelativepath = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
547 directory= ctest_dataOutDir();
548#endif
549
550 tdpath = (char*) ctst_malloc(sizeof(char) *(( strlen(directory) * strlen(tdrelativepath)) + 10));
551
552
553 /* u_getDataDirectory shoul return \source\data ... set the
554 * directory to ..\source\data\..\test\testdata\out\testdata
555 *
556 * Fallback: When Memory mapped file is built
557 * ..\source\data\out\..\..\test\testdata\out\testdata
558 */
559 strcpy(tdpath, directory);
560 strcat(tdpath, tdrelativepath);
561 strcat(tdpath,"testdata");
562
563
564 test=ures_open(tdpath, "testtypes", err);
565
566 /* Fall back did not succeed either so return */
567 if(U_FAILURE(*err)){
568 *err = U_FILE_ACCESS_ERROR;
Claire Ho85bf2e22009-11-24 14:23:02 -0800569 log_data_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath, u_errorName(*err));
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700570 return "";
571 }
572 ures_close(test);
573 _testDataPath = tdpath;
574 return _testDataPath;
575 }
576 return _testDataPath;
577}
578
579#define CTEST_MAX_TIMEZONE_SIZE 256
580static UChar gOriginalTimeZone[CTEST_MAX_TIMEZONE_SIZE] = {0};
581
582/**
583 * Call this once to get a consistent timezone. Use ctest_resetTimeZone to set it back to the original value.
584 * @param optionalTimeZone Set this to a requested timezone.
585 * Set to NULL to use the standard test timezone (Pacific Time)
586 */
587U_CFUNC void ctest_setTimeZone(const char *optionalTimeZone, UErrorCode *status) {
588#if !UCONFIG_NO_FORMATTING
589 UChar zoneID[CTEST_MAX_TIMEZONE_SIZE];
590
591 if (optionalTimeZone == NULL) {
592 optionalTimeZone = "America/Los_Angeles";
593 }
594 if (gOriginalTimeZone[0]) {
Claire Ho85bf2e22009-11-24 14:23:02 -0800595 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 -0700596 optionalTimeZone);
597 }
598 ucal_getDefaultTimeZone(gOriginalTimeZone, CTEST_MAX_TIMEZONE_SIZE, status);
599 if (U_FAILURE(*status)) {
600 log_err("*** Error: Failed to save default time zone: %s\n",
601 u_errorName(*status));
602 *status = U_ZERO_ERROR;
603 }
604
605 u_uastrncpy(zoneID, optionalTimeZone, CTEST_MAX_TIMEZONE_SIZE-1);
606 zoneID[CTEST_MAX_TIMEZONE_SIZE-1] = 0;
607 ucal_setDefaultTimeZone(zoneID, status);
608 if (U_FAILURE(*status)) {
609 log_err("*** Error: Failed to set default time zone to \"%s\": %s\n",
610 optionalTimeZone, u_errorName(*status));
611 }
612#endif
613}
614
615/**
616 * Call this once get back the original timezone
617 */
618U_CFUNC void ctest_resetTimeZone(void) {
619#if !UCONFIG_NO_FORMATTING
620 UErrorCode status = U_ZERO_ERROR;
621
622 ucal_setDefaultTimeZone(gOriginalTimeZone, &status);
623 if (U_FAILURE(status)) {
624 log_err("*** Error: Failed to reset default time zone: %s\n",
625 u_errorName(status));
626 }
627 /* Set to an empty state */
628 gOriginalTimeZone[0] = 0;
629#endif
630}
631
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700632
633void *ctst_malloc(size_t size) {
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800634 ctst_allocated_total++;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700635 if(ctst_allocated >= CTST_MAX_ALLOC - 1) {
636 ctst_allocated = 0;
637 ctst_free = TRUE;
638 }
639 if(ctst_allocated_stuff[ctst_allocated]) {
640 free(ctst_allocated_stuff[ctst_allocated]);
641 }
642 return ctst_allocated_stuff[ctst_allocated++] = malloc(size);
643}
644
645#ifdef CTST_LEAK_CHECK
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800646static void ctst_freeAll() {
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700647 int i;
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800648 if(ctst_free == FALSE) { /* only free up to the allocated mark */
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700649 for(i=0; i<ctst_allocated; i++) {
650 free(ctst_allocated_stuff[i]);
651 ctst_allocated_stuff[i] = NULL;
652 }
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800653 } else { /* free all */
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700654 for(i=0; i<CTST_MAX_ALLOC; i++) {
655 free(ctst_allocated_stuff[i]);
656 ctst_allocated_stuff[i] = NULL;
657 }
658 }
659 ctst_allocated = 0;
660 _testDataPath=NULL;
661}
662
663#define VERBOSE_ASSERTIONS
664
ccornelius59d709d2014-02-20 10:29:46 -0800665U_CFUNC UBool assertSuccessCheck(const char* msg, UErrorCode* ec, UBool possibleDataError) {
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700666 U_ASSERT(ec!=NULL);
667 if (U_FAILURE(*ec)) {
ccornelius59d709d2014-02-20 10:29:46 -0800668 if (possibleDataError) {
669 log_data_err("FAIL: %s (%s)\n", msg, u_errorName(*ec));
670 } else {
671 log_err_status(*ec, "FAIL: %s (%s)\n", msg, u_errorName(*ec));
672 }
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700673 return FALSE;
674 }
675 return TRUE;
676}
677
ccornelius59d709d2014-02-20 10:29:46 -0800678U_CFUNC UBool assertSuccess(const char* msg, UErrorCode* ec) {
679 U_ASSERT(ec!=NULL);
680 return assertSuccessCheck(msg, ec, FALSE);
681}
682
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700683/* if 'condition' is a UBool, the compiler complains bitterly about
684 expressions like 'a > 0' which it evaluates as int */
685U_CFUNC UBool assertTrue(const char* msg, int /*not UBool*/ condition) {
686 if (!condition) {
687 log_err("FAIL: assertTrue() failed: %s\n", msg);
688 }
689#ifdef VERBOSE_ASSERTIONS
690 else {
691 log_verbose("Ok: %s\n", msg);
692 }
693#endif
694 return (UBool)condition;
695}
696
697U_CFUNC UBool assertEquals(const char* message, const char* expected,
698 const char* actual) {
699 if (uprv_strcmp(expected, actual) != 0) {
700 log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
701 message, actual, expected);
702 return FALSE;
703 }
704#ifdef VERBOSE_ASSERTIONS
705 else {
706 log_verbose("Ok: %s; got \"%s\"\n", message, actual);
707 }
708#endif
709 return TRUE;
710}
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700711
Fredrik Roubert57b72de2018-06-21 14:29:02 +0200712U_CFUNC UBool assertUEquals(const char* message, const UChar* expected,
713 const UChar* actual) {
714 for (int32_t i=0;; i++) {
715 if (expected[i] != actual[i]) {
716 log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
717 message, austrdup(actual), austrdup(expected));
718 return FALSE;
719 }
720 UChar curr = expected[i];
721 U_ASSERT(curr == actual[i]);
722 if (curr == 0) {
723 break;
724 }
725 }
726#ifdef VERBOSE_ASSERTIONS
727 log_verbose("Ok: %s; got \"%s\"\n", message, austrdup(actual));
728#endif
729 return TRUE;
730}
731
732U_CFUNC UBool assertIntEquals(const char* message, int64_t expected, int64_t actual) {
733 if (expected != actual) {
734 log_err("FAIL: %s; got \"%d\"; expected \"%d\"\n",
735 message, actual, expected);
736 return FALSE;
737 }
738#ifdef VERBOSE_ASSERTIONS
739 else {
740 log_verbose("Ok: %s; got \"%d\"\n", message, actual);
741 }
742#endif
743 return TRUE;
744}
745
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700746#endif