| 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" |
| 14 |
#include "llvm/ADT/StringExtras.h" |
| 15 |
#include "gtest/gtest.h" |
| 16 |
#include <stdlib.h> |
| 17 |
|
| 18 |
using namespace llvm; |
| 19 |
|
| 20 |
namespace { |
| 21 |
|
| 22 |
TEST(CRCTest, CRC32) { |
| 23 |
EXPECT_EQ(0x414FA339U, llvm::crc32(arrayRefFromStringRef( |
| 24 |
"The quick brown fox jumps over the lazy dog"))); |
| 25 |
|
| 26 |
// CRC-32/ISO-HDLC test vector |
| 27 |
// http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat.crc-32c |
| 28 |
EXPECT_EQ(0xCBF43926U, llvm::crc32(arrayRefFromStringRef("123456789"))); |
| 29 |
|
| 30 |
// Check the CRC-32 of each byte value, exercising all of CRCTable. |
| 31 |
for (int i = 0; i < 256; i++) { |
| 32 |
// Compute CRCTable[i] using Hacker's Delight (2nd ed.) Figure 14-7. |
| 33 |
uint32_t crc = i; |
| 34 |
for (int j = 7; j >= 0; j--) { |
| 35 |
uint32_t mask = -(crc & 1); |
| 36 |
crc = (crc >> 1) ^ (0xEDB88320 & mask); |
| 37 |
} |
| 38 |
|
| 39 |
// CRCTable[i] is the CRC-32 of i without the initial and final bit flips. |
| 40 |
uint8_t byte = i; |
| 41 |
EXPECT_EQ(crc, ~llvm::crc32(0xFFFFFFFFU, byte)); |
| 42 |
} |
| 43 |
|
| 44 |
EXPECT_EQ(0x00000000U, llvm::crc32(arrayRefFromStringRef(""))); |
| 45 |
} |
| 46 |
|
| 47 |
#if (SIZE_MAX > UINT32_MAX) && defined(EXPENSIVE_CHECKS) |
| 48 |
TEST(CRCTest, LargeCRC32) { |
| 49 |
// Check that crc32 can handle inputs with sizes larger than 32 bits. |
| 50 |
size_t TestSize = (size_t)UINT32_MAX + 42; |
| 51 |
uint8_t *TestData = (uint8_t*)calloc(TestSize, 1); |
| 52 |
if (!TestData) |
| 53 |
return; |
| 54 |
|
| 55 |
// Test expectation generated with: |
| 56 |
// $ truncate --size=`echo 2^32-1+42 | bc` /tmp/foo |
| 57 |
// $ crc32 /tmp/foo |
| 58 |
EXPECT_EQ(0xE46F28FBU, llvm::crc32(ArrayRef(TestData, TestSize))); |
| 59 |
|
| 60 |
free(TestData); |
| 61 |
} |
| 62 |
#endif |
| 63 |
|
| 64 |
} // end anonymous namespace |