hp_test2.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:16k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2 of the License, or
  6.    (at your option) any later version.
  7.    
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  16. /* Test av isam-databas: stor test */
  17. #ifndef USE_MY_FUNC /* We want to be able to dbug this !! */
  18. #define USE_MY_FUNC
  19. #endif
  20. #ifdef DBUG_OFF
  21. #undef DBUG_OFF
  22. #endif
  23. #ifndef SAFEMALLOC
  24. #define SAFEMALLOC
  25. #endif
  26. #include "heapdef.h" /* Because of _hp_find_block */
  27. #include <signal.h>
  28. #define MAX_RECORDS 100000
  29. #define MAX_KEYS 3
  30. static int get_options(int argc, char *argv[]);
  31. static int rnd(int max_value);
  32. static sig_handler endprog(int sig_number);
  33. static uint flag=0,verbose=0,testflag=0,recant=10000,silent=0;
  34. static uint keys=MAX_KEYS;
  35. static uint16 key1[1001];
  36. static my_bool key3[MAX_RECORDS];
  37. static int calc_check(byte *buf,uint length);
  38. /* Huvudprogrammet */
  39. int main(argc,argv)
  40. int argc;
  41. char *argv[];
  42. {
  43.   register uint i,j;
  44.   uint ant,n1,n2,n3;
  45.   uint reclength,write_count,update,delete,check2,dupp_keys,found_key;
  46.   int error;
  47.   ulong pos;
  48.   unsigned long key_check;
  49.   char record[128],record2[128],record3[128],key[10];
  50.   const char *filename,*filename2;
  51.   HP_INFO *file,*file2;
  52.   HP_KEYDEF keyinfo[MAX_KEYS];
  53.   HP_KEYSEG keyseg[MAX_KEYS*5];
  54.   HEAP_PTR position;
  55.   MY_INIT(argv[0]); /* init my_sys library & pthreads */
  56.   LINT_INIT(position);
  57.   filename= "test2";
  58.   filename2= "test2_2";
  59.   file=file2=0;
  60.   get_options(argc,argv);
  61.   reclength=37;
  62.   write_count=update=delete=0;
  63.   key_check=0;
  64.   keyinfo[0].seg=keyseg;
  65.   keyinfo[0].keysegs=1;
  66.   keyinfo[0].flag= 0;
  67.   keyinfo[0].seg[0].type=HA_KEYTYPE_BINARY;
  68.   keyinfo[0].seg[0].start=0;
  69.   keyinfo[0].seg[0].length=6;
  70.   keyinfo[1].seg=keyseg+1;
  71.   keyinfo[1].keysegs=2;
  72.   keyinfo[1].flag=0;
  73.   keyinfo[1].seg[0].type=HA_KEYTYPE_BINARY;
  74.   keyinfo[1].seg[0].start=7;
  75.   keyinfo[1].seg[0].length=6;
  76.   keyinfo[1].seg[1].type=HA_KEYTYPE_TEXT;
  77.   keyinfo[1].seg[1].start=0; /* Tv}delad nyckel */
  78.   keyinfo[1].seg[1].length=6;
  79.   keyinfo[2].seg=keyseg+3;
  80.   keyinfo[2].keysegs=1;
  81.   keyinfo[2].flag=HA_NOSAME;
  82.   keyinfo[2].seg[0].type=HA_KEYTYPE_BINARY;
  83.   keyinfo[2].seg[0].start=12;
  84.   keyinfo[2].seg[0].length=8;
  85.   bzero((char*) key1,sizeof(key1));
  86.   bzero((char*) key3,sizeof(key3));
  87.   printf("- Creating heap-filen");
  88.   if (heap_create(filename))
  89.     goto err;
  90.   if (!(file=heap_open(filename,2,keys,keyinfo,reclength,(ulong) flag*100000L,
  91.        (ulong) recant/2)))
  92.     goto err;
  93.   signal(SIGINT,endprog);
  94.   printf("- Writing records:sn");
  95.   strmov(record,"          ..... key");
  96.   for (i=0 ; i < recant ; i++)
  97.   {
  98.     n1=rnd(1000); n2=rnd(100); n3=rnd(min(recant*5,MAX_RECORDS));
  99.     sprintf(record,"%6d:%4d:%8d:Pos: %4d          ",n1,n2,n3,write_count);
  100.     if (heap_write(file,record))
  101.     {
  102.       if (my_errno != HA_ERR_FOUND_DUPP_KEY || key3[n3] == 0)
  103.       {
  104. printf("Error: %d in write at record: %dn",my_errno,i);
  105. goto err;
  106.       }
  107.       if (verbose) printf("   Double key: %dn",n3);
  108.     }
  109.     else
  110.     {
  111.       if (key3[n3] == 1)
  112.       {
  113. printf("Error: Didn't get error when writing second key: '%8d'n",n3);
  114. goto err;
  115.       }
  116.       write_count++; key1[n1]++; key3[n3]=1;
  117.       key_check+=n1;
  118.     }
  119.     if (testflag == 1 && heap_check_heap(file,0))
  120.     {
  121.       puts("Heap keys crashed");
  122.       goto err;
  123.     }
  124.   }
  125.   if (testflag == 1)
  126.     goto end;
  127.   if (heap_check_heap(file,0))
  128.   {
  129.     puts("Heap keys crashed");
  130.     goto err;
  131.   }
  132.   printf("- Deleten");
  133.   for (i=0 ; i < write_count/10 ; i++)
  134.   {
  135.     for (j=rnd(1000)+1 ; j>0 && key1[j] == 0 ; j--) ;
  136.     if (j != 0)
  137.     {
  138.       sprintf(key,"%6d",j);
  139.       if (heap_rkey(file,record,0,key))
  140.       {
  141. printf("can't find key1: "%s"n",key);
  142. goto err;
  143.       }
  144. #ifdef NOT_USED
  145.       if (file->current_ptr == _hp_find_block(&file->s->block,0) ||
  146.   file->current_ptr == _hp_find_block(&file->s->block,1))
  147. continue; /* Don't remove 2 first records */
  148. #endif
  149.       if (heap_delete(file,record))
  150.       {
  151. printf("error: %d; can't delete record: "%s"n", my_errno,record);
  152. goto err;
  153.       }
  154.       delete++;
  155.       key1[atoi(record+keyinfo[0].seg[0].start)]--;
  156.       key3[atoi(record+keyinfo[2].seg[0].start)]=0;
  157.       key_check-=atoi(record);
  158.       if (testflag == 2 && heap_check_heap(file,0))
  159.       {
  160. puts("Heap keys crashed");
  161. goto err;
  162.       }
  163.     }
  164.     else
  165.       puts("Warning: Skipping delete test because no dupplicate keys");
  166.   }
  167.   if (testflag==2) goto end;
  168.   if (heap_check_heap(file,0))
  169.   {
  170.     puts("Heap keys crashed");
  171.     goto err;
  172.   }
  173.   printf("- Updaten");
  174.   for (i=0 ; i < write_count/10 ; i++)
  175.   {
  176.     n1=rnd(1000); n2=rnd(100); n3=rnd(min(recant*2,MAX_RECORDS));
  177.     sprintf(record2,"%6d:%4d:%8d:XXX: %4d          ",n1,n2,n3,update);
  178.     if (rnd(2) == 1)
  179.     {
  180.       if (heap_scan_init(file))
  181. goto err;
  182.       j=rnd(write_count-delete);
  183.       while ((error=heap_scan(file,record) == HA_ERR_RECORD_DELETED) ||
  184.      (!error && j))
  185.       {
  186. if (!error)
  187.   j--;
  188.       }
  189.       if (error)
  190. goto err;
  191.     }
  192.     else
  193.     {
  194.       for (j=rnd(1000)+1 ; j>0 && key1[j] == 0 ; j--) ;
  195.       if (!key1[j])
  196. continue;
  197.       sprintf(key,"%6d",j);
  198.       if (heap_rkey(file,record,0,key))
  199.       {
  200. printf("can't find key1: "%s"n",key);
  201. goto err;
  202.       }
  203.     }
  204.     if (heap_update(file,record,record2))
  205.     {
  206.       if (my_errno != HA_ERR_FOUND_DUPP_KEY || key3[n3] == 0)
  207.       {
  208. printf("error: %d; can't uppdate:nFrom: "%s"nTo:   "%s"n",
  209.        my_errno,record,record2);
  210. goto err;
  211.       }
  212.       if (verbose)
  213. printf("Double key when tryed to uppdate:nFrom: "%s"nTo:   "%s"n",record,record2);
  214.     }
  215.     else
  216.     {
  217.       key1[atoi(record+keyinfo[0].seg[0].start)]--;
  218.       key3[atoi(record+keyinfo[2].seg[0].start)]=0;
  219.       key1[n1]++; key3[n3]=1;
  220.       update++;
  221.       key_check=key_check-atoi(record)+n1;
  222.     }
  223.     if (testflag == 3 && heap_check_heap(file,0))
  224.     {
  225.       puts("Heap keys crashed");
  226.       goto err;
  227.     }
  228.   }
  229.   if (testflag == 3) goto end;
  230.   if (heap_check_heap(file,0))
  231.   {
  232.     puts("Heap keys crashed");
  233.     goto err;
  234.   }
  235.   for (i=999, dupp_keys=found_key=0 ; i>0 ; i--)
  236.   {
  237.     if (key1[i] > dupp_keys) { dupp_keys=key1[i]; found_key=i; }
  238.     sprintf(key,"%6d",found_key);
  239.   }
  240.   if (dupp_keys > 3)
  241.   {
  242.     if (!silent)
  243.       printf("- Read first key - next - delete - next -> lastn");
  244.     DBUG_PRINT("progpos",("first - next - delete - next -> last"));
  245.     if (heap_rkey(file,record,0,key))
  246.       goto err;
  247.     if (heap_rnext(file,record3)) goto err;
  248.     if (heap_delete(file,record3)) goto err;
  249.     key_check-=atoi(record3);
  250.     key1[atoi(record+keyinfo[0].seg[0].start)]--;
  251.     key3[atoi(record+keyinfo[2].seg[0].start)]=0;
  252.     delete++;
  253.     ant=2;
  254.     while ((error=heap_rnext(file,record3)) == 0 ||
  255.    error == HA_ERR_RECORD_DELETED)
  256.       if (! error)
  257. ant++;
  258.     if (ant != dupp_keys)
  259.     {
  260.       printf("next: I can only find: %d records of %dn",
  261.      ant,dupp_keys);
  262.       goto end;
  263.     }
  264.     dupp_keys--;
  265.     if (heap_check_heap(file,0))
  266.     {
  267.       puts("Heap keys crashed");
  268.       goto err;
  269.     }
  270.     if (!silent)
  271.       printf("- Read last key - delete - prev - prev - delete - prev -> firstn");
  272.     if (heap_rlast(file,record3)) goto err;
  273.     if (heap_delete(file,record3)) goto err;
  274.     key_check-=atoi(record3);
  275.     key1[atoi(record+keyinfo[0].seg[0].start)]--;
  276.     key3[atoi(record+keyinfo[2].seg[0].start)]=0;
  277.     delete++;
  278.     if (heap_rprev(file,record3) || heap_rprev(file,record3))
  279.       goto err;
  280.     if (heap_delete(file,record3)) goto err;
  281.     key_check-=atoi(record3);
  282.     key1[atoi(record+keyinfo[0].seg[0].start)]--;
  283.     key3[atoi(record+keyinfo[2].seg[0].start)]=0;
  284.     delete++;
  285.     ant=3;
  286.     while ((error=heap_rprev(file,record3)) == 0 ||
  287.    error == HA_ERR_RECORD_DELETED)
  288.     {
  289.       if (! error)
  290. ant++;
  291.     }
  292.     if (ant != dupp_keys)
  293.     {
  294.       printf("next: I can only find: %d records of %dn",
  295.      ant,dupp_keys);
  296.       goto end;
  297.     }
  298.     dupp_keys-=2;
  299.     if (heap_check_heap(file,0))
  300.     {
  301.       puts("Heap keys crashed");
  302.       goto err;
  303.     }
  304.   }
  305.   else
  306.     puts("Warning: Not enough duplicated keys:  Skipping delete key check");
  307.   if (!silent)
  308.     printf("- Read (first) - next - delete - next -> lastn");
  309.   DBUG_PRINT("progpos",("first - next - delete - next -> last"));
  310.   if (heap_scan_init(file))
  311.     goto err;
  312.   while ((error=heap_scan(file,record3) == HA_ERR_RECORD_DELETED)) ;
  313.   if (error)
  314.     goto err;
  315.   if (heap_delete(file,record3)) goto err;
  316.   key_check-=atoi(record3);
  317.   delete++;
  318.   key1[atoi(record+keyinfo[0].seg[0].start)]--;
  319.   key3[atoi(record+keyinfo[2].seg[0].start)]=0;
  320.   ant=0;
  321.   while ((error=heap_scan(file,record3)) == 0 ||
  322.  error == HA_ERR_RECORD_DELETED)
  323.     if (! error)
  324.       ant++;
  325.   if (ant != write_count-delete)
  326.   {
  327.     printf("next: Found: %d records of %dn",ant,write_count-delete);
  328.     goto end;
  329.   }
  330.   if (heap_check_heap(file,0))
  331.   {
  332.     puts("Heap keys crashed");
  333.     goto err;
  334.   }
  335.   puts("- Test if: Read rrnd - same - rkey - same");
  336.   DBUG_PRINT("progpos",("Read rrnd - same"));
  337.   pos=rnd(write_count-delete-5)+5;
  338.   heap_scan_init(file);
  339.   i=5;
  340.   while ((error=heap_scan(file,record)) == HA_ERR_RECORD_DELETED ||
  341.  (error == 0 && pos))
  342.   {
  343.     if (!error)
  344.       pos--;
  345.     if (i-- == 0)
  346.     {
  347.       bmove(record3,record,reclength);
  348.       position=heap_position(file);
  349.     }
  350.   }
  351.   if (error)
  352.     goto err;
  353.   bmove(record2,record,reclength);
  354.   if (heap_rsame(file,record,-1) || heap_rsame(file,record2,2))
  355.     goto err;
  356.   if (bcmp(record2,record,reclength))
  357.   {
  358.     puts("heap_rsame didn't find right record");
  359.     goto end;
  360.   }
  361.   puts("- Test of read through position");
  362.   if (heap_rrnd(file,record,position))
  363.     goto err;
  364.   if (bcmp(record3,record,reclength))
  365.   {
  366.     puts("heap_frnd didn't find right record");
  367.     goto end;
  368.   }
  369.   printf("- heap_infon");
  370.   {
  371.     HEAPINFO info;
  372.     heap_info(file,&info,0);
  373.     /* We have to test with delete +1 as this may be the case if the last
  374.        inserted row was a duplicate key */
  375.     if (info.records != write_count-delete ||
  376. (info.deleted != delete && info.deleted != delete+1))
  377.     {
  378.       puts("Wrong info from heap_info");
  379.       printf("Got: records: %ld(%d)  deleted: %ld(%d)n",
  380.      info.records,write_count-delete,info.deleted,delete);
  381.     }
  382.   }
  383. #ifdef OLD_HEAP_VERSION
  384.   {
  385.     uint check;
  386.     printf("- Read through all records with rndn");
  387.     if (heap_extra(file,HA_EXTRA_RESET) || heap_extra(file,HA_EXTRA_CACHE))
  388.     {
  389.       puts("got error from heap_extra");
  390.       goto end;
  391.     }
  392.     ant=check=0;
  393.     while ((error=heap_rrnd(file,record,(ulong) -1)) != HA_ERR_END_OF_FILE &&
  394.    ant < write_count + 10)
  395.     {
  396.       if (!error)
  397.       {
  398. ant++;
  399. check+=calc_check(record,reclength);
  400.       }
  401.     }
  402.     if (ant != write_count-delete)
  403.     {
  404.       printf("rrnd: I can only find: %d records of %dn", ant,
  405.      write_count-delete);
  406.       goto end;
  407.     }
  408.     if (heap_extra(file,HA_EXTRA_NO_CACHE))
  409.     {
  410.       puts("got error from heap_extra(HA_EXTRA_NO_CACHE)");
  411.       goto end;
  412.     }
  413.   }
  414. #endif
  415.   printf("- Read through all records with scann");
  416.   if (heap_extra(file,HA_EXTRA_RESET) || heap_extra(file,HA_EXTRA_CACHE))
  417.   {
  418.     puts("got error from heap_extra");
  419.     goto end;
  420.   }
  421.   ant=check2=0;
  422.   heap_scan_init(file);
  423.   while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE &&
  424.  ant < write_count + 10)
  425.   {
  426.     if (!error)
  427.     {
  428.       ant++;
  429.       check2+=calc_check(record,reclength);
  430.     }
  431.   }
  432.   if (ant != write_count-delete)
  433.   {
  434.     printf("scan: I can only find: %d records of %dn", ant,
  435.    write_count-delete);
  436.     goto end;
  437.   }
  438. #ifdef OLD_HEAP_VERSION
  439.   if (check != check2)
  440.   {
  441.     puts("scan: Checksum didn't match reading with rrnd");
  442.     goto end;
  443.   }
  444. #endif
  445.   if (heap_extra(file,HA_EXTRA_NO_CACHE))
  446.   {
  447.     puts("got error from heap_extra(HA_EXTRA_NO_CACHE)");
  448.     goto end;
  449.   }
  450.   for (i=999, dupp_keys=found_key=0 ; i>0 ; i--)
  451.   {
  452.     if (key1[i] > dupp_keys) { dupp_keys=key1[i]; found_key=i; }
  453.     sprintf(key,"%6d",found_key);
  454.   }
  455.   printf("- Read through all keys with first-next-last-prevn");
  456.   ant=0;
  457.   for (error=heap_rkey(file,record,0,key) ;
  458.       ! error ;
  459.        error=heap_rnext(file,record))
  460.     ant++;
  461.   if (ant != dupp_keys)
  462.   {
  463.     printf("first-next: I can only find: %d records of %dn", ant,
  464.    dupp_keys);
  465.     goto end;
  466.   }
  467.   ant=0;
  468.   for (error=heap_rlast(file,record) ;
  469.       ! error ;
  470.       error=heap_rprev(file,record))
  471.   {
  472.     ant++;
  473.     check2+=calc_check(record,reclength);
  474.   }
  475.   if (ant != dupp_keys)
  476.   {
  477.     printf("last-prev: I can only find: %d records of %dn", ant,
  478.    dupp_keys);
  479.     goto end;
  480.   }
  481.   if (testflag == 4) goto end;
  482.   printf("- Reading through all rows through keysn");
  483.   if (!(file2=heap_open(filename,2,0,0,0,0,0)))
  484.     goto err;
  485.   if (heap_scan_init(file))
  486.     goto err;
  487.   while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE)
  488.   {
  489.     if (error == 0)
  490.     {
  491.       if (heap_rkey(file2,record2,2,record+keyinfo[2].seg[0].start))
  492.       {
  493. printf("can't find key3: "%.8s"n",
  494.        record+keyinfo[2].seg[0].start);
  495. goto err;
  496.       }
  497.     }
  498.   }
  499.   heap_close(file2);
  500.   printf("- Creating output heap-file 2n");
  501.   if (!(file2=heap_open(filename2,2,1,keyinfo,reclength,0L,0L)))
  502.     goto err;
  503.   printf("- Copying and removing recordsn");
  504.   if (heap_scan_init(file))
  505.     goto err;
  506.   while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE)
  507.   {
  508.     if (error == 0)
  509.     {
  510.       if (heap_write(file2,record))
  511. goto err;
  512.       key_check-=atoi(record);
  513.       write_count++;
  514.       if (heap_delete(file,record))
  515. goto err;
  516.       delete++;
  517.     }
  518.     pos++;
  519.   }
  520.   printf("- Checking heap tablesn");
  521.   if (heap_check_heap(file,1) || heap_check_heap(file2,1))
  522.   {
  523.     puts("Heap keys crashed");
  524.     goto err;
  525.   }
  526.   if (my_errno != HA_ERR_END_OF_FILE)
  527.     printf("error: %d from heap_rrndn",my_errno);
  528.   if (key_check)
  529.     printf("error: Some read got wrong: check is %ldn",(long) key_check);
  530. end:
  531.   printf("nFollowing test have been made:n");
  532.   printf("Write records: %dnUpdate records: %dnDelete records: %dn", write_count,update,delete);
  533.   heap_clear(file);
  534.   if (heap_close(file) || (file2 && heap_close(file2)))
  535.     goto err;
  536.   heap_delete_all(filename2);
  537.   heap_panic(HA_PANIC_CLOSE);
  538.   my_end(MY_GIVE_INFO);
  539.   return(0);
  540. err:
  541.   printf("Got error: %d when using heap-databasen",my_errno);
  542.   VOID(heap_close(file));
  543.   return(1);
  544. } /* main */
  545. /* Read options */
  546. static int get_options(int argc,char *argv[])
  547. {
  548.   char *pos,*progname;
  549.   DEBUGGER_OFF;
  550.   progname= argv[0];
  551.   while (--argc >0 && *(pos = *(++argv)) == '-' ) {
  552.     switch(*++pos) {
  553.     case 'B': /* Big file */
  554.       flag=1;
  555.       break;
  556.     case 'v': /* verbose */
  557.       verbose=1;
  558.       break;
  559.     case 'm': /* records */
  560.       recant=atoi(++pos);
  561.       break;
  562.     case 's':
  563.       silent=1;
  564.       break;
  565.     case 't':
  566.       testflag=atoi(++pos); /* testmod */
  567.       break;
  568.     case 'V':
  569.     case 'I':
  570.     case '?':
  571.       printf("%s  Ver 1.1 for %s at %sn",progname,SYSTEM_TYPE,MACHINE_TYPE);
  572.       puts("TCX Datakonsult AB, by Monty, for your professional usen");
  573.       printf("Usage: %s [-?ABIKLsWv] [-m#] [-t#]n",progname);
  574.       exit(0);
  575.     case '#':
  576.       DEBUGGER_ON;
  577.       DBUG_PUSH (++pos);
  578.       break;
  579.     }
  580.   }
  581.   return 0;
  582. } /* get options */
  583. /* Generate a random value in intervall 0 <=x <= n */
  584. static int rnd(max_value)
  585. int max_value;
  586. {
  587.   return (int) ((rand() & 32767)/32767.0*max_value);
  588. } /* rnd */
  589. static sig_handler endprog(int sig_number __attribute__((unused)))
  590. {
  591. #ifndef THREAD
  592.   if (my_dont_interrupt)
  593.     my_remember_signal(sig_number,endprog);
  594.   else
  595. #endif
  596.   {
  597.     heap_panic(HA_PANIC_CLOSE);
  598.     my_end(1);
  599.     exit(1);
  600.   }
  601. }
  602. static int calc_check(buf,length)
  603. byte *buf;
  604. uint length;
  605. {
  606.   int check=0;
  607.   while (length--)
  608.     check+= (int) (uchar) *(buf++);
  609.   return check;
  610. }