815af87292105ec3172cda98639d8c1c36d7be86
[folly.git] / folly / test / ChecksumTest.cpp
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "folly/Checksum.h"
18 #include <gflags/gflags.h>
19 #include <gtest/gtest.h>
20 #include "folly/Benchmark.h"
21 #include "folly/Hash.h"
22 #include "folly/detail/ChecksumDetail.h"
23
24 namespace {
25 const unsigned int BUFFER_SIZE = 64 * 1024 * sizeof(uint64_t);
26 uint8_t buffer[BUFFER_SIZE];
27
28 struct ExpectedResult {
29   size_t offset;
30   size_t length;
31   uint32_t crc32c;
32 };
33
34 ExpectedResult expectedResults[] = {
35     // Zero-byte input
36     { 0, 0, ~0U },
37     // Small aligned inputs to test special cases in SIMD implementations
38     { 8, 1, 1543413366 },
39     { 8, 2, 523493126 },
40     { 8, 3, 1560427360 },
41     { 8, 4, 3422504776 },
42     { 8, 5, 447841138 },
43     { 8, 6, 3910050499 },
44     { 8, 7, 3346241981 },
45     // Small unaligned inputs
46     { 9, 1, 3855826643 },
47     { 10, 2, 560880875 },
48     { 11, 3, 1479707779 },
49     { 12, 4, 2237687071 },
50     { 13, 5, 4063855784 },
51     { 14, 6, 2553454047 },
52     { 15, 7, 1349220140 },
53     // Larger inputs to test leftover chunks at the end of aligned blocks
54     { 8, 8, 627613930 },
55     { 8, 9, 2105929409 },
56     { 8, 10, 2447068514 },
57     { 8, 11, 863807079 },
58     { 8, 12, 292050879 },
59     { 8, 13, 1411837737 },
60     { 8, 14, 2614515001 },
61     { 8, 15, 3579076296 },
62     { 8, 16, 2897079161 },
63     { 8, 17, 675168386 },
64     // Much larger inputs
65     { 0, BUFFER_SIZE, 2961263300 },
66     { 1, BUFFER_SIZE / 2, 1708529329 },
67 };
68
69 void testCRC32C(
70     std::function<uint32_t(const uint8_t*, size_t, uint32_t)> impl) {
71   for (auto expected : expectedResults) {
72     uint32_t result = impl(buffer + expected.offset, expected.length, ~0U);
73     EXPECT_EQ(result, expected.crc32c);
74   }
75 }
76
77 void testCRC32CContinuation(
78     std::function<uint32_t(const uint8_t*, size_t, uint32_t)> impl) {
79   for (auto expected : expectedResults) {
80     size_t partialLength = expected.length / 2;
81     uint32_t partialChecksum = impl(
82         buffer + expected.offset, partialLength, ~0U);
83     uint32_t result = impl(
84         buffer + expected.offset + partialLength,
85         expected.length - partialLength, partialChecksum);
86     EXPECT_EQ(result, expected.crc32c);
87   }
88 }
89
90 } // namespace
91
92 TEST(Checksum, crc32c_software) {
93   testCRC32C(folly::detail::crc32c_sw);
94 }
95
96 TEST(Checksum, crc32c_continuation_software) {
97   testCRC32CContinuation(folly::detail::crc32c_sw);
98 }
99
100
101 TEST(Checksum, crc32c_hardware) {
102   if (folly::detail::crc32c_hw_supported()) {
103     testCRC32C(folly::detail::crc32c_hw);
104   } else {
105     LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests" <<
106         " (not supported on this CPU)";
107   }
108 }
109
110 TEST(Checksum, crc32c_continuation_hardware) {
111   if (folly::detail::crc32c_hw_supported()) {
112     testCRC32CContinuation(folly::detail::crc32c_hw);
113   } else {
114     LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests" <<
115         " (not supported on this CPU)";
116   }
117 }
118
119 TEST(Checksum, crc32c_autodetect) {
120   testCRC32C(folly::crc32c);
121 }
122
123 TEST(Checksum, crc32c_continuation_autodetect) {
124   testCRC32CContinuation(folly::crc32c);
125 }
126
127 int main(int argc, char** argv) {
128   testing::InitGoogleTest(&argc, argv);
129   google::ParseCommandLineFlags(&argc, &argv, true);
130
131   // Populate a buffer with a deterministic pattern
132   // on which to compute checksums
133   const uint8_t* src = buffer;
134   uint64_t* dst = (uint64_t*)buffer;
135   const uint64_t* end = (const uint64_t*)(buffer + BUFFER_SIZE);
136   *dst++ = 0;
137   while (dst < end) {
138     *dst++ = folly::hash::fnv64_buf((const char*)src, sizeof(uint64_t));
139     src += sizeof(uint64_t);
140   }
141
142   auto ret = RUN_ALL_TESTS();
143   if (!ret && FLAGS_benchmark) {
144     folly::runBenchmarks();
145   }
146   return ret;
147 }