1 // Copyright 2017 The CRC32C Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4
5 #include "crc32c/crc32c.h"
6
7 #include <cstddef>
8 #include <cstdint>
9 #include <cstring>
10
11 #include "gtest/gtest.h"
12
13 #include "./crc32c_extend_unittests.h"
14
TEST(Crc32CTest,Crc32c)15 TEST(Crc32CTest, Crc32c) {
16 // From rfc3720 section B.4.
17 uint8_t buf[32];
18
19 std::memset(buf, 0, sizeof(buf));
20 EXPECT_EQ(static_cast<uint32_t>(0x8a9136aa),
21 crc32c::Crc32c(buf, sizeof(buf)));
22
23 std::memset(buf, 0xff, sizeof(buf));
24 EXPECT_EQ(static_cast<uint32_t>(0x62a8ab43),
25 crc32c::Crc32c(buf, sizeof(buf)));
26
27 for (size_t i = 0; i < 32; ++i)
28 buf[i] = static_cast<uint8_t>(i);
29 EXPECT_EQ(static_cast<uint32_t>(0x46dd794e),
30 crc32c::Crc32c(buf, sizeof(buf)));
31
32 for (size_t i = 0; i < 32; ++i)
33 buf[i] = static_cast<uint8_t>(31 - i);
34 EXPECT_EQ(static_cast<uint32_t>(0x113fdb5c),
35 crc32c::Crc32c(buf, sizeof(buf)));
36
37 uint8_t data[48] = {
38 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
40 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00,
41 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 };
43 EXPECT_EQ(static_cast<uint32_t>(0xd9963a56),
44 crc32c::Crc32c(data, sizeof(data)));
45 }
46
47 namespace crc32c {
48
49 struct ApiTestTraits {
Extendcrc32c::ApiTestTraits50 static uint32_t Extend(uint32_t crc, const uint8_t* data, size_t count) {
51 return ::crc32c::Extend(crc, data, count);
52 }
53 };
54
55 INSTANTIATE_TYPED_TEST_SUITE_P(Api, ExtendTest, ApiTestTraits);
56
57 } // namespace crc32c
58
TEST(CRC32CTest,Crc32cCharPointer)59 TEST(CRC32CTest, Crc32cCharPointer) {
60 char buf[32];
61
62 std::memset(buf, 0, sizeof(buf));
63 EXPECT_EQ(static_cast<uint32_t>(0x8a9136aa),
64 crc32c::Crc32c(buf, sizeof(buf)));
65
66 std::memset(buf, 0xff, sizeof(buf));
67 EXPECT_EQ(static_cast<uint32_t>(0x62a8ab43),
68 crc32c::Crc32c(buf, sizeof(buf)));
69
70 for (size_t i = 0; i < 32; ++i)
71 buf[i] = static_cast<char>(i);
72 EXPECT_EQ(static_cast<uint32_t>(0x46dd794e),
73 crc32c::Crc32c(buf, sizeof(buf)));
74
75 for (size_t i = 0; i < 32; ++i)
76 buf[i] = static_cast<char>(31 - i);
77 EXPECT_EQ(static_cast<uint32_t>(0x113fdb5c),
78 crc32c::Crc32c(buf, sizeof(buf)));
79 }
80
TEST(CRC32CTest,Crc32cStdString)81 TEST(CRC32CTest, Crc32cStdString) {
82 std::string buf;
83 buf.resize(32);
84
85 for (size_t i = 0; i < 32; ++i)
86 buf[i] = static_cast<char>(0x00);
87 EXPECT_EQ(static_cast<uint32_t>(0x8a9136aa), crc32c::Crc32c(buf));
88
89 for (size_t i = 0; i < 32; ++i)
90 buf[i] = '\xff';
91 EXPECT_EQ(static_cast<uint32_t>(0x62a8ab43), crc32c::Crc32c(buf));
92
93 for (size_t i = 0; i < 32; ++i)
94 buf[i] = static_cast<char>(i);
95 EXPECT_EQ(static_cast<uint32_t>(0x46dd794e), crc32c::Crc32c(buf));
96
97 for (size_t i = 0; i < 32; ++i)
98 buf[i] = static_cast<char>(31 - i);
99 EXPECT_EQ(static_cast<uint32_t>(0x113fdb5c), crc32c::Crc32c(buf));
100 }
101
102 #if __cplusplus > 201402L
103 #if __has_include(<string_view>)
104
TEST(CRC32CTest,Crc32cStdStringView)105 TEST(CRC32CTest, Crc32cStdStringView) {
106 uint8_t buf[32];
107 std::string_view view(reinterpret_cast<const char*>(buf), sizeof(buf));
108
109 std::memset(buf, 0, sizeof(buf));
110 EXPECT_EQ(static_cast<uint32_t>(0x8a9136aa), crc32c::Crc32c(view));
111
112 std::memset(buf, 0xff, sizeof(buf));
113 EXPECT_EQ(static_cast<uint32_t>(0x62a8ab43), crc32c::Crc32c(view));
114
115 for (size_t i = 0; i < 32; ++i)
116 buf[i] = static_cast<uint8_t>(i);
117 EXPECT_EQ(static_cast<uint32_t>(0x46dd794e), crc32c::Crc32c(view));
118
119 for (size_t i = 0; i < 32; ++i)
120 buf[i] = static_cast<uint8_t>(31 - i);
121 EXPECT_EQ(static_cast<uint32_t>(0x113fdb5c), crc32c::Crc32c(view));
122 }
123
124 #endif // __has_include(<string_view>)
125 #endif // __cplusplus > 201402L
126
127 #define TESTED_EXTEND Extend
128 #include "./crc32c_extend_unittests.h"
129 #undef TESTED_EXTEND
130