1// © 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-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8
9/***********************************************************************
10* Modification history
11* Date        Name        Description
12* 02/14/2001  synwee      Added attributes in TestTertiary and
13*                         TestSecondary
14***********************************************************************/
15
16#include "unicode/utypes.h"
17
18#if !UCONFIG_NO_COLLATION
19
20#include "unicode/coll.h"
21#include "unicode/tblcoll.h"
22#include "unicode/unistr.h"
23#include "unicode/sortkey.h"
24#include "cmemory.h"
25#include "frcoll.h"
26
27#include "sfwdchit.h"
28
29CollationFrenchTest::CollationFrenchTest()
30: myCollation(0)
31{
32    UErrorCode status = U_ZERO_ERROR;
33    myCollation = Collator::createInstance(Locale::getCanadaFrench(), status);
34    if(!myCollation || U_FAILURE(status)) {
35        errcheckln(status, __FILE__ "failed to create! err " + UnicodeString(u_errorName(status)));
36        /* if it wasn't already: */
37        delete myCollation;
38        myCollation = NULL;
39    }
40}
41
42CollationFrenchTest::~CollationFrenchTest()
43{
44    delete myCollation;
45}
46
47const UChar CollationFrenchTest::testSourceCases[][CollationFrenchTest::MAX_TOKEN_LEN] =
48{
49    {0x0061/*'a'*/, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},
50    {0x0043/*'C'*/, 0x004f/*'O'*/, 0x0054/*'T'*/, 0x0045/*'E'*/, 0x0000},
51    {0x0063/*'c'*/, 0x006f/*'o'*/, 0x002d/*'-'*/, 0x006f/*'o'*/, 0x0070/*'p'*/, 0x0000},
52    {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0000},
53    {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
54    {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
55    {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
56    {0x0048/*'H'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x006f/*'o'*/, 0x0000},
57    {0x01f1, 0x0000},
58    {0xfb00, 0x0000},
59    {0x01fa, 0x0000},
60    {0x0101, 0x0000}
61};
62
63const UChar CollationFrenchTest::testTargetCases[][CollationFrenchTest::MAX_TOKEN_LEN] =
64{
65    {0x0041/*'A'*/, 0x0042/*'B'*/, 0x0043/*'C'*/, 0x0000},
66    {0x0063/*'c'*/, 0x00f4, 0x0074/*'t'*/, 0x0065/*'e'*/, 0x0000},
67    {0x0043/*'C'*/, 0x004f/*'O'*/, 0x004f/*'O'*/, 0x0050/*'P'*/, 0x0000},
68    {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9, 0x0000},
69    {0x0070/*'p'*/,  0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9, 0x0000},
70    {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0000},
71    {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
72    {0x0068/*'h'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x004f/*'O'*/, 0x0000},
73    {0x01ee, 0x0000},
74    {0x25ca, 0x0000},
75    {0x00e0, 0x0000},
76    {0x01df, 0x0000}
77};
78
79const Collator::EComparisonResult CollationFrenchTest::results[] =
80{
81    Collator::LESS,
82    Collator::LESS,
83    Collator::LESS, /*Collator::GREATER,*/
84    Collator::LESS,
85    Collator::GREATER,
86    Collator::GREATER,
87    Collator::LESS,
88    Collator::GREATER,
89    Collator::LESS, /*Collator::GREATER,*/
90    Collator::GREATER,
91    Collator::LESS,
92    Collator::LESS
93};
94
95// 0x0300 is grave, 0x0301 is acute
96// the order of elements in this array must be different than the order in CollationEnglishTest
97const UChar CollationFrenchTest::testAcute[][CollationFrenchTest::MAX_TOKEN_LEN] =
98{
99/*00*/    {0x0065/*'e'*/, 0x0065/*'e'*/,  0x0000},
100/*01*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/,  0x0000},
101/*02*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/,  0x0000},
102/*03*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/,  0x0000},
103/*04*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/,  0x0000},
104/*05*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301, 0x0000},
105/*06*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0000},
106/*07*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0000},
107/*08*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0000},
108/*09*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0000},
109/*0a*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
110/*0b*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
111/*0c*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
112/*0d*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
113/*0e*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
114/*0f*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300, 0x0000},
115/*10*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0000},
116/*11*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0000},
117/*12*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0000},
118/*13*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0000},
119/*14*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
120/*15*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
121/*16*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
122/*17*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
123/*18*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000}
124};
125
126const UChar CollationFrenchTest::testBugs[][CollationFrenchTest::MAX_TOKEN_LEN] =
127{
128    {0x0061/*'a'*/, 0x000},
129    {0x0041/*'A'*/, 0x000},
130    {0x0065/*'e'*/, 0x000},
131    {0x0045/*'E'*/, 0x000},
132    {0x00e9, 0x000},
133    {0x00e8, 0x000},
134    {0x00ea, 0x000},
135    {0x00eb, 0x000},
136    {0x0065/*'e'*/, 0x0061/*'a'*/, 0x000},
137    {0x0078/*'x'*/, 0x000}
138};
139
140void CollationFrenchTest::TestTertiary(/* char* par */)
141{
142    int32_t i = 0;
143    UErrorCode status = U_ZERO_ERROR;
144    myCollation->setStrength(Collator::TERTIARY);
145    myCollation->setAttribute(UCOL_FRENCH_COLLATION, UCOL_ON, status);
146    myCollation->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, status);
147    if (U_FAILURE(status)) {
148        errln("Error setting attribute in French collator");
149    }
150    else
151    {
152        for (i = 0; i < 12 ; i++)
153        {
154            doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
155        }
156    }
157}
158
159void CollationFrenchTest::TestSecondary(/* char* par */)
160{
161    //test acute and grave ordering
162    int32_t i = 0;
163    int32_t j;
164    Collator::EComparisonResult expected;
165    UErrorCode status = U_ZERO_ERROR;
166    //myCollation->setAttribute(UCOL_FRENCH_COLLATION, UCOL_ON, status);
167    myCollation->setStrength(Collator::SECONDARY);
168    if (U_FAILURE(status))
169        errln("Error setting attribute in French collator");
170    else
171    {
172        const int32_t testAcuteSize = UPRV_LENGTHOF(testAcute);
173        for (i = 0; i < testAcuteSize; i++)
174        {
175            for (j = 0; j < testAcuteSize; j++)
176            {
177                if (i <  j)
178                    expected = Collator::LESS;
179                else if (i == j)
180                    expected = Collator::EQUAL;
181                else // (i >  j)
182                    expected = Collator::GREATER;
183                doTest(myCollation, testAcute[i], testAcute[j], expected );
184            }
185        }
186    }
187}
188
189void CollationFrenchTest::TestExtra(/* char* par */)
190{
191    int32_t i, j;
192    myCollation->setStrength(Collator::TERTIARY);
193    for (i = 0; i < 9 ; i++)
194    {
195        for (j = i + 1; j < 10; j += 1)
196        {
197            doTest(myCollation, testBugs[i], testBugs[j], Collator::LESS);
198        }
199    }
200}
201
202void CollationFrenchTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
203{
204    if (exec) logln("TestSuite CollationFrenchTest: ");
205
206    if((!myCollation) && exec) {
207        dataerrln(__FILE__ " cannot test - failed to create collator.");
208        name = "some test";
209        return;
210    }
211
212    switch (index) {
213        case 0: name = "TestSecondary"; if (exec)   TestSecondary(/* par */); break;
214        case 1: name = "TestTertiary";  if (exec)   TestTertiary(/* par */); break;
215        case 2: name = "TestExtra";     if (exec)   TestExtra(/* par */); break;
216        default: name = ""; break;
217    }
218}
219
220#endif /* #if !UCONFIG_NO_COLLATION */
221