• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static void
SortTest(void)27 SortTest(void) {
28     uint16_t small[]={ 8, 1, 2, 5, 4, 3, 7, 6 };
29     int32_t medium[]={ 10, 8, 1, 2, 5, 5, -1, 6, 4, 3, 9, 7, 5 };
30     uint32_t large[]={ 21, 10, 20, 19, 11, 12, 13, 10, 10, 10, 10,
31                        8, 1, 2, 5, 10, 10, 4, 17, 18, 3, 9, 10, 7, 6, 14, 15, 16 };
32 
33     int32_t i;
34     UErrorCode errorCode;
35 
36     /* sort small array (stable) */
37     errorCode=U_ZERO_ERROR;
38     uprv_sortArray(small, LENGTHOF(small), sizeof(small[0]), uprv_uint16Comparator, NULL, TRUE, &errorCode);
39     if(U_FAILURE(errorCode)) {
40         log_err("uprv_sortArray(small) failed - %s\n", u_errorName(errorCode));
41         return;
42     }
43     for(i=1; i<LENGTHOF(small); ++i) {
44         if(small[i-1]>small[i]) {
45             log_err("uprv_sortArray(small) mis-sorted [%d]=%u > [%d]=%u\n", i-1, small[i-1], i, small[i]);
46             return;
47         }
48     }
49 
50     /* for medium, add bits that will not be compared, to test stability */
51     for(i=0; i<LENGTHOF(medium); ++i) {
52         medium[i]=(medium[i]<<4)|i;
53     }
54 
55     /* sort medium array (stable) */
56     uprv_sortArray(medium, LENGTHOF(medium), sizeof(medium[0]), uprv_int32Comparator, NULL, TRUE, &errorCode);
57     if(U_FAILURE(errorCode)) {
58         log_err("uprv_sortArray(medium) failed - %s\n", u_errorName(errorCode));
59         return;
60     }
61     for(i=1; i<LENGTHOF(medium); ++i) {
62         if(medium[i-1]>=medium[i]) {
63             log_err("uprv_sortArray(medium) mis-sorted [%d]=%u > [%d]=%u\n", i-1, medium[i-1], i, medium[i]);
64             return;
65         }
66     }
67 
68     /* sort large array (not stable) */
69     errorCode=U_ZERO_ERROR;
70     uprv_sortArray(large, LENGTHOF(large), sizeof(large[0]), uprv_uint32Comparator, NULL, FALSE, &errorCode);
71     if(U_FAILURE(errorCode)) {
72         log_err("uprv_sortArray(large) failed - %s\n", u_errorName(errorCode));
73         return;
74     }
75     for(i=1; i<LENGTHOF(large); ++i) {
76         if(large[i-1]>large[i]) {
77             log_err("uprv_sortArray(large) mis-sorted [%d]=%u > [%d]=%u\n", i-1, large[i-1], i, large[i]);
78             return;
79         }
80     }
81 }
82 
83 void
84 addSortTest(TestNode** root);
85 
86 void
addSortTest(TestNode ** root)87 addSortTest(TestNode** root) {
88     addTest(root, &SortTest, "tsutil/sorttest/SortTest");
89 }
90