]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/numbertest_permutation.cpp
1 // © 2019 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
12 #include "numbertest.h"
14 #include "unicode/numberformatter.h"
16 void NumberPermutationTest::runIndexedTest(int32_t index
, UBool exec
, const char*& name
, char*) {
18 logln("TestSuite NumberPermutationTest: ");
21 TESTCASE_AUTO(testPermutations
);
25 static const char16_t* kSkeletonParts
[] = {
28 u
"scientific/+ee/sign-always",
33 u
"measure-unit/length-furlong",
37 u
"unit-width-full-name",
46 u
"rounding-mode-floor",
49 u
"integer-width/##00",
61 u
"sign-accounting-except-zero",
63 // Decimal Separator Display
68 static const double kNumbersToTest
[]{0, 91827.3645, -0.22222};
71 * Test permutations of 3 orthogonal skeleton parts from the list above.
72 * Compare the results against the golden data file:
73 * numberpermutationtest.txt
74 * To regenerate that file, run intltest with the -G option.
76 void NumberPermutationTest::testPermutations() {
77 IcuTestErrorCode
status(*this, "testPermutations");
79 const struct LocaleData
{
81 const char16_t* ustring
;
88 // Convert kSkeletonParts to a more convenient data structure
89 auto skeletonParts
= std::vector
<std::vector
<const char16_t*>>();
90 auto currentSection
= std::vector
<const char16_t*>();
91 for (int32_t i
= 0; i
< UPRV_LENGTHOF(kSkeletonParts
); i
++) {
92 const char16_t* skeletonPart
= kSkeletonParts
[i
];
93 if (skeletonPart
== nullptr) {
94 skeletonParts
.push_back(currentSection
);
95 currentSection
.clear();
97 currentSection
.push_back(skeletonPart
);
101 // Build up the golden data string as we evaluate all permutations
102 std::vector
<UnicodeString
> resultLines
;
103 resultLines
.push_back(u
"# © 2019 and later: Unicode, Inc. and others.");
104 resultLines
.push_back(u
"# License & terms of use: http://www.unicode.org/copyright.html");
105 resultLines
.push_back(UnicodeString());
107 // Take combinations of 3 orthogonal options
108 for (size_t i
= 0; i
< skeletonParts
.size() - 2; i
++) {
109 const auto& skeletons1
= skeletonParts
[i
];
110 for (size_t j
= i
+ 1; j
< skeletonParts
.size() - 1; j
++) {
111 const auto& skeletons2
= skeletonParts
[j
];
112 for (size_t k
= j
+ 1; k
< skeletonParts
.size(); k
++) {
113 const auto& skeletons3
= skeletonParts
[k
];
115 // Evaluate all combinations of skeletons for these options
116 for (const auto& skel1
: skeletons1
) {
117 for (const auto& skel2
: skeletons2
) {
118 for (const auto& skel3
: skeletons3
) {
119 // Compute the skeleton
120 UnicodeString skeleton
;
127 resultLines
.push_back(skeleton
);
129 // Check several locales and several numbers in each locale
130 for (const auto& locData
: localesToTest
) {
131 auto lnf
= NumberFormatter::forSkeleton(skeleton
, status
)
132 .locale(locData
.locale
);
133 resultLines
.push_back(UnicodeString(u
" ").append(locData
.ustring
));
134 for (const auto& input
: kNumbersToTest
) {
135 resultLines
.push_back(UnicodeString(u
" ").append(
136 lnf
.formatDouble(input
, status
).toTempString(status
)));
140 resultLines
.push_back(UnicodeString());
146 // Quick mode: test all fields at least once but stop early.
148 infoln(u
"Quick mode: stopped after " + Int64ToUnicodeString(resultLines
.size()) +
157 CharString
goldenFilePath(getSourceTestData(status
), status
);
158 goldenFilePath
.appendPathPart("numberpermutationtest.txt", status
);
160 // Compare it to the golden file
161 const char* codePage
= "UTF-8";
162 LocalUCHARBUFPointer
f(ucbuf_open(goldenFilePath
.data(), &codePage
, TRUE
, FALSE
, status
));
163 if (!assertSuccess("Can't open data file", status
)) {
167 int32_t lineNumber
= 1;
169 for (const auto& actualLine
: resultLines
) {
170 const UChar
* lineBuf
= ucbuf_readline(f
.getAlias(), &lineLength
, status
);
171 if (lineBuf
== nullptr) {
172 errln("More lines generated than are in the data file!");
175 UnicodeString
expectedLine(lineBuf
, lineLength
- 1);
176 assertEquals(u
"Line #" + Int64ToUnicodeString(lineNumber
) + u
" differs", //
177 expectedLine
, actualLine
);
180 // Quick mode: test all fields at least once but stop early.
181 if (!quick
&& ucbuf_readline(f
.getAlias(), &lineLength
, status
) != nullptr) {
182 errln("Fewer lines generated than are in the data file!");
185 // Overwrite the golden data if requested
186 if (write_golden_data
) {
187 std::ofstream outFile
;
188 outFile
.open(goldenFilePath
.data());
189 for (const auto& uniLine
: resultLines
) {
190 std::string byteLine
;
191 uniLine
.toUTF8String(byteLine
);
192 outFile
<< byteLine
<< std::endl
;
198 #endif /* #if !UCONFIG_NO_FORMATTING */