]> git.saurik.com Git - apple/icu.git/blob - icuSources/tools/ctestfw/tstdtmod.cpp
ICU-62123.0.1.tar.gz
[apple/icu.git] / icuSources / tools / ctestfw / tstdtmod.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 2002-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8
9 /* Created by weiv 05/09/2002 */
10
11 #include <stdarg.h>
12
13 #include "unicode/tstdtmod.h"
14 #include "cmemory.h"
15 #include <stdio.h>
16 #include "cstr.h"
17 #include "cstring.h"
18
19 TestLog::~TestLog() {}
20
21 IcuTestErrorCode::~IcuTestErrorCode() {
22 // Safe because our errlog() does not throw exceptions.
23 if(isFailure()) {
24 errlog(FALSE, nullptr);
25 }
26 }
27
28 UBool IcuTestErrorCode::errIfFailureAndReset() {
29 if(isFailure()) {
30 errlog(FALSE, nullptr);
31 reset();
32 return TRUE;
33 } else {
34 reset();
35 return FALSE;
36 }
37 }
38
39 UBool IcuTestErrorCode::errIfFailureAndReset(const char *fmt, ...) {
40 if(isFailure()) {
41 char buffer[4000];
42 va_list ap;
43 va_start(ap, fmt);
44 vsprintf(buffer, fmt, ap);
45 va_end(ap);
46 errlog(FALSE, buffer);
47 reset();
48 return TRUE;
49 } else {
50 reset();
51 return FALSE;
52 }
53 }
54
55 UBool IcuTestErrorCode::errDataIfFailureAndReset() {
56 if(isFailure()) {
57 errlog(TRUE, nullptr);
58 reset();
59 return TRUE;
60 } else {
61 reset();
62 return FALSE;
63 }
64 }
65
66 UBool IcuTestErrorCode::errDataIfFailureAndReset(const char *fmt, ...) {
67 if(isFailure()) {
68 char buffer[4000];
69 va_list ap;
70 va_start(ap, fmt);
71 vsprintf(buffer, fmt, ap);
72 va_end(ap);
73 errlog(TRUE, buffer);
74 reset();
75 return TRUE;
76 } else {
77 reset();
78 return FALSE;
79 }
80 }
81
82 void IcuTestErrorCode::setScope(const char* message) {
83 scopeMessage.remove().append({ message, -1, US_INV });
84 }
85
86 void IcuTestErrorCode::setScope(const UnicodeString& message) {
87 scopeMessage = message;
88 }
89
90 void IcuTestErrorCode::handleFailure() const {
91 errlog(FALSE, nullptr);
92 }
93
94 void IcuTestErrorCode::errlog(UBool dataErr, const char* extraMessage) const {
95 UnicodeString msg(testName, -1, US_INV);
96 msg.append(u" failure: ").append(UnicodeString(errorName(), -1, US_INV));
97
98 if (!scopeMessage.isEmpty()) {
99 msg.append(u" scope: ").append(scopeMessage);
100 }
101
102 if (extraMessage != nullptr) {
103 msg.append(u" - ").append(UnicodeString(extraMessage, -1, US_INV));
104 }
105
106 if (dataErr || get() == U_MISSING_RESOURCE_ERROR || get() == U_FILE_ACCESS_ERROR) {
107 testClass.dataerrln(msg);
108 } else {
109 testClass.errln(msg);
110 }
111 }
112
113 TestDataModule *TestDataModule::getTestDataModule(const char* name, TestLog& log, UErrorCode &status)
114 {
115 if(U_FAILURE(status)) {
116 return NULL;
117 }
118 TestDataModule *result = NULL;
119
120 // TODO: probe for resource bundle and then for XML.
121 // According to that, construct an appropriate driver object
122
123 result = new RBTestDataModule(name, log, status);
124 if(U_SUCCESS(status)) {
125 return result;
126 } else {
127 delete result;
128 return NULL;
129 }
130 }
131
132 TestDataModule::TestDataModule(const char* name, TestLog& log, UErrorCode& /*status*/)
133 : testName(name),
134 fInfo(NULL),
135 fLog(log)
136 {
137 }
138
139 TestDataModule::~TestDataModule() {
140 if(fInfo != NULL) {
141 delete fInfo;
142 }
143 }
144
145 const char * TestDataModule::getName() const
146 {
147 return testName;
148 }
149
150
151
152 RBTestDataModule::~RBTestDataModule()
153 {
154 ures_close(fTestData);
155 ures_close(fModuleBundle);
156 ures_close(fInfoRB);
157 uprv_free(tdpath);
158 }
159
160 RBTestDataModule::RBTestDataModule(const char* name, TestLog& log, UErrorCode& status)
161 : TestDataModule(name, log, status),
162 fModuleBundle(NULL),
163 fTestData(NULL),
164 fInfoRB(NULL),
165 tdpath(NULL)
166 {
167 fNumberOfTests = 0;
168 fDataTestValid = TRUE;
169 fModuleBundle = getTestBundle(name, status);
170 if(fDataTestValid) {
171 fTestData = ures_getByKey(fModuleBundle, "TestData", NULL, &status);
172 fNumberOfTests = ures_getSize(fTestData);
173 fInfoRB = ures_getByKey(fModuleBundle, "Info", NULL, &status);
174 if(status != U_ZERO_ERROR) {
175 log.errln(UNICODE_STRING_SIMPLE("Unable to initalize test data - missing mandatory description resources!"));
176 fDataTestValid = FALSE;
177 } else {
178 fInfo = new RBDataMap(fInfoRB, status);
179 }
180 }
181 }
182
183 UBool RBTestDataModule::getInfo(const DataMap *& info, UErrorCode &/*status*/) const
184 {
185 info = fInfo;
186 if(fInfo) {
187 return TRUE;
188 } else {
189 return FALSE;
190 }
191 }
192
193 TestData* RBTestDataModule::createTestData(int32_t index, UErrorCode &status) const
194 {
195 TestData *result = NULL;
196 UErrorCode intStatus = U_ZERO_ERROR;
197
198 if(fDataTestValid == TRUE) {
199 // Both of these resources get adopted by a TestData object.
200 UResourceBundle *DataFillIn = ures_getByIndex(fTestData, index, NULL, &status);
201 UResourceBundle *headers = ures_getByKey(fInfoRB, "Headers", NULL, &intStatus);
202
203 if(U_SUCCESS(status)) {
204 result = new RBTestData(DataFillIn, headers, status);
205
206 if(U_SUCCESS(status)) {
207 return result;
208 } else {
209 delete result;
210 }
211 } else {
212 ures_close(DataFillIn);
213 ures_close(headers);
214 }
215 } else {
216 status = U_MISSING_RESOURCE_ERROR;
217 }
218 return NULL;
219 }
220
221 TestData* RBTestDataModule::createTestData(const char* name, UErrorCode &status) const
222 {
223 TestData *result = NULL;
224 UErrorCode intStatus = U_ZERO_ERROR;
225
226 if(fDataTestValid == TRUE) {
227 // Both of these resources get adopted by a TestData object.
228 UResourceBundle *DataFillIn = ures_getByKey(fTestData, name, NULL, &status);
229 UResourceBundle *headers = ures_getByKey(fInfoRB, "Headers", NULL, &intStatus);
230
231 if(U_SUCCESS(status)) {
232 result = new RBTestData(DataFillIn, headers, status);
233 if(U_SUCCESS(status)) {
234 return result;
235 } else {
236 delete result;
237 }
238 } else {
239 ures_close(DataFillIn);
240 ures_close(headers);
241 }
242 } else {
243 status = U_MISSING_RESOURCE_ERROR;
244 }
245 return NULL;
246 }
247
248
249
250 //Get test data from ResourceBundles
251 UResourceBundle*
252 RBTestDataModule::getTestBundle(const char* bundleName, UErrorCode &status)
253 {
254 if(U_SUCCESS(status)) {
255 UResourceBundle *testBundle = NULL;
256 const char* icu_data = fLog.getTestDataPath(status);
257 if (testBundle == NULL) {
258 testBundle = ures_openDirect(icu_data, bundleName, &status);
259 if (status != U_ZERO_ERROR) {
260 fLog.dataerrln(UNICODE_STRING_SIMPLE("Could not load test data from resourcebundle: ") + UnicodeString(bundleName, -1, US_INV));
261 fDataTestValid = FALSE;
262 }
263 }
264 return testBundle;
265 } else {
266 return NULL;
267 }
268 }
269