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