X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/729e4ab9bc6618bc3d8a898e575df7f4019e29ca..0f5d89e82340278ed3d7d50029f37cab2c41a57e:/icuSources/test/intltest/normconf.cpp diff --git a/icuSources/test/intltest/normconf.cpp b/icuSources/test/intltest/normconf.cpp index fae7cf69..62710dbb 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-2010, International Business Machines +* Copyright (c) 1997-2016, International Business Machines * Corporation and others. All Rights Reserved. ************************************************************************ */ @@ -9,41 +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 "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,exec) 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, exec); -#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION - CASE(1, TestConformance32, exec); -#endif - // 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() {} @@ -174,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)); @@ -279,6 +280,15 @@ void NormalizerConformanceTest::TestConformance(FileStream *input, int32_t optio } } +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, @@ -299,56 +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, 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", s8.length(), iter.sourceIndex()); + pass &= assertEquals("edits destination length", out8.length(), iter.destinationIndex()); + return pass; +} + /** * Do a normalization using the iterative API in the given direction. * @param dir either +1 or -1 @@ -474,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; @@ -508,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;