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