1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/********************************************************************
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * COPYRIGHT:
385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Copyright (c) 1997-2009, International Business Machines Corporation and
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * others. All Rights Reserved.
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ********************************************************************/
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/********************************************************************************
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* File CCURRTST.C
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Modification History:
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*        Name                     Description
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*     Madhu Katragadda             Ported for C API
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*********************************************************************************
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <stdlib.h>
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h"
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if (!UCONFIG_NO_COLLATION)  /* This is not a formatting test. This is a collation test. */
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/ucol.h"
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/uloc.h"
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cintltst.h"
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "ccurrtst.h"
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "ccolltst.h"
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/ustring.h"
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cmemory.h"
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define ARRAY_LENGTH(array) (sizeof array / sizeof array[0])
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid addCurrencyCollTest(TestNode** root)
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    addTest(root, &currTest, "tscoll/ccurrtst/currTest");
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid currTest()
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* All the currency symbols, in UCA order*/
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    static const UChar currency[][2] =
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x00A4, 0x0000}, /*00A4; L; [14 36, 03, 03]    # [082B.0020.0002] # CURRENCY SIGN*/
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x00A2, 0x0000}, /*00A2; L; [14 38, 03, 03]    # [082C.0020.0002] # CENT SIGN*/
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0xFFE0, 0x0000}, /*FFE0; L; [14 38, 03, 05]    # [082C.0020.0003] # FULLWIDTH CENT SIGN*/
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x0024, 0x0000}, /*0024; L; [14 3A, 03, 03]    # [082D.0020.0002] # DOLLAR SIGN*/
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0xFF04, 0x0000}, /*FF04; L; [14 3A, 03, 05]    # [082D.0020.0003] # FULLWIDTH DOLLAR SIGN*/
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0xFE69, 0x0000}, /*FE69; L; [14 3A, 03, 1D]    # [082D.0020.000F] # SMALL DOLLAR SIGN*/
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x00A3, 0x0000}, /*00A3; L; [14 3C, 03, 03]    # [082E.0020.0002] # POUND SIGN*/
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0xFFE1, 0x0000}, /*FFE1; L; [14 3C, 03, 05]    # [082E.0020.0003] # FULLWIDTH POUND SIGN*/
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x00A5, 0x0000}, /*00A5; L; [14 3E, 03, 03]    # [082F.0020.0002] # YEN SIGN*/
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0xFFE5, 0x0000}, /*FFE5; L; [14 3E, 03, 05]    # [082F.0020.0003] # FULLWIDTH YEN SIGN*/
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x09F2, 0x0000}, /*09F2; L; [14 40, 03, 03]    # [0830.0020.0002] # BENGALI RUPEE MARK*/
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x09F3, 0x0000}, /*09F3; L; [14 42, 03, 03]    # [0831.0020.0002] # BENGALI RUPEE SIGN*/
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x0E3F, 0x0000}, /*0E3F; L; [14 44, 03, 03]    # [0832.0020.0002] # THAI CURRENCY SYMBOL BAHT*/
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x17DB, 0x0000}, /*17DB; L; [14 46, 03, 03]    # [0833.0020.0002] # KHMER CURRENCY SYMBOL RIEL*/
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20A0, 0x0000}, /*20A0; L; [14 48, 03, 03]    # [0834.0020.0002] # EURO-CURRENCY SIGN*/
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20A1, 0x0000}, /*20A1; L; [14 4A, 03, 03]    # [0835.0020.0002] # COLON SIGN*/
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20A2, 0x0000}, /*20A2; L; [14 4C, 03, 03]    # [0836.0020.0002] # CRUZEIRO SIGN*/
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20A3, 0x0000}, /*20A3; L; [14 4E, 03, 03]    # [0837.0020.0002] # FRENCH FRANC SIGN*/
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20A4, 0x0000}, /*20A4; L; [14 50, 03, 03]    # [0838.0020.0002] # LIRA SIGN*/
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20A5, 0x0000}, /*20A5; L; [14 52, 03, 03]    # [0839.0020.0002] # MILL SIGN*/
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20A6, 0x0000}, /*20A6; L; [14 54, 03, 03]    # [083A.0020.0002] # NAIRA SIGN*/
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20A7, 0x0000}, /*20A7; L; [14 56, 03, 03]    # [083B.0020.0002] # PESETA SIGN*/
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20A9, 0x0000}, /*20A9; L; [14 58, 03, 03]    # [083C.0020.0002] # WON SIGN*/
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0xFFE6, 0x0000}, /*FFE6; L; [14 58, 03, 05]    # [083C.0020.0003] # FULLWIDTH WON SIGN*/
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20AA, 0x0000}, /*20AA; L; [14 5A, 03, 03]    # [083D.0020.0002] # NEW SHEQEL SIGN*/
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20AB, 0x0000}, /*20AB; L; [14 5C, 03, 03]    # [083E.0020.0002] # DONG SIGN*/
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20AC, 0x0000}, /*20AC; L; [14 5E, 03, 03]    # [083F.0020.0002] # EURO SIGN*/
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20AD, 0x0000}, /*20AD; L; [14 60, 03, 03]    # [0840.0020.0002] # KIP SIGN*/
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20AE, 0x0000}, /*20AE; L; [14 62, 03, 03]    # [0841.0020.0002] # TUGRIK SIGN*/
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      { 0x20AF, 0x0000}, /*20AF; L; [14 64, 03, 03]    # [0842.0020.0002] # DRACHMA SIGN*/
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    };
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if 0
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* All the currency symbols, in collation order*/
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    static const UChar currency[][2] =
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x00a4, 0x0000}, /* generic currency*/
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x0e3f, 0x0000}, /* baht*/
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x00a2, 0x0000}, /* cent*/
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x20a1, 0x0000}, /* colon*/
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x20a2, 0x0000}, /* cruzeiro*/
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x0024, 0x0000}, /* dollar */
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x20ab, 0x0000}, /* dong */
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x20ac, 0x0000}, /* euro */
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x20a3, 0x0000}, /* franc */
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x20a4, 0x0000}, /* lira */
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x20a5, 0x0000}, /* mill */
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x20a6, 0x0000}, /* naira */
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x20a7, 0x0000}, /* peseta */
94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x00a3, 0x0000}, /* pound */
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x20a8, 0x0000}, /* rupee */
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x20aa, 0x0000}, /* shekel*/
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x20a9, 0x0000}, /* won*/
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        { 0x00a5, 0x0000}  /* yen*/
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    };
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UChar source[2], target[2];
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t i, j, sortklen;
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int res;
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UCollator *c;
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint8_t *sortKey1, *sortKey2;
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UErrorCode status = U_ZERO_ERROR;
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UCollationResult compareResult, keyResult;
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UCollationResult expectedResult = UCOL_EQUAL;
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    log_verbose("Testing currency of all locales\n");
111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    c = ucol_open("en_US", &status);
112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (U_FAILURE(status))
113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
11485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        log_err_status(status, "collator open failed! :%s\n", myErrorName(status));
115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return;
116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /*Compare each currency symbol against all the
119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     currency symbols, including itself*/
120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (i = 0; i < ARRAY_LENGTH(currency); i += 1)
121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for (j = 0; j < ARRAY_LENGTH(currency); j += 1)
123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        {
124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru             u_strcpy(source, currency[i]);
125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru             u_strcpy(target, currency[j]);
126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if (i < j)
128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            {
129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                expectedResult = UCOL_LESS;
130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            else if ( i == j)
132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            {
133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                expectedResult = UCOL_EQUAL;
134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            else
136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            {
137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                expectedResult = UCOL_GREATER;
138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            compareResult = ucol_strcoll(c, source, u_strlen(source), target, u_strlen(target));
141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            status = U_ZERO_ERROR;
143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            sortklen=ucol_getSortKey(c, source, u_strlen(source),  NULL, 0);
145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            sortKey1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            ucol_getSortKey(c, source, u_strlen(source), sortKey1, sortklen+1);
147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            sortklen=ucol_getSortKey(c, target, u_strlen(target),  NULL, 0);
149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            sortKey2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            ucol_getSortKey(c, target, u_strlen(target), sortKey2, sortklen+1);
151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            res = uprv_memcmp(sortKey1, sortKey2, sortklen);
153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if (res < 0) keyResult = (UCollationResult)-1;
154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            else if (res > 0) keyResult = (UCollationResult)1;
155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            else keyResult = (UCollationResult)0;
156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            reportCResult( source, target, sortKey1, sortKey2, compareResult, keyResult, compareResult, expectedResult );
158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            free(sortKey1);
160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            free(sortKey2);
161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ucol_close(c);
165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif /* #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_COLLATION */
168