]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/sorttest.c
ICU-6.2.8.tar.gz
[apple/icu.git] / icuSources / test / cintltst / sorttest.c
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2003, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: csorttst.c
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2003aug04
14 * created by: Markus W. Scherer
15 *
16 * Test internal sorting functions.
17 */
18
19 #include "unicode/utypes.h"
20 #include "cmemory.h"
21 #include "cintltst.h"
22 #include "uarrsort.h"
23
24 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
25
26 /* compare only the top 28 bits of int32_t items (bits 3..0 contain the original index) */
27 static int32_t U_CALLCONV
28 _int32Top28Comparator(const void *context, const void *left, const void *right) {
29 return (*(const int32_t *)left>>4) - (*(const int32_t *)right>>4);
30 }
31
32 static void
33 SortTest(void) {
34 uint16_t small[]={ 8, 1, 2, 5, 4, 3, 7, 6 };
35 int32_t medium[]={ 10, 8, 1, 2, 5, 5, 6, 4, 3, 9, 7, 5 };
36 uint32_t large[]={ 21, 10, 20, 19, 11, 12, 13, 10, 10, 10, 10,
37 8, 1, 2, 5, 10, 10, 4, 17, 18, 3, 9, 10, 7, 6, 14, 15, 16 };
38
39 int32_t i;
40 UErrorCode errorCode;
41
42 /* sort small array (stable) */
43 errorCode=U_ZERO_ERROR;
44 uprv_sortArray(small, LENGTHOF(small), sizeof(small[0]), uprv_uint16Comparator, NULL, TRUE, &errorCode);
45 if(U_FAILURE(errorCode)) {
46 log_err("uprv_sortArray(small) failed - %s\n", u_errorName(errorCode));
47 return;
48 }
49 for(i=1; i<LENGTHOF(small); ++i) {
50 if(small[i-1]>small[i]) {
51 log_err("uprv_sortArray(small) mis-sorted [%d]=%u > [%d]=%u\n", i-1, small[i-1], i, small[i]);
52 return;
53 }
54 }
55
56 /* for medium, add bits that will not be compared, to test stability */
57 for(i=0; i<LENGTHOF(medium); ++i) {
58 medium[i]=(medium[i]<<4)|i;
59 }
60
61 /* sort medium array (stable) */
62 uprv_sortArray(medium, LENGTHOF(medium), sizeof(medium[0]), _int32Top28Comparator, NULL, TRUE, &errorCode);
63 if(U_FAILURE(errorCode)) {
64 log_err("uprv_sortArray(medium) failed - %s\n", u_errorName(errorCode));
65 return;
66 }
67 for(i=1; i<LENGTHOF(medium); ++i) {
68 if(medium[i-1]>=medium[i]) {
69 log_err("uprv_sortArray(medium) mis-sorted [%d]=%u > [%d]=%u\n", i-1, medium[i-1], i, medium[i]);
70 return;
71 }
72 }
73
74 /* sort large array (not stable) */
75 errorCode=U_ZERO_ERROR;
76 uprv_sortArray(large, LENGTHOF(large), sizeof(large[0]), uprv_uint32Comparator, NULL, FALSE, &errorCode);
77 if(U_FAILURE(errorCode)) {
78 log_err("uprv_sortArray(large) failed - %s\n", u_errorName(errorCode));
79 return;
80 }
81 for(i=1; i<LENGTHOF(large); ++i) {
82 if(large[i-1]>large[i]) {
83 log_err("uprv_sortArray(large) mis-sorted [%d]=%u > [%d]=%u\n", i-1, large[i-1], i, large[i]);
84 return;
85 }
86 }
87 }
88
89 void
90 addSortTest(TestNode** root);
91
92 void
93 addSortTest(TestNode** root) {
94 addTest(root, &SortTest, "tsutil/sorttest/SortTest");
95 }