• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 * Copyright (C) 2014-2015, International Business Machines Corporation and    *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 *
7 * File MEASFMTTEST.CPP
8 *
9 *******************************************************************************
10 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 #include "intltest.h"
15 
16 #if !UCONFIG_NO_FORMATTING
17 
18 #include "unicode/decimfmt.h"
19 #include "unicode/measfmt.h"
20 #include "unicode/measure.h"
21 #include "unicode/measunit.h"
22 #include "unicode/tmunit.h"
23 #include "unicode/plurrule.h"
24 #include "charstr.h"
25 #include "unicode/reldatefmt.h"
26 
27 struct ExpectedResult {
28     const Measure *measures;
29     int32_t count;
30     const char *expected;
31 };
32 
33 class MeasureFormatTest : public IntlTest {
34 public:
MeasureFormatTest()35     MeasureFormatTest() {
36     }
37 
38     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
39 private:
40     void TestBasic();
41     void TestCompatible53_1();
42     void TestCompatible54_1();
43     void TestCompatible55_1();
44     void TestCompatible56_1();
45     void TestGetAvailable();
46     void TestExamplesInDocs();
47     void TestFormatPeriodEn();
48     void Test10219FractionalPlurals();
49     void TestGreek();
50     void TestFormatSingleArg();
51     void TestFormatMeasuresZeroArg();
52     void TestSimplePer();
53     void TestNumeratorPlurals();
54     void TestMultiples();
55     void TestGram();
56     void TestCurrencies();
57     void TestFieldPosition();
58     void TestFieldPositionMultiple();
59     void TestBadArg();
60     void TestEquality();
61     void TestGroupingSeparator();
62     void TestDoubleZero();
63     void TestUnitPerUnitResolution();
64     void TestIndividualPluralFallback();
65     void verifyFormat(
66         const char *description,
67         const MeasureFormat &fmt,
68         const Measure *measures,
69         int32_t measureCount,
70         const char *expected);
71     void verifyFormatWithPrefix(
72         const char *description,
73         const MeasureFormat &fmt,
74         const UnicodeString &prefix,
75         const Measure *measures,
76         int32_t measureCount,
77         const char *expected);
78     void verifyFormat(
79         const char *description,
80         const MeasureFormat &fmt,
81         const ExpectedResult *expectedResults,
82         int32_t count);
83     void helperTestSimplePer(
84         const Locale &locale,
85         UMeasureFormatWidth width,
86         double value,
87         const MeasureUnit &unit,
88         const MeasureUnit &perUnit,
89         const char *expected);
90     void helperTestSimplePer(
91         const Locale &locale,
92         UMeasureFormatWidth width,
93         double value,
94         const MeasureUnit &unit,
95         const MeasureUnit &perUnit,
96         const char *expected,
97         int32_t field,
98         int32_t expected_start,
99         int32_t expected_end);
100     void helperTestMultiples(
101         const Locale &locale,
102         UMeasureFormatWidth width,
103         const char *expected);
104     void verifyFieldPosition(
105         const char *description,
106         const MeasureFormat &fmt,
107         const UnicodeString &prefix,
108         const Measure *measures,
109         int32_t measureCount,
110         NumberFormat::EAlignmentFields field,
111         int32_t start,
112         int32_t end);
113 };
114 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)115 void MeasureFormatTest::runIndexedTest(
116         int32_t index, UBool exec, const char *&name, char *) {
117     if (exec) {
118         logln("TestSuite MeasureFormatTest: ");
119     }
120     TESTCASE_AUTO_BEGIN;
121     TESTCASE_AUTO(TestBasic);
122     TESTCASE_AUTO(TestCompatible53_1);
123     TESTCASE_AUTO(TestCompatible54_1);
124     TESTCASE_AUTO(TestCompatible55_1);
125     TESTCASE_AUTO(TestCompatible56_1);
126     TESTCASE_AUTO(TestGetAvailable);
127     TESTCASE_AUTO(TestExamplesInDocs);
128     TESTCASE_AUTO(TestFormatPeriodEn);
129     TESTCASE_AUTO(Test10219FractionalPlurals);
130     TESTCASE_AUTO(TestGreek);
131     TESTCASE_AUTO(TestFormatSingleArg);
132     TESTCASE_AUTO(TestFormatMeasuresZeroArg);
133     TESTCASE_AUTO(TestSimplePer);
134     TESTCASE_AUTO(TestNumeratorPlurals);
135     TESTCASE_AUTO(TestMultiples);
136     TESTCASE_AUTO(TestGram);
137     TESTCASE_AUTO(TestCurrencies);
138     TESTCASE_AUTO(TestFieldPosition);
139     TESTCASE_AUTO(TestFieldPositionMultiple);
140     TESTCASE_AUTO(TestBadArg);
141     TESTCASE_AUTO(TestEquality);
142     TESTCASE_AUTO(TestGroupingSeparator);
143     TESTCASE_AUTO(TestDoubleZero);
144     TESTCASE_AUTO(TestUnitPerUnitResolution);
145     TESTCASE_AUTO(TestIndividualPluralFallback);
146     TESTCASE_AUTO_END;
147 }
148 
TestCompatible53_1()149 void MeasureFormatTest::TestCompatible53_1() {
150     UErrorCode status = U_ZERO_ERROR;
151     LocalPointer<MeasureUnit> measureUnit;
152     measureUnit.adoptInstead(MeasureUnit::createGForce(status));
153     measureUnit.adoptInstead(MeasureUnit::createArcMinute(status));
154     measureUnit.adoptInstead(MeasureUnit::createArcSecond(status));
155     measureUnit.adoptInstead(MeasureUnit::createDegree(status));
156     measureUnit.adoptInstead(MeasureUnit::createAcre(status));
157     measureUnit.adoptInstead(MeasureUnit::createHectare(status));
158     measureUnit.adoptInstead(MeasureUnit::createSquareFoot(status));
159     measureUnit.adoptInstead(MeasureUnit::createSquareKilometer(status));
160     measureUnit.adoptInstead(MeasureUnit::createSquareMeter(status));
161     measureUnit.adoptInstead(MeasureUnit::createSquareMile(status));
162     measureUnit.adoptInstead(MeasureUnit::createDay(status));
163     measureUnit.adoptInstead(MeasureUnit::createHour(status));
164     measureUnit.adoptInstead(MeasureUnit::createMillisecond(status));
165     measureUnit.adoptInstead(MeasureUnit::createMinute(status));
166     measureUnit.adoptInstead(MeasureUnit::createMonth(status));
167     measureUnit.adoptInstead(MeasureUnit::createSecond(status));
168     measureUnit.adoptInstead(MeasureUnit::createWeek(status));
169     measureUnit.adoptInstead(MeasureUnit::createYear(status));
170     measureUnit.adoptInstead(MeasureUnit::createCentimeter(status));
171     measureUnit.adoptInstead(MeasureUnit::createFoot(status));
172     measureUnit.adoptInstead(MeasureUnit::createInch(status));
173     measureUnit.adoptInstead(MeasureUnit::createKilometer(status));
174     measureUnit.adoptInstead(MeasureUnit::createLightYear(status));
175     measureUnit.adoptInstead(MeasureUnit::createMeter(status));
176     measureUnit.adoptInstead(MeasureUnit::createMile(status));
177     measureUnit.adoptInstead(MeasureUnit::createMillimeter(status));
178     measureUnit.adoptInstead(MeasureUnit::createPicometer(status));
179     measureUnit.adoptInstead(MeasureUnit::createYard(status));
180     measureUnit.adoptInstead(MeasureUnit::createGram(status));
181     measureUnit.adoptInstead(MeasureUnit::createKilogram(status));
182     measureUnit.adoptInstead(MeasureUnit::createOunce(status));
183     measureUnit.adoptInstead(MeasureUnit::createPound(status));
184     measureUnit.adoptInstead(MeasureUnit::createHorsepower(status));
185     measureUnit.adoptInstead(MeasureUnit::createKilowatt(status));
186     measureUnit.adoptInstead(MeasureUnit::createWatt(status));
187     measureUnit.adoptInstead(MeasureUnit::createHectopascal(status));
188     measureUnit.adoptInstead(MeasureUnit::createInchHg(status));
189     measureUnit.adoptInstead(MeasureUnit::createMillibar(status));
190     measureUnit.adoptInstead(MeasureUnit::createKilometerPerHour(status));
191     measureUnit.adoptInstead(MeasureUnit::createMeterPerSecond(status));
192     measureUnit.adoptInstead(MeasureUnit::createMilePerHour(status));
193     measureUnit.adoptInstead(MeasureUnit::createCelsius(status));
194     measureUnit.adoptInstead(MeasureUnit::createFahrenheit(status));
195     measureUnit.adoptInstead(MeasureUnit::createCubicKilometer(status));
196     measureUnit.adoptInstead(MeasureUnit::createCubicMile(status));
197     measureUnit.adoptInstead(MeasureUnit::createLiter(status));
198     assertSuccess("", status);
199 }
200 
TestCompatible54_1()201 void MeasureFormatTest::TestCompatible54_1() {
202     UErrorCode status = U_ZERO_ERROR;
203     LocalPointer<MeasureUnit> measureUnit;
204     measureUnit.adoptInstead(MeasureUnit::createGForce(status));
205     measureUnit.adoptInstead(MeasureUnit::createMeterPerSecondSquared(status));
206     measureUnit.adoptInstead(MeasureUnit::createArcMinute(status));
207     measureUnit.adoptInstead(MeasureUnit::createArcSecond(status));
208     measureUnit.adoptInstead(MeasureUnit::createDegree(status));
209     measureUnit.adoptInstead(MeasureUnit::createRadian(status));
210     measureUnit.adoptInstead(MeasureUnit::createAcre(status));
211     measureUnit.adoptInstead(MeasureUnit::createHectare(status));
212     measureUnit.adoptInstead(MeasureUnit::createSquareCentimeter(status));
213     measureUnit.adoptInstead(MeasureUnit::createSquareFoot(status));
214     measureUnit.adoptInstead(MeasureUnit::createSquareInch(status));
215     measureUnit.adoptInstead(MeasureUnit::createSquareKilometer(status));
216     measureUnit.adoptInstead(MeasureUnit::createSquareMeter(status));
217     measureUnit.adoptInstead(MeasureUnit::createSquareMile(status));
218     measureUnit.adoptInstead(MeasureUnit::createSquareYard(status));
219     measureUnit.adoptInstead(MeasureUnit::createLiterPerKilometer(status));
220     measureUnit.adoptInstead(MeasureUnit::createMilePerGallon(status));
221     measureUnit.adoptInstead(MeasureUnit::createBit(status));
222     measureUnit.adoptInstead(MeasureUnit::createByte(status));
223     measureUnit.adoptInstead(MeasureUnit::createGigabit(status));
224     measureUnit.adoptInstead(MeasureUnit::createGigabyte(status));
225     measureUnit.adoptInstead(MeasureUnit::createKilobit(status));
226     measureUnit.adoptInstead(MeasureUnit::createKilobyte(status));
227     measureUnit.adoptInstead(MeasureUnit::createMegabit(status));
228     measureUnit.adoptInstead(MeasureUnit::createMegabyte(status));
229     measureUnit.adoptInstead(MeasureUnit::createTerabit(status));
230     measureUnit.adoptInstead(MeasureUnit::createTerabyte(status));
231     measureUnit.adoptInstead(MeasureUnit::createDay(status));
232     measureUnit.adoptInstead(MeasureUnit::createHour(status));
233     measureUnit.adoptInstead(MeasureUnit::createMicrosecond(status));
234     measureUnit.adoptInstead(MeasureUnit::createMillisecond(status));
235     measureUnit.adoptInstead(MeasureUnit::createMinute(status));
236     measureUnit.adoptInstead(MeasureUnit::createMonth(status));
237     measureUnit.adoptInstead(MeasureUnit::createNanosecond(status));
238     measureUnit.adoptInstead(MeasureUnit::createSecond(status));
239     measureUnit.adoptInstead(MeasureUnit::createWeek(status));
240     measureUnit.adoptInstead(MeasureUnit::createYear(status));
241     measureUnit.adoptInstead(MeasureUnit::createAmpere(status));
242     measureUnit.adoptInstead(MeasureUnit::createMilliampere(status));
243     measureUnit.adoptInstead(MeasureUnit::createOhm(status));
244     measureUnit.adoptInstead(MeasureUnit::createVolt(status));
245     measureUnit.adoptInstead(MeasureUnit::createCalorie(status));
246     measureUnit.adoptInstead(MeasureUnit::createFoodcalorie(status));
247     measureUnit.adoptInstead(MeasureUnit::createJoule(status));
248     measureUnit.adoptInstead(MeasureUnit::createKilocalorie(status));
249     measureUnit.adoptInstead(MeasureUnit::createKilojoule(status));
250     measureUnit.adoptInstead(MeasureUnit::createKilowattHour(status));
251     measureUnit.adoptInstead(MeasureUnit::createGigahertz(status));
252     measureUnit.adoptInstead(MeasureUnit::createHertz(status));
253     measureUnit.adoptInstead(MeasureUnit::createKilohertz(status));
254     measureUnit.adoptInstead(MeasureUnit::createMegahertz(status));
255     measureUnit.adoptInstead(MeasureUnit::createAstronomicalUnit(status));
256     measureUnit.adoptInstead(MeasureUnit::createCentimeter(status));
257     measureUnit.adoptInstead(MeasureUnit::createDecimeter(status));
258     measureUnit.adoptInstead(MeasureUnit::createFathom(status));
259     measureUnit.adoptInstead(MeasureUnit::createFoot(status));
260     measureUnit.adoptInstead(MeasureUnit::createFurlong(status));
261     measureUnit.adoptInstead(MeasureUnit::createInch(status));
262     measureUnit.adoptInstead(MeasureUnit::createKilometer(status));
263     measureUnit.adoptInstead(MeasureUnit::createLightYear(status));
264     measureUnit.adoptInstead(MeasureUnit::createMeter(status));
265     measureUnit.adoptInstead(MeasureUnit::createMicrometer(status));
266     measureUnit.adoptInstead(MeasureUnit::createMile(status));
267     measureUnit.adoptInstead(MeasureUnit::createMillimeter(status));
268     measureUnit.adoptInstead(MeasureUnit::createNanometer(status));
269     measureUnit.adoptInstead(MeasureUnit::createNauticalMile(status));
270     measureUnit.adoptInstead(MeasureUnit::createParsec(status));
271     measureUnit.adoptInstead(MeasureUnit::createPicometer(status));
272     measureUnit.adoptInstead(MeasureUnit::createYard(status));
273     measureUnit.adoptInstead(MeasureUnit::createLux(status));
274     measureUnit.adoptInstead(MeasureUnit::createCarat(status));
275     measureUnit.adoptInstead(MeasureUnit::createGram(status));
276     measureUnit.adoptInstead(MeasureUnit::createKilogram(status));
277     measureUnit.adoptInstead(MeasureUnit::createMetricTon(status));
278     measureUnit.adoptInstead(MeasureUnit::createMicrogram(status));
279     measureUnit.adoptInstead(MeasureUnit::createMilligram(status));
280     measureUnit.adoptInstead(MeasureUnit::createOunce(status));
281     measureUnit.adoptInstead(MeasureUnit::createOunceTroy(status));
282     measureUnit.adoptInstead(MeasureUnit::createPound(status));
283     measureUnit.adoptInstead(MeasureUnit::createStone(status));
284     measureUnit.adoptInstead(MeasureUnit::createTon(status));
285     measureUnit.adoptInstead(MeasureUnit::createGigawatt(status));
286     measureUnit.adoptInstead(MeasureUnit::createHorsepower(status));
287     measureUnit.adoptInstead(MeasureUnit::createKilowatt(status));
288     measureUnit.adoptInstead(MeasureUnit::createMegawatt(status));
289     measureUnit.adoptInstead(MeasureUnit::createMilliwatt(status));
290     measureUnit.adoptInstead(MeasureUnit::createWatt(status));
291     measureUnit.adoptInstead(MeasureUnit::createHectopascal(status));
292     measureUnit.adoptInstead(MeasureUnit::createInchHg(status));
293     measureUnit.adoptInstead(MeasureUnit::createMillibar(status));
294     measureUnit.adoptInstead(MeasureUnit::createMillimeterOfMercury(status));
295     measureUnit.adoptInstead(MeasureUnit::createPoundPerSquareInch(status));
296     measureUnit.adoptInstead(MeasureUnit::createKarat(status));
297     measureUnit.adoptInstead(MeasureUnit::createKilometerPerHour(status));
298     measureUnit.adoptInstead(MeasureUnit::createMeterPerSecond(status));
299     measureUnit.adoptInstead(MeasureUnit::createMilePerHour(status));
300     measureUnit.adoptInstead(MeasureUnit::createCelsius(status));
301     measureUnit.adoptInstead(MeasureUnit::createFahrenheit(status));
302     measureUnit.adoptInstead(MeasureUnit::createKelvin(status));
303     measureUnit.adoptInstead(MeasureUnit::createAcreFoot(status));
304     measureUnit.adoptInstead(MeasureUnit::createBushel(status));
305     measureUnit.adoptInstead(MeasureUnit::createCentiliter(status));
306     measureUnit.adoptInstead(MeasureUnit::createCubicCentimeter(status));
307     measureUnit.adoptInstead(MeasureUnit::createCubicFoot(status));
308     measureUnit.adoptInstead(MeasureUnit::createCubicInch(status));
309     measureUnit.adoptInstead(MeasureUnit::createCubicKilometer(status));
310     measureUnit.adoptInstead(MeasureUnit::createCubicMeter(status));
311     measureUnit.adoptInstead(MeasureUnit::createCubicMile(status));
312     measureUnit.adoptInstead(MeasureUnit::createCubicYard(status));
313     measureUnit.adoptInstead(MeasureUnit::createCup(status));
314     measureUnit.adoptInstead(MeasureUnit::createDeciliter(status));
315     measureUnit.adoptInstead(MeasureUnit::createFluidOunce(status));
316     measureUnit.adoptInstead(MeasureUnit::createGallon(status));
317     measureUnit.adoptInstead(MeasureUnit::createHectoliter(status));
318     measureUnit.adoptInstead(MeasureUnit::createLiter(status));
319     measureUnit.adoptInstead(MeasureUnit::createMegaliter(status));
320     measureUnit.adoptInstead(MeasureUnit::createMilliliter(status));
321     measureUnit.adoptInstead(MeasureUnit::createPint(status));
322     measureUnit.adoptInstead(MeasureUnit::createQuart(status));
323     measureUnit.adoptInstead(MeasureUnit::createTablespoon(status));
324     measureUnit.adoptInstead(MeasureUnit::createTeaspoon(status));
325     assertSuccess("", status);
326 }
327 
TestCompatible55_1()328 void MeasureFormatTest::TestCompatible55_1() {
329     UErrorCode status = U_ZERO_ERROR;
330     LocalPointer<MeasureUnit> measureUnit;
331     measureUnit.adoptInstead(MeasureUnit::createGForce(status));
332     measureUnit.adoptInstead(MeasureUnit::createMeterPerSecondSquared(status));
333     measureUnit.adoptInstead(MeasureUnit::createArcMinute(status));
334     measureUnit.adoptInstead(MeasureUnit::createArcSecond(status));
335     measureUnit.adoptInstead(MeasureUnit::createDegree(status));
336     measureUnit.adoptInstead(MeasureUnit::createRadian(status));
337     measureUnit.adoptInstead(MeasureUnit::createAcre(status));
338     measureUnit.adoptInstead(MeasureUnit::createHectare(status));
339     measureUnit.adoptInstead(MeasureUnit::createSquareCentimeter(status));
340     measureUnit.adoptInstead(MeasureUnit::createSquareFoot(status));
341     measureUnit.adoptInstead(MeasureUnit::createSquareInch(status));
342     measureUnit.adoptInstead(MeasureUnit::createSquareKilometer(status));
343     measureUnit.adoptInstead(MeasureUnit::createSquareMeter(status));
344     measureUnit.adoptInstead(MeasureUnit::createSquareMile(status));
345     measureUnit.adoptInstead(MeasureUnit::createSquareYard(status));
346     measureUnit.adoptInstead(MeasureUnit::createLiterPerKilometer(status));
347     measureUnit.adoptInstead(MeasureUnit::createMilePerGallon(status));
348     measureUnit.adoptInstead(MeasureUnit::createBit(status));
349     measureUnit.adoptInstead(MeasureUnit::createByte(status));
350     measureUnit.adoptInstead(MeasureUnit::createGigabit(status));
351     measureUnit.adoptInstead(MeasureUnit::createGigabyte(status));
352     measureUnit.adoptInstead(MeasureUnit::createKilobit(status));
353     measureUnit.adoptInstead(MeasureUnit::createKilobyte(status));
354     measureUnit.adoptInstead(MeasureUnit::createMegabit(status));
355     measureUnit.adoptInstead(MeasureUnit::createMegabyte(status));
356     measureUnit.adoptInstead(MeasureUnit::createTerabit(status));
357     measureUnit.adoptInstead(MeasureUnit::createTerabyte(status));
358     measureUnit.adoptInstead(MeasureUnit::createDay(status));
359     measureUnit.adoptInstead(MeasureUnit::createHour(status));
360     measureUnit.adoptInstead(MeasureUnit::createMicrosecond(status));
361     measureUnit.adoptInstead(MeasureUnit::createMillisecond(status));
362     measureUnit.adoptInstead(MeasureUnit::createMinute(status));
363     measureUnit.adoptInstead(MeasureUnit::createMonth(status));
364     measureUnit.adoptInstead(MeasureUnit::createNanosecond(status));
365     measureUnit.adoptInstead(MeasureUnit::createSecond(status));
366     measureUnit.adoptInstead(MeasureUnit::createWeek(status));
367     measureUnit.adoptInstead(MeasureUnit::createYear(status));
368     measureUnit.adoptInstead(MeasureUnit::createAmpere(status));
369     measureUnit.adoptInstead(MeasureUnit::createMilliampere(status));
370     measureUnit.adoptInstead(MeasureUnit::createOhm(status));
371     measureUnit.adoptInstead(MeasureUnit::createVolt(status));
372     measureUnit.adoptInstead(MeasureUnit::createCalorie(status));
373     measureUnit.adoptInstead(MeasureUnit::createFoodcalorie(status));
374     measureUnit.adoptInstead(MeasureUnit::createJoule(status));
375     measureUnit.adoptInstead(MeasureUnit::createKilocalorie(status));
376     measureUnit.adoptInstead(MeasureUnit::createKilojoule(status));
377     measureUnit.adoptInstead(MeasureUnit::createKilowattHour(status));
378     measureUnit.adoptInstead(MeasureUnit::createGigahertz(status));
379     measureUnit.adoptInstead(MeasureUnit::createHertz(status));
380     measureUnit.adoptInstead(MeasureUnit::createKilohertz(status));
381     measureUnit.adoptInstead(MeasureUnit::createMegahertz(status));
382     measureUnit.adoptInstead(MeasureUnit::createAstronomicalUnit(status));
383     measureUnit.adoptInstead(MeasureUnit::createCentimeter(status));
384     measureUnit.adoptInstead(MeasureUnit::createDecimeter(status));
385     measureUnit.adoptInstead(MeasureUnit::createFathom(status));
386     measureUnit.adoptInstead(MeasureUnit::createFoot(status));
387     measureUnit.adoptInstead(MeasureUnit::createFurlong(status));
388     measureUnit.adoptInstead(MeasureUnit::createInch(status));
389     measureUnit.adoptInstead(MeasureUnit::createKilometer(status));
390     measureUnit.adoptInstead(MeasureUnit::createLightYear(status));
391     measureUnit.adoptInstead(MeasureUnit::createMeter(status));
392     measureUnit.adoptInstead(MeasureUnit::createMicrometer(status));
393     measureUnit.adoptInstead(MeasureUnit::createMile(status));
394     measureUnit.adoptInstead(MeasureUnit::createMillimeter(status));
395     measureUnit.adoptInstead(MeasureUnit::createNanometer(status));
396     measureUnit.adoptInstead(MeasureUnit::createNauticalMile(status));
397     measureUnit.adoptInstead(MeasureUnit::createParsec(status));
398     measureUnit.adoptInstead(MeasureUnit::createPicometer(status));
399     measureUnit.adoptInstead(MeasureUnit::createYard(status));
400     measureUnit.adoptInstead(MeasureUnit::createLux(status));
401     measureUnit.adoptInstead(MeasureUnit::createCarat(status));
402     measureUnit.adoptInstead(MeasureUnit::createGram(status));
403     measureUnit.adoptInstead(MeasureUnit::createKilogram(status));
404     measureUnit.adoptInstead(MeasureUnit::createMetricTon(status));
405     measureUnit.adoptInstead(MeasureUnit::createMicrogram(status));
406     measureUnit.adoptInstead(MeasureUnit::createMilligram(status));
407     measureUnit.adoptInstead(MeasureUnit::createOunce(status));
408     measureUnit.adoptInstead(MeasureUnit::createOunceTroy(status));
409     measureUnit.adoptInstead(MeasureUnit::createPound(status));
410     measureUnit.adoptInstead(MeasureUnit::createStone(status));
411     measureUnit.adoptInstead(MeasureUnit::createTon(status));
412     measureUnit.adoptInstead(MeasureUnit::createGigawatt(status));
413     measureUnit.adoptInstead(MeasureUnit::createHorsepower(status));
414     measureUnit.adoptInstead(MeasureUnit::createKilowatt(status));
415     measureUnit.adoptInstead(MeasureUnit::createMegawatt(status));
416     measureUnit.adoptInstead(MeasureUnit::createMilliwatt(status));
417     measureUnit.adoptInstead(MeasureUnit::createWatt(status));
418     measureUnit.adoptInstead(MeasureUnit::createHectopascal(status));
419     measureUnit.adoptInstead(MeasureUnit::createInchHg(status));
420     measureUnit.adoptInstead(MeasureUnit::createMillibar(status));
421     measureUnit.adoptInstead(MeasureUnit::createMillimeterOfMercury(status));
422     measureUnit.adoptInstead(MeasureUnit::createPoundPerSquareInch(status));
423     measureUnit.adoptInstead(MeasureUnit::createKarat(status));
424     measureUnit.adoptInstead(MeasureUnit::createKilometerPerHour(status));
425     measureUnit.adoptInstead(MeasureUnit::createMeterPerSecond(status));
426     measureUnit.adoptInstead(MeasureUnit::createMilePerHour(status));
427     measureUnit.adoptInstead(MeasureUnit::createCelsius(status));
428     measureUnit.adoptInstead(MeasureUnit::createFahrenheit(status));
429     measureUnit.adoptInstead(MeasureUnit::createGenericTemperature(status));
430     measureUnit.adoptInstead(MeasureUnit::createKelvin(status));
431     measureUnit.adoptInstead(MeasureUnit::createAcreFoot(status));
432     measureUnit.adoptInstead(MeasureUnit::createBushel(status));
433     measureUnit.adoptInstead(MeasureUnit::createCentiliter(status));
434     measureUnit.adoptInstead(MeasureUnit::createCubicCentimeter(status));
435     measureUnit.adoptInstead(MeasureUnit::createCubicFoot(status));
436     measureUnit.adoptInstead(MeasureUnit::createCubicInch(status));
437     measureUnit.adoptInstead(MeasureUnit::createCubicKilometer(status));
438     measureUnit.adoptInstead(MeasureUnit::createCubicMeter(status));
439     measureUnit.adoptInstead(MeasureUnit::createCubicMile(status));
440     measureUnit.adoptInstead(MeasureUnit::createCubicYard(status));
441     measureUnit.adoptInstead(MeasureUnit::createCup(status));
442     measureUnit.adoptInstead(MeasureUnit::createDeciliter(status));
443     measureUnit.adoptInstead(MeasureUnit::createFluidOunce(status));
444     measureUnit.adoptInstead(MeasureUnit::createGallon(status));
445     measureUnit.adoptInstead(MeasureUnit::createHectoliter(status));
446     measureUnit.adoptInstead(MeasureUnit::createLiter(status));
447     measureUnit.adoptInstead(MeasureUnit::createMegaliter(status));
448     measureUnit.adoptInstead(MeasureUnit::createMilliliter(status));
449     measureUnit.adoptInstead(MeasureUnit::createPint(status));
450     measureUnit.adoptInstead(MeasureUnit::createQuart(status));
451     measureUnit.adoptInstead(MeasureUnit::createTablespoon(status));
452     measureUnit.adoptInstead(MeasureUnit::createTeaspoon(status));
453     assertSuccess("", status);
454 }
455 
TestCompatible56_1()456 void MeasureFormatTest::TestCompatible56_1() {
457     UErrorCode status = U_ZERO_ERROR;
458     LocalPointer<MeasureUnit> measureUnit;
459     measureUnit.adoptInstead(MeasureUnit::createGForce(status));
460     measureUnit.adoptInstead(MeasureUnit::createMeterPerSecondSquared(status));
461     measureUnit.adoptInstead(MeasureUnit::createArcMinute(status));
462     measureUnit.adoptInstead(MeasureUnit::createArcSecond(status));
463     measureUnit.adoptInstead(MeasureUnit::createDegree(status));
464     measureUnit.adoptInstead(MeasureUnit::createRadian(status));
465     measureUnit.adoptInstead(MeasureUnit::createRevolutionAngle(status));
466     measureUnit.adoptInstead(MeasureUnit::createAcre(status));
467     measureUnit.adoptInstead(MeasureUnit::createHectare(status));
468     measureUnit.adoptInstead(MeasureUnit::createSquareCentimeter(status));
469     measureUnit.adoptInstead(MeasureUnit::createSquareFoot(status));
470     measureUnit.adoptInstead(MeasureUnit::createSquareInch(status));
471     measureUnit.adoptInstead(MeasureUnit::createSquareKilometer(status));
472     measureUnit.adoptInstead(MeasureUnit::createSquareMeter(status));
473     measureUnit.adoptInstead(MeasureUnit::createSquareMile(status));
474     measureUnit.adoptInstead(MeasureUnit::createSquareYard(status));
475     measureUnit.adoptInstead(MeasureUnit::createLiterPer100Kilometers(status));
476     measureUnit.adoptInstead(MeasureUnit::createLiterPerKilometer(status));
477     measureUnit.adoptInstead(MeasureUnit::createMilePerGallon(status));
478     measureUnit.adoptInstead(MeasureUnit::createBit(status));
479     measureUnit.adoptInstead(MeasureUnit::createByte(status));
480     measureUnit.adoptInstead(MeasureUnit::createGigabit(status));
481     measureUnit.adoptInstead(MeasureUnit::createGigabyte(status));
482     measureUnit.adoptInstead(MeasureUnit::createKilobit(status));
483     measureUnit.adoptInstead(MeasureUnit::createKilobyte(status));
484     measureUnit.adoptInstead(MeasureUnit::createMegabit(status));
485     measureUnit.adoptInstead(MeasureUnit::createMegabyte(status));
486     measureUnit.adoptInstead(MeasureUnit::createTerabit(status));
487     measureUnit.adoptInstead(MeasureUnit::createTerabyte(status));
488     measureUnit.adoptInstead(MeasureUnit::createCentury(status));
489     measureUnit.adoptInstead(MeasureUnit::createDay(status));
490     measureUnit.adoptInstead(MeasureUnit::createHour(status));
491     measureUnit.adoptInstead(MeasureUnit::createMicrosecond(status));
492     measureUnit.adoptInstead(MeasureUnit::createMillisecond(status));
493     measureUnit.adoptInstead(MeasureUnit::createMinute(status));
494     measureUnit.adoptInstead(MeasureUnit::createMonth(status));
495     measureUnit.adoptInstead(MeasureUnit::createNanosecond(status));
496     measureUnit.adoptInstead(MeasureUnit::createSecond(status));
497     measureUnit.adoptInstead(MeasureUnit::createWeek(status));
498     measureUnit.adoptInstead(MeasureUnit::createYear(status));
499     measureUnit.adoptInstead(MeasureUnit::createAmpere(status));
500     measureUnit.adoptInstead(MeasureUnit::createMilliampere(status));
501     measureUnit.adoptInstead(MeasureUnit::createOhm(status));
502     measureUnit.adoptInstead(MeasureUnit::createVolt(status));
503     measureUnit.adoptInstead(MeasureUnit::createCalorie(status));
504     measureUnit.adoptInstead(MeasureUnit::createFoodcalorie(status));
505     measureUnit.adoptInstead(MeasureUnit::createJoule(status));
506     measureUnit.adoptInstead(MeasureUnit::createKilocalorie(status));
507     measureUnit.adoptInstead(MeasureUnit::createKilojoule(status));
508     measureUnit.adoptInstead(MeasureUnit::createKilowattHour(status));
509     measureUnit.adoptInstead(MeasureUnit::createGigahertz(status));
510     measureUnit.adoptInstead(MeasureUnit::createHertz(status));
511     measureUnit.adoptInstead(MeasureUnit::createKilohertz(status));
512     measureUnit.adoptInstead(MeasureUnit::createMegahertz(status));
513     measureUnit.adoptInstead(MeasureUnit::createAstronomicalUnit(status));
514     measureUnit.adoptInstead(MeasureUnit::createCentimeter(status));
515     measureUnit.adoptInstead(MeasureUnit::createDecimeter(status));
516     measureUnit.adoptInstead(MeasureUnit::createFathom(status));
517     measureUnit.adoptInstead(MeasureUnit::createFoot(status));
518     measureUnit.adoptInstead(MeasureUnit::createFurlong(status));
519     measureUnit.adoptInstead(MeasureUnit::createInch(status));
520     measureUnit.adoptInstead(MeasureUnit::createKilometer(status));
521     measureUnit.adoptInstead(MeasureUnit::createLightYear(status));
522     measureUnit.adoptInstead(MeasureUnit::createMeter(status));
523     measureUnit.adoptInstead(MeasureUnit::createMicrometer(status));
524     measureUnit.adoptInstead(MeasureUnit::createMile(status));
525     measureUnit.adoptInstead(MeasureUnit::createMileScandinavian(status));
526     measureUnit.adoptInstead(MeasureUnit::createMillimeter(status));
527     measureUnit.adoptInstead(MeasureUnit::createNanometer(status));
528     measureUnit.adoptInstead(MeasureUnit::createNauticalMile(status));
529     measureUnit.adoptInstead(MeasureUnit::createParsec(status));
530     measureUnit.adoptInstead(MeasureUnit::createPicometer(status));
531     measureUnit.adoptInstead(MeasureUnit::createYard(status));
532     measureUnit.adoptInstead(MeasureUnit::createLux(status));
533     measureUnit.adoptInstead(MeasureUnit::createCarat(status));
534     measureUnit.adoptInstead(MeasureUnit::createGram(status));
535     measureUnit.adoptInstead(MeasureUnit::createKilogram(status));
536     measureUnit.adoptInstead(MeasureUnit::createMetricTon(status));
537     measureUnit.adoptInstead(MeasureUnit::createMicrogram(status));
538     measureUnit.adoptInstead(MeasureUnit::createMilligram(status));
539     measureUnit.adoptInstead(MeasureUnit::createOunce(status));
540     measureUnit.adoptInstead(MeasureUnit::createOunceTroy(status));
541     measureUnit.adoptInstead(MeasureUnit::createPound(status));
542     measureUnit.adoptInstead(MeasureUnit::createStone(status));
543     measureUnit.adoptInstead(MeasureUnit::createTon(status));
544     measureUnit.adoptInstead(MeasureUnit::createGigawatt(status));
545     measureUnit.adoptInstead(MeasureUnit::createHorsepower(status));
546     measureUnit.adoptInstead(MeasureUnit::createKilowatt(status));
547     measureUnit.adoptInstead(MeasureUnit::createMegawatt(status));
548     measureUnit.adoptInstead(MeasureUnit::createMilliwatt(status));
549     measureUnit.adoptInstead(MeasureUnit::createWatt(status));
550     measureUnit.adoptInstead(MeasureUnit::createHectopascal(status));
551     measureUnit.adoptInstead(MeasureUnit::createInchHg(status));
552     measureUnit.adoptInstead(MeasureUnit::createMillibar(status));
553     measureUnit.adoptInstead(MeasureUnit::createMillimeterOfMercury(status));
554     measureUnit.adoptInstead(MeasureUnit::createPoundPerSquareInch(status));
555     measureUnit.adoptInstead(MeasureUnit::createKarat(status));
556     measureUnit.adoptInstead(MeasureUnit::createKilometerPerHour(status));
557     measureUnit.adoptInstead(MeasureUnit::createKnot(status));
558     measureUnit.adoptInstead(MeasureUnit::createMeterPerSecond(status));
559     measureUnit.adoptInstead(MeasureUnit::createMilePerHour(status));
560     measureUnit.adoptInstead(MeasureUnit::createCelsius(status));
561     measureUnit.adoptInstead(MeasureUnit::createFahrenheit(status));
562     measureUnit.adoptInstead(MeasureUnit::createGenericTemperature(status));
563     measureUnit.adoptInstead(MeasureUnit::createKelvin(status));
564     measureUnit.adoptInstead(MeasureUnit::createAcreFoot(status));
565     measureUnit.adoptInstead(MeasureUnit::createBushel(status));
566     measureUnit.adoptInstead(MeasureUnit::createCentiliter(status));
567     measureUnit.adoptInstead(MeasureUnit::createCubicCentimeter(status));
568     measureUnit.adoptInstead(MeasureUnit::createCubicFoot(status));
569     measureUnit.adoptInstead(MeasureUnit::createCubicInch(status));
570     measureUnit.adoptInstead(MeasureUnit::createCubicKilometer(status));
571     measureUnit.adoptInstead(MeasureUnit::createCubicMeter(status));
572     measureUnit.adoptInstead(MeasureUnit::createCubicMile(status));
573     measureUnit.adoptInstead(MeasureUnit::createCubicYard(status));
574     measureUnit.adoptInstead(MeasureUnit::createCup(status));
575     measureUnit.adoptInstead(MeasureUnit::createCupMetric(status));
576     measureUnit.adoptInstead(MeasureUnit::createDeciliter(status));
577     measureUnit.adoptInstead(MeasureUnit::createFluidOunce(status));
578     measureUnit.adoptInstead(MeasureUnit::createGallon(status));
579     measureUnit.adoptInstead(MeasureUnit::createHectoliter(status));
580     measureUnit.adoptInstead(MeasureUnit::createLiter(status));
581     measureUnit.adoptInstead(MeasureUnit::createMegaliter(status));
582     measureUnit.adoptInstead(MeasureUnit::createMilliliter(status));
583     measureUnit.adoptInstead(MeasureUnit::createPint(status));
584     measureUnit.adoptInstead(MeasureUnit::createPintMetric(status));
585     measureUnit.adoptInstead(MeasureUnit::createQuart(status));
586     measureUnit.adoptInstead(MeasureUnit::createTablespoon(status));
587     measureUnit.adoptInstead(MeasureUnit::createTeaspoon(status));
588     assertSuccess("", status);
589 }
590 
TestBasic()591 void MeasureFormatTest::TestBasic() {
592     UErrorCode status = U_ZERO_ERROR;
593     MeasureUnit *ptr1 = MeasureUnit::createArcMinute(status);
594     MeasureUnit *ptr2 = MeasureUnit::createArcMinute(status);
595     if (!(*ptr1 == *ptr2)) {
596         errln("Expect == to work.");
597     }
598     if (*ptr1 != *ptr2) {
599         errln("Expect != to work.");
600     }
601     MeasureUnit *ptr3 = MeasureUnit::createMeter(status);
602     if (*ptr1 == *ptr3) {
603         errln("Expect == to work.");
604     }
605     if (!(*ptr1 != *ptr3)) {
606         errln("Expect != to work.");
607     }
608     MeasureUnit *ptr4 = (MeasureUnit *) ptr1->clone();
609     if (*ptr1 != *ptr4) {
610         errln("Expect clone to work.");
611     }
612     MeasureUnit stack;
613     stack = *ptr1;
614     if (*ptr1 != stack) {
615         errln("Expect assignment to work.");
616     }
617 
618     delete ptr1;
619     delete ptr2;
620     delete ptr3;
621     delete ptr4;
622 }
623 
TestGetAvailable()624 void MeasureFormatTest::TestGetAvailable() {
625     MeasureUnit *units = NULL;
626     UErrorCode status = U_ZERO_ERROR;
627     int32_t totalCount = MeasureUnit::getAvailable(units, 0, status);
628     while (status == U_BUFFER_OVERFLOW_ERROR) {
629         status = U_ZERO_ERROR;
630         delete [] units;
631         units = new MeasureUnit[totalCount];
632         totalCount = MeasureUnit::getAvailable(units, totalCount, status);
633     }
634     if (U_FAILURE(status)) {
635         dataerrln("Failure creating format object - %s", u_errorName(status));
636         delete [] units;
637         return;
638     }
639     if (totalCount < 200) {
640         errln("Expect at least 200 measure units including currencies.");
641     }
642     delete [] units;
643     StringEnumeration *types = MeasureUnit::getAvailableTypes(status);
644     if (U_FAILURE(status)) {
645         dataerrln("Failure getting types - %s", u_errorName(status));
646         delete types;
647         return;
648     }
649     if (types->count(status) < 10) {
650         errln("Expect at least 10 distinct unit types.");
651     }
652     units = NULL;
653     int32_t unitCapacity = 0;
654     int32_t unitCountSum = 0;
655     for (
656             const char* type = types->next(NULL, status);
657             type != NULL;
658             type = types->next(NULL, status)) {
659         int32_t unitCount = MeasureUnit::getAvailable(type, units, unitCapacity, status);
660         while (status == U_BUFFER_OVERFLOW_ERROR) {
661             status = U_ZERO_ERROR;
662             delete [] units;
663             units = new MeasureUnit[unitCount];
664             unitCapacity = unitCount;
665             unitCount = MeasureUnit::getAvailable(type, units, unitCapacity, status);
666         }
667         if (U_FAILURE(status)) {
668             dataerrln("Failure getting units - %s", u_errorName(status));
669             delete [] units;
670             delete types;
671             return;
672         }
673         if (unitCount < 1) {
674             errln("Expect at least one unit count per type.");
675         }
676         unitCountSum += unitCount;
677     }
678     if (unitCountSum != totalCount) {
679         errln("Expected total unit count to equal sum of unit counts by type.");
680     }
681     delete [] units;
682     delete types;
683 }
684 
TestExamplesInDocs()685 void MeasureFormatTest::TestExamplesInDocs() {
686     UErrorCode status = U_ZERO_ERROR;
687     MeasureFormat fmtFr(Locale::getFrench(), UMEASFMT_WIDTH_SHORT, status);
688     MeasureFormat fmtFrFull(
689             Locale::getFrench(), UMEASFMT_WIDTH_WIDE, status);
690     MeasureFormat fmtFrNarrow(
691             Locale::getFrench(), UMEASFMT_WIDTH_NARROW, status);
692     MeasureFormat fmtEn(Locale::getUS(), UMEASFMT_WIDTH_WIDE, status);
693     if (!assertSuccess("Error creating formatters", status)) {
694         return;
695     }
696     Measure measureC(23, MeasureUnit::createCelsius(status), status);
697     Measure measureF(70, MeasureUnit::createFahrenheit(status), status);
698     Measure feetAndInches[] = {
699             Measure(70, MeasureUnit::createFoot(status), status),
700             Measure(5.3, MeasureUnit::createInch(status), status)};
701     Measure footAndInch[] = {
702             Measure(1, MeasureUnit::createFoot(status), status),
703             Measure(1, MeasureUnit::createInch(status), status)};
704     Measure inchAndFeet[] = {
705             Measure(1, MeasureUnit::createInch(status), status),
706             Measure(2, MeasureUnit::createFoot(status), status)};
707     if (!assertSuccess("Error creating measurements.", status)) {
708         return;
709     }
710     verifyFormat(
711             "Celsius",
712             fmtFr,
713             &measureC,
714             1,
715             "23 \\u00B0C");
716     verifyFormatWithPrefix(
717             "Celsius",
718             fmtFr,
719             "Prefix: ",
720             &measureC,
721             1,
722             "Prefix: 23 \\u00B0C");
723     verifyFormat(
724             "Fahrenheit",
725             fmtFr,
726             &measureF,
727             1,
728             "70 \\u00B0F");
729     verifyFormat(
730             "Feet and inches",
731             fmtFrFull,
732             feetAndInches,
733             UPRV_LENGTHOF(feetAndInches),
734             "70 pieds et 5,3 pouces");
735     verifyFormatWithPrefix(
736             "Feet and inches",
737             fmtFrFull,
738             "Prefix: ",
739             feetAndInches,
740             UPRV_LENGTHOF(feetAndInches),
741             "Prefix: 70 pieds et 5,3 pouces");
742     verifyFormat(
743             "Foot and inch",
744             fmtFrFull,
745             footAndInch,
746             UPRV_LENGTHOF(footAndInch),
747             "1 pied et 1 pouce");
748     verifyFormat(
749             "Foot and inch narrow",
750             fmtFrNarrow,
751             footAndInch,
752             UPRV_LENGTHOF(footAndInch),
753             "1\\u2032 1\\u2033");
754     verifyFormat(
755             "Inch and feet",
756             fmtEn,
757             inchAndFeet,
758             UPRV_LENGTHOF(inchAndFeet),
759             "1 inch, 2 feet");
760 }
761 
TestFormatPeriodEn()762 void MeasureFormatTest::TestFormatPeriodEn() {
763     UErrorCode status = U_ZERO_ERROR;
764     Measure t_19m[] = {Measure(19, MeasureUnit::createMinute(status), status)};
765     Measure t_1h_23_5s[] = {
766             Measure(1.0, MeasureUnit::createHour(status), status),
767             Measure(23.5, MeasureUnit::createSecond(status), status)
768     };
769     Measure t_1h_23_5m[] = {
770             Measure(1.0, MeasureUnit::createHour(status), status),
771             Measure(23.5, MeasureUnit::createMinute(status), status)
772     };
773     Measure t_1h_0m_23s[] = {
774             Measure(
775                     1.0,
776                     TimeUnit::createInstance(
777                             TimeUnit::UTIMEUNIT_HOUR, status),
778                     status),
779             Measure(
780                     0.0,
781                     TimeUnit::createInstance(
782                             TimeUnit::UTIMEUNIT_MINUTE, status),
783                      status),
784             Measure(
785                     23,
786                     TimeUnit::createInstance(
787                             TimeUnit::UTIMEUNIT_SECOND, status),
788                     status)
789     };
790     Measure t_2y_5M_3w_4d[] = {
791             Measure(2.0, MeasureUnit::createYear(status), status),
792             Measure(5.0, MeasureUnit::createMonth(status), status),
793             Measure(3.0, MeasureUnit::createWeek(status), status),
794             Measure(4.0, MeasureUnit::createDay(status), status)
795     };
796     Measure t_1m_59_9996s[] = {
797             Measure(1.0, MeasureUnit::createMinute(status), status),
798             Measure(59.9996, MeasureUnit::createSecond(status), status)
799     };
800     Measure t_5h_17m[] = {
801             Measure(5.0, MeasureUnit::createHour(status), status),
802             Measure(17.0, MeasureUnit::createMinute(status), status)
803     };
804     Measure t_neg5h_17m[] = {
805             Measure(-5.0, MeasureUnit::createHour(status), status),
806             Measure(17.0, MeasureUnit::createMinute(status), status)
807     };
808     Measure t_19m_28s[] = {
809             Measure(19.0, MeasureUnit::createMinute(status), status),
810             Measure(28.0, MeasureUnit::createSecond(status), status)
811     };
812     Measure t_0h_0m_9s[] = {
813             Measure(0.0, MeasureUnit::createHour(status), status),
814             Measure(0.0, MeasureUnit::createMinute(status), status),
815             Measure(9.0, MeasureUnit::createSecond(status), status)
816     };
817     Measure t_0h_0m_17s[] = {
818             Measure(0.0, MeasureUnit::createHour(status), status),
819             Measure(0.0, MeasureUnit::createMinute(status), status),
820             Measure(17.0, MeasureUnit::createSecond(status), status)
821     };
822     Measure t_6h_56_92m[] = {
823             Measure(6.0, MeasureUnit::createHour(status), status),
824             Measure(56.92, MeasureUnit::createMinute(status), status)
825     };
826     Measure t_3h_4s_5m[] = {
827             Measure(3.0, MeasureUnit::createHour(status), status),
828             Measure(4.0, MeasureUnit::createSecond(status), status),
829             Measure(5.0, MeasureUnit::createMinute(status), status)
830     };
831     Measure t_6_7h_56_92m[] = {
832             Measure(6.7, MeasureUnit::createHour(status), status),
833             Measure(56.92, MeasureUnit::createMinute(status), status)
834     };
835 
836     Measure t_3h_5h[] = {
837             Measure(3.0, MeasureUnit::createHour(status), status),
838             Measure(5.0, MeasureUnit::createHour(status), status)
839     };
840 
841     if (!assertSuccess("Error creating Measure objects", status)) {
842         return;
843     }
844 
845     ExpectedResult fullData[] = {
846             {t_1m_59_9996s, UPRV_LENGTHOF(t_1m_59_9996s), "1 minute, 59.9996 seconds"},
847             {t_19m, UPRV_LENGTHOF(t_19m), "19 minutes"},
848             {t_1h_23_5s, UPRV_LENGTHOF(t_1h_23_5s), "1 hour, 23.5 seconds"},
849             {t_1h_23_5m, UPRV_LENGTHOF(t_1h_23_5m), "1 hour, 23.5 minutes"},
850             {t_1h_0m_23s, UPRV_LENGTHOF(t_1h_0m_23s), "1 hour, 0 minutes, 23 seconds"},
851             {t_2y_5M_3w_4d, UPRV_LENGTHOF(t_2y_5M_3w_4d), "2 years, 5 months, 3 weeks, 4 days"}};
852 
853     ExpectedResult abbrevData[] = {
854             {t_1m_59_9996s, UPRV_LENGTHOF(t_1m_59_9996s), "1 min, 59.9996 sec"},
855             {t_19m, UPRV_LENGTHOF(t_19m), "19 min"},
856             {t_1h_23_5s, UPRV_LENGTHOF(t_1h_23_5s), "1 hr, 23.5 sec"},
857             {t_1h_23_5m, UPRV_LENGTHOF(t_1h_23_5m), "1 hr, 23.5 min"},
858             {t_1h_0m_23s, UPRV_LENGTHOF(t_1h_0m_23s), "1 hr, 0 min, 23 sec"},
859             {t_2y_5M_3w_4d, UPRV_LENGTHOF(t_2y_5M_3w_4d), "2 yrs, 5 mths, 3 wks, 4 days"}};
860 
861     ExpectedResult narrowData[] = {
862             {t_1m_59_9996s, UPRV_LENGTHOF(t_1m_59_9996s), "1m 59.9996s"},
863             {t_19m, UPRV_LENGTHOF(t_19m), "19m"},
864             {t_1h_23_5s, UPRV_LENGTHOF(t_1h_23_5s), "1h 23.5s"},
865             {t_1h_23_5m, UPRV_LENGTHOF(t_1h_23_5m), "1h 23.5m"},
866             {t_1h_0m_23s, UPRV_LENGTHOF(t_1h_0m_23s), "1h 0m 23s"},
867             {t_2y_5M_3w_4d, UPRV_LENGTHOF(t_2y_5M_3w_4d), "2y 5m 3w 4d"}};
868 
869     ExpectedResult numericData[] = {
870             {t_1m_59_9996s, UPRV_LENGTHOF(t_1m_59_9996s), "1:59.9996"},
871             {t_19m, UPRV_LENGTHOF(t_19m), "19m"},
872             {t_1h_23_5s, UPRV_LENGTHOF(t_1h_23_5s), "1:00:23.5"},
873             {t_1h_23_5m, UPRV_LENGTHOF(t_1h_23_5m), "1:23.5"},
874             {t_1h_0m_23s, UPRV_LENGTHOF(t_1h_0m_23s), "1:00:23"},
875             {t_5h_17m, UPRV_LENGTHOF(t_5h_17m), "5:17"},
876             {t_neg5h_17m, UPRV_LENGTHOF(t_neg5h_17m), "-5h 17m"},
877             {t_19m_28s, UPRV_LENGTHOF(t_19m_28s), "19:28"},
878             {t_2y_5M_3w_4d, UPRV_LENGTHOF(t_2y_5M_3w_4d), "2y 5m 3w 4d"},
879             {t_0h_0m_9s, UPRV_LENGTHOF(t_0h_0m_9s), "0:00:09"},
880             {t_6h_56_92m, UPRV_LENGTHOF(t_6h_56_92m), "6:56.92"},
881             {t_6_7h_56_92m, UPRV_LENGTHOF(t_6_7h_56_92m), "6:56.92"},
882             {t_3h_4s_5m, UPRV_LENGTHOF(t_3h_4s_5m), "3h 4s 5m"},
883             {t_3h_5h, UPRV_LENGTHOF(t_3h_5h), "3h 5h"}};
884 
885     ExpectedResult fullDataDe[] = {
886             {t_1m_59_9996s, UPRV_LENGTHOF(t_1m_59_9996s), "1 Minute und 59,9996 Sekunden"},
887             {t_19m, UPRV_LENGTHOF(t_19m), "19 Minuten"},
888             {t_1h_23_5s, UPRV_LENGTHOF(t_1h_23_5s), "1 Stunde und 23,5 Sekunden"},
889             {t_1h_23_5m, UPRV_LENGTHOF(t_1h_23_5m), "1 Stunde und 23,5 Minuten"},
890             {t_1h_0m_23s, UPRV_LENGTHOF(t_1h_0m_23s), "1 Stunde, 0 Minuten und 23 Sekunden"},
891             {t_2y_5M_3w_4d, UPRV_LENGTHOF(t_2y_5M_3w_4d), "2 Jahre, 5 Monate, 3 Wochen und 4 Tage"}};
892 
893     ExpectedResult numericDataDe[] = {
894             {t_1m_59_9996s, UPRV_LENGTHOF(t_1m_59_9996s), "1:59,9996"},
895             {t_19m, UPRV_LENGTHOF(t_19m), "19 Min."},
896             {t_1h_23_5s, UPRV_LENGTHOF(t_1h_23_5s), "1:00:23,5"},
897             {t_1h_23_5m, UPRV_LENGTHOF(t_1h_23_5m), "1:23,5"},
898             {t_1h_0m_23s, UPRV_LENGTHOF(t_1h_0m_23s), "1:00:23"},
899             {t_5h_17m, UPRV_LENGTHOF(t_5h_17m), "5:17"},
900             {t_19m_28s, UPRV_LENGTHOF(t_19m_28s), "19:28"},
901             {t_2y_5M_3w_4d, UPRV_LENGTHOF(t_2y_5M_3w_4d), "2 J, 5 M, 3 W und 4 T"},
902             {t_0h_0m_17s, UPRV_LENGTHOF(t_0h_0m_17s), "0:00:17"},
903             {t_6h_56_92m, UPRV_LENGTHOF(t_6h_56_92m), "6:56,92"},
904             {t_3h_5h, UPRV_LENGTHOF(t_3h_5h), "3 Std., 5 Std."}};
905 
906     Locale en(Locale::getEnglish());
907     LocalPointer<NumberFormat> nf(NumberFormat::createInstance(en, status));
908     if (U_FAILURE(status)) {
909         dataerrln("Error creating number format en object - %s", u_errorName(status));
910         return;
911     }
912     nf->setMaximumFractionDigits(4);
913     MeasureFormat mf(en, UMEASFMT_WIDTH_WIDE, (NumberFormat *) nf->clone(), status);
914     if (!assertSuccess("Error creating measure format en WIDE", status)) {
915         return;
916     }
917     verifyFormat("en WIDE", mf, fullData, UPRV_LENGTHOF(fullData));
918 
919     // exercise copy constructor
920     {
921         MeasureFormat mf2(mf);
922         verifyFormat("en WIDE copy", mf2, fullData, UPRV_LENGTHOF(fullData));
923     }
924     // exercise clone
925     {
926         MeasureFormat *mf3 = (MeasureFormat *) mf.clone();
927         verifyFormat("en WIDE copy", *mf3, fullData, UPRV_LENGTHOF(fullData));
928         delete mf3;
929     }
930     mf = MeasureFormat(en, UMEASFMT_WIDTH_SHORT, (NumberFormat *) nf->clone(), status);
931     if (!assertSuccess("Error creating measure format en SHORT", status)) {
932         return;
933     }
934     verifyFormat("en SHORT", mf, abbrevData, UPRV_LENGTHOF(abbrevData));
935     mf = MeasureFormat(en, UMEASFMT_WIDTH_NARROW, (NumberFormat *) nf->clone(), status);
936     if (!assertSuccess("Error creating measure format en NARROW", status)) {
937         return;
938     }
939     verifyFormat("en NARROW", mf, narrowData, UPRV_LENGTHOF(narrowData));
940     mf = MeasureFormat(en, UMEASFMT_WIDTH_NUMERIC, (NumberFormat *) nf->clone(), status);
941     if (!assertSuccess("Error creating measure format en NUMERIC", status)) {
942         return;
943     }
944     verifyFormat("en NUMERIC", mf, numericData, UPRV_LENGTHOF(numericData));
945 
946     Locale de(Locale::getGerman());
947     nf.adoptInstead(NumberFormat::createInstance(de, status));
948     if (!assertSuccess("Error creating number format de object", status)) {
949         return;
950     }
951     nf->setMaximumFractionDigits(4);
952     mf = MeasureFormat(de, UMEASFMT_WIDTH_WIDE, (NumberFormat *) nf->clone(), status);
953     if (!assertSuccess("Error creating measure format de WIDE", status)) {
954         return;
955     }
956     verifyFormat("de WIDE", mf, fullDataDe, UPRV_LENGTHOF(fullDataDe));
957     mf = MeasureFormat(de, UMEASFMT_WIDTH_NUMERIC, (NumberFormat *) nf->clone(), status);
958     if (!assertSuccess("Error creating measure format de NUMERIC", status)) {
959         return;
960     }
961     verifyFormat("de NUMERIC", mf, numericDataDe, UPRV_LENGTHOF(numericDataDe));
962 }
963 
Test10219FractionalPlurals()964 void MeasureFormatTest::Test10219FractionalPlurals() {
965     Locale en(Locale::getEnglish());
966     double values[] = {1.588, 1.011};
967     const char *expected[2][3] = {
968             {"1 minute", "1.5 minutes", "1.58 minutes"},
969             {"1 minute", "1.0 minutes", "1.01 minutes"}
970     };
971     UErrorCode status = U_ZERO_ERROR;
972     for (int j = 0; j < UPRV_LENGTHOF(values); j++) {
973         for (int i = 0; i < UPRV_LENGTHOF(expected[j]); i++) {
974             DecimalFormat *df =
975                 (DecimalFormat *) NumberFormat::createInstance(en, status);
976             if (U_FAILURE(status)) {
977                 dataerrln("Error creating Number format - %s", u_errorName(status));
978                 return;
979             }
980             df->setRoundingMode(DecimalFormat::kRoundDown);
981             df->setMinimumFractionDigits(i);
982             df->setMaximumFractionDigits(i);
983             MeasureFormat mf(en, UMEASFMT_WIDTH_WIDE, df, status);
984             if (!assertSuccess("Error creating Measure format", status)) {
985                 return;
986             }
987             Measure measure(values[j], MeasureUnit::createMinute(status), status);
988             if (!assertSuccess("Error creating Measure unit", status)) {
989                 return;
990             }
991             verifyFormat("Test10219", mf, &measure, 1, expected[j][i]);
992         }
993     }
994 }
995 
toMeasureUnit(MeasureUnit * adopted)996 static MeasureUnit toMeasureUnit(MeasureUnit *adopted) {
997     MeasureUnit result(*adopted);
998     delete adopted;
999     return result;
1000 }
1001 
TestGreek()1002 void MeasureFormatTest::TestGreek() {
1003     Locale locales[] = {Locale("el_GR"), Locale("el")};
1004     UErrorCode status = U_ZERO_ERROR;
1005     MeasureUnit units[] = {
1006         toMeasureUnit(MeasureUnit::createSecond(status)),
1007         toMeasureUnit(MeasureUnit::createMinute(status)),
1008         toMeasureUnit(MeasureUnit::createHour(status)),
1009         toMeasureUnit(MeasureUnit::createDay(status)),
1010         toMeasureUnit(MeasureUnit::createWeek(status)),
1011         toMeasureUnit(MeasureUnit::createMonth(status)),
1012         toMeasureUnit(MeasureUnit::createYear(status))};
1013     if (!assertSuccess("Error creating Measure units", status)) {
1014         return;
1015     }
1016     UMeasureFormatWidth styles[] = {
1017             UMEASFMT_WIDTH_WIDE,
1018             UMEASFMT_WIDTH_SHORT};
1019     int32_t numbers[] = {1, 7};
1020     const char *expected[] = {
1021         // "el_GR" 1 wide
1022         "1 \\u03B4\\u03B5\\u03C5\\u03C4\\u03B5\\u03C1\\u03CC\\u03BB\\u03B5\\u03C0\\u03C4\\u03BF",
1023         "1 \\u03BB\\u03B5\\u03C0\\u03C4\\u03CC",
1024         "1 \\u03CE\\u03C1\\u03B1",
1025         "1 \\u03B7\\u03BC\\u03AD\\u03C1\\u03B1",
1026         "1 \\u03B5\\u03B2\\u03B4\\u03BF\\u03BC\\u03AC\\u03B4\\u03B1",
1027         "1 \\u03BC\\u03AE\\u03BD\\u03B1\\u03C2",
1028         "1 \\u03AD\\u03C4\\u03BF\\u03C2",
1029         // "el_GR" 1 short
1030         "1 \\u03B4\\u03B5\\u03C5\\u03C4.",
1031         "1 \\u03BB\\u03B5\\u03C0.",
1032         "1 \\u03CE\\u03C1\\u03B1",
1033         "1 \\u03B7\\u03BC\\u03AD\\u03C1\\u03B1",
1034         "1 \\u03B5\\u03B2\\u03B4.",
1035         "1 \\u03BC\\u03AE\\u03BD.",
1036         "1 \\u03AD\\u03C4.",	        // year (one)
1037         // "el_GR" 7 wide
1038         "7 \\u03B4\\u03B5\\u03C5\\u03C4\\u03B5\\u03C1\\u03CC\\u03BB\\u03B5\\u03C0\\u03C4\\u03B1",
1039         "7 \\u03BB\\u03B5\\u03C0\\u03C4\\u03AC",
1040         "7 \\u03CE\\u03C1\\u03B5\\u03C2",
1041         "7 \\u03B7\\u03BC\\u03AD\\u03C1\\u03B5\\u03C2",
1042         "7 \\u03B5\\u03B2\\u03B4\\u03BF\\u03BC\\u03AC\\u03B4\\u03B5\\u03C2",
1043         "7 \\u03BC\\u03AE\\u03BD\\u03B5\\u03C2",
1044         "7 \\u03AD\\u03C4\\u03B7",
1045         // "el_GR" 7 short
1046         "7 \\u03B4\\u03B5\\u03C5\\u03C4.",
1047         "7 \\u03BB\\u03B5\\u03C0.",
1048         "7 \\u03CE\\u03C1.",		    // hour (other)
1049         "7 \\u03B7\\u03BC\\u03AD\\u03C1\\u03B5\\u03C2",
1050         "7 \\u03B5\\u03B2\\u03B4.",
1051         "7 \\u03BC\\u03AE\\u03BD.",
1052         "7 \\u03AD\\u03C4.",            // year (other)
1053 
1054         // "el" 1 wide
1055         "1 \\u03B4\\u03B5\\u03C5\\u03C4\\u03B5\\u03C1\\u03CC\\u03BB\\u03B5\\u03C0\\u03C4\\u03BF",
1056         "1 \\u03BB\\u03B5\\u03C0\\u03C4\\u03CC",
1057         "1 \\u03CE\\u03C1\\u03B1",
1058         "1 \\u03B7\\u03BC\\u03AD\\u03C1\\u03B1",
1059         "1 \\u03B5\\u03B2\\u03B4\\u03BF\\u03BC\\u03AC\\u03B4\\u03B1",
1060         "1 \\u03BC\\u03AE\\u03BD\\u03B1\\u03C2",
1061         "1 \\u03AD\\u03C4\\u03BF\\u03C2",
1062         // "el" 1 short
1063         "1 \\u03B4\\u03B5\\u03C5\\u03C4.",
1064         "1 \\u03BB\\u03B5\\u03C0.",
1065         "1 \\u03CE\\u03C1\\u03B1",
1066         "1 \\u03B7\\u03BC\\u03AD\\u03C1\\u03B1",
1067         "1 \\u03B5\\u03B2\\u03B4.",
1068         "1 \\u03BC\\u03AE\\u03BD.",
1069         "1 \\u03AD\\u03C4.",	        // year (one)
1070         // "el" 7 wide
1071         "7 \\u03B4\\u03B5\\u03C5\\u03C4\\u03B5\\u03C1\\u03CC\\u03BB\\u03B5\\u03C0\\u03C4\\u03B1",
1072         "7 \\u03BB\\u03B5\\u03C0\\u03C4\\u03AC",
1073         "7 \\u03CE\\u03C1\\u03B5\\u03C2",
1074         "7 \\u03B7\\u03BC\\u03AD\\u03C1\\u03B5\\u03C2",
1075         "7 \\u03B5\\u03B2\\u03B4\\u03BF\\u03BC\\u03AC\\u03B4\\u03B5\\u03C2",
1076         "7 \\u03BC\\u03AE\\u03BD\\u03B5\\u03C2",
1077         "7 \\u03AD\\u03C4\\u03B7",
1078         // "el" 7 short
1079         "7 \\u03B4\\u03B5\\u03C5\\u03C4.",
1080         "7 \\u03BB\\u03B5\\u03C0.",
1081         "7 \\u03CE\\u03C1.",		    // hour (other)
1082         "7 \\u03B7\\u03BC\\u03AD\\u03C1\\u03B5\\u03C2",
1083         "7 \\u03B5\\u03B2\\u03B4.",
1084         "7 \\u03BC\\u03AE\\u03BD.",
1085         "7 \\u03AD\\u03C4."};           // year (other)
1086 
1087     int32_t counter = 0;
1088     for (int32_t locIndex = 0; locIndex < UPRV_LENGTHOF(locales); ++locIndex ) {
1089         for( int32_t numIndex = 0; numIndex < UPRV_LENGTHOF(numbers); ++numIndex ) {
1090             for ( int32_t styleIndex = 0; styleIndex < UPRV_LENGTHOF(styles); ++styleIndex ) {
1091                 for ( int32_t unitIndex = 0; unitIndex < UPRV_LENGTHOF(units); ++unitIndex ) {
1092                     Measure measure(numbers[numIndex], new MeasureUnit(units[unitIndex]), status);
1093                     if (!assertSuccess("Error creating Measure", status)) {
1094                         return;
1095                     }
1096                     MeasureFormat fmt(locales[locIndex], styles[styleIndex], status);
1097                     if (!assertSuccess("Error creating Measure format", status)) {
1098                         return;
1099                     }
1100                     verifyFormat("TestGreek", fmt, &measure, 1, expected[counter]);
1101                     ++counter;
1102                 }
1103             }
1104         }
1105     }
1106 }
1107 
TestFormatSingleArg()1108 void MeasureFormatTest::TestFormatSingleArg() {
1109     UErrorCode status = U_ZERO_ERROR;
1110     MeasureFormat fmt("en", UMEASFMT_WIDTH_WIDE, status);
1111     if (!assertSuccess("Error creating formatter", status)) {
1112         return;
1113     }
1114     UnicodeString buffer;
1115     FieldPosition pos(0);
1116     fmt.format(
1117             new Measure(3.5, MeasureUnit::createFoot(status), status),
1118             buffer,
1119             pos,
1120             status);
1121     if (!assertSuccess("Error formatting", status)) {
1122         return;
1123     }
1124     assertEquals(
1125             "TestFormatSingleArg",
1126             UnicodeString("3.5 feet"),
1127             buffer);
1128 }
1129 
TestFormatMeasuresZeroArg()1130 void MeasureFormatTest::TestFormatMeasuresZeroArg() {
1131     UErrorCode status = U_ZERO_ERROR;
1132     MeasureFormat fmt("en", UMEASFMT_WIDTH_WIDE, status);
1133     verifyFormat("TestFormatMeasuresZeroArg", fmt, NULL, 0, "");
1134 }
1135 
TestSimplePer()1136 void MeasureFormatTest::TestSimplePer() {
1137     Locale en("en");
1138     UErrorCode status = U_ZERO_ERROR;
1139     LocalPointer<MeasureUnit> second(MeasureUnit::createSecond(status));
1140     LocalPointer<MeasureUnit> minute(MeasureUnit::createMinute(status));
1141     LocalPointer<MeasureUnit> pound(MeasureUnit::createPound(status));
1142     if (!assertSuccess("", status)) {
1143         return;
1144     }
1145 
1146     helperTestSimplePer(
1147             en, UMEASFMT_WIDTH_WIDE,
1148             1.0, *pound, *second, "1 pound per second");
1149     helperTestSimplePer(
1150             en, UMEASFMT_WIDTH_WIDE,
1151             2.0, *pound, *second, "2 pounds per second");
1152     helperTestSimplePer(
1153             en, UMEASFMT_WIDTH_WIDE,
1154             1.0, *pound, *minute, "1 pound per minute");
1155     helperTestSimplePer(
1156             en, UMEASFMT_WIDTH_WIDE,
1157             2.0, *pound, *minute, "2 pounds per minute");
1158 
1159     helperTestSimplePer(
1160             en, UMEASFMT_WIDTH_SHORT, 1.0, *pound, *second, "1 lb/s");
1161     helperTestSimplePer(
1162             en, UMEASFMT_WIDTH_SHORT, 2.0, *pound, *second, "2 lb/s");
1163     helperTestSimplePer(
1164             en, UMEASFMT_WIDTH_SHORT, 1.0, *pound, *minute, "1 lb/min");
1165     helperTestSimplePer(
1166             en, UMEASFMT_WIDTH_SHORT, 2.0, *pound, *minute, "2 lb/min");
1167 
1168     helperTestSimplePer(
1169             en, UMEASFMT_WIDTH_NARROW, 1.0, *pound, *second, "1#/s");
1170     helperTestSimplePer(
1171             en, UMEASFMT_WIDTH_NARROW, 2.0, *pound, *second, "2#/s");
1172     helperTestSimplePer(
1173             en, UMEASFMT_WIDTH_NARROW, 1.0, *pound, *minute, "1#/min");
1174     helperTestSimplePer(
1175             en, UMEASFMT_WIDTH_NARROW, 2.0, *pound, *minute, "2#/min");
1176 
1177     helperTestSimplePer(
1178             en, UMEASFMT_WIDTH_SHORT,
1179             23.3, *pound, *second, "23.3 lb/s",
1180             NumberFormat::kDecimalSeparatorField,
1181             2, 3);
1182 
1183     helperTestSimplePer(
1184             en, UMEASFMT_WIDTH_SHORT,
1185             23.3, *pound, *second, "23.3 lb/s",
1186             NumberFormat::kIntegerField,
1187             0, 2);
1188 
1189     helperTestSimplePer(
1190             en, UMEASFMT_WIDTH_SHORT,
1191             23.3, *pound, *minute, "23.3 lb/min",
1192             NumberFormat::kDecimalSeparatorField,
1193             2, 3);
1194 
1195     helperTestSimplePer(
1196             en, UMEASFMT_WIDTH_SHORT,
1197             23.3, *pound, *minute, "23.3 lb/min",
1198             NumberFormat::kIntegerField,
1199             0, 2);
1200 }
1201 
TestNumeratorPlurals()1202 void MeasureFormatTest::TestNumeratorPlurals() {
1203     Locale pl("pl");
1204     UErrorCode status = U_ZERO_ERROR;
1205     LocalPointer<MeasureUnit> second(MeasureUnit::createSecond(status));
1206     LocalPointer<MeasureUnit> foot(MeasureUnit::createFoot(status));
1207     if (!assertSuccess("", status)) {
1208         return;
1209     }
1210 
1211     helperTestSimplePer(
1212             pl,
1213             UMEASFMT_WIDTH_WIDE,
1214             1.0, *foot, *second, "1 stopa na sekund\\u0119");
1215     helperTestSimplePer(
1216             pl,
1217             UMEASFMT_WIDTH_WIDE,
1218             2.0, *foot, *second, "2 stopy na sekund\\u0119");
1219     helperTestSimplePer(
1220             pl,
1221             UMEASFMT_WIDTH_WIDE,
1222             5.0, *foot, *second, "5 st\\u00f3p na sekund\\u0119");
1223     helperTestSimplePer(
1224             pl, UMEASFMT_WIDTH_WIDE,
1225             1.5, *foot, *second, "1,5 stopy na sekund\\u0119");
1226 }
1227 
helperTestSimplePer(const Locale & locale,UMeasureFormatWidth width,double value,const MeasureUnit & unit,const MeasureUnit & perUnit,const char * expected)1228 void MeasureFormatTest::helperTestSimplePer(
1229         const Locale &locale,
1230         UMeasureFormatWidth width,
1231         double value,
1232         const MeasureUnit &unit,
1233         const MeasureUnit &perUnit,
1234         const char *expected) {
1235     helperTestSimplePer(
1236             locale,
1237             width,
1238             value,
1239             unit,
1240             perUnit,
1241             expected,
1242             FieldPosition::DONT_CARE,
1243             0,
1244             0);
1245 }
1246 
helperTestSimplePer(const Locale & locale,UMeasureFormatWidth width,double value,const MeasureUnit & unit,const MeasureUnit & perUnit,const char * expected,int32_t field,int32_t expected_start,int32_t expected_end)1247 void MeasureFormatTest::helperTestSimplePer(
1248         const Locale &locale,
1249         UMeasureFormatWidth width,
1250         double value,
1251         const MeasureUnit &unit,
1252         const MeasureUnit &perUnit,
1253         const char *expected,
1254         int32_t field,
1255         int32_t expected_start,
1256         int32_t expected_end) {
1257     UErrorCode status = U_ZERO_ERROR;
1258     FieldPosition pos(field);
1259     MeasureFormat fmt(locale, width, status);
1260     if (!assertSuccess("Error creating format object", status)) {
1261         return;
1262     }
1263     Measure measure(value, (MeasureUnit *) unit.clone(), status);
1264     if (!assertSuccess("Error creating measure object", status)) {
1265         return;
1266     }
1267     UnicodeString prefix("prefix: ");
1268     UnicodeString buffer(prefix);
1269     fmt.formatMeasurePerUnit(
1270             measure,
1271             perUnit,
1272             buffer,
1273             pos,
1274             status);
1275     if (!assertSuccess("Error formatting measures with per", status)) {
1276         return;
1277     }
1278     UnicodeString uexpected(expected);
1279     uexpected = prefix + uexpected;
1280     assertEquals(
1281             "TestSimplePer",
1282             uexpected.unescape(),
1283             buffer);
1284     if (field != FieldPosition::DONT_CARE) {
1285         assertEquals(
1286                 "Start", expected_start, pos.getBeginIndex() - prefix.length());
1287         assertEquals(
1288                 "End", expected_end, pos.getEndIndex() - prefix.length());
1289     }
1290 }
1291 
TestMultiples()1292 void MeasureFormatTest::TestMultiples() {
1293     Locale ru("ru");
1294     Locale en("en");
1295     helperTestMultiples(en, UMEASFMT_WIDTH_WIDE, "2 miles, 1 foot, 2.3 inches");
1296     helperTestMultiples(en, UMEASFMT_WIDTH_SHORT, "2 mi, 1 ft, 2.3 in");
1297     helperTestMultiples(en, UMEASFMT_WIDTH_NARROW, "2mi 1\\u2032 2.3\\u2033");
1298     helperTestMultiples(ru, UMEASFMT_WIDTH_WIDE, "2 \\u043C\\u0438\\u043B\\u0438 1 \\u0444\\u0443\\u0442 \\u0438 2,3 \\u0434\\u044E\\u0439\\u043C\\u0430");
1299     helperTestMultiples(ru, UMEASFMT_WIDTH_SHORT, "2 \\u043C\\u0438\\u043B\\u0438 1 \\u0444\\u0443\\u0442 \\u0438 2,3 \\u0434\\u044E\\u0439\\u043C.");
1300     helperTestMultiples(ru, UMEASFMT_WIDTH_NARROW, "2 \\u043C\\u0438\\u043B\\u044C 1 \\u0444\\u0443\\u0442 2,3 \\u0434\\u044E\\u0439\\u043C\\u0430");
1301 }
1302 
helperTestMultiples(const Locale & locale,UMeasureFormatWidth width,const char * expected)1303 void MeasureFormatTest::helperTestMultiples(
1304         const Locale &locale,
1305         UMeasureFormatWidth width,
1306         const char *expected) {
1307     UErrorCode status = U_ZERO_ERROR;
1308     FieldPosition pos(0);
1309     MeasureFormat fmt(locale, width, status);
1310     if (!assertSuccess("Error creating format object", status)) {
1311         return;
1312     }
1313     Measure measures[] = {
1314             Measure(2, MeasureUnit::createMile(status), status),
1315             Measure(1, MeasureUnit::createFoot(status), status),
1316             Measure(2.3, MeasureUnit::createInch(status), status)};
1317     if (!assertSuccess("Error creating measures", status)) {
1318         return;
1319     }
1320     UnicodeString buffer;
1321     fmt.formatMeasures(measures, UPRV_LENGTHOF(measures), buffer, pos, status);
1322     if (!assertSuccess("Error formatting measures", status)) {
1323         return;
1324     }
1325     assertEquals("TestMultiples", UnicodeString(expected).unescape(), buffer);
1326 }
1327 
TestGram()1328 void MeasureFormatTest::TestGram() {
1329     UErrorCode status = U_ZERO_ERROR;
1330     MeasureFormat fmt("en", UMEASFMT_WIDTH_SHORT, status);
1331     if (!assertSuccess("Error creating format object", status)) {
1332         return;
1333     }
1334     Measure gram(1, MeasureUnit::createGram(status), status);
1335     Measure gforce(1, MeasureUnit::createGForce(status), status);
1336     if (!assertSuccess("Error creating measures", status)) {
1337         return;
1338     }
1339     verifyFormat("TestGram", fmt, &gram, 1, "1 g");
1340     verifyFormat("TestGram", fmt, &gforce, 1, "1 G");
1341 }
1342 
TestCurrencies()1343 void MeasureFormatTest::TestCurrencies() {
1344     UChar USD[] = {'U', 'S', 'D', 0};
1345     UErrorCode status = U_ZERO_ERROR;
1346     CurrencyAmount USD_1(1.0, USD, status);
1347     CurrencyAmount USD_2(2.0, USD, status);
1348     CurrencyAmount USD_NEG_1(-1.0, USD, status);
1349     if (!assertSuccess("Error creating measures", status)) {
1350         return;
1351     }
1352     Locale en("en");
1353     MeasureFormat fmt(en, UMEASFMT_WIDTH_WIDE, status);
1354     if (!assertSuccess("Error creating format object", status)) {
1355         return;
1356     }
1357     verifyFormat("TestCurrenciesWide", fmt, &USD_NEG_1, 1, "-1.00 US dollars");
1358     verifyFormat("TestCurrenciesWide", fmt, &USD_1, 1, "1.00 US dollars");
1359     verifyFormat("TestCurrenciesWide", fmt, &USD_2, 1, "2.00 US dollars");
1360     fmt = MeasureFormat(en, UMEASFMT_WIDTH_SHORT, status);
1361     if (!assertSuccess("Error creating format object", status)) {
1362         return;
1363     }
1364     verifyFormat("TestCurrenciesShort", fmt, &USD_NEG_1, 1, "-USD1.00");
1365     verifyFormat("TestCurrenciesShort", fmt, &USD_1, 1, "USD1.00");
1366     verifyFormat("TestCurrenciesShort", fmt, &USD_2, 1, "USD2.00");
1367     fmt = MeasureFormat(en, UMEASFMT_WIDTH_NARROW, status);
1368     if (!assertSuccess("Error creating format object", status)) {
1369         return;
1370     }
1371     verifyFormat("TestCurrenciesNarrow", fmt, &USD_NEG_1, 1, "-$1.00");
1372     verifyFormat("TestCurrenciesNarrow", fmt, &USD_1, 1, "$1.00");
1373     verifyFormat("TestCurrenciesNarrow", fmt, &USD_2, 1, "$2.00");
1374     fmt = MeasureFormat(en, UMEASFMT_WIDTH_NUMERIC, status);
1375     if (!assertSuccess("Error creating format object", status)) {
1376         return;
1377     }
1378     verifyFormat("TestCurrenciesNumeric", fmt, &USD_NEG_1, 1, "-$1.00");
1379     verifyFormat("TestCurrenciesNumeric", fmt, &USD_1, 1, "$1.00");
1380     verifyFormat("TestCurrenciesNumeric", fmt, &USD_2, 1, "$2.00");
1381 }
1382 
TestFieldPosition()1383 void MeasureFormatTest::TestFieldPosition() {
1384     UErrorCode status = U_ZERO_ERROR;
1385     MeasureFormat fmt("en", UMEASFMT_WIDTH_SHORT, status);
1386     if (!assertSuccess("Error creating format object", status)) {
1387         return;
1388     }
1389     Measure measure(43.5, MeasureUnit::createFoot(status), status);
1390     if (!assertSuccess("Error creating measure object 1", status)) {
1391         return;
1392     }
1393     UnicodeString prefix("123456: ");
1394     verifyFieldPosition(
1395             "",
1396             fmt,
1397             prefix,
1398             &measure,
1399             1,
1400             NumberFormat::kDecimalSeparatorField,
1401             10,
1402             11);
1403     measure = Measure(43, MeasureUnit::createFoot(status), status);
1404     if (!assertSuccess("Error creating measure object 2", status)) {
1405         return;
1406     }
1407     verifyFieldPosition(
1408             "",
1409             fmt,
1410             prefix,
1411             &measure,
1412             1,
1413             NumberFormat::kDecimalSeparatorField,
1414             0,
1415             0);
1416 }
1417 
TestFieldPositionMultiple()1418 void MeasureFormatTest::TestFieldPositionMultiple() {
1419     UErrorCode status = U_ZERO_ERROR;
1420     MeasureFormat fmt("en", UMEASFMT_WIDTH_SHORT, status);
1421     if (!assertSuccess("Error creating format object", status)) {
1422         return;
1423     }
1424     Measure first[] = {
1425             Measure(354, MeasureUnit::createMeter(status), status),
1426             Measure(23, MeasureUnit::createCentimeter(status), status)};
1427     Measure second[] = {
1428             Measure(354, MeasureUnit::createMeter(status), status),
1429             Measure(23, MeasureUnit::createCentimeter(status), status),
1430             Measure(5.4, MeasureUnit::createMillimeter(status), status)};
1431     Measure third[] = {
1432             Measure(3, MeasureUnit::createMeter(status), status),
1433             Measure(23, MeasureUnit::createCentimeter(status), status),
1434             Measure(5, MeasureUnit::createMillimeter(status), status)};
1435     if (!assertSuccess("Error creating measure objects", status)) {
1436         return;
1437     }
1438     UnicodeString prefix("123456: ");
1439     verifyFieldPosition(
1440             "Integer",
1441             fmt,
1442             prefix,
1443             first,
1444             UPRV_LENGTHOF(first),
1445             NumberFormat::kIntegerField,
1446             8,
1447             11);
1448     verifyFieldPosition(
1449             "Decimal separator",
1450             fmt,
1451             prefix,
1452             second,
1453             UPRV_LENGTHOF(second),
1454             NumberFormat::kDecimalSeparatorField,
1455             23,
1456             24);
1457     verifyFieldPosition(
1458             "no decimal separator",
1459             fmt,
1460             prefix,
1461             third,
1462             UPRV_LENGTHOF(third),
1463             NumberFormat::kDecimalSeparatorField,
1464             0,
1465             0);
1466 }
1467 
TestBadArg()1468 void MeasureFormatTest::TestBadArg() {
1469     UErrorCode status = U_ZERO_ERROR;
1470     MeasureFormat fmt("en", UMEASFMT_WIDTH_SHORT, status);
1471     if (!assertSuccess("Error creating format object", status)) {
1472         return;
1473     }
1474     FieldPosition pos(0);
1475     UnicodeString buffer;
1476     fmt.format(
1477             9.3,
1478             buffer,
1479             pos,
1480             status);
1481     if (status != U_ILLEGAL_ARGUMENT_ERROR) {
1482         errln("Expected ILLEGAL_ARGUMENT_ERROR");
1483     }
1484 }
1485 
TestEquality()1486 void MeasureFormatTest::TestEquality() {
1487     UErrorCode status = U_ZERO_ERROR;
1488     NumberFormat* nfeq = NumberFormat::createInstance("en", status);
1489     NumberFormat* nfne = NumberFormat::createInstance("fr", status);
1490     MeasureFormat fmt("en", UMEASFMT_WIDTH_SHORT, status);
1491     MeasureFormat fmtEq2("en", UMEASFMT_WIDTH_SHORT, nfeq, status);
1492     MeasureFormat fmtne1("en", UMEASFMT_WIDTH_WIDE, status);
1493     MeasureFormat fmtne2("fr", UMEASFMT_WIDTH_SHORT, status);
1494     MeasureFormat fmtne3("en", UMEASFMT_WIDTH_SHORT, nfne, status);
1495     if (U_FAILURE(status)) {
1496         dataerrln("Error creating MeasureFormats - %s", u_errorName(status));
1497         return;
1498     }
1499     MeasureFormat fmtEq(fmt);
1500     assertTrue("Equal", fmt == fmtEq);
1501     assertTrue("Equal2", fmt == fmtEq2);
1502     assertFalse("Equal Neg", fmt != fmtEq);
1503     assertTrue("Not Equal 1", fmt != fmtne1);
1504     assertFalse("Not Equal Neg 1", fmt == fmtne1);
1505     assertTrue("Not Equal 2", fmt != fmtne2);
1506     assertTrue("Not Equal 3", fmt != fmtne3);
1507 }
1508 
TestGroupingSeparator()1509 void MeasureFormatTest::TestGroupingSeparator() {
1510     UErrorCode status = U_ZERO_ERROR;
1511     Locale en("en");
1512     MeasureFormat fmt(en, UMEASFMT_WIDTH_SHORT, status);
1513     if (!assertSuccess("Error creating format object", status)) {
1514         return;
1515     }
1516     Measure ms[] = {
1517             Measure((int32_t)INT32_MAX, MeasureUnit::createYear(status), status),
1518             Measure((int32_t)INT32_MIN, MeasureUnit::createMonth(status), status),
1519             Measure(-987, MeasureUnit::createDay(status), status),
1520             Measure(1362, MeasureUnit::createHour(status), status),
1521             Measure(987, MeasureUnit::createMinute(status), status)};
1522     FieldPosition pos(NumberFormat::kGroupingSeparatorField);
1523     UnicodeString appendTo;
1524     fmt.formatMeasures(ms, 5, appendTo, pos, status);
1525     if (!assertSuccess("Error formatting", status)) {
1526         return;
1527     }
1528     assertEquals(
1529             "grouping separator",
1530             "2,147,483,647 yrs, -2,147,483,648 mths, -987 days, 1,362 hr, 987 min",
1531             appendTo);
1532     assertEquals("begin index", 9, pos.getBeginIndex());
1533     assertEquals("end index", 10, pos.getEndIndex());
1534 }
1535 
TestDoubleZero()1536 void MeasureFormatTest::TestDoubleZero() {
1537     UErrorCode status = U_ZERO_ERROR;
1538     Measure measures[] = {
1539         Measure(4.7, MeasureUnit::createHour(status), status),
1540         Measure(23, MeasureUnit::createMinute(status), status),
1541         Measure(16, MeasureUnit::createSecond(status), status)};
1542     Locale en("en");
1543     NumberFormat *nf = NumberFormat::createInstance(en, status);
1544     MeasureFormat fmt("en", UMEASFMT_WIDTH_WIDE, nf, status);
1545     UnicodeString appendTo;
1546     FieldPosition pos(FieldPosition::DONT_CARE);
1547     if (U_FAILURE(status)) {
1548         dataerrln("Error creating formatter - %s", u_errorName(status));
1549         return;
1550     }
1551     nf->setMinimumFractionDigits(2);
1552     nf->setMaximumFractionDigits(2);
1553     fmt.formatMeasures(measures, UPRV_LENGTHOF(measures), appendTo, pos, status);
1554     if (!assertSuccess("Error formatting", status)) {
1555         return;
1556     }
1557     assertEquals(
1558             "TestDoubleZero",
1559             UnicodeString("4 hours, 23 minutes, 16.00 seconds"),
1560             appendTo);
1561     measures[0] = Measure(-4.7, MeasureUnit::createHour(status), status);
1562     appendTo.remove();
1563     fmt.formatMeasures(measures, UPRV_LENGTHOF(measures), appendTo, pos, status);
1564     if (!assertSuccess("Error formatting", status)) {
1565         return;
1566     }
1567     assertEquals(
1568             "TestDoubleZero",
1569             UnicodeString("-4 hours, 23 minutes, 16.00 seconds"),
1570             appendTo);
1571 }
1572 
TestUnitPerUnitResolution()1573 void MeasureFormatTest::TestUnitPerUnitResolution() {
1574     UErrorCode status = U_ZERO_ERROR;
1575     Locale en("en");
1576     MeasureFormat fmt("en", UMEASFMT_WIDTH_SHORT, status);
1577     Measure measure(50, MeasureUnit::createPound(status), status);
1578     LocalPointer<MeasureUnit> sqInch(MeasureUnit::createSquareInch(status));
1579     if (!assertSuccess("Create of format unit and per unit", status)) {
1580         return;
1581     }
1582     FieldPosition pos(0);
1583     UnicodeString actual;
1584     fmt.formatMeasurePerUnit(
1585             measure,
1586             *sqInch,
1587             actual,
1588             pos,
1589             status);
1590     assertEquals("", "50 psi", actual);
1591 }
1592 
TestIndividualPluralFallback()1593 void MeasureFormatTest::TestIndividualPluralFallback() {
1594     // See ticket #11986 "incomplete fallback in MeasureFormat".
1595     // In CLDR 28, fr_CA temperature-generic/short has only the "one" form,
1596     // and falls back to fr for the "other" form.
1597     IcuTestErrorCode errorCode(*this, "TestIndividualPluralFallback");
1598     MeasureFormat mf("fr_CA", UMEASFMT_WIDTH_SHORT, errorCode);
1599     LocalPointer<Measure> twoDeg(
1600         new Measure(2, MeasureUnit::createGenericTemperature(errorCode), errorCode), errorCode);
1601     UnicodeString expected = UNICODE_STRING_SIMPLE("2\\u00B0").unescape();
1602     UnicodeString actual;
1603     assertEquals("2 deg temp in fr_CA", expected, mf.format(twoDeg.orphan(), actual, errorCode));
1604 }
1605 
verifyFieldPosition(const char * description,const MeasureFormat & fmt,const UnicodeString & prefix,const Measure * measures,int32_t measureCount,NumberFormat::EAlignmentFields field,int32_t start,int32_t end)1606 void MeasureFormatTest::verifyFieldPosition(
1607         const char *description,
1608         const MeasureFormat &fmt,
1609         const UnicodeString &prefix,
1610         const Measure *measures,
1611         int32_t measureCount,
1612         NumberFormat::EAlignmentFields field,
1613         int32_t start,
1614         int32_t end) {
1615     // 8 char lead
1616     UnicodeString result(prefix);
1617     FieldPosition pos(field);
1618     UErrorCode status = U_ZERO_ERROR;
1619     CharString ch;
1620     const char *descPrefix = ch.append(description, status)
1621             .append(": ", status).data();
1622     CharString beginIndex;
1623     beginIndex.append(descPrefix, status).append("beginIndex", status);
1624     CharString endIndex;
1625     endIndex.append(descPrefix, status).append("endIndex", status);
1626     fmt.formatMeasures(measures, measureCount, result, pos, status);
1627     if (!assertSuccess("Error formatting", status)) {
1628         return;
1629     }
1630     assertEquals(beginIndex.data(), start, pos.getBeginIndex());
1631     assertEquals(endIndex.data(), end, pos.getEndIndex());
1632 }
1633 
verifyFormat(const char * description,const MeasureFormat & fmt,const Measure * measures,int32_t measureCount,const char * expected)1634 void MeasureFormatTest::verifyFormat(
1635         const char *description,
1636         const MeasureFormat &fmt,
1637         const Measure *measures,
1638         int32_t measureCount,
1639         const char *expected) {
1640     verifyFormatWithPrefix(
1641             description,
1642             fmt,
1643             "",
1644             measures,
1645             measureCount,
1646             expected);
1647 }
1648 
verifyFormatWithPrefix(const char * description,const MeasureFormat & fmt,const UnicodeString & prefix,const Measure * measures,int32_t measureCount,const char * expected)1649 void MeasureFormatTest::verifyFormatWithPrefix(
1650         const char *description,
1651         const MeasureFormat &fmt,
1652         const UnicodeString &prefix,
1653         const Measure *measures,
1654         int32_t measureCount,
1655         const char *expected) {
1656     UnicodeString result(prefix);
1657     FieldPosition pos(0);
1658     UErrorCode status = U_ZERO_ERROR;
1659     fmt.formatMeasures(measures, measureCount, result, pos, status);
1660     if (!assertSuccess("Error formatting", status)) {
1661         return;
1662     }
1663     assertEquals(description, UnicodeString(expected).unescape(), result);
1664 }
1665 
verifyFormat(const char * description,const MeasureFormat & fmt,const ExpectedResult * expectedResults,int32_t count)1666 void MeasureFormatTest::verifyFormat(
1667         const char *description,
1668         const MeasureFormat &fmt,
1669         const ExpectedResult *expectedResults,
1670         int32_t count) {
1671     for (int32_t i = 0; i < count; ++i) {
1672         verifyFormat(description, fmt, expectedResults[i].measures, expectedResults[i].count, expectedResults[i].expected);
1673     }
1674 }
1675 
createMeasureFormatTest()1676 extern IntlTest *createMeasureFormatTest() {
1677     return new MeasureFormatTest();
1678 }
1679 
1680 #endif
1681 
1682