jlibtests.cpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. /*
  14. * Jlib regression tests
  15. *
  16. */
  17. #ifdef _USE_CPPUNIT
  18. #include <memory>
  19. #include "jsem.hpp"
  20. #include "jfile.hpp"
  21. #include "jdebug.hpp"
  22. #include "jset.hpp"
  23. #include "rmtfile.hpp"
  24. #include "jqueue.hpp"
  25. #include "jregexp.hpp"
  26. #include "unittests.hpp"
  27. static const unsigned oneMinute = 60000; // msec
  28. class JlibSemTest : public CppUnit::TestFixture
  29. {
  30. public:
  31. CPPUNIT_TEST_SUITE(JlibSemTest);
  32. CPPUNIT_TEST(testSimple);
  33. CPPUNIT_TEST_SUITE_END();
  34. protected:
  35. void testTimedAvailable(Semaphore & sem)
  36. {
  37. //Shouldn't cause a reschedule, definitely shouldn't wait for 100s
  38. if(!sem.wait(100))
  39. ASSERT(false);
  40. }
  41. void testTimedElapsed(Semaphore & sem, unsigned time)
  42. {
  43. unsigned now = msTick();
  44. sem.wait(time);
  45. unsigned taken = msTick() - now;
  46. VStringBuffer errMsg("values: time: %u, taken: %u", time, taken);
  47. CPPUNIT_ASSERT_MESSAGE(errMsg.str(), taken >= time && taken < time + oneMinute);
  48. PROGLOG("%s", errMsg.str());
  49. }
  50. void testSimple()
  51. {
  52. //Some very basic semaphore tests.
  53. Semaphore sem;
  54. sem.signal();
  55. sem.wait();
  56. testTimedElapsed(sem, 100);
  57. sem.signal();
  58. testTimedAvailable(sem);
  59. sem.reinit(2);
  60. sem.wait();
  61. testTimedAvailable(sem);
  62. testTimedElapsed(sem, 5);
  63. }
  64. };
  65. CPPUNIT_TEST_SUITE_REGISTRATION( JlibSemTest );
  66. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSemTest, "JlibSemTest" );
  67. class JlibSemTestStress : public CppUnit::TestFixture
  68. {
  69. public:
  70. CPPUNIT_TEST_SUITE(JlibSemTestStress);
  71. CPPUNIT_TEST(testSimple);
  72. CPPUNIT_TEST_SUITE_END();
  73. protected:
  74. void testTimedElapsed(Semaphore & sem, unsigned time, unsigned loopCount)
  75. {
  76. unsigned __int64 sumTaken = 0;
  77. unsigned maxTaken = 0;
  78. unsigned timeLimit = time + oneMinute;
  79. unsigned numberOfOut = 0;
  80. bool isSignaled = false;
  81. PROGLOG("Start loop");
  82. for (int i = 0 ; i <= loopCount; i++)
  83. {
  84. unsigned now = msTick();
  85. if (sem.wait(time))
  86. {
  87. isSignaled = true;
  88. break;
  89. }
  90. unsigned taken = msTick() - now;
  91. sumTaken += taken;
  92. maxTaken = (taken > maxTaken ? taken : maxTaken);
  93. numberOfOut += (taken > timeLimit ? 1 : 0);
  94. }
  95. VStringBuffer errMsg("values: time: %d, loop: %d, sum taken: %llu, average taken: %llu, max taken: %d, out of limit: %d times, signaled: %s",
  96. time, loopCount, sumTaken, sumTaken/loopCount, maxTaken, numberOfOut, (isSignaled ? "yes" : "no"));
  97. CPPUNIT_ASSERT_MESSAGE(errMsg.str(), 0 == numberOfOut && !isSignaled );
  98. PROGLOG("%s", errMsg.str());
  99. }
  100. void testSimple()
  101. {
  102. //Very basic semaphore stress tests.
  103. Semaphore sem;
  104. sem.signal();
  105. if (!sem.wait(1000))
  106. {
  107. VStringBuffer errMsg("Semaphore stalled (%s:%d)", sanitizeSourceFile(__FILE__), __LINE__);
  108. CPPUNIT_FAIL(errMsg.str());
  109. }
  110. testTimedElapsed(sem, 5, 1000);
  111. }
  112. };
  113. CPPUNIT_TEST_SUITE_REGISTRATION( JlibSemTestStress );
  114. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSemTestStress, "JlibSemTestStress" );
  115. /* =========================================================== */
  116. class JlibSetTest : public CppUnit::TestFixture
  117. {
  118. protected:
  119. void testBitsetHelpers()
  120. {
  121. CPPUNIT_ASSERT_EQUAL(0U, countTrailingUnsetBits(1U));
  122. CPPUNIT_ASSERT_EQUAL(31U, countLeadingUnsetBits(1U));
  123. CPPUNIT_ASSERT_EQUAL(1U, getMostSignificantBit(1U));
  124. CPPUNIT_ASSERT_EQUAL(4U, countTrailingUnsetBits(0x110U));
  125. CPPUNIT_ASSERT_EQUAL(23U, countLeadingUnsetBits(0x110U));
  126. CPPUNIT_ASSERT_EQUAL(9U, getMostSignificantBit(0x110U));
  127. CPPUNIT_ASSERT_EQUAL(0U, countTrailingUnsetBits(0xFFFFFFFFU));
  128. CPPUNIT_ASSERT_EQUAL(0U, countLeadingUnsetBits(0xFFFFFFFFU));
  129. CPPUNIT_ASSERT_EQUAL(32U, getMostSignificantBit(0xFFFFFFFFU));
  130. CPPUNIT_ASSERT_EQUAL(52U, countTrailingUnsetBits(I64C(0x1010000000000000U)));
  131. }
  132. void testSet1(bool initial, IBitSet *bs, unsigned start, unsigned numBits, bool setValue, bool clearValue)
  133. {
  134. unsigned end = start+numBits;
  135. if (initial)
  136. bs->incl(start, end-1);
  137. for (unsigned i=start; i < end; i++)
  138. {
  139. ASSERT(bs->test(i) == clearValue);
  140. bs->set(i, setValue);
  141. ASSERT(bs->test(i) == setValue);
  142. bs->set(i+5, setValue);
  143. ASSERT(bs->scan(0, setValue) == i);
  144. ASSERT(bs->scan(i+1, setValue) == i+5);
  145. bs->set(i, clearValue);
  146. bs->set(i+5, clearValue);
  147. //Clearing i+5 above may extend the set - so need to calculate the end carefully
  148. unsigned last = i+5 < end ? end : i + 6;
  149. unsigned match1 = bs->scan(0, setValue);
  150. CPPUNIT_ASSERT_EQUAL((unsigned)(initial ? last : -1), match1);
  151. bs->invert(i);
  152. ASSERT(bs->test(i) == setValue);
  153. bs->invert(i);
  154. ASSERT(bs->test(i) == clearValue);
  155. bool wasSet = bs->testSet(i, setValue);
  156. ASSERT(wasSet == clearValue);
  157. bool wasSet2 = bs->testSet(i, clearValue);
  158. ASSERT(wasSet2 == setValue);
  159. ASSERT(bs->test(i) == clearValue);
  160. bs->set(i, setValue);
  161. unsigned match = bs->scanInvert(0, setValue);
  162. ASSERT(match == i);
  163. ASSERT(bs->test(i) == clearValue);
  164. }
  165. bs->reset();
  166. if (initial)
  167. {
  168. bs->incl(start, end);
  169. bs->excl(start+5, end-5);
  170. }
  171. else
  172. bs->incl(start+5, end-5);
  173. unsigned inclStart = bs->scan(start, setValue);
  174. ASSERT((start+5) == inclStart);
  175. unsigned inclEnd = bs->scan(start+5, clearValue);
  176. ASSERT((end-5) == (inclEnd-1));
  177. }
  178. void testSet(bool initial, unsigned passes, bool timed)
  179. {
  180. unsigned now = msTick();
  181. bool setValue = !initial;
  182. bool clearValue = initial;
  183. const unsigned numBits = 400;
  184. for (unsigned pass=0; pass < passes; pass++)
  185. {
  186. Owned<IBitSet> bs = createThreadSafeBitSet();
  187. testSet1(initial, bs, 0, numBits, setValue, clearValue);
  188. }
  189. if (timed)
  190. {
  191. unsigned elapsed = msTick()-now;
  192. DBGLOG("Bit test (%u) %d passes time taken = %dms", initial, passes, elapsed);
  193. }
  194. now = msTick();
  195. for (unsigned pass=0; pass < passes; pass++)
  196. {
  197. Owned<IBitSet> bs = createBitSet();
  198. testSet1(initial, bs, 0, numBits, setValue, clearValue);
  199. }
  200. if (timed)
  201. {
  202. unsigned elapsed = msTick()-now;
  203. DBGLOG("Bit test [thread-unsafe version] (%u) %d passes time taken = %dms", initial, passes, elapsed);
  204. }
  205. now = msTick();
  206. size32_t bitSetMemSz = getBitSetMemoryRequirement(numBits+5);
  207. MemoryBuffer mb;
  208. void *mem = mb.reserveTruncate(bitSetMemSz);
  209. for (unsigned pass=0; pass < passes; pass++)
  210. {
  211. Owned<IBitSet> bs = createBitSet(bitSetMemSz, mem);
  212. testSet1(initial, bs, 0, numBits, setValue, clearValue);
  213. }
  214. if (timed)
  215. {
  216. unsigned elapsed = msTick()-now;
  217. DBGLOG("Bit test [thread-unsafe version, fixed memory] (%u) %d passes time taken = %dms\n", initial, passes, elapsed);
  218. }
  219. }
  220. };
  221. class JlibSetTestQuick : public JlibSetTest
  222. {
  223. public:
  224. CPPUNIT_TEST_SUITE(JlibSetTestQuick);
  225. CPPUNIT_TEST(testBitsetHelpers);
  226. CPPUNIT_TEST(testSimple);
  227. CPPUNIT_TEST_SUITE_END();
  228. void testSimple()
  229. {
  230. testSet(false, 100, false);
  231. testSet(true, 100, false);
  232. }
  233. };
  234. class JlibSetTestStress : public JlibSetTest
  235. {
  236. public:
  237. CPPUNIT_TEST_SUITE(JlibSetTestStress);
  238. CPPUNIT_TEST(testParallel);
  239. CPPUNIT_TEST(testSimple);
  240. CPPUNIT_TEST_SUITE_END();
  241. void testSimple()
  242. {
  243. testSet(false, 10000, true);
  244. testSet(true, 10000, true);
  245. }
  246. protected:
  247. class CBitThread : public CSimpleInterfaceOf<IInterface>, implements IThreaded
  248. {
  249. IBitSet &bitSet;
  250. unsigned startBit, numBits;
  251. bool initial, setValue, clearValue;
  252. CThreaded threaded;
  253. Owned<IException> exception;
  254. CppUnit::Exception *cppunitException;
  255. public:
  256. CBitThread(IBitSet &_bitSet, unsigned _startBit, unsigned _numBits, bool _initial)
  257. : threaded("CBitThread", this), bitSet(_bitSet), startBit(_startBit), numBits(_numBits), initial(_initial)
  258. {
  259. cppunitException = NULL;
  260. setValue = !initial;
  261. clearValue = initial;
  262. }
  263. void start() { threaded.start(); }
  264. void join()
  265. {
  266. threaded.join();
  267. if (exception)
  268. throw exception.getClear();
  269. else if (cppunitException)
  270. throw cppunitException;
  271. }
  272. virtual void threadmain() override
  273. {
  274. try
  275. {
  276. unsigned endBit = startBit+numBits-1;
  277. if (initial)
  278. bitSet.incl(startBit, endBit);
  279. for (unsigned i=startBit; i < endBit; i++)
  280. {
  281. ASSERT(bitSet.test(i) == clearValue);
  282. bitSet.set(i, setValue);
  283. ASSERT(bitSet.test(i) == setValue);
  284. if (i < (endBit-1))
  285. ASSERT(bitSet.scan(i, clearValue) == i+1); // find next unset (should be i+1)
  286. bitSet.set(i, clearValue);
  287. bitSet.invert(i);
  288. ASSERT(bitSet.test(i) == setValue);
  289. bitSet.invert(i);
  290. ASSERT(bitSet.test(i) == clearValue);
  291. bool wasSet = bitSet.testSet(i, setValue);
  292. ASSERT(wasSet == clearValue);
  293. bool wasSet2 = bitSet.testSet(i, clearValue);
  294. ASSERT(wasSet2 == setValue);
  295. ASSERT(bitSet.test(i) == clearValue);
  296. bitSet.set(i, setValue);
  297. unsigned match = bitSet.scanInvert(startBit, setValue);
  298. ASSERT(match == i);
  299. ASSERT(bitSet.test(i) == clearValue);
  300. }
  301. }
  302. catch (IException *e)
  303. {
  304. exception.setown(e);
  305. }
  306. catch (CppUnit::Exception &e)
  307. {
  308. cppunitException = e.clone();
  309. }
  310. }
  311. };
  312. unsigned testParallelRun(IBitSet &bitSet, unsigned nThreads, unsigned bitsPerThread, bool initial)
  313. {
  314. IArrayOf<CBitThread> bitThreads;
  315. unsigned bitStart = 0;
  316. unsigned bitEnd = 0;
  317. for (unsigned t=0; t<nThreads; t++)
  318. {
  319. bitThreads.append(* new CBitThread(bitSet, bitStart, bitsPerThread, initial));
  320. bitStart += bitsPerThread;
  321. }
  322. unsigned now = msTick();
  323. for (unsigned t=0; t<nThreads; t++)
  324. bitThreads.item(t).start();
  325. Owned<IException> exception;
  326. CppUnit::Exception *cppunitException = NULL;
  327. for (unsigned t=0; t<nThreads; t++)
  328. {
  329. try
  330. {
  331. bitThreads.item(t).join();
  332. }
  333. catch (IException *e)
  334. {
  335. EXCLOG(e, NULL);
  336. if (!exception)
  337. exception.setown(e);
  338. else
  339. e->Release();
  340. }
  341. catch (CppUnit::Exception *e)
  342. {
  343. cppunitException = e;
  344. }
  345. }
  346. if (exception)
  347. throw exception.getClear();
  348. else if (cppunitException)
  349. throw *cppunitException;
  350. return msTick()-now;
  351. }
  352. void testSetParallel(bool initial)
  353. {
  354. unsigned numBits = 1000000; // 10M
  355. unsigned nThreads = getAffinityCpus();
  356. unsigned bitsPerThread = numBits/nThreads;
  357. bitsPerThread = ((bitsPerThread + (BitsPerItem-1)) / BitsPerItem) * BitsPerItem; // round up to multiple of BitsPerItem
  358. numBits = bitsPerThread*nThreads; // round
  359. fprintf(stdout, "testSetParallel, testing bit set of size : %d, nThreads=%d\n", numBits, nThreads);
  360. Owned<IBitSet> bitSet = createThreadSafeBitSet();
  361. unsigned took = testParallelRun(*bitSet, nThreads, bitsPerThread, initial);
  362. fprintf(stdout, "Thread safe parallel bit set test (%u) time taken = %dms\n", initial, took);
  363. size32_t bitSetMemSz = getBitSetMemoryRequirement(numBits);
  364. MemoryBuffer mb;
  365. void *mem = mb.reserveTruncate(bitSetMemSz);
  366. bitSet.setown(createBitSet(bitSetMemSz, mem));
  367. took = testParallelRun(*bitSet, nThreads, bitsPerThread, initial);
  368. fprintf(stdout, "Thread unsafe parallel bit set test (%u) time taken = %dms\n", initial, took);
  369. }
  370. void testParallel()
  371. {
  372. testSetParallel(false);
  373. testSetParallel(true);
  374. }
  375. };
  376. CPPUNIT_TEST_SUITE_REGISTRATION( JlibSetTestQuick );
  377. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSetTestQuick, "JlibSetTestQuick" );
  378. CPPUNIT_TEST_SUITE_REGISTRATION( JlibSetTestStress );
  379. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSetTestStress, "JlibSetTestStress" );
  380. /* =========================================================== */
  381. class JlibFileIOTestTiming : public CppUnit::TestFixture
  382. {
  383. protected:
  384. unsigned rs, nr10pct, nr150pct;
  385. char *record;
  386. StringBuffer tmpfile;
  387. CPPUNIT_TEST_SUITE( JlibFileIOTestTiming );
  388. CPPUNIT_TEST(testIOSmall);
  389. CPPUNIT_TEST(testIOLarge);
  390. CPPUNIT_TEST_SUITE_END();
  391. public:
  392. JlibFileIOTestTiming()
  393. {
  394. HardwareInfo hdwInfo;
  395. getHardwareInfo(hdwInfo);
  396. rs = 65536;
  397. unsigned nr = (unsigned)(1024.0 * (1024.0 * (double)hdwInfo.totalMemory / (double)rs));
  398. nr10pct = nr / 10;
  399. nr150pct = (unsigned)((double)nr * 1.5);
  400. record = (char *)malloc(rs);
  401. for (unsigned i=0;i<rs;i++)
  402. record[i] = 'a';
  403. record[rs-1] = '\n';
  404. tmpfile.set("JlibFileIOTest.txt");
  405. }
  406. ~JlibFileIOTestTiming()
  407. {
  408. free(record);
  409. }
  410. protected:
  411. void testIO(unsigned nr, const char *server)
  412. {
  413. IFile *ifile;
  414. IFileIO *ifileio;
  415. unsigned fsize = (unsigned)(((double)nr * (double)rs) / (1024.0 * 1024.0));
  416. fflush(NULL);
  417. fprintf(stdout,"\n");
  418. fflush(NULL);
  419. for(int j=0; j<2; j++)
  420. {
  421. if (j==0)
  422. fprintf(stdout, "File size: %d (MB) Cache, ", fsize);
  423. else
  424. fprintf(stdout, "\nFile size: %d (MB) Nocache, ", fsize);
  425. if (server != NULL)
  426. {
  427. SocketEndpoint ep;
  428. ep.set(server, 7100);
  429. ifile = createRemoteFile(ep, tmpfile);
  430. fprintf(stdout, "Remote: (%s)\n", server);
  431. }
  432. else
  433. {
  434. ifile = createIFile(tmpfile);
  435. fprintf(stdout, "Local:\n");
  436. }
  437. ifile->remove();
  438. unsigned st = msTick();
  439. IFEflags extraFlags = IFEcache;
  440. if (j==1)
  441. extraFlags = IFEnocache;
  442. ifileio = ifile->open(IFOcreate, extraFlags);
  443. #if 0 // for testing default and explicitly set share mode to Windows dafilesrv
  444. if (server != NULL)
  445. ifile->setShareMode((IFSHmode)IFSHread);
  446. #endif
  447. try
  448. {
  449. ifile->setFilePermissions(0666);
  450. }
  451. catch (...)
  452. {
  453. fprintf(stdout, "ifile->setFilePermissions() exception\n");
  454. }
  455. unsigned iter = nr / 40;
  456. if (iter < 1)
  457. iter = 1;
  458. __int64 pos = 0;
  459. for (unsigned i=0;i<nr;i++)
  460. {
  461. ifileio->write(pos, rs, record);
  462. pos += rs;
  463. if ((i % iter) == 0)
  464. {
  465. fprintf(stdout,".");
  466. fflush(NULL);
  467. }
  468. }
  469. ifileio->close();
  470. double rsec = (double)(msTick() - st)/1000.0;
  471. unsigned iorate = (unsigned)((double)fsize / rsec);
  472. fprintf(stdout, "\nwrite - elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);
  473. st = msTick();
  474. extraFlags = IFEcache;
  475. if (j==1)
  476. extraFlags = IFEnocache;
  477. ifileio = ifile->open(IFOread, extraFlags);
  478. pos = 0;
  479. for (unsigned i=0;i<nr;i++)
  480. {
  481. ifileio->read(pos, rs, record);
  482. pos += rs;
  483. if ((i % iter) == 0)
  484. {
  485. fprintf(stdout,".");
  486. fflush(NULL);
  487. }
  488. }
  489. ifileio->close();
  490. rsec = (double)(msTick() - st)/1000.0;
  491. iorate = (unsigned)((double)fsize / rsec);
  492. fprintf(stdout, "\nread -- elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);
  493. ifileio->Release();
  494. ifile->remove();
  495. ifile->Release();
  496. }
  497. }
  498. void testIOSmall()
  499. {
  500. testIO(nr10pct, NULL);
  501. }
  502. void testIOLarge()
  503. {
  504. testIO(nr150pct, NULL);
  505. }
  506. };
  507. class JlibFileIOTestStress : public JlibFileIOTestTiming
  508. {
  509. protected:
  510. CPPUNIT_TEST_SUITE( JlibFileIOTestStress );
  511. CPPUNIT_TEST(testIORemote);
  512. CPPUNIT_TEST_SUITE_END();
  513. void testIORemote()
  514. {
  515. const char * server = ".";
  516. testIO(nr10pct, server);
  517. }
  518. };
  519. CPPUNIT_TEST_SUITE_REGISTRATION( JlibFileIOTestTiming );
  520. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibFileIOTestTiming, "JlibFileIOTestTiming" );
  521. CPPUNIT_TEST_SUITE_REGISTRATION( JlibFileIOTestStress );
  522. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibFileIOTestTiming, "JlibFileIOTestStress" );
  523. /* =========================================================== */
  524. class JlibStringBufferTiming : public CppUnit::TestFixture
  525. {
  526. CPPUNIT_TEST_SUITE( JlibStringBufferTiming );
  527. CPPUNIT_TEST(testSwap);
  528. CPPUNIT_TEST_SUITE_END();
  529. public:
  530. void testSwap()
  531. {
  532. StringBuffer l;
  533. StringBuffer r;
  534. for (unsigned len=0; len<40; len++)
  535. {
  536. const unsigned numIter = 100000000;
  537. cycle_t start = get_cycles_now();
  538. for (unsigned pass=0; pass < numIter; pass++)
  539. {
  540. l.swapWith(r);
  541. }
  542. cycle_t elapsed = get_cycles_now() - start;
  543. DBGLOG("Each iteration of size %u took %.2f nanoseconds", len, (double)cycle_to_nanosec(elapsed) / numIter);
  544. l.append("a");
  545. r.append("b");
  546. }
  547. }
  548. };
  549. CPPUNIT_TEST_SUITE_REGISTRATION( JlibStringBufferTiming );
  550. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibStringBufferTiming, "JlibStringBufferTiming" );
  551. /* =========================================================== */
  552. static const unsigned split4_2[] = {0, 2, 4 };
  553. static const unsigned split100_2[] = {0, 50, 100 };
  554. static const unsigned split100_10[] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
  555. static const unsigned split7_10[] = {0,1,1,2,3,3,4,5,6,6,7 };
  556. static const unsigned split10_3[] = {0,3,7,10 };
  557. static const unsigned split58_10[] = {0,6,12,17,23,29,35,41,46,52,58 };
  558. static const unsigned split9_2T[] = { 0,5,9 };
  559. static const unsigned split9_2F[] = { 0,4,9 };
  560. static const unsigned split15_3[] = { 0,5,10,15 };
  561. class JlibQuantileTest : public CppUnit::TestFixture
  562. {
  563. CPPUNIT_TEST_SUITE( JlibQuantileTest );
  564. CPPUNIT_TEST(testQuantile);
  565. CPPUNIT_TEST(testRandom);
  566. CPPUNIT_TEST_SUITE_END();
  567. public:
  568. JlibQuantileTest()
  569. {
  570. }
  571. void testQuantilePos(unsigned numItems, unsigned numDivisions, bool roundUp, const unsigned * expected)
  572. {
  573. if (numDivisions == 0)
  574. return;
  575. QuantilePositionIterator iter(numItems, numDivisions, roundUp);
  576. QuantileFilterIterator filter(numItems, numDivisions, roundUp);
  577. unsigned prevPos = 0;
  578. iter.first();
  579. for (unsigned i=0; i <= numDivisions; i++)
  580. {
  581. //Check the values from the quantile iterator match those that are expected
  582. unsigned pos = (unsigned)iter.get();
  583. #if 0
  584. printf("(%d,%d) %d=%d\n", numItems, numDivisions, i, pos);
  585. #endif
  586. if (expected)
  587. CPPUNIT_ASSERT_EQUAL(expected[i], pos);
  588. //Check that the quantile filter correctly returns true and false for subsequent calls.
  589. while (prevPos < pos)
  590. {
  591. CPPUNIT_ASSERT(!filter.get());
  592. filter.next();
  593. prevPos++;
  594. }
  595. if (prevPos == pos)
  596. {
  597. CPPUNIT_ASSERT(filter.get());
  598. filter.next();
  599. prevPos++;
  600. }
  601. iter.next();
  602. }
  603. }
  604. void testQuantile()
  605. {
  606. testQuantilePos(4, 2, false, split4_2);
  607. testQuantilePos(100, 2, false, split100_2);
  608. testQuantilePos(100, 10, false, split100_10);
  609. testQuantilePos(7, 10, false, split7_10);
  610. testQuantilePos(10, 3, false, split10_3);
  611. testQuantilePos(10, 3, true, split10_3);
  612. testQuantilePos(58, 10, false, split58_10);
  613. //testQuantilePos(9, 2, true, split9_2T);
  614. testQuantilePos(9, 2, false, split9_2F);
  615. testQuantilePos(15, 3, false, split15_3);
  616. testQuantilePos(1231, 57, false, NULL);
  617. testQuantilePos(1, 63, false, NULL);
  618. testQuantilePos(10001, 17, false, NULL);
  619. }
  620. void testRandom()
  621. {
  622. //test various random combinations to ensure the results are consistent.
  623. for (unsigned i=0; i < 10; i++)
  624. testQuantilePos(random() % 1000000, random() % 10000, true, NULL);
  625. }
  626. };
  627. CPPUNIT_TEST_SUITE_REGISTRATION( JlibQuantileTest );
  628. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibQuantileTest, "JlibQuantileTest" );
  629. /* =========================================================== */
  630. class JlibReaderWriterTestTiming : public CppUnit::TestFixture
  631. {
  632. CPPUNIT_TEST_SUITE(JlibReaderWriterTestTiming);
  633. CPPUNIT_TEST(testCombinations);
  634. CPPUNIT_TEST_SUITE_END();
  635. const static unsigned spinScaling = 1000;
  636. static unsigned spinCalculation(unsigned prev, unsigned scale)
  637. {
  638. unsigned value = prev;
  639. for (unsigned i = 0; i < scale*spinScaling; i++)
  640. {
  641. value = (value * 0x1234FEDB + 0x87654321);
  642. }
  643. return value;
  644. }
  645. class Reader : public Thread
  646. {
  647. public:
  648. Reader(IRowQueue & _source, Semaphore & _doneSem, unsigned _workScale)
  649. : Thread("Reader"), source(_source), doneSem(_doneSem), workScale(_workScale), work(0)
  650. {
  651. }
  652. virtual int run()
  653. {
  654. for (;;)
  655. {
  656. const void * next;
  657. if (!source.dequeue(next))
  658. break;
  659. if (!next)
  660. break;
  661. std::atomic<byte> * value = (std::atomic<byte> *)next;
  662. (*value)++;
  663. if (workScale)
  664. work = spinCalculation(work, workScale);
  665. }
  666. doneSem.signal();
  667. return 0;
  668. }
  669. private:
  670. IRowQueue & source;
  671. Semaphore & doneSem;
  672. volatile unsigned work;
  673. unsigned workScale;
  674. };
  675. class WriterBase : public Thread
  676. {
  677. public:
  678. WriterBase(IRowQueue & _target, size_t _len, byte * _buffer, Semaphore & _startSem, Semaphore & _doneSem, unsigned _workScale)
  679. : Thread("Writer"), target(_target), len(_len), buffer(_buffer), startSem(_startSem), doneSem(_doneSem), workScale(_workScale), work(0)
  680. {
  681. }
  682. protected:
  683. size_t len;
  684. byte * buffer;
  685. IRowQueue & target;
  686. Semaphore & startSem;
  687. Semaphore & doneSem;
  688. volatile unsigned work;
  689. unsigned workScale;
  690. };
  691. class Writer : public WriterBase
  692. {
  693. public:
  694. Writer(IRowQueue & _target, size_t _len, byte * _buffer, Semaphore & _startSem, Semaphore & _doneSem, unsigned _workScale)
  695. : WriterBase(_target, _len, _buffer, _startSem, _doneSem, _workScale)
  696. {
  697. }
  698. virtual int run()
  699. {
  700. startSem.wait();
  701. for (size_t i = 0; i < len; i++)
  702. {
  703. if (workScale)
  704. work = spinCalculation(work, workScale);
  705. target.enqueue(buffer + i);
  706. }
  707. target.noteWriterStopped();
  708. doneSem.signal();
  709. return 0;
  710. }
  711. };
  712. public:
  713. const static size_t bufferSize = 0x100000;//0x100000*64;
  714. void testQueue(IRowQueue & queue, unsigned numProducers, unsigned numConsumers, unsigned queueElements, unsigned readerWork, unsigned writerWork)
  715. {
  716. const size_t sizePerProducer = bufferSize / numProducers;
  717. const size_t testSize = sizePerProducer * numProducers;
  718. OwnedMalloc<byte> buffer(bufferSize, true);
  719. Semaphore startSem;
  720. Semaphore writerDoneSem;
  721. Semaphore stopSem;
  722. Reader * * consumers = new Reader *[numConsumers];
  723. for (unsigned i2 = 0; i2 < numConsumers; i2++)
  724. {
  725. consumers[i2] = new Reader(queue, stopSem, readerWork);
  726. consumers[i2]->start();
  727. }
  728. WriterBase * * producers = new WriterBase *[numProducers];
  729. for (unsigned i1 = 0; i1 < numProducers; i1++)
  730. {
  731. producers[i1] = new Writer(queue, sizePerProducer, buffer + i1 * sizePerProducer, startSem, writerDoneSem, writerWork);
  732. producers[i1]->start();
  733. }
  734. cycle_t startTime = get_cycles_now();
  735. //Start the writers
  736. startSem.signal(numProducers);
  737. //Wait for the writers to complete
  738. for (unsigned i7 = 0; i7 < numProducers; i7++)
  739. writerDoneSem.wait();
  740. //Wait for the readers to complete
  741. for (unsigned i3 = 0; i3 < numConsumers; i3++)
  742. stopSem.wait();
  743. cycle_t stopTime = get_cycles_now();
  744. //All bytes should have been changed to 1, if not a queue item got lost.
  745. unsigned failures = 0;
  746. unsigned numClear = 0;
  747. size_t failPos = ~(size_t)0;
  748. byte failValue = 0;
  749. for (size_t pos = 0; pos < testSize; pos++)
  750. {
  751. if (buffer[pos] != 1)
  752. {
  753. failures++;
  754. if (failPos == ~(size_t)0)
  755. {
  756. failPos = pos;
  757. failValue = buffer[pos];
  758. }
  759. }
  760. if (buffer[pos] == 0)
  761. numClear++;
  762. }
  763. unsigned timeMs = cycle_to_nanosec(stopTime - startTime) / 1000000;
  764. unsigned expectedReadWorkTime = (unsigned)(((double)unitWorkTimeMs * readerWork) / numConsumers);
  765. unsigned expectedWriteWorkTime = (unsigned)(((double)unitWorkTimeMs * writerWork) / numProducers);
  766. unsigned expectedWorkTime = std::max(expectedReadWorkTime, expectedWriteWorkTime);
  767. if (failures)
  768. {
  769. printf("Fail: Test %u producers %u consumers %u queueItems %u(%u) mismatches fail(@%u=%u)\n", numProducers, numConsumers, queueElements, failures, numClear, (unsigned)failPos, failValue);
  770. ASSERT(failures == 0);
  771. }
  772. else
  773. printf("Pass: Test %u(@%u) producers %u(@%u) consumers %u queueItems in %ums [%dms]\n", numProducers, writerWork, numConsumers, readerWork, queueElements, timeMs, timeMs-expectedWorkTime);
  774. for (unsigned i4 = 0; i4 < numConsumers; i4++)
  775. {
  776. consumers[i4]->join();
  777. consumers[i4]->Release();
  778. }
  779. delete[] consumers;
  780. for (unsigned i5 = 0; i5 < numProducers; i5++)
  781. {
  782. producers[i5]->join();
  783. producers[i5]->Release();
  784. }
  785. delete[] producers;
  786. }
  787. void testQueue(unsigned numProducers, unsigned numConsumers, unsigned numElements = 0, unsigned readWork = 0, unsigned writeWork = 0)
  788. {
  789. unsigned queueElements = (numElements != 0) ? numElements : (numProducers + numConsumers) * 2;
  790. Owned<IRowQueue> queue = createRowQueue(numConsumers, numProducers, queueElements, 0);
  791. testQueue(*queue, numProducers, numConsumers, queueElements, readWork, writeWork);
  792. }
  793. void testWorkQueue(unsigned numProducers, unsigned numConsumers, unsigned numElements)
  794. {
  795. for (unsigned readWork = 1; readWork <= 8; readWork = readWork * 2)
  796. {
  797. for (unsigned writeWork = 1; writeWork <= 8; writeWork = writeWork * 2)
  798. {
  799. testQueue(numProducers, numConsumers, numElements, readWork, writeWork);
  800. }
  801. }
  802. }
  803. void testCombinations()
  804. {
  805. // 1:1
  806. for (unsigned i=0; i < 10; i++)
  807. testQueue(1, 1, 10);
  808. //One to Many
  809. testQueue(1, 10, 5);
  810. testQueue(1, 5, 5);
  811. testQueue(1, 5, 10);
  812. testQueue(1, 127, 10);
  813. testQueue(1, 127, 127);
  814. //Many to One
  815. testQueue(10, 1, 5);
  816. testQueue(5, 1, 5);
  817. testQueue(5, 1, 10);
  818. testQueue(127, 1, 127);
  819. cycle_t startTime = get_cycles_now();
  820. volatile unsigned value = 0;
  821. for (unsigned pass = 0; pass < 10; pass++)
  822. {
  823. for (unsigned i2 = 0; i2 < bufferSize; i2++)
  824. value = spinCalculation(value, 1);
  825. }
  826. cycle_t stopTime = get_cycles_now();
  827. unitWorkTimeMs = cycle_to_nanosec(stopTime - startTime) / (1000000 * 10);
  828. printf("Work(1) takes %ums\n", unitWorkTimeMs);
  829. //How does it scale with number of queue elements?
  830. for (unsigned elem = 16; elem < 256; elem *= 2)
  831. {
  832. testQueue(16, 1, elem, 1, 1);
  833. }
  834. #if 1
  835. //Many to Many
  836. for (unsigned readWork = 1; readWork <= 8; readWork = readWork * 2)
  837. {
  838. for (unsigned writeWork = 1; writeWork <= 8; writeWork = writeWork * 2)
  839. {
  840. testQueue(1, 1, 63, readWork, writeWork);
  841. testQueue(1, 2, 63, readWork, writeWork);
  842. testQueue(1, 4, 63, readWork, writeWork);
  843. testQueue(1, 8, 63, readWork, writeWork);
  844. testQueue(1, 16, 63, readWork, writeWork);
  845. testQueue(2, 1, 63, readWork, writeWork);
  846. testQueue(4, 1, 63, readWork, writeWork);
  847. testQueue(8, 1, 63, readWork, writeWork);
  848. testQueue(16, 1, 63, readWork, writeWork);
  849. testQueue(2, 2, 63, readWork, writeWork);
  850. testQueue(4, 4, 63, readWork, writeWork);
  851. testQueue(8, 8, 63, readWork, writeWork);
  852. testQueue(16, 8, 63, readWork, writeWork);
  853. testQueue(16, 16, 63, readWork, writeWork);
  854. testQueue(32, 1, 63, readWork, writeWork);
  855. testQueue(64, 1, 63, readWork, writeWork);
  856. testQueue(1, 32, 63, readWork, writeWork);
  857. testQueue(1, 64, 63, readWork, writeWork);
  858. }
  859. }
  860. #else
  861. //Many to Many
  862. testWorkQueue(1, 1, 63);
  863. testWorkQueue(1, 2, 63);
  864. testWorkQueue(1, 4, 63);
  865. testWorkQueue(1, 8, 63);
  866. testWorkQueue(1, 16, 63);
  867. testWorkQueue(2, 1, 63);
  868. testWorkQueue(4, 1, 63);
  869. testWorkQueue(8, 1, 63);
  870. testWorkQueue(16, 1, 63);
  871. testWorkQueue(2, 2, 63);
  872. testWorkQueue(4, 4, 63);
  873. testWorkQueue(8, 8, 63);
  874. #endif
  875. testQueue(2, 2, 4);
  876. testQueue(2, 2, 8);
  877. testQueue(2, 2, 16);
  878. testQueue(2, 2, 32);
  879. testQueue(2, 2, 100);
  880. }
  881. protected:
  882. unsigned unitWorkTimeMs;
  883. };
  884. CPPUNIT_TEST_SUITE_REGISTRATION(JlibReaderWriterTestTiming);
  885. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(JlibReaderWriterTestTiming, "JlibReaderWriterTestTiming");
  886. /* =========================================================== */
  887. class JlibWildMatchBase : public CppUnit::TestFixture
  888. {
  889. protected:
  890. void testSet(unsigned length, const char * const * patterns, bool reportTiming)
  891. {
  892. std::unique_ptr<char[]> search(generateSearchString(length));
  893. CCycleTimer timer;
  894. testPatterns(search.get(), patterns);
  895. if (reportTiming)
  896. printf("%u: %u ms\n", length, timer.elapsedMs());
  897. }
  898. char * generateSearchString(size_t len)
  899. {
  900. char * target = new char[len+1];
  901. fillSearchString(target, len);
  902. target[len] = 0;
  903. return target;
  904. }
  905. void fillSearchString(char * target, size_t len)
  906. {
  907. for (unsigned repeat=0; ; repeat++)
  908. {
  909. for (unsigned char fill = 'a'; fill <= 'z'; fill++)
  910. {
  911. for (unsigned i=0; i < repeat; i++)
  912. {
  913. *target++ = fill;
  914. if (--len == 0)
  915. return;
  916. }
  917. }
  918. }
  919. }
  920. void testPatterns(const char * search, const char * const * patterns)
  921. {
  922. for (const char * const * cur = patterns; *cur; cur++)
  923. {
  924. const char * pattern = *cur;
  925. bool expected = true;
  926. bool nocase = false;
  927. if (*pattern == '!')
  928. {
  929. expected = false;
  930. pattern++;
  931. }
  932. if (*pattern == '~')
  933. {
  934. nocase = true;
  935. pattern++;
  936. }
  937. bool evaluated = WildMatch(search, pattern, nocase);
  938. CPPUNIT_ASSERT_EQUAL_MESSAGE(pattern, expected, evaluated);
  939. }
  940. }
  941. };
  942. const char * const patterns10 [] = {
  943. "!a",
  944. "abcdefghij",
  945. "??????????",
  946. "?*c?*e*",
  947. "!??*b?*h*",
  948. "a*",
  949. "*j",
  950. "a*j",
  951. "a**j",
  952. "a***************j",
  953. "abcde*fghij",
  954. "!abcde*?*fghij",
  955. "*a*j*",
  956. "*a*c*e*g*j*",
  957. "a?c?e?g??j",
  958. "a?c?e?g?*?j",
  959. "!~A",
  960. "!A*",
  961. "~A*",
  962. "~*J",
  963. "~A*J",
  964. "~A**J",
  965. "~A***************J",
  966. "~*A*J*",
  967. "~*A*C*E*G*J*",
  968. "~*A*B*C*D*E*F*G*H*I*J*",
  969. "~*A*?*?*?*J*",
  970. "~*A*?C*?E*?*J*",
  971. "~*A*C?*E?*?*J*",
  972. "!~*A*.B*C*D*E*F*G*H*I*J*",
  973. nullptr
  974. };
  975. const char * const patterns100 [] = {
  976. "a*",
  977. "*h",
  978. "a*h",
  979. "a**h",
  980. "a***************h",
  981. "*a*j*",
  982. "*a*c*e*g*j*",
  983. "!a*jj*fff",
  984. "!a*jj*zzz",
  985. "a*jj*fff*",
  986. "*aa*jj*fff*",
  987. "!a*jj*zy*",
  988. nullptr
  989. };
  990. const char * const patternsLarge [] = {
  991. "!*a*zy*",
  992. "a*",
  993. "a*h*",
  994. "!a*jj*ab",
  995. "!a*jj*zy",
  996. "a*jj*fff*",
  997. "!a*jj*zy*",
  998. /* "!a*c*e*g*i*k*zy*", will completely destroy the performance*/
  999. nullptr
  1000. };
  1001. class JlibWildMatchCore : public JlibWildMatchBase
  1002. {
  1003. CPPUNIT_TEST_SUITE(JlibWildMatchCore);
  1004. CPPUNIT_TEST(testWildMatch);
  1005. CPPUNIT_TEST_SUITE_END();
  1006. public:
  1007. void testWildMatch()
  1008. {
  1009. testSet(10, patterns10, false);
  1010. testSet(100, patterns100, false);
  1011. testSet(1000, patternsLarge, false);
  1012. }
  1013. };
  1014. CPPUNIT_TEST_SUITE_REGISTRATION(JlibWildMatchCore);
  1015. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(JlibWildMatchCore, "JlibWildMatchCore");
  1016. class JlibWildMatchTiming : public JlibWildMatchBase
  1017. {
  1018. CPPUNIT_TEST_SUITE(JlibWildMatchTiming);
  1019. CPPUNIT_TEST(testWildMatch);
  1020. CPPUNIT_TEST_SUITE_END();
  1021. public:
  1022. void testWildMatch()
  1023. {
  1024. testSet(10000, patternsLarge, true);
  1025. testSet(100000, patternsLarge, true);
  1026. testSet(1000000, patternsLarge, true);
  1027. }
  1028. };
  1029. CPPUNIT_TEST_SUITE_REGISTRATION(JlibWildMatchTiming);
  1030. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(JlibWildMatchTiming, "JlibWildMatchTiming");
  1031. const EnumMapping mapping[] = {
  1032. { 1, "one" },
  1033. { 3, "three" },
  1034. { 5, "five" },
  1035. {0, nullptr }
  1036. };
  1037. const char * strings[] = { "zero", "one", "two", "three", "four", nullptr };
  1038. class JlibMapping : public CppUnit::TestFixture
  1039. {
  1040. CPPUNIT_TEST_SUITE(JlibMapping);
  1041. CPPUNIT_TEST(testEnum);
  1042. CPPUNIT_TEST(testMatch);
  1043. CPPUNIT_TEST_SUITE_END();
  1044. public:
  1045. void testEnum()
  1046. {
  1047. CPPUNIT_ASSERT(streq("one", getEnumText(1, mapping)));
  1048. CPPUNIT_ASSERT(streq("three", getEnumText(3, mapping)));
  1049. CPPUNIT_ASSERT(streq("five", getEnumText(5, mapping)));
  1050. CPPUNIT_ASSERT(streq("two", getEnumText(2, mapping, "two")));
  1051. CPPUNIT_ASSERT(!getEnumText(2, mapping, nullptr));
  1052. CPPUNIT_ASSERT_EQUAL(1, getEnum("one", mapping));
  1053. CPPUNIT_ASSERT_EQUAL(3, getEnum("three", mapping));
  1054. CPPUNIT_ASSERT_EQUAL(5, getEnum("five", mapping));
  1055. CPPUNIT_ASSERT_EQUAL(99, getEnum("seven", mapping, 99));
  1056. }
  1057. void testMatch()
  1058. {
  1059. CPPUNIT_ASSERT_EQUAL(0U, matchString("zero", strings));
  1060. CPPUNIT_ASSERT_EQUAL(1U, matchString("one", strings));
  1061. CPPUNIT_ASSERT_EQUAL(4U, matchString("four", strings));
  1062. CPPUNIT_ASSERT_EQUAL(UINT_MAX, matchString("ten", strings));
  1063. }
  1064. };
  1065. CPPUNIT_TEST_SUITE_REGISTRATION(JlibMapping);
  1066. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(JlibMapping, "JlibMapping");
  1067. class JlibIPTTest : public CppUnit::TestFixture
  1068. {
  1069. CPPUNIT_TEST_SUITE(JlibIPTTest);
  1070. CPPUNIT_TEST(test);
  1071. CPPUNIT_TEST_SUITE_END();
  1072. public:
  1073. void test()
  1074. {
  1075. Owned<IPropertyTree> testTree = createPTreeFromXMLString(
  1076. "<cpptest attr1='attrval1' attr2='attrval2'>"
  1077. " <sub1 subattr1='sav1'>sub1val</sub1>"
  1078. " <sub2 subattr2='sav2'>sub2val</sub2>"
  1079. " <subX subattr3='sav3'>subXval</subX>"
  1080. " <item a='1' b='2' c='3' d='4'/>"
  1081. " <item a='2'/>"
  1082. " <item a='3'/>"
  1083. " <array>"
  1084. " <valX>x</valX>"
  1085. " <valX>x</valX>"
  1086. " <valY>y</valY>"
  1087. " <valY>y</valY>"
  1088. " <valZ>z</valZ>"
  1089. " </array>"
  1090. " <binprop bin='1' xsi:type='SOAP-ENC:base64'>CwAAAA==</binprop>"
  1091. "cpptestval"
  1092. "</cpptest>");
  1093. MemoryBuffer mb;
  1094. mb.reserveTruncate(4*1024+1); // Must be > PTREE_COMPRESS_THRESHOLD (see top of jptree.cpp)
  1095. testTree->addProp("binprop/subbinprop", "nonbinval1");
  1096. testTree->addPropBin("binprop/subbinprop", mb.length(), mb.toByteArray());
  1097. testTree->addProp("binprop/subbinprop", "nonbinval2");
  1098. testTree->addPropBin("binprop/subbinprop", mb.length(), mb.toByteArray());
  1099. // test some sets in prep. for 'get' tests
  1100. CPPUNIT_ASSERT(testTree->renameProp("subX", "subY"));
  1101. IPropertyTree *subY = testTree->queryPropTree("subY");
  1102. CPPUNIT_ASSERT(testTree->renameTree(subY, "sub3"));
  1103. IPropertyTree *subtest = testTree->setPropTree("subtest");
  1104. subtest = testTree->addPropTree("subtest", createPTree());
  1105. CPPUNIT_ASSERT(subtest != nullptr);
  1106. subtest = testTree->queryPropTree("subtest[2]");
  1107. CPPUNIT_ASSERT(subtest != nullptr);
  1108. subtest->setProp("str", "str1");
  1109. subtest->addProp("str", "str2");
  1110. subtest->appendProp("str[2]", "-more");
  1111. subtest->setPropBool("bool", true);
  1112. subtest->addPropBool("bool", false);
  1113. subtest->setPropInt("int", 1);
  1114. subtest->addPropInt("int", 2);
  1115. subtest->setPropInt64("int64", 1);
  1116. subtest->addPropInt64("int64", 2);
  1117. mb.clear().append("binstr1");
  1118. subtest->setPropBin("bin", mb.length(), mb.toByteArray());
  1119. mb.clear().append("binstr2");
  1120. subtest->addPropBin("bin", mb.length(), mb.toByteArray());
  1121. mb.clear().append("-more");
  1122. subtest->appendPropBin("bin[1]", mb.length(), mb.toByteArray());
  1123. // position insertion.
  1124. testTree->addProp("newprop", "v1");
  1125. testTree->addProp("newprop", "v2");
  1126. testTree->addProp("newprop[2]", "v3");
  1127. CPPUNIT_ASSERT(streq("v3", testTree->queryProp("newprop[2]")));
  1128. CPPUNIT_ASSERT(testTree->hasProp("sub1"));
  1129. CPPUNIT_ASSERT(testTree->hasProp("sub1/@subattr1"));
  1130. CPPUNIT_ASSERT(testTree->hasProp("sub2/@subattr2"));
  1131. CPPUNIT_ASSERT(testTree->hasProp("@attr1"));
  1132. CPPUNIT_ASSERT(!testTree->isBinary("@attr1"));
  1133. CPPUNIT_ASSERT(!testTree->isBinary("sub1"));
  1134. CPPUNIT_ASSERT(testTree->isBinary("binprop"));
  1135. CPPUNIT_ASSERT(!testTree->isCompressed("binprop"));
  1136. CPPUNIT_ASSERT(!testTree->isBinary("binprop/subbinprop[1]"));
  1137. CPPUNIT_ASSERT(testTree->isBinary("binprop/subbinprop[2]"));
  1138. CPPUNIT_ASSERT(!testTree->isCompressed("binprop/subbinprop[3]"));
  1139. CPPUNIT_ASSERT(testTree->isCompressed("binprop/subbinprop[4]"));
  1140. // testing if subX was renamed correctly
  1141. CPPUNIT_ASSERT(!testTree->hasProp("subX"));
  1142. CPPUNIT_ASSERT(!testTree->hasProp("subY"));
  1143. CPPUNIT_ASSERT(testTree->hasProp("sub3"));
  1144. StringBuffer astr;
  1145. CPPUNIT_ASSERT(testTree->getProp("sub1", astr));
  1146. CPPUNIT_ASSERT(streq("sub1val", astr.str()));
  1147. CPPUNIT_ASSERT(streq("sub2val", testTree->queryProp("sub2")));
  1148. subtest = testTree->queryPropTree("subtest[2]");
  1149. CPPUNIT_ASSERT(subtest != nullptr);
  1150. CPPUNIT_ASSERT(subtest->getPropBool("bool[1]"));
  1151. CPPUNIT_ASSERT(!subtest->getPropBool("bool[2]"));
  1152. CPPUNIT_ASSERT(1 == subtest->getPropInt("int[1]"));
  1153. CPPUNIT_ASSERT(2 == subtest->getPropInt("int[2]"));
  1154. CPPUNIT_ASSERT(1 == subtest->getPropInt64("int64[1]"));
  1155. CPPUNIT_ASSERT(2 == subtest->getPropInt64("int64[2]"));
  1156. subtest->getPropBin("bin[1]", mb.clear());
  1157. const char *ptr = (const char *)mb.toByteArray();
  1158. CPPUNIT_ASSERT(streq("binstr1", ptr)); // NB: null terminator was added at set time.
  1159. CPPUNIT_ASSERT(streq("-more", ptr+strlen("binstr1")+1)); // NB: null terminator was added at set time.
  1160. subtest->getPropBin("bin[2]", mb.clear());
  1161. CPPUNIT_ASSERT(streq("binstr2", mb.toByteArray())); // NB: null terminator was added at set time.
  1162. CPPUNIT_ASSERT(testTree->hasProp("subtest/bin[2]"));
  1163. CPPUNIT_ASSERT(testTree->removeProp("subtest/bin[2]"));
  1164. CPPUNIT_ASSERT(!testTree->hasProp("subtest/bin[2]"));
  1165. CPPUNIT_ASSERT(testTree->hasProp("subtest"));
  1166. CPPUNIT_ASSERT(testTree->removeTree(subtest)); // this is subtest[2]
  1167. subtest = testTree->queryPropTree("subtest"); // now just 1
  1168. CPPUNIT_ASSERT(testTree->removeTree(subtest));
  1169. CPPUNIT_ASSERT(!testTree->hasProp("subtest"));
  1170. IPropertyTree *item3 = testTree->queryPropTree("item[@a='3']");
  1171. CPPUNIT_ASSERT(nullptr != item3);
  1172. CPPUNIT_ASSERT(2 == testTree->queryChildIndex(item3));
  1173. CPPUNIT_ASSERT(streq("item", item3->queryName()));
  1174. Owned<IPropertyTreeIterator> iter = testTree->getElements("item");
  1175. unsigned a=1;
  1176. ForEach(*iter)
  1177. {
  1178. CPPUNIT_ASSERT(a == iter->query().getPropInt("@a"));
  1179. ++a;
  1180. }
  1181. Owned<IAttributeIterator> attrIter = testTree->queryPropTree("item[1]")->getAttributes();
  1182. CPPUNIT_ASSERT(4 == attrIter->count());
  1183. unsigned i = 0;
  1184. ForEach(*attrIter)
  1185. {
  1186. const char *name = attrIter->queryName();
  1187. const char *val = attrIter->queryValue();
  1188. CPPUNIT_ASSERT('a'+i == *(name+1));
  1189. CPPUNIT_ASSERT('1'+i == *val);
  1190. ++i;
  1191. }
  1192. IPropertyTree *array = testTree->queryPropTree("array");
  1193. CPPUNIT_ASSERT(array != nullptr);
  1194. CPPUNIT_ASSERT(array->hasChildren());
  1195. CPPUNIT_ASSERT(3 == array->numUniq());
  1196. CPPUNIT_ASSERT(5 == array->numChildren());
  1197. CPPUNIT_ASSERT(!testTree->isCaseInsensitive());
  1198. CPPUNIT_ASSERT(3 == testTree->getCount("sub*"));
  1199. }
  1200. };
  1201. CPPUNIT_TEST_SUITE_REGISTRATION(JlibIPTTest);
  1202. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(JlibIPTTest, "JlibIPTTest");
  1203. #include "jdebug.hpp"
  1204. #include "jmutex.hpp"
  1205. class AtomicTimingTest : public CppUnit::TestFixture
  1206. {
  1207. CPPUNIT_TEST_SUITE(AtomicTimingTest);
  1208. CPPUNIT_TEST(runAllTests);
  1209. CPPUNIT_TEST_SUITE_END();
  1210. public:
  1211. class CasCounter
  1212. {
  1213. public:
  1214. CasCounter() = default;
  1215. CasCounter(unsigned __int64 _value) : value{_value} {}
  1216. operator unsigned __int64() { return value; }
  1217. unsigned __int64 operator = (unsigned __int64 _value)
  1218. {
  1219. value = _value;
  1220. return _value;
  1221. }
  1222. unsigned __int64 operator ++(int)
  1223. {
  1224. unsigned __int64 expected = value.load();
  1225. while (!value.compare_exchange_weak(expected, expected + 1))
  1226. {
  1227. }
  1228. return expected+1;
  1229. }
  1230. std::atomic<unsigned __int64> value = { 0 };
  1231. };
  1232. template <typename LOCK, typename BLOCK, typename COUNTER, unsigned NUMVALUES, unsigned NUMLOCKS>
  1233. class LockTester
  1234. {
  1235. public:
  1236. LockTester()
  1237. {
  1238. value1 = 0;
  1239. }
  1240. class LockTestThread : public Thread
  1241. {
  1242. public:
  1243. LockTestThread(Semaphore & _startSem, Semaphore & _endSem, LOCK & _lock1, COUNTER & _value1, LOCK & _lock2, COUNTER * _extraValues, unsigned _numIterations)
  1244. : startSem(_startSem), endSem(_endSem),
  1245. lock1(_lock1), value1(_value1),
  1246. lock2(_lock2), extraValues(_extraValues),
  1247. numIterations(_numIterations)
  1248. {
  1249. }
  1250. virtual void execute()
  1251. {
  1252. {
  1253. BLOCK block(lock1);
  1254. value1++;
  1255. if (NUMVALUES >= 2)
  1256. extraValues[1]++;
  1257. if (NUMVALUES >= 3)
  1258. extraValues[2]++;
  1259. if (NUMVALUES >= 4)
  1260. extraValues[3]++;
  1261. if (NUMVALUES >= 5)
  1262. extraValues[4]++;
  1263. }
  1264. if (NUMLOCKS == 2)
  1265. {
  1266. BLOCK block(lock2);
  1267. extraValues[1]++;
  1268. }
  1269. }
  1270. virtual int run()
  1271. {
  1272. startSem.wait();
  1273. for (unsigned i = 0; i < numIterations; i++)
  1274. execute();
  1275. endSem.signal();
  1276. return 0;
  1277. }
  1278. protected:
  1279. Semaphore & startSem;
  1280. Semaphore & endSem;
  1281. LOCK & lock1;
  1282. LOCK & lock2;
  1283. COUNTER & value1;
  1284. COUNTER * extraValues;
  1285. const unsigned numIterations;
  1286. };
  1287. unsigned __int64 run(const char * title, unsigned numThreads, unsigned numIterations)
  1288. {
  1289. value1 = 0;
  1290. for (unsigned ix = 1; ix < NUMVALUES; ix++)
  1291. extraValues[ix] = 0;
  1292. for (unsigned i = 0; i < numThreads; i++)
  1293. {
  1294. LockTestThread * next = new LockTestThread(startSem, endSem, lock, value1, lock, extraValues, numIterations);
  1295. threads.append(*next);
  1296. next->start();
  1297. }
  1298. cycle_t startCycles = get_cycles_now();
  1299. startSem.signal(numThreads);
  1300. for (unsigned i2 = 0; i2 < numThreads; i2++)
  1301. endSem.wait();
  1302. cycle_t endCycles = get_cycles_now();
  1303. unsigned __int64 expected = (unsigned __int64)numIterations * numThreads;
  1304. unsigned __int64 averageTime = cycle_to_nanosec(endCycles - startCycles) / (numIterations * numThreads);
  1305. printf("%s@%u/%u threads(%u) %" I64F "uns/iteration lost(%" I64F "d)\n", title, NUMVALUES, NUMLOCKS, numThreads, averageTime, expected - value1);
  1306. for (unsigned i3 = 0; i3 < numThreads; i3++)
  1307. threads.item(i3).join();
  1308. return averageTime;
  1309. }
  1310. protected:
  1311. CIArrayOf<LockTestThread> threads;
  1312. Semaphore startSem;
  1313. Semaphore endSem;
  1314. LOCK lock;
  1315. COUNTER value1;
  1316. COUNTER extraValues[NUMVALUES];
  1317. };
  1318. #define DO_TEST(LOCK, CLOCK, COUNTER, NUMVALUES, NUMLOCKS) \
  1319. { \
  1320. const char * title = #LOCK "," #COUNTER;\
  1321. LockTester<LOCK, CLOCK, COUNTER, NUMVALUES, NUMLOCKS> tester;\
  1322. uncontendedTimes.append(tester.run(title, 1, numIterations));\
  1323. minorTimes.append(tester.run(title, 2, numIterations));\
  1324. typicalTimes.append(tester.run(title, numCores / 2, numIterations));\
  1325. tester.run(title, numCores, numIterations);\
  1326. tester.run(title, numCores + 1, numIterations);\
  1327. contendedTimes.append(tester.run(title, numCores * 2, numIterations));\
  1328. }
  1329. //Use to common out a test
  1330. #define XDO_TEST(LOCK, CLOCK, COUNTER, NUMVALUES, NUMLOCKS) \
  1331. { \
  1332. uncontendedTimes.append(0);\
  1333. minorTimes.append(0);\
  1334. typicalTimes.append(0);\
  1335. contendedTimes.append(0);\
  1336. }
  1337. class Null
  1338. {};
  1339. const unsigned numIterations = 1000000;
  1340. const unsigned numCores = getAffinityCpus();
  1341. void runAllTests()
  1342. {
  1343. DO_TEST(CriticalSection, CriticalBlock, unsigned __int64, 1, 1);
  1344. DO_TEST(CriticalSection, CriticalBlock, unsigned __int64, 2, 1);
  1345. DO_TEST(CriticalSection, CriticalBlock, unsigned __int64, 5, 1);
  1346. DO_TEST(CriticalSection, CriticalBlock, unsigned __int64, 1, 2);
  1347. DO_TEST(SpinLock, SpinBlock, unsigned __int64, 1, 1);
  1348. DO_TEST(SpinLock, SpinBlock, unsigned __int64, 2, 1);
  1349. DO_TEST(SpinLock, SpinBlock, unsigned __int64, 5, 1);
  1350. DO_TEST(SpinLock, SpinBlock, unsigned __int64, 1, 2);
  1351. DO_TEST(Null, Null, std::atomic<unsigned __int64>, 1, 1);
  1352. DO_TEST(Null, Null, std::atomic<unsigned __int64>, 2, 1);
  1353. DO_TEST(Null, Null, std::atomic<unsigned __int64>, 5, 1);
  1354. DO_TEST(Null, Null, std::atomic<unsigned __int64>, 1, 2);
  1355. DO_TEST(Null, Null, RelaxedAtomic<unsigned __int64>, 1, 1);
  1356. DO_TEST(Null, Null, RelaxedAtomic<unsigned __int64>, 5, 1);
  1357. DO_TEST(Null, Null, CasCounter, 1, 1);
  1358. DO_TEST(Null, Null, CasCounter, 5, 1);
  1359. DO_TEST(Null, Null, unsigned __int64, 1, 1);
  1360. DO_TEST(Null, Null, unsigned __int64, 2, 1);
  1361. DO_TEST(Null, Null, unsigned __int64, 5, 1);
  1362. //Read locks will fail to prevent values being lost, but the timings are useful in comparison with CriticalSection
  1363. DO_TEST(ReadWriteLock, ReadLockBlock, unsigned __int64, 1, 1);
  1364. DO_TEST(ReadWriteLock, ReadLockBlock, unsigned __int64, 2, 1);
  1365. DO_TEST(ReadWriteLock, ReadLockBlock, unsigned __int64, 5, 1);
  1366. DO_TEST(ReadWriteLock, ReadLockBlock, unsigned __int64, 1, 2);
  1367. DO_TEST(ReadWriteLock, WriteLockBlock, unsigned __int64, 1, 1);
  1368. DO_TEST(ReadWriteLock, WriteLockBlock, unsigned __int64, 2, 1);
  1369. DO_TEST(ReadWriteLock, WriteLockBlock, unsigned __int64, 5, 1);
  1370. DO_TEST(ReadWriteLock, WriteLockBlock, unsigned __int64, 1, 2);
  1371. printf("Summary\n");
  1372. summariseTimings("Uncontended", uncontendedTimes);
  1373. summariseTimings("Minor", minorTimes);
  1374. summariseTimings("Typical", typicalTimes);
  1375. summariseTimings("Over", contendedTimes);
  1376. }
  1377. void summariseTimings(const char * option, UInt64Array & times)
  1378. {
  1379. printf("%11s 1x: cs(%3" I64F "u) spin(%3" I64F "u) atomic(%3" I64F "u) ratomic(%3" I64F "u) cas(%3" I64F "u) rd(%3" I64F "u) wr(%3" I64F "u) "
  1380. "5x: cs(%3" I64F "u) spin(%3" I64F "u) atomic(%3" I64F "u) ratomic(%3" I64F "u) cas(%3" I64F "u) rd(%3" I64F "u) wr(%3" I64F "u)\n", option,
  1381. times.item(0), times.item(4), times.item(8), times.item(12), times.item(14), times.item(19), times.item(23),
  1382. times.item(2), times.item(6), times.item(10), times.item(13), times.item(15), times.item(21), times.item(25));
  1383. }
  1384. private:
  1385. UInt64Array uncontendedTimes;
  1386. UInt64Array minorTimes;
  1387. UInt64Array typicalTimes;
  1388. UInt64Array contendedTimes;
  1389. };
  1390. CPPUNIT_TEST_SUITE_REGISTRATION(AtomicTimingTest);
  1391. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(AtomicTimingTest, "AtomicTimingTest");
  1392. //=====================================================================================================================
  1393. class MachineInfoTimingTest : public CppUnit::TestFixture
  1394. {
  1395. CPPUNIT_TEST_SUITE(MachineInfoTimingTest);
  1396. CPPUNIT_TEST(runAllTests);
  1397. CPPUNIT_TEST_SUITE_END();
  1398. public:
  1399. void getSystemTiming()
  1400. {
  1401. const unsigned num = 10000;
  1402. CpuInfo temp;
  1403. CCycleTimer timer;
  1404. for (unsigned i=0; i < num; i++)
  1405. temp.getSystemTimes();
  1406. printf("Time to get system cpu activity = %" I64F "uns\n", timer.elapsedNs()/num);
  1407. }
  1408. void getProcessTiming()
  1409. {
  1410. const unsigned num = 10000;
  1411. CpuInfo temp;
  1412. CCycleTimer timer;
  1413. for (unsigned i=0; i < num; i++)
  1414. temp.getProcessTimes();
  1415. printf("Time to get process cpu activity = %" I64F "uns\n", timer.elapsedNs()/num);
  1416. }
  1417. void runAllTests()
  1418. {
  1419. getSystemTiming();
  1420. getSystemTiming(); // Second call seems to be faster - so more representative
  1421. getProcessTiming();
  1422. getProcessTiming(); // Second call seems to be faster - so more representative
  1423. CpuInfo prevSystem;
  1424. CpuInfo prevProcess;
  1425. CpuInfo curProcess(true, false);
  1426. CpuInfo curSystem(false, true);
  1427. volatile unsigned x = 0;
  1428. for (unsigned i=0; i < 10; i++)
  1429. {
  1430. prevProcess = curProcess;
  1431. prevSystem = curSystem;
  1432. curProcess.getProcessTimes();
  1433. curSystem.getSystemTimes();
  1434. CpuInfo deltaProcess = curProcess - prevProcess;
  1435. CpuInfo deltaSystem = curSystem - prevSystem;
  1436. if (deltaSystem.getTotalNs())
  1437. {
  1438. printf(" System: User(%u) System(%u) Total(%u) %u%% Ctx(%" I64F "u) ",
  1439. (unsigned)(deltaSystem.getUserNs() / 1000000), (unsigned)(deltaSystem.getSystemNs() / 1000000), (unsigned)(deltaSystem.getTotalNs() / 1000000),
  1440. (unsigned)((deltaSystem.getUserNs() * 100) / deltaSystem.getTotalNs()), deltaSystem.getNumContextSwitches());
  1441. printf(" Process: User(%u) System(%u) Total(%u) %u%% Ctx(%" I64F "u)\n",
  1442. (unsigned)(deltaProcess.getUserNs() / 1000000), (unsigned)(deltaProcess.getSystemNs() / 1000000), (unsigned)(deltaProcess.getTotalNs() / 1000000),
  1443. (unsigned)((deltaProcess.getUserNs() * 100) / deltaSystem.getTotalNs()), deltaProcess.getNumContextSwitches());
  1444. }
  1445. for (unsigned j=0; j < i*100000000; j++)
  1446. x += j*j;
  1447. Sleep(1000);
  1448. }
  1449. }
  1450. };
  1451. CPPUNIT_TEST_SUITE_REGISTRATION(MachineInfoTimingTest);
  1452. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(MachineInfoTimingTest, "MachineInfoTimingTest");
  1453. class JlibIOTest : public CppUnit::TestFixture
  1454. {
  1455. CPPUNIT_TEST_SUITE(JlibIOTest);
  1456. CPPUNIT_TEST(test);
  1457. CPPUNIT_TEST_SUITE_END();
  1458. public:
  1459. void test()
  1460. {
  1461. unsigned numTestLines = 1000;
  1462. const char *newlines[] = { "\n", "\r\n" };
  1463. for (unsigned pEol=0; pEol<2; pEol++) // twice, once for preserveEols=false, once for preserveEols=true
  1464. {
  1465. for (unsigned nl=0; nl<2; nl++) // twice, once for each type of newline
  1466. {
  1467. const char *testTxt = " : Some random text for test line";
  1468. OwnedIFile iFile = createIFile("JlibIOTest.txt");
  1469. CRC32 writeCrc, readCrc;
  1470. {
  1471. OwnedIFileIO iFileIO = iFile->open(IFOcreate);
  1472. OwnedIFileIOStream stream = createIOStream(iFileIO);
  1473. for (unsigned l=0; l<numTestLines; l++)
  1474. {
  1475. VStringBuffer line("%u%s%s", l+1, testTxt, newlines[nl]);
  1476. stream->write(line.length(), line.str());
  1477. writeCrc.tally(line.length(), line.str());
  1478. }
  1479. }
  1480. {
  1481. OwnedIFileIO iFileIO = iFile->open(IFOread);
  1482. OwnedIFileIOStream stream = createIOStream(iFileIO); // NB: unbuffered
  1483. Owned<IStreamLineReader> lineReader = createLineReader(stream, 0==pEol, strlen(testTxt)); // NB: deliberately make chunkSize small so will end up having to read more
  1484. while (true)
  1485. {
  1486. StringBuffer line;
  1487. if (!lineReader->readLine(line))
  1488. {
  1489. if (pEol==1)
  1490. line.append(newlines[nl]);
  1491. readCrc.tally(line.length(), line.str());
  1492. }
  1493. else
  1494. break;
  1495. }
  1496. }
  1497. CPPUNIT_ASSERT(writeCrc.get() == readCrc.get());
  1498. }
  1499. }
  1500. }
  1501. };
  1502. CPPUNIT_TEST_SUITE_REGISTRATION(JlibIOTest);
  1503. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(JlibIOTest, "JlibIOTest");
  1504. #endif // _USE_CPPUNIT