intl/icu/source/tools/ctestfw/tstdtmod.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial