U_CDECL_END int main(int argc, char* argv[]) { int32_t nerrors = 0; TestNode *root = NULL; UErrorCode errorCode = U_ZERO_ERROR; UDate startTime, endTime; int32_t diffTime; startTime = uprv_getRawUTCtime(); /* Check whether ICU will initialize without forcing the build data directory into * the ICU_DATA path. Success here means either the data dll contains data, or that * this test program was run with ICU_DATA set externally. Failure of this check * is normal when ICU data is not packaged into a shared library. * * Whether or not this test succeeds, we want to cleanup and reinitialize * with a data path so that data loading from individual files can be tested. */ u_init(&errorCode); if (U_FAILURE(errorCode)) { fprintf(stderr, "#### Note: ICU Init without build-specific setDataDirectory() failed.\n"); } u_cleanup(); errorCode = U_ZERO_ERROR; if (!initArgs(argc, argv, argHandler, (void *) &STANDARD_TEST_FILE)) { /* Error already displayed. */ return -1; } /* Initialize ICU */ ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */ u_init(&errorCode); if (U_FAILURE(errorCode)) { fprintf(stderr, "#### ERROR! %s: u_init() failed with status = \"%s\".\n" "*** Check the ICU_DATA environment variable and \n" "*** check that the data files are present.\n", argv[0], u_errorName(errorCode)); return 1; } fprintf(stdout, "Default charset for this run is %s\n", ucnv_getDefaultName()); addAllTests(&root); nerrors = runTestRequest(root, argc, argv); #if 1 { FILE* fileToRemove = fopen(STANDARD_TEST_FILE, "r"); /* This should delete any temporary files. */ if (fileToRemove) { fclose(fileToRemove); log_verbose("Deleting: %s\n", STANDARD_TEST_FILE); if (remove(STANDARD_TEST_FILE) != 0) { /* Maybe someone didn't close the file correctly. */ fprintf(stderr, "FAIL: Could not delete %s\n", STANDARD_TEST_FILE); nerrors += 1; } } } #endif cleanUpTestTree(root); DataDrivenLogger::cleanUp(); u_cleanup(); endTime = uprv_getRawUTCtime(); diffTime = (int32_t)(endTime - startTime); printf("Elapsed Time: %02d:%02d:%02d.%03d\n", (int)((diffTime%U_MILLIS_PER_DAY)/U_MILLIS_PER_HOUR), (int)((diffTime%U_MILLIS_PER_HOUR)/U_MILLIS_PER_MINUTE), (int)((diffTime%U_MILLIS_PER_MINUTE)/U_MILLIS_PER_SECOND), (int)(diffTime%U_MILLIS_PER_SECOND)); return nerrors; }
int main(int argc, const char* const argv[]) { int nerrors = 0; UBool defaultDataFound; TestNode *root; const char *warnOrErr = "Failure"; UDate startTime, endTime; int32_t diffTime; /* initial check for the default converter */ UErrorCode errorCode = U_ZERO_ERROR; UResourceBundle *rb; UConverter *cnv; U_MAIN_INIT_ARGS(argc, argv); startTime = uprv_getRawUTCtime(); gOrigArgc = argc; gOrigArgv = argv; if (!initArgs(argc, argv, NULL, NULL)) { /* Error already displayed. */ return -1; } /* Check whether ICU will initialize without forcing the build data directory into * the ICU_DATA path. Success here means either the data dll contains data, or that * this test program was run with ICU_DATA set externally. Failure of this check * is normal when ICU data is not packaged into a shared library. * * Whether or not this test succeeds, we want to cleanup and reinitialize * with a data path so that data loading from individual files can be tested. */ defaultDataFound = TRUE; u_init(&errorCode); if (U_FAILURE(errorCode)) { fprintf(stderr, "#### Note: ICU Init without build-specific setDataDirectory() failed. %s\n", u_errorName(errorCode)); defaultDataFound = FALSE; } u_cleanup(); #ifdef URES_DEBUG fprintf(stderr, "After initial u_cleanup: RB cache %s empty.\n", ures_dumpCacheContents()?"WAS NOT":"was"); #endif while (getTestOption(REPEAT_TESTS_OPTION) > 0) { /* Loop runs once per complete execution of the tests * used for -r (repeat) test option. */ if (!initArgs(argc, argv, NULL, NULL)) { /* Error already displayed. */ return -1; } errorCode = U_ZERO_ERROR; /* Initialize ICU */ if (!defaultDataFound) { ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */ } u_init(&errorCode); if (U_FAILURE(errorCode)) { fprintf(stderr, "#### ERROR! %s: u_init() failed with status = \"%s\".\n" "*** Check the ICU_DATA environment variable and \n" "*** check that the data files are present.\n", argv[0], u_errorName(errorCode)); if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) { fprintf(stderr, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n"); u_cleanup(); return 1; } } /* try more data */ cnv = ucnv_open(TRY_CNV_2, &errorCode); if(cnv != 0) { /* ok */ ucnv_close(cnv); } else { fprintf(stderr, "*** %s! The converter for " TRY_CNV_2 " cannot be opened.\n" "*** Check the ICU_DATA environment variable and \n" "*** check that the data files are present.\n", warnOrErr); if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) { fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n"); u_cleanup(); return 1; } } rb = ures_open(NULL, "en", &errorCode); if(U_SUCCESS(errorCode)) { /* ok */ ures_close(rb); } else { fprintf(stderr, "*** %s! The \"en\" locale resource bundle cannot be opened.\n" "*** Check the ICU_DATA environment variable and \n" "*** check that the data files are present.\n", warnOrErr); if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) { fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n"); u_cleanup(); return 1; } } errorCode = U_ZERO_ERROR; rb = ures_open(NULL, NULL, &errorCode); if(U_SUCCESS(errorCode)) { /* ok */ if (errorCode == U_USING_DEFAULT_WARNING || errorCode == U_USING_FALLBACK_WARNING) { fprintf(stderr, "#### Note: The default locale %s is not available\n", uloc_getDefault()); } ures_close(rb); } else { fprintf(stderr, "*** %s! Can not open a resource bundle for the default locale %s\n", warnOrErr, uloc_getDefault()); if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) { fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n" "*** purposely removed, to continue test anyway.\n"); u_cleanup(); return 1; } } fprintf(stdout, "Default locale for this run is %s\n", uloc_getDefault()); /* Build a tree of all tests. * Subsequently will be used to find / iterate the tests to run */ root = NULL; addAllTests(&root); /* Tests acutally run HERE. TODO: separate command line option parsing & setting from test execution!! */ nerrors = runTestRequest(root, argc, argv); setTestOption(REPEAT_TESTS_OPTION, DECREMENT_OPTION_VALUE); if (getTestOption(REPEAT_TESTS_OPTION) > 0) { printf("Repeating tests %d more time(s)\n", getTestOption(REPEAT_TESTS_OPTION)); } cleanUpTestTree(root); #ifdef CTST_LEAK_CHECK ctst_freeAll(); /* To check for leaks */ u_cleanup(); /* nuke the hashtable.. so that any still-open cnvs are leaked */ if(getTestOption(VERBOSITY_OPTION) && ctst_allocated_total>0) { fprintf(stderr,"ctst_freeAll(): cleaned up after %d allocations (queue of %d)\n", ctst_allocated_total, CTST_MAX_ALLOC); } #ifdef URES_DEBUG if(ures_dumpCacheContents()) { fprintf(stderr, "Error: After final u_cleanup, RB cache was not empty.\n"); nerrors++; } else { fprintf(stderr,"OK: After final u_cleanup, RB cache was empty.\n"); } #endif #endif } /* End of loop that repeats the entire test, if requested. (Normally doesn't loop) */ #ifdef UNISTR_COUNT_FINAL_STRING_LENGTHS unistr_printLengths(); #endif endTime = uprv_getRawUTCtime(); diffTime = (int32_t)(endTime - startTime); printf("Elapsed Time: %02d:%02d:%02d.%03d\n", (int)((diffTime%U_MILLIS_PER_DAY)/U_MILLIS_PER_HOUR), (int)((diffTime%U_MILLIS_PER_HOUR)/U_MILLIS_PER_MINUTE), (int)((diffTime%U_MILLIS_PER_MINUTE)/U_MILLIS_PER_SECOND), (int)(diffTime%U_MILLIS_PER_SECOND)); return nerrors ? 1 : 0; }
//---------------------------------------------------------------------------- // // main for gendict // //---------------------------------------------------------------------------- int main(int argc, char **argv) { // // Pick up and check the command line arguments, // using the standard ICU tool utils option handling. // U_MAIN_INIT_ARGS(argc, argv); progName = argv[0]; argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); if(argc<0) { // Unrecognized option fprintf(stderr, "error in command line argument \"%s\"\n", argv[-argc]); usageAndDie(U_ILLEGAL_ARGUMENT_ERROR); } if(options[ARG_HELP].doesOccur || options[ARG_QMARK].doesOccur) { // -? or -h for help. usageAndDie(U_ZERO_ERROR); } UBool verbose = options[ARG_VERBOSE].doesOccur; if (argc < 3) { fprintf(stderr, "input and output file must both be specified.\n"); usageAndDie(U_ILLEGAL_ARGUMENT_ERROR); } const char *outFileName = argv[2]; const char *wordFileName = argv[1]; startTime = uprv_getRawUTCtime(); // initialize start timer if (options[ARG_ICUDATADIR].doesOccur) { u_setDataDirectory(options[ARG_ICUDATADIR].value); } const char *copyright = NULL; if (options[ARG_COPYRIGHT].doesOccur) { copyright = U_COPYRIGHT_STRING; } if (options[ARG_UCHARS].doesOccur == options[ARG_BYTES].doesOccur) { fprintf(stderr, "you must specify exactly one type of trie to output!\n"); usageAndDie(U_ILLEGAL_ARGUMENT_ERROR); } UBool isBytesTrie = options[ARG_BYTES].doesOccur; if (isBytesTrie != options[ARG_TRANSFORM].doesOccur) { fprintf(stderr, "you must provide a transformation for a bytes trie, and must not provide one for a uchars trie!\n"); usageAndDie(U_ILLEGAL_ARGUMENT_ERROR); } IcuToolErrorCode status("gendict/main()"); #if UCONFIG_NO_BREAK_ITERATION || UCONFIG_NO_FILE_IO const char* outDir=NULL; UNewDataMemory *pData; char msg[1024]; UErrorCode tempstatus = U_ZERO_ERROR; /* write message with just the name */ // potential for a buffer overflow here... sprintf(msg, "gendict writes dummy %s because of UCONFIG_NO_BREAK_ITERATION and/or UCONFIG_NO_FILE_IO, see uconfig.h", outFileName); fprintf(stderr, "%s\n", msg); /* write the dummy data file */ pData = udata_create(outDir, NULL, outFileName, &dataInfo, NULL, &tempstatus); udata_writeBlock(pData, msg, strlen(msg)); udata_finish(pData, &tempstatus); return (int)tempstatus; #else // Read in the dictionary source file if (verbose) { printf("Opening file %s...\n", wordFileName); } const char *codepage = "UTF-8"; UCHARBUF *f = ucbuf_open(wordFileName, &codepage, TRUE, FALSE, status); if (status.isFailure()) { fprintf(stderr, "error opening input file: ICU Error \"%s\"\n", status.errorName()); exit(status.reset()); } if (verbose) { printf("Initializing dictionary builder of type %s...\n", (isBytesTrie ? "BytesTrie" : "UCharsTrie")); } DataDict dict(isBytesTrie, status); if (status.isFailure()) { fprintf(stderr, "new DataDict: ICU Error \"%s\"\n", status.errorName()); exit(status.reset()); } if (options[ARG_TRANSFORM].doesOccur) { dict.setTransform(options[ARG_TRANSFORM].value); } UnicodeString fileLine; if (verbose) { puts("Adding words to dictionary..."); } UBool hasValues = FALSE; UBool hasValuelessContents = FALSE; int lineCount = 0; int wordCount = 0; int minlen = 255; int maxlen = 0; UBool isOk = TRUE; while (readLine(f, fileLine, status)) { lineCount++; if (fileLine.isEmpty()) continue; // Parse word [spaces value]. int32_t keyLen; for (keyLen = 0; keyLen < fileLine.length() && !u_isspace(fileLine[keyLen]); ++keyLen) {} if (keyLen == 0) { fprintf(stderr, "Error: no word on line %i!\n", lineCount); isOk = FALSE; continue; } int32_t valueStart; for (valueStart = keyLen; valueStart < fileLine.length() && u_isspace(fileLine[valueStart]); ++valueStart) {} if (keyLen < valueStart) { int32_t valueLength = fileLine.length() - valueStart; if (valueLength > 15) { fprintf(stderr, "Error: value too long on line %i!\n", lineCount); isOk = FALSE; continue; } char s[16]; fileLine.extract(valueStart, valueLength, s, 16, US_INV); char *end; unsigned long value = uprv_strtoul(s, &end, 0); if (end == s || *end != 0 || (int32_t)uprv_strlen(s) != valueLength || value > 0xffffffff) { fprintf(stderr, "Error: value syntax error or value too large on line %i!\n", lineCount); isOk = FALSE; continue; } dict.addWord(fileLine.tempSubString(0, keyLen), (int32_t)value, status); hasValues = TRUE; wordCount++; if (keyLen < minlen) minlen = keyLen; if (keyLen > maxlen) maxlen = keyLen; } else { dict.addWord(fileLine.tempSubString(0, keyLen), 0, status); hasValuelessContents = TRUE; wordCount++; if (keyLen < minlen) minlen = keyLen; if (keyLen > maxlen) maxlen = keyLen; } if (status.isFailure()) { fprintf(stderr, "ICU Error \"%s\": Failed to add word to trie at input line %d in input file\n", status.errorName(), lineCount); exit(status.reset()); } } if (verbose) { printf("Processed %d lines, added %d words, minlen %d, maxlen %d\n", lineCount, wordCount, minlen, maxlen); } if (!isOk && status.isSuccess()) { status.set(U_ILLEGAL_ARGUMENT_ERROR); } if (hasValues && hasValuelessContents) { fprintf(stderr, "warning: file contained both valued and unvalued strings!\n"); } if (verbose) { printf("Serializing data...isBytesTrie? %d\n", isBytesTrie); } int32_t outDataSize; const void *outData; UnicodeString usp; if (isBytesTrie) { StringPiece sp = dict.serializeBytes(status); outDataSize = sp.size(); outData = sp.data(); } else { dict.serializeUChars(usp, status); outDataSize = usp.length() * U_SIZEOF_UCHAR; outData = usp.getBuffer(); } if (status.isFailure()) { fprintf(stderr, "gendict: got failure of type %s while serializing, if U_ILLEGAL_ARGUMENT_ERROR possibly due to duplicate dictionary entries\n", status.errorName()); exit(status.reset()); } if (verbose) { puts("Opening output file..."); } UNewDataMemory *pData = udata_create(NULL, NULL, outFileName, &dataInfo, copyright, status); if (status.isFailure()) { fprintf(stderr, "gendict: could not open output file \"%s\", \"%s\"\n", outFileName, status.errorName()); exit(status.reset()); } if (verbose) { puts("Writing to output file..."); } int32_t indexes[DictionaryData::IX_COUNT] = { DictionaryData::IX_COUNT * sizeof(int32_t), 0, 0, 0, 0, 0, 0, 0 }; int32_t size = outDataSize + indexes[DictionaryData::IX_STRING_TRIE_OFFSET]; indexes[DictionaryData::IX_RESERVED1_OFFSET] = size; indexes[DictionaryData::IX_RESERVED2_OFFSET] = size; indexes[DictionaryData::IX_TOTAL_SIZE] = size; indexes[DictionaryData::IX_TRIE_TYPE] = isBytesTrie ? DictionaryData::TRIE_TYPE_BYTES : DictionaryData::TRIE_TYPE_UCHARS; if (hasValues) { indexes[DictionaryData::IX_TRIE_TYPE] |= DictionaryData::TRIE_HAS_VALUES; } indexes[DictionaryData::IX_TRANSFORM] = dict.getTransform(); udata_writeBlock(pData, indexes, sizeof(indexes)); udata_writeBlock(pData, outData, outDataSize); size_t bytesWritten = udata_finish(pData, status); if (status.isFailure()) { fprintf(stderr, "gendict: error \"%s\" writing the output file\n", status.errorName()); exit(status.reset()); } if (bytesWritten != (size_t)size) { fprintf(stderr, "Error writing to output file \"%s\"\n", outFileName); exit(U_INTERNAL_PROGRAM_ERROR); } printf("%s: done writing\t%s (%ds).\n", progName, outFileName, elapsedTime()); #ifdef TEST_GENDICT if (isBytesTrie) { BytesTrie::Iterator it(outData, outDataSize, status); while (it.hasNext()) { it.next(status); const StringPiece s = it.getString(); int32_t val = it.getValue(); printf("%s -> %i\n", s.data(), val); } } else { UCharsTrie::Iterator it((const UChar *)outData, outDataSize, status); while (it.hasNext()) { it.next(status); const UnicodeString s = it.getString(); int32_t val = it.getValue(); char tmp[1024]; s.extract(0, s.length(), tmp, 1024); printf("%s -> %i\n", tmp, val); } } #endif return 0; #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ }
static int elapsedTime() { return (int)uprv_floor((uprv_getRawUTCtime()-startTime)/1000.0); }
/** * Run or list tests (according to mode) in a subtree. * * @param root root of the subtree to operate on * @param depth The depth of this tree (0=root) * @param nodeList an array of MAXTESTS depth that's used for keeping track of where we are. nodeList[depth] points to the 'parent' at depth depth. * @param mode what mode we are operating in. */ static void iterateTestsWithLevel ( const TestNode* root, int depth, const TestNode** nodeList, TestMode mode) { int i; char pathToFunction[MAXTESTNAME] = ""; char separatorString[2] = { TEST_SEPARATOR, '\0'}; #if SHOW_TIMES UDate allStartTime = -1, allStopTime = -1; #endif if(depth<2) { allStartTime = uprv_getRawUTCtime(); } if ( root == NULL ) return; /* record the current root node, and increment depth. */ nodeList[depth++] = root; /* depth is now the depth of root's children. */ /* Collect the 'path' to the current subtree. */ for ( i=0;i<(depth-1);i++ ) { strcat(pathToFunction, nodeList[i]->name); strcat(pathToFunction, separatorString); } strcat(pathToFunction, nodeList[i]->name); /* including 'root' */ /* print test name and space. */ INDENT_LEVEL = depth-1; if(root->name[0]) { log_testinfo_i("%s ", root->name); } else { log_testinfo_i("(%s) ", ARGV_0); } ON_LINE = TRUE; /* we are still on the line with the test name */ if ( (mode == RUNTESTS) && (root->test != NULL)) /* if root is a leaf node, run it */ { int myERROR_COUNT = ERROR_COUNT; int myGLOBAL_PRINT_COUNT = GLOBAL_PRINT_COUNT; #if SHOW_TIMES UDate startTime, stopTime; char timeDelta[256]; char timeSeconds[256]; #else const char timeDelta[] = "(unknown)"; const char timeSeconds[] = "0.000"; #endif currentTest = root; INDENT_LEVEL = depth; /* depth of subitems */ ONE_ERROR=0; HANGING_OUTPUT=FALSE; #if SHOW_TIMES startTime = uprv_getRawUTCtime(); #endif root->test(); /* PERFORM THE TEST ************************/ #if SHOW_TIMES stopTime = uprv_getRawUTCtime(); #endif if(HANGING_OUTPUT) { log_testinfo("\n"); HANGING_OUTPUT=FALSE; } INDENT_LEVEL = depth-1; /* depth of root */ currentTest = NULL; if((ONE_ERROR>0)&&(ERROR_COUNT==0)) { ERROR_COUNT++; /* There was an error without a newline */ } ONE_ERROR=0; #if SHOW_TIMES str_timeDelta(timeDelta, stopTime-startTime); sprintf(timeSeconds, "%f", (stopTime-startTime)/1000.0); #endif ctest_xml_testcase(pathToFunction, pathToFunction, timeSeconds, (myERROR_COUNT!=ERROR_COUNT)?"error":NULL); if (myERROR_COUNT != ERROR_COUNT) { log_testinfo_i("} ---[%d ERRORS in %s] ", ERROR_COUNT - myERROR_COUNT, pathToFunction); strcpy(ERROR_LOG[ERRONEOUS_FUNCTION_COUNT++], pathToFunction); } else { if(!ON_LINE) { /* had some output */ int spaces = FLAG_INDENT-(depth-1); log_testinfo_i("} %*s[OK] ", spaces, "---"); if((GLOBAL_PRINT_COUNT-myGLOBAL_PRINT_COUNT)>PAGE_SIZE_LIMIT) { log_testinfo(" %s ", pathToFunction); /* in case they forgot. */ } } else { /* put -- out at 30 sp. */ int spaces = FLAG_INDENT-(strlen(root->name)+depth); if(spaces<0) spaces=0; log_testinfo(" %*s[OK] ", spaces,"---"); } } #if SHOW_TIMES if(timeDelta[0]) printf("%s", timeDelta); #endif ON_LINE = TRUE; /* we are back on-line */ } INDENT_LEVEL = depth-1; /* root */ /* we want these messages to be at 0 indent. so just push the indent level breifly. */ if(mode==SHOWTESTS) { log_testinfo("---%s%c\n",pathToFunction, nodeList[i]->test?' ':TEST_SEPARATOR ); } INDENT_LEVEL = depth; if(root->child) { int myERROR_COUNT = ERROR_COUNT; int myGLOBAL_PRINT_COUNT = GLOBAL_PRINT_COUNT; if(mode!=SHOWTESTS) { INDENT_LEVEL=depth-1; log_testinfo("{\n"); INDENT_LEVEL=depth; } iterateTestsWithLevel ( root->child, depth, nodeList, mode ); if(mode!=SHOWTESTS) { INDENT_LEVEL=depth-1; log_testinfo_i("} "); /* TODO: summarize subtests */ if((depth>1) && (ERROR_COUNT > myERROR_COUNT)) { log_testinfo("[%d %s in %s] ", ERROR_COUNT-myERROR_COUNT, (ERROR_COUNT-myERROR_COUNT)==1?"error":"errors", pathToFunction); } else if((GLOBAL_PRINT_COUNT-myGLOBAL_PRINT_COUNT)>PAGE_SIZE_LIMIT || (depth<1)) { if(pathToFunction[0]) { log_testinfo(" %s ", pathToFunction); /* in case they forgot. */ } else { log_testinfo(" / (%s) ", ARGV_0); } } ON_LINE=TRUE; } } depth--; #if SHOW_TIMES if(depth<2) { allStopTime = uprv_getRawUTCtime(); print_timeDelta(allStopTime-allStartTime); } #endif if(mode!=SHOWTESTS && ON_LINE) { log_testinfo("\n"); } if ( depth != 0 ) { /* DO NOT iterate over siblings of the root. TODO: why not? */ iterateTestsWithLevel ( root->sibling, depth, nodeList, mode ); } }