blob: 59d649ab59af7cb86ce0bd41095b20e3d7d87587 [file] [log] [blame]
Zihan Chen0a7db3e2020-06-23 16:36:06 -07001// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#include "pw_checksum/crc32.h"
15
16#include <span>
17#include <string_view>
18
19#include "gtest/gtest.h"
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070020#include "pw_bytes/array.h"
Zihan Chen0a7db3e2020-06-23 16:36:06 -070021
22namespace pw::checksum {
23namespace {
24
25// The expected CRC32 values were calculated using
26//
27// http://www.sunshine2k.de/coding/javascript/crc/crc_js.html
28//
29// with polynomial 0x4C11DB7, initial value 0xFFFFFFFF.
30
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070031constexpr auto kBytes = bytes::Array<1, 2, 3, 4, 5, 6, 7, 8, 9>();
32constexpr auto kBytesPart0 = bytes::Array<1, 2, 3, 4, 5>();
33constexpr auto kBytesPart1 = bytes::Array<6, 7, 8, 9>();
Zihan Chen0a7db3e2020-06-23 16:36:06 -070034constexpr uint32_t kBufferCrc = 0x40EFAB9E;
35
36constexpr std::string_view kString =
37 "In the beginning the Universe was created. This has made a lot of "
38 "people very angry and been widely regarded as a bad move.";
39constexpr uint32_t kStringCrc = 0x9EC87F88;
40
41TEST(Crc32, Empty) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070042 EXPECT_EQ(Crc32::Calculate(std::span<std::byte>()), PW_CHECKSUM_EMPTY_CRC32);
Zihan Chen0a7db3e2020-06-23 16:36:06 -070043}
44
45TEST(Crc32, Buffer) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070046 EXPECT_EQ(Crc32::Calculate(std::as_bytes(std::span(kBytes))), kBufferCrc);
Zihan Chen0a7db3e2020-06-23 16:36:06 -070047}
48
49TEST(Crc32, String) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070050 EXPECT_EQ(Crc32::Calculate(std::as_bytes(std::span(kString))), kStringCrc);
51}
52
53TEST(Crc32Class, ByteByByte) {
54 Crc32 crc;
55 for (std::byte b : kBytes) {
56 crc.Update(b);
57 }
58 EXPECT_EQ(crc.value(), kBufferCrc);
59}
60
61TEST(Crc32Class, Buffer) {
62 Crc32 crc32;
63 crc32.Update(std::as_bytes(std::span(kBytes)));
64 EXPECT_EQ(crc32.value(), kBufferCrc);
65}
66
67TEST(Crc32Class, BufferAppend) {
68 Crc32 crc32;
69 crc32.Update(kBytesPart0);
70 crc32.Update(kBytesPart1);
71 EXPECT_EQ(crc32.value(), kBufferCrc);
72}
73
74TEST(Crc32Class, String) {
75 Crc32 crc32;
76 crc32.Update(std::as_bytes(std::span(kString)));
77 EXPECT_EQ(crc32.value(), kStringCrc);
Zihan Chen0a7db3e2020-06-23 16:36:06 -070078}
79
80extern "C" uint32_t CallChecksumCrc32(const void* data, size_t size_bytes);
81extern "C" uint32_t CallChecksumCrc32Append(const void* data,
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070082 size_t size_bytes,
83 uint32_t value);
Zihan Chen0a7db3e2020-06-23 16:36:06 -070084
85TEST(Crc32FromC, Buffer) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070086 EXPECT_EQ(CallChecksumCrc32(kBytes.data(), kBytes.size()), kBufferCrc);
Zihan Chen0a7db3e2020-06-23 16:36:06 -070087}
88
89TEST(Crc32FromC, String) {
90 EXPECT_EQ(CallChecksumCrc32(kString.data(), kString.size()), kStringCrc);
91}
92
93TEST(Crc32AppendFromC, Buffer) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070094 uint32_t crc = PW_CHECKSUM_EMPTY_CRC32;
95 for (std::byte b : kBytes) {
96 crc = CallChecksumCrc32Append(&b, 1, crc);
97 }
98
99 EXPECT_EQ(crc, kBufferCrc);
Zihan Chen0a7db3e2020-06-23 16:36:06 -0700100}
101
102TEST(Crc32AppendFromC, String) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -0700103 EXPECT_EQ(CallChecksumCrc32Append(
104 kString.data(), kString.size(), PW_CHECKSUM_EMPTY_CRC32),
Zihan Chen0a7db3e2020-06-23 16:36:06 -0700105 kStringCrc);
106}
107
108} // namespace
109} // namespace pw::checksum