PageRenderTime 34ms CodeModel.GetById 17ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/libnpk/tests/libnpk_hash.cpp

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