]>
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
, u
"destructor: expected success", nullptr);
28 UBool
IcuTestErrorCode::errIfFailureAndReset() {
30 errlog(FALSE
, u
"expected success", nullptr);
39 UBool
IcuTestErrorCode::errIfFailureAndReset(const char *fmt
, ...) {
44 vsprintf(buffer
, fmt
, ap
);
46 errlog(FALSE
, u
"expected success", buffer
);
55 UBool
IcuTestErrorCode::errDataIfFailureAndReset() {
57 errlog(TRUE
, u
"data: expected success", nullptr);
66 UBool
IcuTestErrorCode::errDataIfFailureAndReset(const char *fmt
, ...) {
71 vsprintf(buffer
, fmt
, ap
);
73 errlog(TRUE
, u
"data: expected success", buffer
);
82 UBool
IcuTestErrorCode::expectErrorAndReset(UErrorCode expectedError
) {
83 if(get() != expectedError
) {
84 errlog(FALSE
, UnicodeString(u
"expected: ") + u_errorName(expectedError
), nullptr);
86 UBool retval
= isFailure();
91 UBool
IcuTestErrorCode::expectErrorAndReset(UErrorCode expectedError
, const char *fmt
, ...) {
92 if(get() != expectedError
) {
96 vsprintf(buffer
, fmt
, ap
);
98 errlog(FALSE
, UnicodeString(u
"expected: ") + u_errorName(expectedError
), buffer
);
100 UBool retval
= isFailure();
105 void IcuTestErrorCode::setScope(const char* message
) {
106 scopeMessage
.remove().append({ message
, -1, US_INV
});
109 void IcuTestErrorCode::setScope(const UnicodeString
& message
) {
110 scopeMessage
= message
;
113 void IcuTestErrorCode::handleFailure() const {
114 errlog(FALSE
, u
"(handleFailure)", nullptr);
117 void IcuTestErrorCode::errlog(UBool dataErr
, const UnicodeString
& mainMessage
, const char* extraMessage
) const {
118 UnicodeString
msg(testName
, -1, US_INV
);
119 msg
.append(u
' ').append(mainMessage
);
120 msg
.append(u
" but got error: ").append(UnicodeString(errorName(), -1, US_INV
));
122 if (!scopeMessage
.isEmpty()) {
123 msg
.append(u
" scope: ").append(scopeMessage
);
126 if (extraMessage
!= nullptr) {
127 msg
.append(u
" - ").append(UnicodeString(extraMessage
, -1, US_INV
));
130 if (dataErr
|| get() == U_MISSING_RESOURCE_ERROR
|| get() == U_FILE_ACCESS_ERROR
) {
131 testClass
.dataerrln(msg
);
133 testClass
.errln(msg
);
137 TestDataModule
*TestDataModule::getTestDataModule(const char* name
, TestLog
& log
, UErrorCode
&status
)
139 if(U_FAILURE(status
)) {
142 TestDataModule
*result
= NULL
;
144 // TODO: probe for resource bundle and then for XML.
145 // According to that, construct an appropriate driver object
147 result
= new RBTestDataModule(name
, log
, status
);
148 if(U_SUCCESS(status
)) {
156 TestDataModule::TestDataModule(const char* name
, TestLog
& log
, UErrorCode
& /*status*/)
163 TestDataModule::~TestDataModule() {
169 const char * TestDataModule::getName() const
176 RBTestDataModule::~RBTestDataModule()
178 ures_close(fTestData
);
179 ures_close(fModuleBundle
);
184 RBTestDataModule::RBTestDataModule(const char* name
, TestLog
& log
, UErrorCode
& status
)
185 : TestDataModule(name
, log
, status
),
192 fDataTestValid
= TRUE
;
193 fModuleBundle
= getTestBundle(name
, status
);
195 fTestData
= ures_getByKey(fModuleBundle
, "TestData", NULL
, &status
);
196 fNumberOfTests
= ures_getSize(fTestData
);
197 fInfoRB
= ures_getByKey(fModuleBundle
, "Info", NULL
, &status
);
198 if(status
!= U_ZERO_ERROR
) {
199 log
.errln(UNICODE_STRING_SIMPLE("Unable to initalize test data - missing mandatory description resources!"));
200 fDataTestValid
= FALSE
;
202 fInfo
= new RBDataMap(fInfoRB
, status
);
207 UBool
RBTestDataModule::getInfo(const DataMap
*& info
, UErrorCode
&/*status*/) const
217 TestData
* RBTestDataModule::createTestData(int32_t index
, UErrorCode
&status
) const
219 TestData
*result
= NULL
;
220 UErrorCode intStatus
= U_ZERO_ERROR
;
222 if(fDataTestValid
== TRUE
) {
223 // Both of these resources get adopted by a TestData object.
224 UResourceBundle
*DataFillIn
= ures_getByIndex(fTestData
, index
, NULL
, &status
);
225 UResourceBundle
*headers
= ures_getByKey(fInfoRB
, "Headers", NULL
, &intStatus
);
227 if(U_SUCCESS(status
)) {
228 result
= new RBTestData(DataFillIn
, headers
, status
);
230 if(U_SUCCESS(status
)) {
236 ures_close(DataFillIn
);
240 status
= U_MISSING_RESOURCE_ERROR
;
245 TestData
* RBTestDataModule::createTestData(const char* name
, UErrorCode
&status
) const
247 TestData
*result
= NULL
;
248 UErrorCode intStatus
= U_ZERO_ERROR
;
250 if(fDataTestValid
== TRUE
) {
251 // Both of these resources get adopted by a TestData object.
252 UResourceBundle
*DataFillIn
= ures_getByKey(fTestData
, name
, NULL
, &status
);
253 UResourceBundle
*headers
= ures_getByKey(fInfoRB
, "Headers", NULL
, &intStatus
);
255 if(U_SUCCESS(status
)) {
256 result
= new RBTestData(DataFillIn
, headers
, status
);
257 if(U_SUCCESS(status
)) {
263 ures_close(DataFillIn
);
267 status
= U_MISSING_RESOURCE_ERROR
;
274 //Get test data from ResourceBundles
276 RBTestDataModule::getTestBundle(const char* bundleName
, UErrorCode
&status
)
278 if(U_SUCCESS(status
)) {
279 UResourceBundle
*testBundle
= NULL
;
280 const char* icu_data
= fLog
.getTestDataPath(status
);
281 if (testBundle
== NULL
) {
282 testBundle
= ures_openDirect(icu_data
, bundleName
, &status
);
283 if (status
!= U_ZERO_ERROR
) {
284 fLog
.dataerrln(UNICODE_STRING_SIMPLE("Could not load test data from resourcebundle: ") + UnicodeString(bundleName
, -1, US_INV
));
285 fDataTestValid
= FALSE
;