]> mj.ucw.cz Git - libucw.git/blob - lib/str-test.c
adac0fb81d8660409d16eb863a7025c94b51ef41
[libucw.git] / lib / str-test.c
1 /*
2  *      Checking the correctness of str_len() and str_hash() and proving, that
3  *      it is faster than the classical version ;-)
4  */
5
6 #include "lib/str_hash.h"
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/time.h>
12
13 /* It will be divided by (10 + strlen()).  */
14 #define TEST_TIME       1000000
15
16 static void
17 random_string(char *str, int len)
18 {
19         int i;
20         for (i=0; i<len; i++)
21                 str[i] = random() % 255 + 1;
22         str[len] = 0;
23 }
24
25 static uns
26 elapsed_time(void)
27 {
28         static struct timeval last_tv, tv;
29         uns elapsed;
30         gettimeofday(&tv, NULL);
31         elapsed = (tv.tv_sec - last_tv.tv_sec) * 1000000 + (tv.tv_usec - last_tv.tv_usec);
32         last_tv = tv;
33         return elapsed;
34 }
35
36 int
37 main(void)
38 {
39         char *strings[] = {
40                 "",
41                 "a",
42                 "aa",
43                 "aaa",
44                 "aaaa",
45                 "aaaaa",
46                 "aaaaaa",
47                 "aaaaaaa",
48                 "aaaaaaaa",
49                 "aaaaaaaaa",
50                 "aaaaaaaaaa",
51                 "\200aaaa",
52                 "\200",
53                 "\200\200",
54                 "\200\200\200",
55                 "\200\200\200\200",
56                 "\200\200\200\200\200",
57                 "kelapS treboR",
58                 "Robert Spalek",
59                 "uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu",
60                 "********************************",
61                 "****************************************************************",
62                 NULL
63         };
64         int lengths[] = {
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,
70                 -1
71         };
72         int i;
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++)
80         {
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;
85                 uns j;
86                 for (j=0; j<count; j++)
87                 {
88                         random_string(str, lengths[i]);
89                         elapsed_time();
90                         /* Avoid "optimizing" by gcc, since the functions are
91                          * attributed as ((const)).  */
92                         tot1 += strlen(str);
93                         el1 += elapsed_time();
94                         tot2 += str_len(str);
95                         el2 += elapsed_time();
96                         hash ^= str_hash(str);
97                         elh += elapsed_time();
98                 }
99                 if (tot1 != tot2)
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);
104         }
105 /*
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));
111 */
112         return 0;
113 }