X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/46f4442e9a5a4f3b98b7c1083586332f6a8a99a4..3d1f044b704633e2e541231cd17ae9ecf9ad5c7a:/icuSources/test/intltest/normconf.cpp?ds=sidebyside diff --git a/icuSources/test/intltest/normconf.cpp b/icuSources/test/intltest/normconf.cpp index 10e7ae89..7c574ddb 100644 --- a/icuSources/test/intltest/normconf.cpp +++ b/icuSources/test/intltest/normconf.cpp @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ************************************************************************ -* Copyright (c) 1997-2008, International Business Machines +* Copyright (c) 1997-2016, International Business Machines * Corporation and others. All Rights Reserved. ************************************************************************ */ @@ -9,40 +11,40 @@ #if !UCONFIG_NO_NORMALIZATION +#include +#include "unicode/bytestream.h" +#include "unicode/edits.h" #include "unicode/uchar.h" +#include "unicode/normalizer2.h" #include "unicode/normlzr.h" #include "unicode/uniset.h" #include "unicode/putil.h" -#include "unormimp.h" +#include "cmemory.h" #include "cstring.h" #include "filestrm.h" #include "normconf.h" +#include "uassert.h" #include -#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0])) - -#define CASE(id,test) case id: \ - name = #test; \ - if (exec) { \ - logln(#test "---"); \ - logln((UnicodeString)""); \ - test(); \ - } \ - break - void NormalizerConformanceTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) { - switch (index) { - CASE(0, TestConformance); - CASE(1, TestConformance32); - // CASE(2, TestCase6); - default: name = ""; break; - } + TESTCASE_AUTO_BEGIN; + TESTCASE_AUTO(TestConformance); + TESTCASE_AUTO(TestConformance32); + TESTCASE_AUTO(TestCase6); + TESTCASE_AUTO_END; } #define FIELD_COUNT 5 NormalizerConformanceTest::NormalizerConformanceTest() : - normalizer(UnicodeString(), UNORM_NFC) {} + normalizer(UnicodeString(), UNORM_NFC) { + UErrorCode errorCode = U_ZERO_ERROR; + nfc = Normalizer2::getNFCInstance(errorCode); + nfd = Normalizer2::getNFDInstance(errorCode); + nfkc = Normalizer2::getNFKCInstance(errorCode); + nfkd = Normalizer2::getNFKDInstance(errorCode); + assertSuccess("", errorCode, true, __FILE__, __LINE__); +} NormalizerConformanceTest::~NormalizerConformanceTest() {} @@ -141,7 +143,7 @@ NormalizerConformanceTest::openNormalizationTestFile(const char *filename) { } #endif - dataerrln("[DATA] Failed to open %s", filename); + dataerrln("Failed to open %s", filename); return NULL; } @@ -173,7 +175,7 @@ void NormalizerConformanceTest::TestConformance(FileStream *input, int32_t optio // UnicodeSet for all code points that are not mentioned in NormalizationTest.txt UnicodeSet other(0, 0x10ffff); - int32_t count, countMoreCases = sizeof(moreCases)/sizeof(moreCases[0]); + int32_t count, countMoreCases = UPRV_LENGTHOF(moreCases); for (count = 1;;++count) { if (!T_FileStream_eof(input)) { T_FileStream_readLine(input, lineBuf, (int32_t)sizeof(lineBuf)); @@ -225,7 +227,7 @@ void NormalizerConformanceTest::TestConformance(FileStream *input, int32_t optio } else { ++failCount; if(status == U_FILE_ACCESS_ERROR) { - errln("Something is wrong with the normalizer, skipping the rest of the test."); + dataerrln("Something is wrong with the normalizer, skipping the rest of the test."); break; } } @@ -261,7 +263,7 @@ void NormalizerConformanceTest::TestConformance(FileStream *input, int32_t optio } else { ++failCount; if(status == U_FILE_ACCESS_ERROR) { - errln("Something is wrong with the normalizer, skipping the rest of the test."); + dataerrln("Something is wrong with the normalizer, skipping the rest of the test.: %s", u_errorName(status)); break; } } @@ -271,13 +273,22 @@ void NormalizerConformanceTest::TestConformance(FileStream *input, int32_t optio } if (failCount != 0) { - errln((UnicodeString)"Total: " + failCount + " lines/code points failed, " + + dataerrln((UnicodeString)"Total: " + failCount + " lines/code points failed, " + passCount + " lines/code points passed"); } else { logln((UnicodeString)"Total: " + passCount + " lines/code points passed"); } } +namespace { + +UBool isNormalizedUTF8(const Normalizer2 &norm2, const UnicodeString &s, UErrorCode &errorCode) { + std::string s8; + return norm2.isNormalizedUTF8(s.toUTF8String(s8), errorCode); +} + +} // namespace + /** * Verify the conformance of the given line of the Unicode * normalization (UTR 15) test suite file. For each line, @@ -298,40 +309,17 @@ UBool NormalizerConformanceTest::checkConformance(const UnicodeString* field, int32_t options, UErrorCode &status) { UBool pass = TRUE, result; - //UErrorCode status = U_ZERO_ERROR; UnicodeString out, fcd; int32_t fieldNum; for (int32_t i=0; i sink(&out8, static_cast(exp8.length())); + norm2->normalizeUTF8(0, s8, sink, editsPtr, errorCode); + if (U_FAILURE(errorCode)) { + errln("Normalizer2.%s.normalizeUTF8(%s) failed: %s", + modeString, s8.c_str(), u_errorName(errorCode)); + return FALSE; + } + if (out8 != exp8) { + errln("Normalizer2.%s.normalizeUTF8(%s)=%s != %s", + modeString, s8.c_str(), out8.c_str(), exp8.c_str()); + return FALSE; + } + if (editsPtr == nullptr) { + return TRUE; } + + // Do the Edits cover the entire input & output? + UBool pass = TRUE; + pass &= assertEquals("edits.hasChanges()", (UBool)(s8 != out8), edits.hasChanges()); + pass &= assertEquals("edits.lengthDelta()", + (int32_t)(out8.length() - s8.length()), edits.lengthDelta()); + Edits::Iterator iter = edits.getCoarseIterator(); + while (iter.next(errorCode)) {} + pass &= assertEquals("edits source length", static_cast(s8.length()), iter.sourceIndex()); + pass &= assertEquals("edits destination length", static_cast(out8.length()), iter.destinationIndex()); return pass; } @@ -457,21 +541,11 @@ void NormalizerConformanceTest::iterativeNorm(const UnicodeString& str, } } -/** - * @param op name of normalization form, e.g., "KC" - * @param s string being normalized - * @param got value received - * @param exp expected value - * @param msg description of this test - * @param return true if got == exp - */ -UBool NormalizerConformanceTest::assertEqual(const char *op, +UBool NormalizerConformanceTest::assertEqual(const char *op, const char *op2, const UnicodeString& s, const UnicodeString& got, const UnicodeString& exp, - const char *msg, - int32_t field) -{ + const char *msg) { if (exp == got) return TRUE; @@ -491,7 +565,7 @@ UBool NormalizerConformanceTest::assertEqual(const char *op, expPretty.extract(0, expPretty.length(), expChars, expPretty.length() + 1); expChars[expPretty.length()] = 0; - errln(" %s%d)%s(%s)=%s, exp. %s", msg, field, op, sChars, gotChars, expChars); + errln(" %s: %s%s(%s)=%s, exp. %s", msg, op, op2, sChars, gotChars, expChars); delete []sChars; delete []gotChars;