18 #include <ndb_global.h>
22 #include <HugoTransactions.hpp>
23 #include <Bitmask.hpp>
26 static const char* _dbname =
"TEST_DB";
27 static int g_loops = 7;
31 NDB_STD_OPTS(
"ndb_desc"),
32 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
41 static int testBitmask();
44 main(
int argc,
char** argv){
46 const char *load_default_groups[]= {
"mysql_cluster",0 };
47 load_defaults(
"my",load_default_groups,&argc,&argv);
50 if ((ho_error=handle_options(&argc, &argv, my_long_options,
51 ndb_std_get_one_option)))
52 return NDBT_ProgramExit(NDBT_WRONGARGS);
54 int res = NDBT_FAILED;
57 for (
int i=0;
i<(10*g_loops);
i++)
59 if (NDBT_OK != (res= testBitmask()))
60 return NDBT_ProgramExit(res);
66 return NDBT_ProgramExit(NDBT_FAILED);
71 pNdb =
new Ndb(&con, _dbname);
78 for (
int i = 0;
i < (argc ? argc : g_loops) ;
i++)
83 pTab = create_random_table(pNdb);
88 NDBT_Tables::createTable(pNdb, argv[i]);
94 ndbout <<
"Failed to create table" << endl;
99 if(transactions(pNdb, pTab))
102 if(unique_indexes(pNdb, pTab))
105 if(ordered_indexes(pNdb, pTab))
108 if(node_restart(pNdb, pTab))
111 if(system_restart(pNdb, pTab))
118 if(res != NDBT_OK && pTab)
124 return NDBT_ProgramExit(res);
129 create_random_table(
Ndb* pNdb)
133 Uint32 cols = 1 + (rand() % (NDB_MAX_ATTRIBUTES_IN_TABLE - 1));
134 const Uint32 maxLength = 4090;
135 Uint32 length = maxLength;
136 Uint8 defbuf[(maxLength + 7)/8];
139 name.assfmt(
"TAB_%d", rand() & 65535);
141 for(Uint32 i = 0; i<cols && length > 2; i++)
144 name.assfmt(
"COL_%d", i);
158 Uint32 len = 1 + (rand() % (length - 1));
159 memset(defbuf, 0, (length + 7)/8);
160 for (Uint32 j = 0; j < len/8; j++)
164 int nullable = (rand() >> 16) & 1;
186 i |= trans.loadTable(pNdb, 1000);
187 i |= trans.pkReadRecords(pNdb, 1000, 13);
188 i |= trans.scanReadRecords(pNdb, 1000, 25);
189 i |= trans.pkUpdateRecords(pNdb, 1000, 37);
190 i |= trans.scanUpdateRecords(pNdb, 1000, 25);
191 i |= trans.pkDelRecords(pNdb, 500, 23);
192 i |= trans.clearTable(pNdb);
228 #define BITMASK_DEBUG 0
231 bool cmp(
const Uint32 b1[],
const Uint32 b2[], Uint32 len)
233 Uint32 sz32 = (len + 31) >> 5;
234 for(Uint32 i = 0; i<len; i++)
243 void print(
const Uint32 src[], Uint32 len, Uint32 pos = 0)
246 for(
unsigned i = 0; i<len; i++)
263 void rand(Uint32 dst[], Uint32 len)
265 for(Uint32 i = 0; i<len; i++)
270 int checkCopyField(
const Uint32 totalTests)
272 ndbout <<
"Testing : Checking Bitmaskimpl::copyField";
274 const Uint32 numWords= 95;
275 const Uint32 maxBitsToCopy= (numWords * 32);
277 Uint32 sourceBuf[numWords];
278 Uint32 targetTest[numWords];
279 Uint32 targetCopy[numWords];
281 rand(sourceBuf, maxBitsToCopy);
284 rand(targetTest, maxBitsToCopy);
285 for (Uint32 i=0; i<maxBitsToCopy; i++)
289 if (!cmp(targetTest, targetCopy, maxBitsToCopy))
291 ndbout_c(
"copyField :: Initial setup mismatch");
297 Uint32 len= rand() % maxBitsToCopy;
298 Uint32 slack= maxBitsToCopy - len;
299 Uint32 srcPos= slack ? rand() % slack : 0;
300 Uint32 dstPos= slack ? rand() % slack : 0;
303 ndbout_c(
"copyField :: Running test with len=%u, srcPos=%u, dstPos=%u, "
304 "srcOff=%u, dstOff=%u",
305 len, srcPos, dstPos, srcPos & 31, dstPos & 31);
311 for (Uint32 i=0; i< len; i++)
317 for (Uint32 i=0; i<maxBitsToCopy; i++)
322 ndbout_c(
"copyField :: Mismatch at bit %u, should be %u but is %u",
338 int checkNoTramplingGetSetField(
const Uint32 totalTests)
340 const Uint32 numWords= 67;
341 const Uint32 maxBitsToCopy= (numWords * 32);
342 Uint32 sourceBuf[numWords];
343 Uint32 targetBuf[numWords];
345 ndbout <<
"Testing : Bitmask NoTrampling\n";
347 memset(sourceBuf, 0x00, (numWords*4));
352 Uint32 srcStart= rand() % (maxBitsToCopy -1);
353 Uint32 length= (rand() % ((maxBitsToCopy -1) - srcStart)) + 1;
356 ndbout <<
"Testing start %u, length %u \n"
360 memset(targetBuf, 0xff, (numWords*4));
365 Uint32 firstUntrampledWord= (length + 31)/32;
369 Uint32 targetWord= targetBuf[
word];
371 ndbout <<
"word=%d, targetWord=%u, firstUntrampledWord..=%u"
372 <<
word << targetWord << firstUntrampledWord;
374 if (! (
word < firstUntrampledWord) ?
376 (targetWord == 0xffffffff))
378 ndbout <<
"Notrampling getField failed for srcStart "
380 <<
" length " << length
381 <<
" at word " <<
word <<
"\n";
382 ndbout <<
"word=%d, targetWord=%u, firstUntrampledWord..=%u"
383 << word << targetWord << firstUntrampledWord;
390 memset(targetBuf, 0xff, (numWords*4));
397 Uint32 targetWord= targetBuf[
word];
399 for (Uint32 bit=0; bit< 32; bit++)
401 Uint32 bitNum= (
word << 5) + bit;
402 bool expectedValue= !((bitNum >= srcStart) &&
403 (bitNum < (srcStart + length)));
404 bool actualValue= (((targetWord >> bit) & 1) == 1);
406 ndbout <<
"bitNum=%u expectedValue=%u, actual value=%u"
407 << bitNum << expectedValue << actualValue;
409 if (actualValue != expectedValue)
411 ndbout <<
"Notrampling setField failed for srcStart "
413 <<
" length " << length
414 <<
" at word " <<
word <<
" bit " << bit <<
"\n";
415 ndbout <<
"bitNum=%u expectedValue=%u, actual value=%u"
416 << bitNum << expectedValue << actualValue;
428 int simple(
int pos,
int size)
430 ndbout <<
"Testing : Bitmask simple pos: " << pos <<
" size: " << size <<
"\n";
434 Uint32 sz32 = (size + pos + 32) >> 5;
435 const Uint32 sz = 4 * sz32;
438 _mask.fill(sz32+1, zero);
439 _src.fill(sz32+1, zero);
440 _dst.fill(sz32+1, zero);
442 Uint32 * src = _src.getBase();
443 Uint32 * dst = _dst.getBase();
444 Uint32 * mask = _mask.getBase();
446 memset(src, 0x0, sz);
447 memset(dst, 0x0, sz);
448 memset(mask, 0xFF, sz);
454 printf(
"src: "); print(src, size+31); printf(
"\n");
455 printf(
"msk: "); print(mask, (sz32 << 5) + 31); printf(
"\n");
456 printf(
"dst: "); print(dst, size+31); printf(
"\n");
458 return (cmp(src, dst, size+31)?0 : -1);
470 testRanges(Uint32 bitmask_size)
473 bitmask_size = (bitmask_size + 31) & ~31;
474 Uint32 sz32 = (bitmask_size >> 5);
478 ndbout_c(
"Testing : Bitmask ranges for bitmask of size %d", bitmask_size);
480 alloc_mask.fill(sz32, zero);
481 test_mask.fill(sz32, zero);
487 for(
int i = 0; i<5000; i++)
490 tmp.fill(sz32, zero);
492 Uint32 pos = lrand() % (bitmask_size - 1);
502 for(j = 0; j<alloc_list.size(); j++)
504 min = alloc_list[j].pos;
505 max = min + alloc_list[j].size;
506 if(pos >= min && pos < max)
511 if (! ((pos >= min) && (pos < max)))
513 printf(
"Failed with pos %u, min %u, max %u\n",
521 printf(
"freeing [ %d %d ]", min, max);
523 print(tmp.getBase(), max - min);
527 Alloc& a = alloc_list[j];
528 for(k = 0; k<a.data.size(); k++)
529 printf(
"%.8x ", a.data[k]);
532 if(!cmp(tmp.getBase(), alloc_list[j].data.getBase(), max - min))
543 tmp.fill(sz32, zero);
550 while(pos+free < bitmask_size &&
555 (free <= 64 && ((lrand() % 100) > 80)) ? free : (lrand() % free);
557 sz = pos + sz == bitmask_size ? sz - 1 : sz;
561 a.data.fill(((sz+31)>> 5)-1, zero);
563 printf(
"pos %d -> alloc [ %d %d ]", pos, pos, pos+sz);
564 for(
size_t j = 0; j<sz; j++)
567 if((lrand() % 1000) > 500)
573 print(a.data.getBase(), sz);
578 alloc_list.push_back(a);
582 #define NDB_BM_SUPPORT_RANGE
583 #ifdef NDB_BM_SUPPORT_RANGE
584 for(Uint32 i = 0; i<1000; i++)
586 Uint32 sz32 = 10+rand() % 100;
589 map.fill(sz32, zero);
591 Uint32 sz = 32 * sz32;
592 Uint32 start = (rand() % sz);
593 Uint32 stop = start + ((rand() % (sz - start)) & 0xFFFFFFFF);
596 check.fill(sz32, zero);
599 for(Uint32 j = 0; j<sz; j++)
601 bool expect = (j >= start && j<stop);
609 ndbout_c(
" FAIL 1 sz: %d [ %d %d ]", sz, start, stop);
611 for(Uint32 j = 0; j<sz32; j++)
612 printf(
"%.8x ", check[j]);
616 for(Uint32 j = 0; j<sz32; j++)
617 printf(
"%.8x ", map[j]);
626 Uint32 one = ~(Uint32)0;
628 check.fill(sz32, one);
630 for(Uint32 j = 0; j<sz; j++)
632 bool expect = (j >= start && j<stop);
640 ndbout_c(
" FAIL 2 sz: %d [ %d %d ]", sz, start, stop);
642 for(Uint32 j = 0; j<sz32; j++)
643 printf(
"%.8x ", check[j]);
647 for(Uint32 j = 0; j<sz32; j++)
648 printf(
"%.8x ", map[j]);
665 if ((res= checkNoTramplingGetSetField(100 )) != 0)
668 if ((res= checkCopyField(1000)) != 0)
671 if ((res= simple(rand() % 33,
676 if ((res= testRanges(1+(rand() % 1000)