]>
git.saurik.com Git - apple/icu.git/blob - 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 /********************************************************************
5 * Copyright (c) 2002-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
9 /* Created by weiv 05/09/2002 */
13 #include "unicode/tstdtmod.h"
19 TestLog::~TestLog() {}
21 IcuTestErrorCode::~IcuTestErrorCode() {
22 // Safe because our errlog() does not throw exceptions.
24 errlog(FALSE
, nullptr);
28 UBool
IcuTestErrorCode::errIfFailureAndReset() {
30 errlog(FALSE
, nullptr);
39 UBool
IcuTestErrorCode::errIfFailureAndReset(const char *fmt
, ...) {
44 vsprintf(buffer
, fmt
, ap
);
46 errlog(FALSE
, buffer
);
55 UBool
IcuTestErrorCode::errDataIfFailureAndReset() {
57 errlog(TRUE
, nullptr);
66 UBool
IcuTestErrorCode::errDataIfFailureAndReset(const char *fmt
, ...) {
71 vsprintf(buffer
, fmt
, ap
);
82 void IcuTestErrorCode::setScope(const char* message
) {
83 scopeMessage
.remove().append({ message
, -1, US_INV
});
86 void IcuTestErrorCode::setScope(const UnicodeString
& message
) {
87 scopeMessage
= message
;
90 void IcuTestErrorCode::handleFailure() const {
91 errlog(FALSE
, nullptr);
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
));
98 if (!scopeMessage
.isEmpty()) {
99 msg
.append(u
" scope: ").append(scopeMessage
);
102 if (extraMessage
!= nullptr) {
103 msg
.append(u
" - ").append(UnicodeString(extraMessage
, -1, US_INV
));
106 if (dataErr
|| get() == U_MISSING_RESOURCE_ERROR
|| get() == U_FILE_ACCESS_ERROR
) {
107 testClass
.dataerrln(msg
);
109 testClass
.errln(msg
);
113 TestDataModule
*TestDataModule::getTestDataModule(const char* name
, TestLog
& log
, UErrorCode
&status
)
115 if(U_FAILURE(status
)) {
118 TestDataModule
*result
= NULL
;
120 // TODO: probe for resource bundle and then for XML.
121 // According to that, construct an appropriate driver object
123 result
= new RBTestDataModule(name
, log
, status
);
124 if(U_SUCCESS(status
)) {
132 TestDataModule::TestDataModule(const char* name
, TestLog
& log
, UErrorCode
& /*status*/)
139 TestDataModule::~TestDataModule() {
145 const char * TestDataModule::getName() const
152 RBTestDataModule::~RBTestDataModule()
154 ures_close(fTestData
);
155 ures_close(fModuleBundle
);
160 RBTestDataModule::RBTestDataModule(const char* name
, TestLog
& log
, UErrorCode
& status
)
161 : TestDataModule(name
, log
, status
),
168 fDataTestValid
= TRUE
;
169 fModuleBundle
= getTestBundle(name
, status
);
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
;
178 fInfo
= new RBDataMap(fInfoRB
, status
);
183 UBool
RBTestDataModule::getInfo(const DataMap
*& info
, UErrorCode
&/*status*/) const
193 TestData
* RBTestDataModule::createTestData(int32_t index
, UErrorCode
&status
) const
195 TestData
*result
= NULL
;
196 UErrorCode intStatus
= U_ZERO_ERROR
;
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
);
203 if(U_SUCCESS(status
)) {
204 result
= new RBTestData(DataFillIn
, headers
, status
);
206 if(U_SUCCESS(status
)) {
212 ures_close(DataFillIn
);
216 status
= U_MISSING_RESOURCE_ERROR
;
221 TestData
* RBTestDataModule::createTestData(const char* name
, UErrorCode
&status
) const
223 TestData
*result
= NULL
;
224 UErrorCode intStatus
= U_ZERO_ERROR
;
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
);
231 if(U_SUCCESS(status
)) {
232 result
= new RBTestData(DataFillIn
, headers
, status
);
233 if(U_SUCCESS(status
)) {
239 ures_close(DataFillIn
);
243 status
= U_MISSING_RESOURCE_ERROR
;
250 //Get test data from ResourceBundles
252 RBTestDataModule::getTestBundle(const char* bundleName
, UErrorCode
&status
)
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
;