Changeset 36508 in vbox for trunk/src/VBox
- Timestamp:
- Apr 1, 2011 3:03:59 PM (14 years ago)
- svn:sync-xref-src-repo-rev:
- 70924
- Location:
- trunk/src/VBox/Runtime
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/string/ministring.cpp
r36501 r36508 8 8 9 9 /* 10 * Copyright (C) 2007-201 0Oracle Corporation10 * Copyright (C) 2007-2011 Oracle Corporation 11 11 * 12 12 * This file is part of VirtualBox Open Source Edition (OSE), as … … 200 200 } 201 201 202 size_t MiniString::find(const char *pcszFind, size_t pos /*= 0*/) 203 const 202 size_t MiniString::find(const char *pcszFind, size_t pos /*= 0*/) const 204 203 { 205 204 const char *pszThis, *p; … … 224 223 } 225 224 226 MiniString MiniString::substrCP(size_t pos /*= 0*/, size_t n /*= npos*/) 227 const 225 MiniString MiniString::substrCP(size_t pos /*= 0*/, size_t n /*= npos*/) const 228 226 { 229 227 MiniString ret; … … 288 286 if (cs == CaseSensitive) 289 287 return ::RTStrCmp(&m_psz[l], that.m_psz) == 0; 290 else 291 return ::RTStrICmp(&m_psz[l], that.m_psz) == 0; 288 return ::RTStrICmp(&m_psz[l], that.m_psz) == 0; 292 289 } 293 290 … … 304 301 if (cs == CaseSensitive) 305 302 return ::RTStrNCmp(m_psz, that.m_psz, l2) == 0; 306 else 307 return ::RTStrNICmp(m_psz, that.m_psz, l2) == 0; 303 return ::RTStrNICmp(m_psz, that.m_psz, l2) == 0; 308 304 } 309 305 … … 314 310 if (cs == CaseSensitive) 315 311 return ::RTStrStr(m_psz, that.m_psz) != NULL; 316 else 317 return ::RTStrIStr(m_psz, that.m_psz) != NULL; 312 return ::RTStrIStr(m_psz, that.m_psz) != NULL; 318 313 } 319 314 … … 332 327 } 333 328 334 iprt::list<iprt::MiniString, iprt::MiniString*> MiniString::split(const iprt::MiniString &strSep, SplitMode mode /* = RemoveEmptyParts */) 335 { 336 iprt::list<iprt::MiniString> res; 329 iprt::list<iprt::MiniString, iprt::MiniString *> 330 MiniString::split(const iprt::MiniString &a_rstrSep, SplitMode mode /* = RemoveEmptyParts */) 331 { 332 iprt::list<iprt::MiniString> strRet; 337 333 if (!m_psz) 338 return res;339 if ( strSep.isEmpty())340 { 341 res.append(iprt::MiniString(m_psz));342 return res;343 } 344 345 size_t cch= m_cch;346 char *pszTmp = m_psz;347 while (cch > 0)348 { 349 char *pszNext = strstr(pszTmp,strSep.c_str());334 return strRet; 335 if (a_rstrSep.isEmpty()) 336 { 337 strRet.append(iprt::MiniString(m_psz)); 338 return strRet; 339 } 340 341 size_t cch = m_cch; 342 char const *pszTmp = m_psz; 343 while (cch > 0) 344 { 345 char const *pszNext = strstr(pszTmp, a_rstrSep.c_str()); 350 346 if (!pszNext) 347 { 348 strRet.append(iprt::MiniString(pszTmp, cch)); 351 349 break; 352 size_t chNext = pszNext - pszTmp; 353 if ( chNext > 0 350 } 351 size_t cchNext = pszNext - pszTmp; 352 if ( cchNext > 0 354 353 || mode == KeepEmptyParts) 355 res.append(iprt::MiniString(pszTmp, chNext)); 356 pszTmp += chNext + strSep.length(); 357 cch -= chNext + strSep.length(); 358 } 359 /* Some characters left at the end? */ 360 if (cch > 0) 361 res.append(iprt::MiniString(pszTmp, cch)); 362 363 return res; 354 strRet.append(iprt::MiniString(pszTmp, cchNext)); 355 pszTmp += cchNext + a_rstrSep.length(); 356 cch -= cchNext + a_rstrSep.length(); 357 } 358 359 return strRet; 364 360 } 365 361 366 362 /* static */ 367 iprt::MiniString MiniString::join(const iprt::list<iprt::MiniString, iprt::MiniString*> &list, const iprt::MiniString &strSep /* = "" */) 368 { 369 MiniString res; 370 if (list.size() > 1) 371 { 372 for(size_t i = 0; i < list.size() - 1; ++i) 373 res += list.at(i) + strSep; 374 res += list.last(); 375 } 376 377 return res; 378 } 379 380 const iprt::MiniString operator+(const iprt::MiniString &one, const iprt::MiniString &other) 381 { 382 iprt::MiniString res(one); 383 384 return res += other; 385 } 386 387 const iprt::MiniString operator+(const iprt::MiniString &one, const char *pcszOther) 388 { 389 iprt::MiniString res(one); 390 391 return res += pcszOther; 392 } 393 394 const iprt::MiniString operator+(const char *pcszOne, const iprt::MiniString &other) 395 { 396 iprt::MiniString res(pcszOne); 397 398 return res += other; 399 } 400 363 iprt::MiniString 364 MiniString::join(const iprt::list<iprt::MiniString, iprt::MiniString*> &a_rList, 365 const iprt::MiniString &a_rstrSep /* = "" */) 366 { 367 MiniString strRet; 368 if (a_rList.size() > 1) 369 { 370 for (size_t i = 0; i < a_rList.size() - 1; ++i) 371 strRet += a_rList.at(i) + a_rstrSep; 372 strRet += a_rList.last(); 373 } 374 375 return strRet; 376 } 377 378 const iprt::MiniString operator+(const iprt::MiniString &a_rStr1, const iprt::MiniString &a_rStr2) 379 { 380 iprt::MiniString strRet(a_rStr1); 381 strRet += a_rStr2; 382 return strRet; 383 } 384 385 const iprt::MiniString operator+(const iprt::MiniString &a_rStr1, const char *a_pszStr2) 386 { 387 iprt::MiniString strRet(a_rStr1); 388 strRet += a_pszStr2; 389 return strRet; 390 } 391 392 const iprt::MiniString operator+(const char *a_psz1, const iprt::MiniString &a_rStr2) 393 { 394 iprt::MiniString strRet(a_psz1); 395 strRet += a_rStr2; 396 return strRet; 397 } 398 -
trunk/src/VBox/Runtime/testcase/Makefile.kmk
r36500 r36508 68 68 tstRTHeapSimple \ 69 69 tstRTInlineAsm \ 70 tstIprtList \ 70 71 tstIprtMiniString \ 71 tstIprtList \72 72 tstLdr \ 73 73 tstLdrLoad \ … … 262 262 tstRTInlineAsmPIC3_DEFS = PIC 263 263 264 tstIprtList_TEMPLATE = VBOXR3TSTEXE 265 tstIprtList_SOURCES = tstIprtList.cpp 266 264 267 tstIprtMiniString_TEMPLATE = VBOXR3TSTEXE 265 268 tstIprtMiniString_SOURCES = tstIprtMiniString.cpp 266 267 tstIprtList_TEMPLATE = VBOXR3TSTEXE268 tstIprtList_SOURCES = tstIprtList.cpp269 269 270 270 tstLdr_SOURCES = tstLdr.cpp -
trunk/src/VBox/Runtime/testcase/tstIprtList.cpp
r36500 r36508 34 34 #include <iprt/rand.h> 35 35 36 36 37 /******************************************************************************* 37 * Test Data*38 * Global Variables * 38 39 *******************************************************************************/ 39 static const char *gs_apcszTestStrings[] = 40 /** Used for the string test. */ 41 static const char *g_apszTestStrings[] = 40 42 { 41 43 "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", … … 141 143 }; 142 144 143 /******************************************************************************* 144 * Tests * 145 *******************************************************************************/ 145 146 /** 147 * Does a list test. 148 * 149 * @param T1 The list type. 150 * @param T2 The input type 151 * @param pcszDesc The test description. 152 * @param paTestData Pointer to the array with the test input data. 153 * @param cTestItems The size of the input data. 154 */ 146 155 template<typename T1, typename T2> 147 static void test(const char * pcszDesc, T2 aTestData[], size_t cSize)156 static void test(const char *pcszDesc, T2 paTestData[], size_t cTestItems) 148 157 { 149 RTTestISubF("%s with size of %u (items=%u)", pcszDesc, sizeof(T1), c Size);158 RTTestISubF("%s with size of %u (items=%u)", pcszDesc, sizeof(T1), cTestItems); 150 159 151 160 /* … … 166 175 /* Add the second half of the test data */ 167 176 size_t cAdded = 1; 177 168 178 /* Start adding the second half of our test list */ 169 for (size_t i = c Size / 2; i < cSize; ++i, ++cAdded)179 for (size_t i = cTestItems / 2; i < cTestItems; ++i, ++cAdded) 170 180 { 171 testList.append( aTestData[i]);181 testList.append(paTestData[i]); 172 182 RTTESTI_CHECK_RETV(testList.size() == cAdded); 173 RTTESTI_CHECK(testList.at(0) == aTestData[cSize/ 2]);174 RTTESTI_CHECK(testList[0] == aTestData[cSize/ 2]);175 RTTESTI_CHECK(testList.first() == aTestData[cSize/ 2]);176 RTTESTI_CHECK(testList.at(cAdded - 1) == aTestData[i]);177 RTTESTI_CHECK(testList[cAdded - 1] == aTestData[i]);178 RTTESTI_CHECK(testList.last() == aTestData[i]);183 RTTESTI_CHECK(testList.at(0) == paTestData[cTestItems / 2]); 184 RTTESTI_CHECK(testList[0] == paTestData[cTestItems / 2]); 185 RTTESTI_CHECK(testList.first() == paTestData[cTestItems / 2]); 186 RTTESTI_CHECK(testList.at(cAdded - 1) == paTestData[i]); 187 RTTESTI_CHECK(testList[cAdded - 1] == paTestData[i]); 188 RTTESTI_CHECK(testList.last() == paTestData[i]); 179 189 } 190 180 191 /* Check that all is correctly appended. */ 181 RTTESTI_CHECK_RETV(testList.size() == c Size/ 2);192 RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2); 182 193 RTTESTI_CHECK_RETV(testList.isEmpty() == false); 183 194 for (size_t i = 0; i < testList.size(); ++i) 184 RTTESTI_CHECK(testList.at(i) == aTestData[cSize / 2 + i]); 195 RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]); 196 185 197 /* Start prepending the first half of our test list. Iterate reverse to get 186 198 * the correct sorting back. */ 187 for (size_t i = c Size/ 2; i > 0; --i, ++cAdded)199 for (size_t i = cTestItems / 2; i > 0; --i, ++cAdded) 188 200 { 189 testList.prepend( aTestData[i - 1]);201 testList.prepend(paTestData[i - 1]); 190 202 RTTESTI_CHECK_RETV(testList.size() == cAdded); 191 RTTESTI_CHECK(testList.at(0) == aTestData[i - 1]);192 RTTESTI_CHECK(testList[0] == aTestData[i - 1]);193 RTTESTI_CHECK(testList.first() == aTestData[i - 1]);194 RTTESTI_CHECK(testList.at(cAdded - 1) == aTestData[cSize- 1]);195 RTTESTI_CHECK(testList[cAdded - 1] == aTestData[cSize- 1]);196 RTTESTI_CHECK(testList.last() == aTestData[cSize- 1]);203 RTTESTI_CHECK(testList.at(0) == paTestData[i - 1]); 204 RTTESTI_CHECK(testList[0] == paTestData[i - 1]); 205 RTTESTI_CHECK(testList.first() == paTestData[i - 1]); 206 RTTESTI_CHECK(testList.at(cAdded - 1) == paTestData[cTestItems - 1]); 207 RTTESTI_CHECK(testList[cAdded - 1] == paTestData[cTestItems - 1]); 208 RTTESTI_CHECK(testList.last() == paTestData[cTestItems - 1]); 197 209 } 210 198 211 /* Check that all is correctly prepended. */ 199 RTTESTI_CHECK_RETV(testList.size() == c Size);212 RTTESTI_CHECK_RETV(testList.size() == cTestItems); 200 213 RTTESTI_CHECK_RETV(testList.isEmpty() == false); 201 214 for (size_t i = 0; i < testList.size(); ++i) 202 RTTESTI_CHECK(testList.at(i) == aTestData[i]);215 RTTESTI_CHECK(testList.at(i) == paTestData[i]); 203 216 204 217 /* … … 206 219 */ 207 220 iprt::list<T1> testList2(testList); 221 208 222 /* Check that all is correctly appended. */ 209 RTTESTI_CHECK_RETV(testList2.size() == c Size);210 for (size_t i = 0; i < testList2.size(); ++i) 211 RTTESTI_CHECK(testList2.at(i) == aTestData[i]);223 RTTESTI_CHECK_RETV(testList2.size() == cTestItems); 224 for (size_t i = 0; i < testList2.size(); ++i) 225 RTTESTI_CHECK(testList2.at(i) == paTestData[i]); 212 226 213 227 /* … … 216 230 iprt::list<T1> testList3; 217 231 testList3 = testList; 232 218 233 /* Check that all is correctly appended. */ 219 RTTESTI_CHECK_RETV(testList3.size() == c Size);234 RTTESTI_CHECK_RETV(testList3.size() == cTestItems); 220 235 for (size_t i = 0; i < testList3.size(); ++i) 221 RTTESTI_CHECK(testList3.at(i) == aTestData[i]);236 RTTESTI_CHECK(testList3.at(i) == paTestData[i]); 222 237 223 238 /* … … 225 240 */ 226 241 testList2.append(testList3); 242 227 243 /* Check that all is correctly appended. */ 228 RTTESTI_CHECK_RETV(testList2.size() == c Size* 2);229 for (size_t i = 0; i < testList2.size(); ++i) 230 RTTESTI_CHECK(testList2.at(i) == aTestData[i % cSize]);244 RTTESTI_CHECK_RETV(testList2.size() == cTestItems * 2); 245 for (size_t i = 0; i < testList2.size(); ++i) 246 RTTESTI_CHECK(testList2.at(i) == paTestData[i % cTestItems]); 231 247 232 248 /* … … 234 250 */ 235 251 testList2.prepend(testList3); 252 236 253 /* Check that all is correctly appended. */ 237 RTTESTI_CHECK_RETV(testList2.size() == c Size* 3);238 for (size_t i = 0; i < testList2.size(); ++i) 239 RTTESTI_CHECK(testList2.at(i) == aTestData[i % cSize]);254 RTTESTI_CHECK_RETV(testList2.size() == cTestItems * 3); 255 for (size_t i = 0; i < testList2.size(); ++i) 256 RTTESTI_CHECK(testList2.at(i) == paTestData[i % cTestItems]); 240 257 241 258 /* … … 243 260 */ 244 261 for (size_t i = 0; i < testList2.size(); ++i) 245 RTTESTI_CHECK(testList2.value(i) == aTestData[i % cSize]);246 for (size_t i = 0; i < testList2.size(); ++i) 247 RTTESTI_CHECK(testList2.value(i, T1()) == aTestData[i % cSize]);262 RTTESTI_CHECK(testList2.value(i) == paTestData[i % cTestItems]); 263 for (size_t i = 0; i < testList2.size(); ++i) 264 RTTESTI_CHECK(testList2.value(i, T1()) == paTestData[i % cTestItems]); 248 265 RTTESTI_CHECK(testList2.value(testList2.size() + 1) == T1()); /* Invalid index */ 249 266 RTTESTI_CHECK(testList2.value(testList2.size() + 1, T1()) == T1()); /* Invalid index */ … … 253 270 */ 254 271 for (size_t i = 0; i < testList.size(); ++i) 255 RTTESTI_CHECK(testList[i] == aTestData[i]); 272 RTTESTI_CHECK(testList[i] == paTestData[i]); 273 256 274 /* 257 275 * operator[] (writing) … … 259 277 * Replace with inverted array. 260 278 */ 261 for (size_t i = 0; i < c Size; ++i)262 testList[i] = aTestData[cSize- i - 1];263 RTTESTI_CHECK_RETV(testList.size() == c Size);264 for (size_t i = 0; i < testList.size(); ++i) 265 RTTESTI_CHECK(testList[i] == aTestData[cSize- i - 1]);279 for (size_t i = 0; i < cTestItems; ++i) 280 testList[i] = paTestData[cTestItems - i - 1]; 281 RTTESTI_CHECK_RETV(testList.size() == cTestItems); 282 for (size_t i = 0; i < testList.size(); ++i) 283 RTTESTI_CHECK(testList[i] == paTestData[cTestItems - i - 1]); 266 284 267 285 /* … … 270 288 * Replace with inverted array (Must be original array when finished). 271 289 */ 272 for (size_t i = 0; i < c Size; ++i)273 testList.replace(i, aTestData[i]);274 RTTESTI_CHECK_RETV(testList.size() == c Size);275 for (size_t i = 0; i < testList.size(); ++i) 276 RTTESTI_CHECK(testList[i] == aTestData[i]);290 for (size_t i = 0; i < cTestItems; ++i) 291 testList.replace(i, paTestData[i]); 292 RTTESTI_CHECK_RETV(testList.size() == cTestItems); 293 for (size_t i = 0; i < testList.size(); ++i) 294 RTTESTI_CHECK(testList[i] == paTestData[i]); 277 295 278 296 /* … … 281 299 282 300 /* Remove Range */ 283 testList2.removeRange(c Size, cSize* 2);284 RTTESTI_CHECK_RETV(testList2.size() == c Size* 2);285 for (size_t i = 0; i < testList2.size(); ++i) 286 RTTESTI_CHECK(testList2.at(i) == aTestData[i % cSize]);301 testList2.removeRange(cTestItems, cTestItems * 2); 302 RTTESTI_CHECK_RETV(testList2.size() == cTestItems * 2); 303 for (size_t i = 0; i < testList2.size(); ++i) 304 RTTESTI_CHECK(testList2.at(i) == paTestData[i % cTestItems]); 287 305 288 306 /* Remove the first half (reverse) */ 289 307 size_t cRemoved = 1; 290 for (size_t i = c Size/ 2; i > 0; --i, ++cRemoved)308 for (size_t i = cTestItems / 2; i > 0; --i, ++cRemoved) 291 309 { 292 310 testList.removeAt(i - 1); 293 RTTESTI_CHECK_RETV(testList.size() == c Size- cRemoved);311 RTTESTI_CHECK_RETV(testList.size() == cTestItems - cRemoved); 294 312 } 295 RTTESTI_CHECK_RETV(testList.size() == cSize / 2); 313 RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2); 314 296 315 /* Check that all is correctly removed and only the second part of the list 297 316 * is still there. */ 298 317 for (size_t i = 0; i < testList.size(); ++i) 299 RTTESTI_CHECK(testList.at(i) == aTestData[cSize/ 2 + i]);318 RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]); 300 319 301 320 /* 302 321 * setCapacitiy 303 322 */ 304 testList.setCapacity(cSize * 5); 305 RTTESTI_CHECK(testList.capacity() == cSize * 5); 306 RTTESTI_CHECK_RETV(testList.size() == cSize / 2); 323 testList.setCapacity(cTestItems * 5); 324 RTTESTI_CHECK(testList.capacity() == cTestItems * 5); 325 RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2); 326 307 327 /* As the capacity just increased, we should still have all entries from 308 328 * the previous list. */ 309 329 for (size_t i = 0; i < testList.size(); ++i) 310 RTTESTI_CHECK(testList.at(i) == aTestData[cSize / 2 + i]); 330 RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]); 331 311 332 /* Decrease the capacity so it will be smaller than the count of items in 312 333 * the list. The list should be shrink automatically, but the remaining 313 334 * items should be still valid. */ 314 testList.setCapacity(c Size/ 4);315 RTTESTI_CHECK_RETV(testList.size() == c Size/ 4);316 RTTESTI_CHECK(testList.capacity() == c Size/ 4);317 for (size_t i = 0; i < testList.size(); ++i) 318 RTTESTI_CHECK(testList.at(i) == aTestData[cSize/ 2 + i]);335 testList.setCapacity(cTestItems / 4); 336 RTTESTI_CHECK_RETV(testList.size() == cTestItems / 4); 337 RTTESTI_CHECK(testList.capacity() == cTestItems / 4); 338 for (size_t i = 0; i < testList.size(); ++i) 339 RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]); 319 340 320 341 /* Clear all */ … … 325 346 } 326 347 348 327 349 int main() 328 350 { … … 331 353 332 354 RTTEST hTest; 333 int rc= RTTestInitAndCreate("tstIprtList", &hTest);334 if (rc )335 return rc ;355 RTEXITCODE rcExit = RTTestInitAndCreate("tstIprtList", &hTest); 356 if (rcExit) 357 return rcExit; 336 358 RTTestBanner(hTest); 337 359 … … 348 370 uint8_t au8TestInts[s_cTestCount]; 349 371 for (size_t i = 0; i < RT_ELEMENTS(au8TestInts); ++i) 350 au8TestInts[i] = ( float)RTRandU32() / UINT32_MAX * UINT8_MAX;372 au8TestInts[i] = (uint8_t)RTRandU32Ex(0, UINT8_MAX); 351 373 test<uint8_t, uint8_t>("Native type", au8TestInts, RT_ELEMENTS(au8TestInts)); 374 352 375 uint16_t au16TestInts[s_cTestCount]; 353 376 for (size_t i = 0; i < RT_ELEMENTS(au16TestInts); ++i) 354 au16TestInts[i] = ( float)RTRandU32() / UINT32_MAX * UINT16_MAX;377 au16TestInts[i] = (uint16_t)RTRandU32Ex(0, UINT16_MAX); 355 378 test<uint16_t, uint16_t>("Native type", au16TestInts, RT_ELEMENTS(au16TestInts)); 379 356 380 uint32_t au32TestInts[s_cTestCount]; 357 381 for (size_t i = 0; i < RT_ELEMENTS(au32TestInts); ++i) 358 382 au32TestInts[i] = RTRandU32(); 359 383 test<uint32_t, uint32_t>("Native type", au32TestInts, RT_ELEMENTS(au32TestInts)); 384 360 385 /* 361 386 * Specialized type. … … 365 390 au64TestInts[i] = RTRandU64(); 366 391 test<uint64_t, uint64_t>("Specialized type", au64TestInts, RT_ELEMENTS(au64TestInts)); 392 367 393 /* 368 394 * Big size type (translate to internal pointer list). 369 395 */ 370 test<iprt::MiniString, const char *>("Class type", gs_apcszTestStrings, RT_ELEMENTS(gs_apcszTestStrings));396 test<iprt::MiniString, const char *>("Class type", g_apszTestStrings, RT_ELEMENTS(g_apszTestStrings)); 371 397 372 398 /*
Note:
See TracChangeset
for help on using the changeset viewer.