1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/tools/ctestfw/tstdtmod.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,228 @@ 1.4 +/******************************************************************** 1.5 + * COPYRIGHT: 1.6 + * Copyright (c) 2002-2010, International Business Machines Corporation and 1.7 + * others. All Rights Reserved. 1.8 + ********************************************************************/ 1.9 + 1.10 +/* Created by weiv 05/09/2002 */ 1.11 + 1.12 +#include <stdarg.h> 1.13 + 1.14 +#include "unicode/tstdtmod.h" 1.15 +#include "cmemory.h" 1.16 +#include <stdio.h> 1.17 + 1.18 +TestLog::~TestLog() {} 1.19 + 1.20 +IcuTestErrorCode::~IcuTestErrorCode() { 1.21 + // Safe because our handleFailure() does not throw exceptions. 1.22 + if(isFailure()) { handleFailure(); } 1.23 +} 1.24 + 1.25 +UBool IcuTestErrorCode::logIfFailureAndReset(const char *fmt, ...) { 1.26 + if(isFailure()) { 1.27 + char buffer[4000]; 1.28 + va_list ap; 1.29 + va_start(ap, fmt); 1.30 + vsprintf(buffer, fmt, ap); 1.31 + va_end(ap); 1.32 + UnicodeString msg(testName, -1, US_INV); 1.33 + msg.append(UNICODE_STRING_SIMPLE(" failure: ")).append(UnicodeString(errorName(), -1, US_INV)); 1.34 + msg.append(UNICODE_STRING_SIMPLE(" - ")).append(UnicodeString(buffer, -1, US_INV)); 1.35 + testClass.errln(msg); 1.36 + reset(); 1.37 + return TRUE; 1.38 + } else { 1.39 + reset(); 1.40 + return FALSE; 1.41 + } 1.42 +} 1.43 + 1.44 +UBool IcuTestErrorCode::logDataIfFailureAndReset(const char *fmt, ...) { 1.45 + if(isFailure()) { 1.46 + char buffer[4000]; 1.47 + va_list ap; 1.48 + va_start(ap, fmt); 1.49 + vsprintf(buffer, fmt, ap); 1.50 + va_end(ap); 1.51 + UnicodeString msg(testName, -1, US_INV); 1.52 + msg.append(UNICODE_STRING_SIMPLE(" failure: ")).append(UnicodeString(errorName(), -1, US_INV)); 1.53 + msg.append(UNICODE_STRING_SIMPLE(" - ")).append(UnicodeString(buffer, -1, US_INV)); 1.54 + testClass.dataerrln(msg); 1.55 + reset(); 1.56 + return TRUE; 1.57 + } else { 1.58 + reset(); 1.59 + return FALSE; 1.60 + } 1.61 +} 1.62 + 1.63 +void IcuTestErrorCode::handleFailure() const { 1.64 + // testClass.errln("%s failure - %s", testName, errorName()); 1.65 + UnicodeString msg(testName, -1, US_INV); 1.66 + msg.append(UNICODE_STRING_SIMPLE(" failure: ")).append(UnicodeString(errorName(), -1, US_INV)); 1.67 + 1.68 + if (get() == U_MISSING_RESOURCE_ERROR) { 1.69 + testClass.dataerrln(msg); 1.70 + } else { 1.71 + testClass.errln(msg); 1.72 + } 1.73 +} 1.74 + 1.75 +TestDataModule *TestDataModule::getTestDataModule(const char* name, TestLog& log, UErrorCode &status) 1.76 +{ 1.77 + if(U_FAILURE(status)) { 1.78 + return NULL; 1.79 + } 1.80 + TestDataModule *result = NULL; 1.81 + 1.82 + // TODO: probe for resource bundle and then for XML. 1.83 + // According to that, construct an appropriate driver object 1.84 + 1.85 + result = new RBTestDataModule(name, log, status); 1.86 + if(U_SUCCESS(status)) { 1.87 + return result; 1.88 + } else { 1.89 + delete result; 1.90 + return NULL; 1.91 + } 1.92 +} 1.93 + 1.94 +TestDataModule::TestDataModule(const char* name, TestLog& log, UErrorCode& /*status*/) 1.95 +: testName(name), 1.96 +fInfo(NULL), 1.97 +fLog(log) 1.98 +{ 1.99 +} 1.100 + 1.101 +TestDataModule::~TestDataModule() { 1.102 + if(fInfo != NULL) { 1.103 + delete fInfo; 1.104 + } 1.105 +} 1.106 + 1.107 +const char * TestDataModule::getName() const 1.108 +{ 1.109 + return testName; 1.110 +} 1.111 + 1.112 + 1.113 + 1.114 +RBTestDataModule::~RBTestDataModule() 1.115 +{ 1.116 + ures_close(fTestData); 1.117 + ures_close(fModuleBundle); 1.118 + ures_close(fInfoRB); 1.119 + uprv_free(tdpath); 1.120 +} 1.121 + 1.122 +RBTestDataModule::RBTestDataModule(const char* name, TestLog& log, UErrorCode& status) 1.123 +: TestDataModule(name, log, status), 1.124 + fModuleBundle(NULL), 1.125 + fTestData(NULL), 1.126 + fInfoRB(NULL), 1.127 + tdpath(NULL) 1.128 +{ 1.129 + fNumberOfTests = 0; 1.130 + fDataTestValid = TRUE; 1.131 + fModuleBundle = getTestBundle(name, status); 1.132 + if(fDataTestValid) { 1.133 + fTestData = ures_getByKey(fModuleBundle, "TestData", NULL, &status); 1.134 + fNumberOfTests = ures_getSize(fTestData); 1.135 + fInfoRB = ures_getByKey(fModuleBundle, "Info", NULL, &status); 1.136 + if(status != U_ZERO_ERROR) { 1.137 + log.errln(UNICODE_STRING_SIMPLE("Unable to initalize test data - missing mandatory description resources!")); 1.138 + fDataTestValid = FALSE; 1.139 + } else { 1.140 + fInfo = new RBDataMap(fInfoRB, status); 1.141 + } 1.142 + } 1.143 +} 1.144 + 1.145 +UBool RBTestDataModule::getInfo(const DataMap *& info, UErrorCode &/*status*/) const 1.146 +{ 1.147 + info = fInfo; 1.148 + if(fInfo) { 1.149 + return TRUE; 1.150 + } else { 1.151 + return FALSE; 1.152 + } 1.153 +} 1.154 + 1.155 +TestData* RBTestDataModule::createTestData(int32_t index, UErrorCode &status) const 1.156 +{ 1.157 + TestData *result = NULL; 1.158 + UErrorCode intStatus = U_ZERO_ERROR; 1.159 + 1.160 + if(fDataTestValid == TRUE) { 1.161 + // Both of these resources get adopted by a TestData object. 1.162 + UResourceBundle *DataFillIn = ures_getByIndex(fTestData, index, NULL, &status); 1.163 + UResourceBundle *headers = ures_getByKey(fInfoRB, "Headers", NULL, &intStatus); 1.164 + 1.165 + if(U_SUCCESS(status)) { 1.166 + result = new RBTestData(DataFillIn, headers, status); 1.167 + 1.168 + if(U_SUCCESS(status)) { 1.169 + return result; 1.170 + } else { 1.171 + delete result; 1.172 + } 1.173 + } else { 1.174 + ures_close(DataFillIn); 1.175 + ures_close(headers); 1.176 + } 1.177 + } else { 1.178 + status = U_MISSING_RESOURCE_ERROR; 1.179 + } 1.180 + return NULL; 1.181 +} 1.182 + 1.183 +TestData* RBTestDataModule::createTestData(const char* name, UErrorCode &status) const 1.184 +{ 1.185 + TestData *result = NULL; 1.186 + UErrorCode intStatus = U_ZERO_ERROR; 1.187 + 1.188 + if(fDataTestValid == TRUE) { 1.189 + // Both of these resources get adopted by a TestData object. 1.190 + UResourceBundle *DataFillIn = ures_getByKey(fTestData, name, NULL, &status); 1.191 + UResourceBundle *headers = ures_getByKey(fInfoRB, "Headers", NULL, &intStatus); 1.192 + 1.193 + if(U_SUCCESS(status)) { 1.194 + result = new RBTestData(DataFillIn, headers, status); 1.195 + if(U_SUCCESS(status)) { 1.196 + return result; 1.197 + } else { 1.198 + delete result; 1.199 + } 1.200 + } else { 1.201 + ures_close(DataFillIn); 1.202 + ures_close(headers); 1.203 + } 1.204 + } else { 1.205 + status = U_MISSING_RESOURCE_ERROR; 1.206 + } 1.207 + return NULL; 1.208 +} 1.209 + 1.210 + 1.211 + 1.212 +//Get test data from ResourceBundles 1.213 +UResourceBundle* 1.214 +RBTestDataModule::getTestBundle(const char* bundleName, UErrorCode &status) 1.215 +{ 1.216 + if(U_SUCCESS(status)) { 1.217 + UResourceBundle *testBundle = NULL; 1.218 + const char* icu_data = fLog.getTestDataPath(status); 1.219 + if (testBundle == NULL) { 1.220 + testBundle = ures_openDirect(icu_data, bundleName, &status); 1.221 + if (status != U_ZERO_ERROR) { 1.222 + fLog.dataerrln(UNICODE_STRING_SIMPLE("Could not load test data from resourcebundle: ") + UnicodeString(bundleName, -1, US_INV)); 1.223 + fDataTestValid = FALSE; 1.224 + } 1.225 + } 1.226 + return testBundle; 1.227 + } else { 1.228 + return NULL; 1.229 + } 1.230 +} 1.231 +