xref: /llvm-project/llvm/unittests/Support/CRCTest.cpp (revision 7fc871591f1399cd88ff301ed84fa67dc3bf7a6b)
118873b22SEugene Leviant //===- llvm/unittest/Support/CRCTest.cpp - CRC tests ----------------------===//
218873b22SEugene Leviant //
318873b22SEugene Leviant // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
418873b22SEugene Leviant // See https://llvm.org/LICENSE.txt for license information.
518873b22SEugene Leviant // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
618873b22SEugene Leviant //
718873b22SEugene Leviant //===----------------------------------------------------------------------===//
818873b22SEugene Leviant //
918873b22SEugene Leviant // This file implements unit tests for CRC calculation functions.
1018873b22SEugene Leviant //
1118873b22SEugene Leviant //===----------------------------------------------------------------------===//
1218873b22SEugene Leviant 
1318873b22SEugene Leviant #include "llvm/Support/CRC.h"
141e1e3ba2SHans Wennborg #include "llvm/ADT/StringExtras.h"
1518873b22SEugene Leviant #include "gtest/gtest.h"
166c4a8bc0SHans Wennborg #include <stdlib.h>
1718873b22SEugene Leviant 
1818873b22SEugene Leviant using namespace llvm;
1918873b22SEugene Leviant 
2018873b22SEugene Leviant namespace {
2118873b22SEugene Leviant 
TEST(CRCTest,CRC32)2218873b22SEugene Leviant TEST(CRCTest, CRC32) {
231e1e3ba2SHans Wennborg   EXPECT_EQ(0x414FA339U, llvm::crc32(arrayRefFromStringRef(
241e1e3ba2SHans Wennborg                              "The quick brown fox jumps over the lazy dog")));
251e1e3ba2SHans Wennborg 
2618873b22SEugene Leviant   // CRC-32/ISO-HDLC test vector
2718873b22SEugene Leviant   // http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat.crc-32c
281e1e3ba2SHans Wennborg   EXPECT_EQ(0xCBF43926U, llvm::crc32(arrayRefFromStringRef("123456789")));
291e1e3ba2SHans Wennborg 
301e1e3ba2SHans Wennborg   // Check the CRC-32 of each byte value, exercising all of CRCTable.
311e1e3ba2SHans Wennborg   for (int i = 0; i < 256; i++) {
321e1e3ba2SHans Wennborg     // Compute CRCTable[i] using Hacker's Delight (2nd ed.) Figure 14-7.
331e1e3ba2SHans Wennborg     uint32_t crc = i;
341e1e3ba2SHans Wennborg     for (int j = 7; j >= 0; j--) {
351e1e3ba2SHans Wennborg       uint32_t mask = -(crc & 1);
361e1e3ba2SHans Wennborg       crc = (crc >> 1) ^ (0xEDB88320 & mask);
371e1e3ba2SHans Wennborg     }
381e1e3ba2SHans Wennborg 
391e1e3ba2SHans Wennborg     // CRCTable[i] is the CRC-32 of i without the initial and final bit flips.
401e1e3ba2SHans Wennborg     uint8_t byte = i;
411e1e3ba2SHans Wennborg     EXPECT_EQ(crc, ~llvm::crc32(0xFFFFFFFFU, byte));
421e1e3ba2SHans Wennborg   }
436c4a8bc0SHans Wennborg 
446c4a8bc0SHans Wennborg   EXPECT_EQ(0x00000000U, llvm::crc32(arrayRefFromStringRef("")));
4518873b22SEugene Leviant }
4618873b22SEugene Leviant 
476c4a8bc0SHans Wennborg #if (SIZE_MAX > UINT32_MAX) && defined(EXPENSIVE_CHECKS)
TEST(CRCTest,LargeCRC32)486c4a8bc0SHans Wennborg TEST(CRCTest, LargeCRC32) {
496c4a8bc0SHans Wennborg   // Check that crc32 can handle inputs with sizes larger than 32 bits.
506c4a8bc0SHans Wennborg   size_t TestSize = (size_t)UINT32_MAX + 42;
516c4a8bc0SHans Wennborg   uint8_t *TestData = (uint8_t*)calloc(TestSize, 1);
526c4a8bc0SHans Wennborg   if (!TestData)
53*7fc87159SPaul Robinson     GTEST_SKIP();
546c4a8bc0SHans Wennborg 
556c4a8bc0SHans Wennborg   // Test expectation generated with:
566c4a8bc0SHans Wennborg   // $ truncate --size=`echo 2^32-1+42 | bc` /tmp/foo
576c4a8bc0SHans Wennborg   // $ crc32 /tmp/foo
5838818b60Sserge-sans-paille   EXPECT_EQ(0xE46F28FBU, llvm::crc32(ArrayRef(TestData, TestSize)));
596c4a8bc0SHans Wennborg 
606c4a8bc0SHans Wennborg   free(TestData);
616c4a8bc0SHans Wennborg }
626c4a8bc0SHans Wennborg #endif
636c4a8bc0SHans Wennborg 
6418873b22SEugene Leviant } // end anonymous namespace
65