From 8e7d1112f4c2e7863748c97e0ffc0a757fa3454a Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Mon, 4 Aug 2003 20:36:18 +0000 Subject: [PATCH] ICU-3138 test internal sort function X-SVN-Rev: 12752 --- icu4c/source/test/cintltst/Makefile.in | 2 +- icu4c/source/test/cintltst/cintltst.dsp | 4 + icu4c/source/test/cintltst/cintltst.vcproj | 3 + icu4c/source/test/cintltst/cutiltst.c | 2 + icu4c/source/test/cintltst/sorttest.c | 92 ++++++++++++++++++++++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 icu4c/source/test/cintltst/sorttest.c diff --git a/icu4c/source/test/cintltst/Makefile.in b/icu4c/source/test/cintltst/Makefile.in index 2d50362eb18..18e153d52b4 100644 --- a/icu4c/source/test/cintltst/Makefile.in +++ b/icu4c/source/test/cintltst/Makefile.in @@ -46,7 +46,7 @@ cnmdptst.o cnormtst.o cnumtst.o cregrtst.o crestst.o creststn.o cturtst.o \ cucdapi.o cucdtst.o custrtst.o cstrcase.o cutiltst.o encoll.o nucnvtst.o nccbtst.o bocu1tst.o \ cbiditst.o cbididat.o eurocreg.o udatatst.o utf16tst.o utransts.o \ ncnvfbts.o ncnvtst.o putiltst.o cstrtest.o mstrmtst.o utf8tst.o ucmptst.o \ -stdnmtst.o ctstdep.o usrchtst.o custrtrn.o trietest.o usettest.o uenumtst.o \ +stdnmtst.o ctstdep.o usrchtst.o custrtrn.o sorttest.o trietest.o usettest.o uenumtst.o \ idnatest.o nfsprep.o spreptst.o sprpdata.o DEPS = $(OBJECTS:.o=.d) diff --git a/icu4c/source/test/cintltst/cintltst.dsp b/icu4c/source/test/cintltst/cintltst.dsp index 2408e38229d..0fc9ebb0d18 100644 --- a/icu4c/source/test/cintltst/cintltst.dsp +++ b/icu4c/source/test/cintltst/cintltst.dsp @@ -325,6 +325,10 @@ SOURCE=.\chashtst.c # End Source File # Begin Source File +SOURCE=.\sorttest.c +# End Source File +# Begin Source File + SOURCE=.\trietest.c # End Source File # Begin Source File diff --git a/icu4c/source/test/cintltst/cintltst.vcproj b/icu4c/source/test/cintltst/cintltst.vcproj index 6780a6cc4f7..57352319313 100644 --- a/icu4c/source/test/cintltst/cintltst.vcproj +++ b/icu4c/source/test/cintltst/cintltst.vcproj @@ -268,6 +268,9 @@ + + diff --git a/icu4c/source/test/cintltst/cutiltst.c b/icu4c/source/test/cintltst/cutiltst.c index fbb77f9c4eb..a7ed4bb1f5f 100644 --- a/icu4c/source/test/cintltst/cutiltst.c +++ b/icu4c/source/test/cintltst/cutiltst.c @@ -25,6 +25,7 @@ void addMemoryStreamTest(TestNode** root); void addTrieTest(TestNode** root); void addEnumerationTest(TestNode** root); void addPosixTest(TestNode** root); +void addSortTest(TestNode** root); void addUtility(TestNode** root); @@ -41,4 +42,5 @@ void addUtility(TestNode** root) addMemoryStreamTest(root); addEnumerationTest(root); addPosixTest(root); + addSortTest(root); } diff --git a/icu4c/source/test/cintltst/sorttest.c b/icu4c/source/test/cintltst/sorttest.c new file mode 100644 index 00000000000..6bedf63aeb8 --- /dev/null +++ b/icu4c/source/test/cintltst/sorttest.c @@ -0,0 +1,92 @@ +/* +******************************************************************************* +* +* Copyright (C) 2003, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* file name: csorttst.c +* encoding: US-ASCII +* tab size: 8 (not used) +* indentation:4 +* +* created on: 2003aug04 +* created by: Markus W. Scherer +* +* Test internal sorting functions. +*/ + +#include "unicode/utypes.h" +#include "cmemory.h" +#include "cintltst.h" +#include "uarrsort.h" + +#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) + +/* compare only the top 28 bits of int32_t items (bits 3..0 contain the original index) */ +static int32_t U_CALLCONV +_int32Top28Comparator(const void *context, const void *left, const void *right) { + return (*(const int32_t *)left>>4) - (*(const int32_t *)right>>4); +} + +void +SortTest(void) { + uint16_t small[]={ 10, 8, 1, 2, 5, 4, 3, 9, 7, 6 }; + int32_t medium[]={ 10, 8, 1, 2, 5, 5, 6, 4, 3, 9, 7, 5 }; + uint32_t large[]={ 21, 10, 20, 19, 11, 12, 13, 10, 10, 10, 10, + 8, 1, 2, 5, 10, 10, 4, 17, 18, 3, 9, 10, 7, 6, 14, 15, 16 }; + + int32_t i; + UErrorCode errorCode; + + /* sort small array (stable) */ + errorCode=U_ZERO_ERROR; + uprv_sortArray(small, LENGTHOF(small), sizeof(small[0]), uprv_uint16Comparator, NULL, TRUE, &errorCode); + if(U_FAILURE(errorCode)) { + log_err("uprv_sortArray(small) failed - %s\n", u_errorName(errorCode)); + return; + } + for(i=1; ismall[i]) { + log_err("uprv_sortArray(small) mis-sorted [%d]=%u > [%d]=%u\n", i-1, small[i-1], i, small[i]); + return; + } + } + + /* for medium, add bits that will not be compared, to test stability */ + for(i=0; i=medium[i]) { + log_err("uprv_sortArray(medium) mis-sorted [%d]=%u > [%d]=%u\n", i-1, medium[i-1], i, medium[i]); + return; + } + } + + /* sort large array (not stable) */ + errorCode=U_ZERO_ERROR; + uprv_sortArray(large, LENGTHOF(large), sizeof(large[0]), uprv_uint32Comparator, NULL, FALSE, &errorCode); + if(U_FAILURE(errorCode)) { + log_err("uprv_sortArray(large) failed - %s\n", u_errorName(errorCode)); + return; + } + for(i=1; ilarge[i]) { + log_err("uprv_sortArray(large) mis-sorted [%d]=%u > [%d]=%u\n", i-1, large[i-1], i, large[i]); + return; + } + } +} + +void +addSortTest(TestNode** root) { + addTest(root, &SortTest, "tsutil/SortTest"); +}