Eugene Leviant | 18873b2 | 2019-04-08 12:31:12 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/CRCTest.cpp - CRC tests ----------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements unit tests for CRC calculation functions. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Support/CRC.h" |
Hans Wennborg | 1e1e3ba | 2019-10-09 09:06:30 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringExtras.h" |
Eugene Leviant | 18873b2 | 2019-04-08 12:31:12 +0000 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | TEST(CRCTest, CRC32) { |
Hans Wennborg | 1e1e3ba | 2019-10-09 09:06:30 +0000 | [diff] [blame] | 22 | EXPECT_EQ(0x414FA339U, llvm::crc32(arrayRefFromStringRef( |
| 23 | "The quick brown fox jumps over the lazy dog"))); |
| 24 | |
Eugene Leviant | 18873b2 | 2019-04-08 12:31:12 +0000 | [diff] [blame] | 25 | // CRC-32/ISO-HDLC test vector |
| 26 | // http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat.crc-32c |
Hans Wennborg | 1e1e3ba | 2019-10-09 09:06:30 +0000 | [diff] [blame] | 27 | EXPECT_EQ(0xCBF43926U, llvm::crc32(arrayRefFromStringRef("123456789"))); |
| 28 | |
| 29 | // Check the CRC-32 of each byte value, exercising all of CRCTable. |
| 30 | for (int i = 0; i < 256; i++) { |
| 31 | // Compute CRCTable[i] using Hacker's Delight (2nd ed.) Figure 14-7. |
| 32 | uint32_t crc = i; |
| 33 | for (int j = 7; j >= 0; j--) { |
| 34 | uint32_t mask = -(crc & 1); |
| 35 | crc = (crc >> 1) ^ (0xEDB88320 & mask); |
| 36 | } |
| 37 | |
| 38 | // CRCTable[i] is the CRC-32 of i without the initial and final bit flips. |
| 39 | uint8_t byte = i; |
| 40 | EXPECT_EQ(crc, ~llvm::crc32(0xFFFFFFFFU, byte)); |
| 41 | } |
Eugene Leviant | 18873b2 | 2019-04-08 12:31:12 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | } // end anonymous namespace |