1#include <IO/HashingWriteBuffer.h>
2#include <IO/WriteBufferFromFile.h>
3
4#include "hashing_buffer.h"
5
6static void test(size_t data_size)
7{
8 std::vector<char> vec(data_size);
9 char * data = vec.data();
10
11 for (size_t i = 0; i < data_size; ++i)
12 data[i] = rand() & 255;
13
14 CityHash_v1_0_2::uint128 reference = referenceHash(data, data_size);
15
16 DB::WriteBufferFromFile sink("/dev/null", 1 << 16);
17
18 {
19 DB::HashingWriteBuffer buf(sink);
20
21 for (size_t pos = 0; pos < data_size;)
22 {
23 size_t len = std::min(static_cast<size_t>(rand() % 10000 + 1), data_size - pos);
24 buf.write(data + pos, len);
25 buf.next();
26 pos += len;
27 }
28
29 if (buf.getHash() != reference)
30 FAIL("failed on data size " << data_size << " writing random chunks of up to 10000 bytes");
31 }
32
33 {
34 DB::HashingWriteBuffer buf(sink);
35
36 for (size_t pos = 0; pos < data_size;)
37 {
38 size_t len = std::min(static_cast<size_t>(rand() % 5 + 1), data_size - pos);
39 buf.write(data + pos, len);
40 buf.next();
41 pos += len;
42 }
43
44 if (buf.getHash() != reference)
45 FAIL("failed on data size " << data_size << " writing random chunks of up to 5 bytes");
46 }
47
48 {
49 DB::HashingWriteBuffer buf(sink);
50
51 for (size_t pos = 0; pos < data_size;)
52 {
53 size_t len = std::min(static_cast<size_t>(2048 + rand() % 3 - 1), data_size - pos);
54 buf.write(data + pos, len);
55 buf.next();
56 pos += len;
57 }
58
59 if (buf.getHash() != reference)
60 FAIL("failed on data size " << data_size << " writing random chunks of 2048 +-1 bytes");
61 }
62
63 {
64 DB::HashingWriteBuffer buf(sink);
65
66 buf.write(data, data_size);
67
68 if (buf.getHash() != reference)
69 FAIL("failed on data size " << data_size << " writing all at once");
70 }
71}
72
73int main()
74{
75 test(5);
76 test(100);
77 test(2048);
78 test(2049);
79 test(100000);
80 test(1 << 17);
81
82 return 0;
83}
84