Zihan Chen | 0a7db3e | 2020-06-23 16:36:06 -0700 | [diff] [blame] | 1 | // 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 Hepler | eb8a34c | 2020-09-02 13:11:03 -0700 | [diff] [blame^] | 20 | #include "pw_bytes/array.h" |
Zihan Chen | 0a7db3e | 2020-06-23 16:36:06 -0700 | [diff] [blame] | 21 | |
| 22 | namespace pw::checksum { |
| 23 | namespace { |
| 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 Hepler | eb8a34c | 2020-09-02 13:11:03 -0700 | [diff] [blame^] | 31 | constexpr auto kBytes = bytes::Array<1, 2, 3, 4, 5, 6, 7, 8, 9>(); |
| 32 | constexpr auto kBytesPart0 = bytes::Array<1, 2, 3, 4, 5>(); |
| 33 | constexpr auto kBytesPart1 = bytes::Array<6, 7, 8, 9>(); |
Zihan Chen | 0a7db3e | 2020-06-23 16:36:06 -0700 | [diff] [blame] | 34 | constexpr uint32_t kBufferCrc = 0x40EFAB9E; |
| 35 | |
| 36 | constexpr 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."; |
| 39 | constexpr uint32_t kStringCrc = 0x9EC87F88; |
| 40 | |
| 41 | TEST(Crc32, Empty) { |
Wyatt Hepler | eb8a34c | 2020-09-02 13:11:03 -0700 | [diff] [blame^] | 42 | EXPECT_EQ(Crc32::Calculate(std::span<std::byte>()), PW_CHECKSUM_EMPTY_CRC32); |
Zihan Chen | 0a7db3e | 2020-06-23 16:36:06 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | TEST(Crc32, Buffer) { |
Wyatt Hepler | eb8a34c | 2020-09-02 13:11:03 -0700 | [diff] [blame^] | 46 | EXPECT_EQ(Crc32::Calculate(std::as_bytes(std::span(kBytes))), kBufferCrc); |
Zihan Chen | 0a7db3e | 2020-06-23 16:36:06 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | TEST(Crc32, String) { |
Wyatt Hepler | eb8a34c | 2020-09-02 13:11:03 -0700 | [diff] [blame^] | 50 | EXPECT_EQ(Crc32::Calculate(std::as_bytes(std::span(kString))), kStringCrc); |
| 51 | } |
| 52 | |
| 53 | TEST(Crc32Class, ByteByByte) { |
| 54 | Crc32 crc; |
| 55 | for (std::byte b : kBytes) { |
| 56 | crc.Update(b); |
| 57 | } |
| 58 | EXPECT_EQ(crc.value(), kBufferCrc); |
| 59 | } |
| 60 | |
| 61 | TEST(Crc32Class, Buffer) { |
| 62 | Crc32 crc32; |
| 63 | crc32.Update(std::as_bytes(std::span(kBytes))); |
| 64 | EXPECT_EQ(crc32.value(), kBufferCrc); |
| 65 | } |
| 66 | |
| 67 | TEST(Crc32Class, BufferAppend) { |
| 68 | Crc32 crc32; |
| 69 | crc32.Update(kBytesPart0); |
| 70 | crc32.Update(kBytesPart1); |
| 71 | EXPECT_EQ(crc32.value(), kBufferCrc); |
| 72 | } |
| 73 | |
| 74 | TEST(Crc32Class, String) { |
| 75 | Crc32 crc32; |
| 76 | crc32.Update(std::as_bytes(std::span(kString))); |
| 77 | EXPECT_EQ(crc32.value(), kStringCrc); |
Zihan Chen | 0a7db3e | 2020-06-23 16:36:06 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | extern "C" uint32_t CallChecksumCrc32(const void* data, size_t size_bytes); |
| 81 | extern "C" uint32_t CallChecksumCrc32Append(const void* data, |
Wyatt Hepler | eb8a34c | 2020-09-02 13:11:03 -0700 | [diff] [blame^] | 82 | size_t size_bytes, |
| 83 | uint32_t value); |
Zihan Chen | 0a7db3e | 2020-06-23 16:36:06 -0700 | [diff] [blame] | 84 | |
| 85 | TEST(Crc32FromC, Buffer) { |
Wyatt Hepler | eb8a34c | 2020-09-02 13:11:03 -0700 | [diff] [blame^] | 86 | EXPECT_EQ(CallChecksumCrc32(kBytes.data(), kBytes.size()), kBufferCrc); |
Zihan Chen | 0a7db3e | 2020-06-23 16:36:06 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TEST(Crc32FromC, String) { |
| 90 | EXPECT_EQ(CallChecksumCrc32(kString.data(), kString.size()), kStringCrc); |
| 91 | } |
| 92 | |
| 93 | TEST(Crc32AppendFromC, Buffer) { |
Wyatt Hepler | eb8a34c | 2020-09-02 13:11:03 -0700 | [diff] [blame^] | 94 | 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 Chen | 0a7db3e | 2020-06-23 16:36:06 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST(Crc32AppendFromC, String) { |
Wyatt Hepler | eb8a34c | 2020-09-02 13:11:03 -0700 | [diff] [blame^] | 103 | EXPECT_EQ(CallChecksumCrc32Append( |
| 104 | kString.data(), kString.size(), PW_CHECKSUM_EMPTY_CRC32), |
Zihan Chen | 0a7db3e | 2020-06-23 16:36:06 -0700 | [diff] [blame] | 105 | kStringCrc); |
| 106 | } |
| 107 | |
| 108 | } // namespace |
| 109 | } // namespace pw::checksum |