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