/libnpk/tests/libnpk_hash.cpp

http://npk.googlecode.com/ · C++ · 79 lines · 67 code · 9 blank · 3 comment · 15 complexity · 5ce92d24cabe0bedbaddcebedbd23ef8 MD5 · raw file

  1. #include "testutil.h"
  2. #include <npk.h>
  3. #include <npk_dev.h>
  4. #include <time.h>
  5. int libnpk_hash( int argc, char * argv [] )
  6. {
  7. const int items = 10000;
  8. srand((unsigned int)time(NULL));
  9. NPK_PACKAGE pack;
  10. NPK_ENTITY entity;
  11. // create a pack
  12. int teakey[4] = {1,2,3,4};
  13. CHECK( NPK_SUCCESS == npk_package_alloc( &pack, teakey ) );
  14. char entityname[items][100];
  15. char ventityname[100];
  16. for( int t = 0; t < items; ++t )
  17. {
  18. sprintf( entityname[t], "a\\b/%c%c%c%c_%d.TXT", rand()%26+65, rand()%26+65, rand()%26+65, rand()%26+65, t );
  19. CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", entityname[t], &entity ) );
  20. }
  21. CHECK( NPK_SUCCESS == npk_package_save( pack, "foo_hash.npk", true ) );
  22. npk_package_close( pack );
  23. // validation
  24. CHECK( pack = npk_package_open( "foo_hash.npk", teakey ) );
  25. for( int t = items-1; t >= 0; --t )
  26. {
  27. strcpy( ventityname, entityname[t] );
  28. char*v = ventityname;
  29. while( *v )
  30. {
  31. if( *v >= 'A' && *v <= 'Z' )
  32. *v = *v + 32;
  33. ++v;
  34. }
  35. #ifdef NPK_CASESENSITIVE
  36. entity = npk_package_get_entity( pack, ventityname );
  37. CHECK( entity == NULL );
  38. #else
  39. entity = npk_package_get_entity( pack, ventityname );
  40. CHECK( entity != NULL );
  41. #endif
  42. entity = npk_package_get_entity( pack, entityname[t] );
  43. CHECK( entity != NULL );
  44. }
  45. // displaying hash bucket status
  46. int b_min = items;
  47. int b_max = 0;
  48. int b_sum = 0;
  49. NPK_PACKAGEBODY* pb = (NPK_PACKAGEBODY*)pack;
  50. for( int i = 0; i < NPK_HASH_BUCKETS; ++i )
  51. {
  52. int c = 0;
  53. NPK_ENTITYBODY* ep = pb->bucket_[i]->pEntityHead_;
  54. while( ep )
  55. {
  56. ++c;
  57. ep = ep->nextInBucket_;
  58. }
  59. if( c > b_max ) b_max = c;
  60. if( c < b_min ) b_min = c;
  61. b_sum += c;
  62. }
  63. printf( "buckets:%d\n", NPK_HASH_BUCKETS );
  64. printf( "total:%d\n", b_sum );
  65. printf( "min:%d\n", b_min );
  66. printf( "max:%d\n", b_max );
  67. printf( "avg:%.2f\n", (double)b_sum/NPK_HASH_BUCKETS );
  68. npk_package_close( pack );
  69. return 0;
  70. }