1#include <IO/ReadBufferFromIStream.h>
2#include <IO/HashingReadBuffer.h>
3#include <IO/WriteBufferFromOStream.h>
4#include "hashing_buffer.h"
5#include <iostream>
6
7static void test(size_t data_size)
8{
9 std::vector<char> vec(data_size);
10 char * data = vec.data();
11
12 for (size_t i = 0; i < data_size; ++i)
13 data[i] = rand() & 255;
14
15 CityHash_v1_0_2::uint128 reference = referenceHash(data, data_size);
16
17 std::vector<size_t> block_sizes = {56, 128, 513, 2048, 3055, 4097, 4096};
18 for (size_t read_buffer_block_size : block_sizes)
19 {
20 std::cout << "block size " << read_buffer_block_size << std::endl;
21 std::stringstream io;
22 DB::WriteBufferFromOStream out_(io);
23 DB::HashingWriteBuffer out(out_);
24 out.write(data, data_size);
25 out.next();
26
27 DB::ReadBufferFromIStream source(io, read_buffer_block_size);
28 DB::HashingReadBuffer buf(source);
29
30 std::vector<char> read_buf(data_size);
31 buf.read(read_buf.data(), data_size);
32
33 bool failed_to_read = false;
34 for (size_t i = 0; i < data_size; ++i)
35 if (read_buf[i] != vec[i])
36 failed_to_read = true;
37
38 if (failed_to_read)
39 {
40 std::cout.write(data, data_size);
41 std::cout << std::endl;
42 std::cout.write(read_buf.data(), data_size);
43 std::cout << std::endl;
44 FAIL("Fail to read data");
45 }
46
47 if (buf.getHash() != reference)
48 FAIL("failed on data size " << data_size << " reading by blocks of size " << read_buffer_block_size);
49 if (buf.getHash() != out.getHash())
50 FAIL("Hash of HashingReadBuffer doesn't match with hash of HashingWriteBuffer on data size " << data_size << " reading by blocks of size " << read_buffer_block_size);
51 }
52}
53
54int main()
55{
56 test(5);
57 test(100);
58 test(2048);
59 test(2049);
60 test(100000);
61 test(1 << 17);
62
63 return 0;
64}
65