2 * Checking the correctness of str_len() and str_hash() and proving, that
3 * it is faster than the classical version ;-)
6 #include "lib/str_hash.h"
13 /* It will be divided by (10 + strlen()). */
14 #define TEST_TIME 1000000
17 random_string(char *str, int len)
21 str[i] = random() % 255 + 1;
28 static struct timeval last_tv, tv;
30 gettimeofday(&tv, NULL);
31 elapsed = (tv.tv_sec - last_tv.tv_sec) * 1000000 + (tv.tv_usec - last_tv.tv_usec);
56 "\200\200\200\200\200",
59 "uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu",
60 "********************************",
61 "****************************************************************",
65 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
66 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
67 30, 40, 50, 60, 70, 80, 90, 100,
68 200, 300, 400, 500, 600, 700, 800, 900, 1000,
69 2000, 4000, 8000, 16000, 32000, 64000,
73 for (i=0; strings[i]; i++)
74 if (strlen(strings[i]) != str_len(strings[i]))
75 die("Internal error on string %d", i);
76 printf("%d strings tested OK\n", i);
77 for (i=0; strings[i]; i++)
78 printf("hash %2d = %08x\n", i, str_hash(strings[i]));
79 for (i=0; lengths[i] >= 0; i++)
81 char str[lengths[i] + 1];
82 uns count = TEST_TIME / (lengths[i] + 10);
83 uns el1 = 0, el2 = 0, elh = 0;
84 uns tot1 = 0, tot2 = 0, hash = 0;
86 for (j=0; j<count; j++)
88 random_string(str, lengths[i]);
90 /* Avoid "optimizing" by gcc, since the functions are
91 * attributed as ((const)). */
93 el1 += elapsed_time();
95 el2 += elapsed_time();
96 hash ^= str_hash(str);
97 elh += elapsed_time();
100 die("Internal error during test %d", i);
101 printf("Test %d: strlen = %d, passes = %d, classical = %d usec, speedup = %.4f\n",
102 i, lengths[i], count, el1, (el1 + 0.) / el2);
103 printf("\t\t total hash = %08x, hash time = %d usec\n", hash, elh);
106 printf("test1: %d\n", hash_modify(10000000, 10000000, 99777555));
107 printf("test1: %d, %d\n", i, hash_modify(i, lengths[i-2], 99777333));
108 printf("test1: %d, %d\n", i, hash_modify(lengths[i-2], i, 99777333));
109 printf("test1: %d,%d,%d->%d\n", i, i*3-2, i*i, hash_modify(4587, i*3-2, i*i));
110 printf("test1: %d\n", hash_modify(lengths[5], 345, i));