X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/73c04bcfe1096173b00431f0cdc742894b15eef0..ef6cf650f4a75c3f97de06b51fa104f2069b9ea2:/icuSources/test/iotest/strtst.c diff --git a/icuSources/test/iotest/strtst.c b/icuSources/test/iotest/strtst.c index 0412f25e..0dfa4342 100644 --- a/icuSources/test/iotest/strtst.c +++ b/icuSources/test/iotest/strtst.c @@ -1,6 +1,6 @@ /* ********************************************************************** -* Copyright (C) 2004-2006, International Business Machines +* Copyright (C) 2004-2016, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * file name: strtst.c @@ -14,6 +14,7 @@ #include "unicode/ustdio.h" #include "unicode/ustring.h" +#include "cmemory.h" #include "iotest.h" #include @@ -32,8 +33,8 @@ static void TestString(void) { U_STRING_DECL(myStringOrig, "My-String", 9); U_STRING_INIT(myStringOrig, "My-String", 9); - u_memset(myUString, 0x0a, sizeof(myUString)/ sizeof(*myUString)); - u_memset(uStringBuf, 0x0a, sizeof(uStringBuf) / sizeof(*uStringBuf)); + u_memset(myUString, 0x0a, UPRV_LENGTHOF(myUString)); + u_memset(uStringBuf, 0x0a, UPRV_LENGTHOF(uStringBuf)); *n = -1234; if (sizeof(void *) == 4) { @@ -164,14 +165,14 @@ static void TestString(void) { u_sprintf(uStringBuf, "Unicode String %%S: %S", myStringOrig); u_sscanf(uStringBuf, "Unicode String %%S: %S", myUString); - u_austrncpy(myString, myUString, sizeof(myString)/sizeof(*myString)); + u_austrncpy(myString, myUString, UPRV_LENGTHOF(myString)); if (strcmp(myString, "My-String")) { log_err("%%S Got: %s, Expected: My String\n", myString); } u_sprintf(uStringBuf, "NULL Unicode String %%S: %S", NULL); u_sscanf(uStringBuf, "NULL Unicode String %%S: %S", myUString); - u_austrncpy(myString, myUString, sizeof(myString)/sizeof(*myString)); + u_austrncpy(myString, myUString, UPRV_LENGTHOF(myString)); if (strcmp(myString, "(null)")) { log_err("%%S Got: %s, Expected: (null)\n", myString); } @@ -202,7 +203,7 @@ static void TestString(void) { static const char longStr[] = "This is a long test12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; retVal = u_sprintf(uStringBuf, longStr); - u_austrncpy(myString, uStringBuf, sizeof(uStringBuf)/sizeof(*uStringBuf)); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(uStringBuf)); if (strcmp(myString, longStr)) { log_err("%%S Got: %s, Expected: %s\n", myString, longStr); } @@ -211,7 +212,7 @@ static void TestString(void) { } retVal = u_sprintf(uStringBuf, "%s", longStr); - u_austrncpy(myString, uStringBuf, sizeof(uStringBuf)/sizeof(*uStringBuf)); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(uStringBuf)); if (strcmp(myString, longStr)) { log_err("%%S Got: %s, Expected: %s\n", myString, longStr); } @@ -219,13 +220,13 @@ static void TestString(void) { log_err("%%S returned different sizes. Got: %d Expected: %d\n", retVal, strlen(longStr)); } - u_uastrncpy(myUString, longStr, sizeof(longStr)/sizeof(*longStr)); + u_uastrncpy(myUString, longStr, UPRV_LENGTHOF(longStr)); u_sprintf_u(uStringBuf, myUString); if (u_strcmp(myUString, uStringBuf)) { log_err("%%S Long strings differ. Expected: %s\n", longStr); } - u_uastrncpy(myUString, longStr, sizeof(longStr)/sizeof(*longStr)); + u_uastrncpy(myUString, longStr, UPRV_LENGTHOF(longStr)); retVal = u_sprintf_u(uStringBuf, myUString+10); if (u_strcmp(myUString+10, uStringBuf)) { log_err("%%S Long strings differ. Expected: %s\n", longStr + 10); @@ -234,8 +235,8 @@ static void TestString(void) { log_err("%%S returned different sizes. Got: %d Expected: %d\n", retVal, strlen(longStr)); } - u_memset(uStringBuf, 1, sizeof(longStr)/sizeof(*longStr)); - u_uastrncpy(myUString, longStr, sizeof(longStr)/sizeof(*longStr)); + u_memset(uStringBuf, 1, UPRV_LENGTHOF(longStr)); + u_uastrncpy(myUString, longStr, UPRV_LENGTHOF(longStr)); retVal = u_snprintf_u(uStringBuf, 10, myUString); if (u_strncmp(myUString, uStringBuf, 10) || uStringBuf[10] != 1 || retVal != 10) { log_err("%%S Long strings differ. Expected the first 10 characters of %s\n", longStr); @@ -251,7 +252,7 @@ static void TestLocalizedString(void) { char cBuffer[256]; int32_t numResult = -1; const char *locale; - UFILE *strFile = u_fstropen(testStr, sizeof(testStr)/sizeof(testStr[0]), "en_US"); + UFILE *strFile = u_fstropen(testStr, UPRV_LENGTHOF(testStr), "en_US"); if (!strFile) { log_err("u_fstropen failed to work\n"); @@ -294,7 +295,7 @@ static void TestLocalizedString(void) { log_err("u_fscanf failed to work on a de string Got: %d\n", numResult); } - strFile = u_fstropen(testStr, sizeof(testStr)/sizeof(testStr[0]), NULL); + strFile = u_fstropen(testStr, UPRV_LENGTHOF(testStr), NULL); u_fprintf(strFile, "%d", 1234); u_frewind(strFile); numResult = -1; @@ -311,9 +312,9 @@ static void TestLocalizedString(void) { #if !UCONFIG_NO_FORMATTING #define Test_u_snprintf(limit, format, value, expectedSize, expectedStr) \ - u_uastrncpy(testStr, "xxxxxxxxxxxxxx", sizeof(testStr)/sizeof(testStr[0]));\ + u_uastrncpy(testStr, "xxxxxxxxxxxxxx", UPRV_LENGTHOF(testStr));\ size = u_snprintf(testStr, limit, format, value);\ - u_austrncpy(cTestResult, testStr, sizeof(cTestResult)/sizeof(cTestResult[0]));\ + u_austrncpy(cTestResult, testStr, UPRV_LENGTHOF(cTestResult));\ if (size != expectedSize || strcmp(cTestResult, expectedStr) != 0) {\ log_err("Unexpected formatting. size=%d expectedSize=%d cTestResult=%s expectedStr=%s\n",\ size, expectedSize, cTestResult, expectedStr);\ @@ -330,26 +331,26 @@ static void TestSnprintf(void) { char cTestResult[256]; int32_t size; - Test_u_snprintf(0, "%d", 123, 0, "xxxxxxxxxxxxxx"); - Test_u_snprintf(2, "%d", 123, 2, "12xxxxxxxxxxxx"); + Test_u_snprintf(0, "%d", 123, 3, "xxxxxxxxxxxxxx"); + Test_u_snprintf(2, "%d", 123, 3, "12xxxxxxxxxxxx"); Test_u_snprintf(3, "%d", 123, 3, "123xxxxxxxxxxx"); Test_u_snprintf(4, "%d", 123, 3, "123"); - Test_u_snprintf(0, "%s", "abcd", 0, "xxxxxxxxxxxxxx"); - Test_u_snprintf(3, "%s", "abcd", 3, "abcxxxxxxxxxxx"); + Test_u_snprintf(0, "%s", "abcd", 4, "xxxxxxxxxxxxxx"); + Test_u_snprintf(3, "%s", "abcd", 4, "abcxxxxxxxxxxx"); Test_u_snprintf(4, "%s", "abcd", 4, "abcdxxxxxxxxxx"); Test_u_snprintf(5, "%s", "abcd", 4, "abcd"); - Test_u_snprintf(0, "%e", 12.34, 0, "xxxxxxxxxxxxxx"); - Test_u_snprintf(1, "%e", 12.34, 1, "1xxxxxxxxxxxxx"); - Test_u_snprintf(2, "%e", 12.34, 2, "1.xxxxxxxxxxxx"); - Test_u_snprintf(3, "%e", 12.34, 3, "1.2xxxxxxxxxxx"); - Test_u_snprintf(5, "%e", 12.34, 5, "1.234xxxxxxxxx"); - Test_u_snprintf(6, "%e", 12.34, 6, "1.2340xxxxxxxx"); - Test_u_snprintf(8, "%e", 12.34, 8, "1.234000xxxxxx"); - Test_u_snprintf(9, "%e", 12.34, 9, "1.234000exxxxx"); - Test_u_snprintf(10, "%e", 12.34, 10, "1.234000e+xxxx"); - Test_u_snprintf(11, "%e", 12.34, 11, "1.234000e+0xxx"); + Test_u_snprintf(0, "%e", 12.34, 13, "xxxxxxxxxxxxxx"); + Test_u_snprintf(1, "%e", 12.34, 13, "1xxxxxxxxxxxxx"); + Test_u_snprintf(2, "%e", 12.34, 13, "1.xxxxxxxxxxxx"); + Test_u_snprintf(3, "%e", 12.34, 13, "1.2xxxxxxxxxxx"); + Test_u_snprintf(5, "%e", 12.34, 13, "1.234xxxxxxxxx"); + Test_u_snprintf(6, "%e", 12.34, 13, "1.2340xxxxxxxx"); + Test_u_snprintf(8, "%e", 12.34, 13, "1.234000xxxxxx"); + Test_u_snprintf(9, "%e", 12.34, 13, "1.234000exxxxx"); + Test_u_snprintf(10, "%e", 12.34, 13, "1.234000e+xxxx"); + Test_u_snprintf(11, "%e", 12.34, 13, "1.234000e+0xxx"); Test_u_snprintf(13, "%e", 12.34, 13, "1.234000e+001x"); Test_u_snprintf(14, "%e", 12.34, 13, "1.234000e+001"); #endif @@ -357,11 +358,11 @@ static void TestSnprintf(void) { #define TestSPrintFormat(uFormat, uValue, cFormat, cValue) \ /* Reinitialize the buffer to verify null termination works. */\ - u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\ - memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));\ + u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));\ + memset(buffer, '*', UPRV_LENGTHOF(buffer));\ \ uNumPrinted = u_sprintf(uBuffer, uFormat, uValue);\ - u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]));\ + u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));\ cNumPrinted = sprintf(buffer, cFormat, cValue);\ if (strcmp(buffer, compBuffer) != 0) {\ log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\ @@ -377,6 +378,8 @@ static void TestSprintfFormat(void) { #if !UCONFIG_NO_FORMATTING static const UChar abcUChars[] = {0x61,0x62,0x63,0}; static const char abcChars[] = "abc"; + const char *reorderFormat = "%2$d==>%1$-10.10s %6$lld %4$-10.10s %3$#x((%5$d"; /* reordering test*/ + const char *reorderResult = "99==>truncateif 1311768467463790322 1234567890 0xf1b93((10"; UChar uBuffer[256]; char buffer[256]; char compBuffer[256]; @@ -488,6 +491,14 @@ static void TestSprintfFormat(void) { TestSPrintFormat("%.2f", -1.234, "%.2f", -1.234); TestSPrintFormat("%3f", 1.234, "%3f", 1.234); TestSPrintFormat("%3f", -1.234, "%3f", -1.234); + + /* Test reordering format */ + u_sprintf(uBuffer, reorderFormat,"truncateiftoolong", 99, 990099, "12345678901234567890", 10, 0x123456789abcdef2LL); + u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer)); + + if (strcmp(compBuffer, reorderResult) != 0) { + log_err("%s Got: \"%s\", Expected: \"%s\"\n", reorderFormat, compBuffer, buffer); + } #endif } @@ -501,28 +512,28 @@ static void TestStringCompatibility(void) { char testBuf[256] = ""; int32_t num; - u_memset(myUString, 0x0a, sizeof(myUString)/ sizeof(*myUString)); - u_memset(uStringBuf, 0x0a, sizeof(uStringBuf) / sizeof(*uStringBuf)); + u_memset(myUString, 0x0a, UPRV_LENGTHOF(myUString)); + u_memset(uStringBuf, 0x0a, UPRV_LENGTHOF(uStringBuf)); /* Compare against C API compatibility */ for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) { sprintf(testBuf, "%x", (int)num); u_sprintf(uStringBuf, "%x", num); - u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0])); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString)); if (strcmp(myString, testBuf) != 0) { log_err("%%x Got: \"%s\", Expected: \"%s\"\n", myString, testBuf); } sprintf(testBuf, "%X", (int)num); u_sprintf(uStringBuf, "%X", num); - u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0])); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString)); if (strcmp(myString, testBuf) != 0) { log_err("%%X Got: \"%s\", Expected: \"%s\"\n", myString, testBuf); } sprintf(testBuf, "%o", (int)num); u_sprintf(uStringBuf, "%o", num); - u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0])); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString)); if (strcmp(myString, testBuf) != 0) { log_err("%%o Got: \"%s\", Expected: \"%s\"\n", myString, testBuf); } @@ -530,49 +541,49 @@ static void TestStringCompatibility(void) { /* sprintf is not compatible on all platforms e.g. the iSeries*/ sprintf(testBuf, "%d", (int)num); u_sprintf(uStringBuf, "%d", num); - u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0])); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString)); if (strcmp(myString, testBuf) != 0) { log_err("%%d Got: \"%s\", Expected: \"%s\"\n", myString, testBuf); } sprintf(testBuf, "%i", (int)num); u_sprintf(uStringBuf, "%i", num); - u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0])); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString)); if (strcmp(myString, testBuf) != 0) { log_err("%%i Got: \"%s\", Expected: \"%s\"\n", myString, testBuf); } sprintf(testBuf, "%f", (double)num); u_sprintf(uStringBuf, "%f", (double)num); - u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0])); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString)); if (strcmp(myString, testBuf) != 0) { log_err("%%f Got: \"%s\", Expected: \"%s\"\n", myString, testBuf); } /* sprintf(testBuf, "%e", (double)num); u_sprintf(uStringBuf, "%e", (double)num); - u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0])); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString)); if (strcmp(myString, testBuf) != 0) { log_err("%%e Got: \"%s\", Expected: \"%s\"\n", myString, testBuf); } sprintf(testBuf, "%E", (double)num); u_sprintf(uStringBuf, "%E", (double)num); - u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0])); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString)); if (strcmp(myString, testBuf) != 0) { log_err("%%E Got: \"%s\", Expected: \"%s\"\n", myString, testBuf); }*/ sprintf(testBuf, "%g", (double)num); u_sprintf(uStringBuf, "%g", (double)num); - u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0])); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString)); if (strcmp(myString, testBuf) != 0) { log_err("%%g Got: \"%s\", Expected: \"%s\"\n", myString, testBuf); } sprintf(testBuf, "%G", (double)num); u_sprintf(uStringBuf, "%G", (double)num); - u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0])); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString)); if (strcmp(myString, testBuf) != 0) { log_err("%%G Got: \"%s\", Expected: \"%s\"\n", myString, testBuf); } @@ -583,7 +594,7 @@ static void TestStringCompatibility(void) { uStringBuf[0] = (UChar)0xfffe; sprintf(testBuf, "%c", (char)num); u_sprintf(uStringBuf, "%c", num); - u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0])); + u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString)); if (testBuf[0] != myString[0] || myString[0] != num) { log_err("%%c Got: 0x%x, Expected: 0x%x\n", myString[0], testBuf[0]); } @@ -600,16 +611,16 @@ static void TestSScanSetFormat(const char *format, const UChar *uValue, const ch int32_t cNumScanned; /* Reinitialize the buffer to verify null termination works. */ - u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer)); - uBuffer[sizeof(uBuffer)/sizeof(*uBuffer)-1] = 0; - memset(buffer, '*', sizeof(buffer)/sizeof(*buffer)); - buffer[sizeof(buffer)/sizeof(*buffer)-1] = 0; + u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer)); + uBuffer[UPRV_LENGTHOF(uBuffer)-1] = 0; + memset(buffer, '*', UPRV_LENGTHOF(buffer)); + buffer[UPRV_LENGTHOF(buffer)-1] = 0; uNumScanned = u_sscanf(uValue, format, uBuffer); if (expectedToPass) { - u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0])); + u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer)); cNumScanned = sscanf(cValue, format, buffer); - if (strncmp(buffer, compBuffer, sizeof(uBuffer)/sizeof(uBuffer[0])) != 0) { + if (strncmp(buffer, compBuffer, UPRV_LENGTHOF(uBuffer)) != 0) { log_err("%s Got: \"%s\", Expected: \"%s\"\n", format, compBuffer, buffer); } if (cNumScanned != uNumScanned) { @@ -678,8 +689,8 @@ static void TestBadSScanfFormat(const char *format, const UChar *uValue, const c int32_t uNumScanned; /* Reinitialize the buffer to verify null termination works. */ - u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer)); - uBuffer[sizeof(uBuffer)/sizeof(*uBuffer)-1] = 0; + u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer)); + uBuffer[UPRV_LENGTHOF(uBuffer)-1] = 0; uNumScanned = u_sscanf(uValue, format, uBuffer); if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) { @@ -706,6 +717,7 @@ static void Test_u_vfprintf(const char *expectedResult, const char *format, ...) va_start(ap, format); count = u_vsprintf(uBuffer, format, ap); + (void)count; /* Suppress set but not used warning */ va_end(ap); u_uastrcpy(uBuffer2, expectedResult); if (u_strcmp(uBuffer, uBuffer2) != 0) {