• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2019 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 package ohos.global.icu.dev.test.number;
5 
6 import java.io.BufferedReader;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 
10 import org.junit.Test;
11 
12 import ohos.global.icu.dev.test.TestFmwk;
13 import ohos.global.icu.dev.test.TestUtil;
14 import ohos.global.icu.number.LocalizedNumberFormatter;
15 import ohos.global.icu.number.NumberFormatter;
16 import ohos.global.icu.util.ULocale;
17 
18 
19 /**
20  * @author sffc
21  *
22  */
23 
24 public class NumberPermutationTest extends TestFmwk {
25 
26     static final String[] kSkeletonParts = {
27         // Notation
28         "compact-short",
29         "scientific/+ee/sign-always",
30         null,
31         // Unit
32         "percent",
33         "currency/EUR",
34         "measure-unit/length-furlong",
35         null,
36         // Unit Width
37         "unit-width-narrow",
38         "unit-width-full-name",
39         null,
40         // Precision
41         "precision-integer",
42         ".000",
43         ".##/@@@+",
44         "@@",
45         null,
46         // Rounding Mode
47         "rounding-mode-floor",
48         null,
49         // Integer Width
50         "integer-width/##00",
51         null,
52         // Scale
53         "scale/0.5",
54         null,
55         // Grouping
56         "group-on-aligned",
57         null,
58         // Symbols
59         "latin",
60         null,
61         // Sign Display
62         "sign-accounting-except-zero",
63         null,
64         // Decimal Separator Display
65         "decimal-always",
66         null,
67     };
68 
69     static final double[] kNumbersToTest = {0, 91827.3645, -0.22222};
70 
71     static final ULocale[] kLocales = {
72             new ULocale("es-MX"),
73             new ULocale("zh-TW"),
74             new ULocale("bn-BD")
75     };
76 
77     /**
78      * Test permutations of 3 orthogonal skeleton parts from the list above.
79      * Compare the results against the golden data file:
80      *     numberpermutationtest.txt
81      * To regenerate that file, run C++ intltest with the -G option.
82      */
83     @Test
testPermutations()84     public void testPermutations() throws IOException {
85         boolean quick = getExhaustiveness() <= 5;
86 
87         // Convert kSkeletonParts to a more convenient data structure
88         ArrayList<ArrayList<String>> skeletonParts = new ArrayList<>();
89         ArrayList<String> currentSection = new ArrayList<>();
90         for (int i = 0; i < kSkeletonParts.length; i++) {
91             String skeletonPart = kSkeletonParts[i];
92             if (skeletonPart == null) {
93                 skeletonParts.add(currentSection);
94                 currentSection = new ArrayList<>();
95             } else {
96                 currentSection.add(skeletonPart);
97             }
98         }
99 
100         // Build up the golden data string as we evaluate all permutations
101         ArrayList<String> resultLines = new ArrayList<>();
102         resultLines.add("# © 2019 and later: Unicode, Inc. and others.");
103         resultLines.add("# License & terms of use: http://www.unicode.org/copyright.html");
104         resultLines.add("");
105 
106         // Take combinations of 3 orthogonal options
107         outer:
108         for (int i = 0; i < skeletonParts.size() - 2; i++) {
109             ArrayList<String> skeletons1 = skeletonParts.get(i);
110             for (int j = i + 1; j < skeletonParts.size() - 1; j++) {
111                 ArrayList<String> skeletons2 = skeletonParts.get(j);
112                 for (int k = j + 1; k < skeletonParts.size(); k++) {
113                     ArrayList<String> skeletons3 = skeletonParts.get(k);
114 
115                     // Evaluate all combinations of skeletons for these options
116                     for (String skel1 : skeletons1) {
117                         for (String skel2 : skeletons2) {
118                             for (String skel3 : skeletons3) {
119                                 // Compute the skeleton
120                                 StringBuilder skeletonBuilder = new StringBuilder();
121                                 skeletonBuilder
122                                     .append(skel1)  //
123                                     .append(' ')   //
124                                     .append(skel2)  //
125                                     .append(' ')   //
126                                     .append(skel3);
127                                 String skeleton = skeletonBuilder.toString();
128                                 resultLines.add(skeleton);
129 
130                                 // Check several locales and several numbers in each locale
131                                 for (ULocale locale : kLocales) {
132                                     LocalizedNumberFormatter lnf =
133                                             NumberFormatter.forSkeleton(skeleton).locale(locale);
134                                     resultLines.add("  " + locale.toLanguageTag());
135                                     for (double input : kNumbersToTest) {
136                                         resultLines.add("    " + lnf.format(input).toString());
137                                     }
138                                 }
139 
140                                 resultLines.add("");
141                             }
142                         }
143                     }
144                 }
145 
146                 // Quick mode: test all fields at least once but stop early.
147                 if (quick) {
148                     logln("Quick mode: stopped after " + resultLines.size() + " lines");
149                     break outer;
150                 }
151             }
152         }
153 
154         // Compare it to the golden file
155         String codePage = "UTF-8";
156         BufferedReader f = TestUtil.getDataReader("numberpermutationtest.txt", codePage);
157         int lineNumber = 1;
158         for (String actualLine : resultLines) {
159             String expectedLine = f.readLine();
160             if (expectedLine == null) {
161                 errln("More lines generated than are in the data file!");
162                 break;
163             }
164             assertEquals("Line #" + lineNumber + " differs",  //
165                 expectedLine, actualLine);
166             lineNumber++;
167         }
168         // Quick mode: test all fields at least once but stop early.
169         if (!quick && f.readLine() != null) {
170             errln("Fewer lines generated than are in the data file!");
171         }
172         f.close();
173     }
174 }
175