blob: 3ac9c78f6eee88d5caf0407247d96b914a8c281e [file] [log] [blame]
Wyatt Hepler92ccb662020-01-21 18:28:41 -08001// 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
Wyatt Hepler12f66a12020-09-02 15:43:02 -070015#include "pw_checksum/crc16_ccitt.h"
Wyatt Hepler92ccb662020-01-21 18:28:41 -080016
17#include <string_view>
18
19#include "gtest/gtest.h"
20
21namespace pw::checksum {
22namespace {
23
24// The expected CRC16 values were calculated using
25//
26// http://www.sunshine2k.de/coding/javascript/crc/crc_js.html
27//
28// with polynomial 0x1021, initial value 0xFFFF.
29constexpr uint8_t kBytes[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
30constexpr uint16_t kBufferCrc = 0x3B0A;
31
32constexpr std::string_view kString =
33 "In the beginning the Universe was created. This has made a lot of "
34 "people very angry and been widely regarded as a bad move.";
35constexpr uint16_t kStringCrc = 0xC184;
36
37TEST(Crc16, Empty) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070038 EXPECT_EQ(Crc16Ccitt::Calculate(std::span<std::byte>()),
39 Crc16Ccitt::kInitialValue);
Wyatt Hepler92ccb662020-01-21 18:28:41 -080040}
41
42TEST(Crc16, ByteByByte) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070043 uint16_t crc = Crc16Ccitt::kInitialValue;
Wyatt Hepler92ccb662020-01-21 18:28:41 -080044 for (size_t i = 0; i < sizeof(kBytes); i++) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070045 crc = Crc16Ccitt::Calculate(std::byte{kBytes[i]}, crc);
Wyatt Hepler92ccb662020-01-21 18:28:41 -080046 }
47 EXPECT_EQ(crc, kBufferCrc);
48}
49
50TEST(Crc16, Buffer) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070051 EXPECT_EQ(Crc16Ccitt::Calculate(std::as_bytes(std::span(kBytes))),
52 kBufferCrc);
Wyatt Hepler92ccb662020-01-21 18:28:41 -080053}
54
55TEST(Crc16, String) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070056 EXPECT_EQ(Crc16Ccitt::Calculate(std::as_bytes(std::span(kString))),
57 kStringCrc);
Wyatt Hepler92ccb662020-01-21 18:28:41 -080058}
59
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070060TEST(Crc16Class, Buffer) {
61 Crc16Ccitt crc16;
62 crc16.Update(std::as_bytes(std::span(kBytes)));
63 EXPECT_EQ(crc16.value(), kBufferCrc);
64}
65
66TEST(Crc16Class, String) {
67 Crc16Ccitt crc16;
68 crc16.Update(std::as_bytes(std::span(kString)));
69 EXPECT_EQ(crc16.value(), kStringCrc);
70}
71
72extern "C" uint16_t CallChecksumCrc16Ccitt(const void* data, size_t size_bytes);
Wyatt Hepler92ccb662020-01-21 18:28:41 -080073
74TEST(Crc16FromC, Buffer) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070075 EXPECT_EQ(CallChecksumCrc16Ccitt(kBytes, sizeof(kBytes)), kBufferCrc);
Wyatt Hepler92ccb662020-01-21 18:28:41 -080076}
77
78TEST(Crc16FromC, String) {
Wyatt Heplereb8a34c2020-09-02 13:11:03 -070079 EXPECT_EQ(CallChecksumCrc16Ccitt(kString.data(), kString.size()), kStringCrc);
Wyatt Hepler92ccb662020-01-21 18:28:41 -080080}
81
82} // namespace
83} // namespace pw::checksum