Imported Upstream version 58.1
[platform/upstream/icu.git] / source / test / cintltst / ccolltst.c
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4  * COPYRIGHT: 
5  * Copyright (c) 1997-2012, International Business Machines Corporation and
6  * others. All Rights Reserved.
7  ********************************************************************/
8 /********************************************************************************
9 *
10 * File CCOLLTST.C
11 *
12 * Modification History:
13 *        Name                     Description            
14 *     Madhu Katragadda               Creation
15 *********************************************************************************
16 */
17 #include <stdio.h>
18
19 #include "unicode/utypes.h"
20
21 #if !UCONFIG_NO_COLLATION
22
23 #include "cintltst.h"
24 #include "ccolltst.h"
25 #include "unicode/ucol.h"
26 #include "unicode/ustring.h"
27 #include "cmemory.h"
28
29 void addCollTest(TestNode** root);
30
31 void addCollTest(TestNode** root)
32 {
33     addCollAPITest(root);
34     addCurrencyCollTest(root);
35 #if !UCONFIG_NO_NORMALIZATION
36     addNormTest(root);
37 #endif
38     addGermanCollTest(root);
39     addSpanishCollTest(root);
40     addFrenchCollTest(root);
41     addKannaCollTest(root);
42     addTurkishCollTest(root);
43     addEnglishCollTest(root);
44     addFinnishCollTest(root);
45     
46     /* WEIVTODO: return tests here */
47     addRuleBasedCollTest(root);
48     addCollIterTest(root);
49     addAllCollTest(root);
50     addMiscCollTest(root);
51 #if !UCONFIG_NO_BREAK_ITERATION && !UCONFIG_NO_FILE_IO
52     addSearchTest(root);
53 #endif
54 }
55
56
57
58 /*Internal functions used*/
59 static char* dumpSk(uint8_t *sourceKey, char *sk) {
60     uint32_t kLen = (uint32_t)strlen((const char *)sourceKey);
61     uint32_t i = 0;
62     
63     *sk = 0;
64     
65     for(i = 0; i<kLen; i++) {
66         sprintf(sk+2*i, "%02X", sourceKey[i]);
67     }
68     return sk;
69 }
70
71 static const char *getCompareResult(UCollationResult result)
72 {
73     if (result == UCOL_LESS)
74     {
75         return "LESS";
76     }
77     else if (result == UCOL_EQUAL)
78     {
79         return "EQUAL";
80     }
81     else if (result == UCOL_GREATER)
82     {
83         return "GREATER";
84     }
85     return "invalid UCollationResult?";
86 }
87
88 void reportCResult( const UChar source[], const UChar target[], 
89                          uint8_t *sourceKey, uint8_t *targetKey,
90                          UCollationResult compareResult,
91                          UCollationResult keyResult,
92                          UCollationResult incResult,
93                          UCollationResult expectedResult )
94 {
95     if (expectedResult < -1 || expectedResult > 1)
96     {
97         log_err("***** invalid call to reportCResult ****\n");
98         return;
99     }
100
101     if (compareResult != expectedResult)
102     {
103         log_err("Compare(%s , %s) returned: %s expected: %s\n", aescstrdup(source,-1), aescstrdup(target,-1),
104             getCompareResult(compareResult), getCompareResult(expectedResult) );
105     }
106
107     if (incResult != expectedResult)
108     {
109         log_err("incCompare(%s , %s) returned: %s expected: %s\n", aescstrdup(source,-1), aescstrdup(target,-1),
110             getCompareResult(incResult), getCompareResult(expectedResult) );
111     }
112
113     if (keyResult != expectedResult)
114     {
115         log_err("KeyCompare(%s , %s) returned: %s expected: %s\n", aescstrdup(source,-1), aescstrdup(target,-1), 
116             getCompareResult(keyResult), getCompareResult(expectedResult) );
117     }
118
119     if (keyResult != compareResult)
120     {
121         log_err("difference between sortkey and compare result for (%s , %s) Keys: %s compare %s\n", aescstrdup(source,-1), aescstrdup(target,-1), 
122             getCompareResult(keyResult), getCompareResult(compareResult));
123     }
124
125     if(keyResult != expectedResult || keyResult != compareResult)
126     {
127         char sk[10000];
128         log_verbose("SortKey1: %s\n", dumpSk(sourceKey, sk));
129         log_verbose("SortKey2: %s\n", dumpSk(targetKey, sk));
130     }
131 }
132
133 #endif /* #if !UCONFIG_NO_COLLATION */