1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include "test.h"
40 #include <util/x1764.h>
41 
42 static void
test0(void)43 test0 (void) {
44     uint32_t c = toku_x1764_memory("", 0);
45     assert(c==~(0U));
46     struct x1764 cs;
47     toku_x1764_init(&cs);
48     toku_x1764_add(&cs, "", 0);
49     c = toku_x1764_finish(&cs);
50     assert(c==~(0U));
51 }
52 
53 static void
test1(void)54 test1 (void) {
55     uint64_t v=0x123456789abcdef0ULL;
56     uint32_t c;
57     int i;
58     for (i=0; i<=8; i++) {
59 	uint64_t expect64 = (i==8) ? v : v&((1LL<<(8*i))-1);
60 	uint32_t expect = expect64 ^ (expect64>>32);
61 	c = toku_x1764_memory(&v, i);
62 	//printf("i=%d c=%08x expect=%08x\n", i, c, expect);
63 	assert(c==~expect);
64     }
65 }
66 
67 // Compute checksums incrementally, using various strides
68 static void
test2(void)69 test2 (void) {
70     enum { N=200 };
71     char v[N];
72     int i;
73     for (i=0; i<N; i++) v[i]=(char)random();
74     for (i=0; i<N; i++) {
75 	int j;
76 	for (j=i; j<=N; j++) {
77 	    // checksum from i (inclusive to j (exclusive)
78 	    uint32_t c = toku_x1764_memory(&v[i], j-i);
79 	    // Now compute the checksum incrementally with various strides.
80 	    int stride;
81 	    for (stride=1; stride<=j-i; stride++) {
82 		int k;
83 		struct x1764 s;
84 		toku_x1764_init(&s);
85 		for (k=i; k+stride<=j; k+=stride) {
86 		    toku_x1764_add(&s, &v[k], stride);
87 		}
88 		toku_x1764_add(&s, &v[k], j-k);
89 		uint32_t c2 = toku_x1764_finish(&s);
90 		assert(c2==c);
91 	    }
92 	    // Now use some random strides.
93 	    {
94 		int k=i;
95 		struct x1764 s;
96 		toku_x1764_init(&s);
97 		while (1) {
98 		    stride=random()%16;
99 		    if (k+stride>j) break;
100 		    toku_x1764_add(&s, &v[k], stride);
101 		    k+=stride;
102 		}
103 		toku_x1764_add(&s, &v[k], j-k);
104 		uint32_t c2 = toku_x1764_finish(&s);
105 		assert(c2==c);
106 	    }
107 	}
108     }
109 }
110 
111 static void
test3(void)112 test3 (void)
113 // Compare the simple version to the highly optimized version.
114 {
115     const int datalen = 1000;
116     char data[datalen];
117     for (int i=0; i<datalen; i++) data[i]=random();
118     for (int off=0; off<32; off++) {
119 	if (verbose) {printf("."); fflush(stdout);}
120 	for (int len=0; len+off<datalen; len++) {
121 	    uint32_t reference_sum = toku_x1764_memory_simple(data+off, len);
122 	    uint32_t fast_sum      = toku_x1764_memory       (data+off, len);
123 	    assert(reference_sum==fast_sum);
124 	}
125     }
126 }
127 
128 int
test_main(int argc,const char * argv[])129 test_main (int argc __attribute__((__unused__)), const char *argv[] __attribute__((__unused__))) {
130     if (verbose) printf("0\n");
131     test0();
132     if (verbose) printf("1\n");
133     test1();
134     if (verbose) printf("2\n");
135     test2();
136     if (verbose) printf("3\n");
137     test3();
138     return 0;
139 }
140