xref: /4.0.0/forestdb/tests/unit/crc_test.cc (revision 2d5b425b)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2010 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include "test.h"
23#include "crc32.h"
24#include "adler32.h"
25#include "hash_functions.h"
26
27void basic_test()
28{
29    TEST_INIT();
30
31    size_t len = 1024*1024*1024 + 7;
32    void *dummy;
33    size_t i;
34    uint32_t r1, r2, r3, r4;
35
36    dummy = (void *)malloc(len);
37    for (i=0;i<len/sizeof(size_t); i+=sizeof(size_t)) {
38        memcpy((uint8_t *)dummy + i*sizeof(size_t), &i, sizeof(size_t));
39    }
40
41    TEST_TIME();
42
43    r1 = crc32_8(dummy, len, 0);
44    (void)r1;
45
46    TEST_TIME();
47
48    r2 = crc32_1(dummy, len, 0);
49    (void)r2;
50
51    TEST_TIME();
52
53    r3 = hash_djb2((uint8_t *)dummy, len);
54    (void)r3;
55
56    TEST_TIME();
57
58    r4 = adler32(1, (uint8_t*)dummy, len);
59    (void)r4;
60
61
62    TEST_TIME();
63
64    DBG("crc value: %u %u\n", r1, r2);
65    DBG("djb2: %u\n", r3);
66    DBG("adler32: %u\n", r4);
67
68    free(dummy);
69
70    TEST_RESULT("crc32 slicing-8 test");
71}
72
73void adler_test()
74{
75    char *str = (char*)"abcdefgh";
76    uint32_t a1, a2, a3;
77
78    a1 = adler32(0, (uint8_t*)str, 8);
79    a2 = adler32(0, (uint8_t*)str, 4);
80    a3 = adler32(a2, (uint8_t*)str+4, 4);
81
82    printf("%u %u %u\n", a1, a2, a3);
83}
84
85void endian_test()
86{
87    uint32_t a, b, c;
88    char buf[12], str[256];
89    uint32_t r1, r2;
90    a = 0xabcddef0;
91    b = 0x13576420;
92    c = 0x01234567;
93    memcpy(buf, &a, sizeof(uint32_t));
94    memcpy(buf + 4, &b, sizeof(uint32_t));
95    memcpy(buf + 8, &c, sizeof(uint32_t));
96
97    r1 = crc32_8((void*)buf, 12, 0);
98    r2 = crc32_8((void*)&a, 4, 0);
99    r2 = crc32_8((void*)&b, 4, r2);
100    r2 = crc32_8((void*)&c, 4, r2);
101    (void)r1;
102    (void)r2;
103
104    DBG("%u %u\n", r1,r2);
105
106    sprintf(str, "1234aaaaaaaa");
107    a = crc32_8_last8((void*)str, strlen(str), 0);
108    sprintf(str, "5678aaaaaaaa");
109    b = crc32_8_last8((void*)str, strlen(str), 0);
110    DBG("%u %u\n", a, b);
111
112    sprintf(str, "./dummy12");
113    a = crc32_8_last8((void*)str, strlen(str), 0);
114    sprintf(str, ";/dummy21");
115    b = crc32_8_last8((void*)str, strlen(str), 0);
116    DBG("%u %u\n", a, b);
117
118}
119
120int main()
121{
122    basic_test();
123    adler_test();
124    endian_test();
125    return 0;
126}
127