1 // Copyright 2017 The Chromium 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.
4
5 #include "components/zucchini/crc32.h"
6
7 #include <stdint.h>
8
9 #include <iterator>
10
11 #include "base/test/gtest_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace zucchini {
15
16 constexpr uint8_t bytes[] = {0x10, 0x32, 0x54, 0x76, 0x98,
17 0xBA, 0xDC, 0xFE, 0x10, 0x00};
18
TEST(Crc32Test,All)19 TEST(Crc32Test, All) {
20 // Results can be verified with any CRC-32 calculator found online.
21
22 // Empty region.
23 EXPECT_EQ(0x00000000U, CalculateCrc32(std::begin(bytes), std::begin(bytes)));
24
25 // Single byte.
26 EXPECT_EQ(0xCFB5FFE9U,
27 CalculateCrc32(std::begin(bytes), std::begin(bytes) + 1));
28
29 // Same byte (0x10) appearing at different location.
30 EXPECT_EQ(0xCFB5FFE9U,
31 CalculateCrc32(std::begin(bytes) + 8, std::begin(bytes) + 9));
32
33 // Single byte of 0.
34 EXPECT_EQ(0xD202EF8DU,
35 CalculateCrc32(std::begin(bytes) + 9, std::end(bytes)));
36
37 // Whole region.
38 EXPECT_EQ(0xA86FD7D6U, CalculateCrc32(std::begin(bytes), std::end(bytes)));
39
40 // Whole region excluding 0 at end.
41 EXPECT_EQ(0x0762F38BU,
42 CalculateCrc32(std::begin(bytes), std::begin(bytes) + 9));
43
44 EXPECT_DCHECK_DEATH(CalculateCrc32(std::begin(bytes) + 1, std::begin(bytes)));
45 }
46
47 } // namespace zucchini
48