zlib / tests /tests_crc32_make_crc_table.c
AryaWu's picture
Upload folder using huggingface_hub
e996a55 verified
#include "unity/unity.h"
#include "zlib.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
/* Wrapper for the local function provided in the source module */
extern void test_make_crc_table(void);
void setUp(void) {
/* Setup code here, or leave empty */
}
void tearDown(void) {
/* Cleanup code here, or leave empty */
}
/* Verify that make_crc_table initializes known CRC table entries */
void test_make_crc_table_initializes_known_entries(void) {
test_make_crc_table();
const unsigned long *tbl = get_crc_table();
TEST_ASSERT_NOT_NULL(tbl);
/* Check several well-known entries from the standard CRC-32 table */
TEST_ASSERT_EQUAL_HEX32(0x00000000u, (uint32_t)tbl[0]);
TEST_ASSERT_EQUAL_HEX32(0x77073096u, (uint32_t)tbl[1]);
TEST_ASSERT_EQUAL_HEX32(0xEE0E612Cu, (uint32_t)tbl[2]);
TEST_ASSERT_EQUAL_HEX32(0x990951BAu, (uint32_t)tbl[3]);
TEST_ASSERT_EQUAL_HEX32(0x076DC419u, (uint32_t)tbl[4]);
TEST_ASSERT_EQUAL_HEX32(0x706AF48Fu, (uint32_t)tbl[5]);
TEST_ASSERT_EQUAL_HEX32(0xE963A535u, (uint32_t)tbl[6]);
TEST_ASSERT_EQUAL_HEX32(0x9E6495A3u, (uint32_t)tbl[7]);
TEST_ASSERT_EQUAL_HEX32(0x0EDB8832u, (uint32_t)tbl[8]);
TEST_ASSERT_EQUAL_HEX32(0x79DCB8A4u, (uint32_t)tbl[9]);
TEST_ASSERT_EQUAL_HEX32(0xE0D5E91Eu, (uint32_t)tbl[10]);
TEST_ASSERT_EQUAL_HEX32(0x97D2D988u, (uint32_t)tbl[11]);
TEST_ASSERT_EQUAL_HEX32(0x09B64C2Bu, (uint32_t)tbl[12]);
TEST_ASSERT_EQUAL_HEX32(0x7EB17CBDu, (uint32_t)tbl[13]);
TEST_ASSERT_EQUAL_HEX32(0xE7B82D07u, (uint32_t)tbl[14]);
TEST_ASSERT_EQUAL_HEX32(0x90BF1D91u, (uint32_t)tbl[15]);
/* Also check the last entry */
TEST_ASSERT_EQUAL_HEX32(0x2D02EF8Du, (uint32_t)tbl[255]);
}
/* Verify idempotency: calling make_crc_table multiple times yields stable results */
void test_make_crc_table_idempotent(void) {
test_make_crc_table();
const unsigned long *tbl1 = get_crc_table();
TEST_ASSERT_NOT_NULL(tbl1);
/* Copy a subset of the table (first 32 and last one) */
uint32_t snapshot[33];
for (int i = 0; i < 32; i++) {
snapshot[i] = (uint32_t)tbl1[i];
}
snapshot[32] = (uint32_t)tbl1[255];
/* Call again and compare */
test_make_crc_table();
const unsigned long *tbl2 = get_crc_table();
TEST_ASSERT_NOT_NULL(tbl2);
for (int i = 0; i < 32; i++) {
TEST_ASSERT_EQUAL_HEX32(snapshot[i], (uint32_t)tbl2[i]);
}
TEST_ASSERT_EQUAL_HEX32(snapshot[32], (uint32_t)tbl2[255]);
}
/* Verify that after make_crc_table, crc32 computes the canonical vector */
void test_make_crc_table_crc32_known_vector(void) {
test_make_crc_table();
const char *msg = "123456789"; /* canonical CRC-32 test vector */
uLong crc = crc32(0L, (const Bytef *)msg, (uInt)strlen(msg));
TEST_ASSERT_EQUAL_HEX32(0xCBF43926u, (uint32_t)crc);
}
/* Verify crc32_combine64 uses the tables correctly by comparing to direct CRC of concatenation */
void test_make_crc_table_crc32_combine_consistency(void) {
test_make_crc_table();
const char *a = "Hello, ";
const char *b = "world!";
const size_t la = strlen(a);
const size_t lb = strlen(b);
uLong crc_a = crc32(0L, (const Bytef *)a, (uInt)la);
uLong crc_b = crc32(0L, (const Bytef *)b, (uInt)lb);
uLong crc_ab_direct = crc32(0L, (const Bytef *)a, (uInt)la);
crc_ab_direct = crc32(crc_ab_direct, (const Bytef *)b, (uInt)lb);
uLong crc_ab_combined = crc32_combine64(crc_a, crc_b, (z_off64_t)lb);
TEST_ASSERT_EQUAL_HEX32((uint32_t)crc_ab_direct, (uint32_t)crc_ab_combined);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_make_crc_table_initializes_known_entries);
RUN_TEST(test_make_crc_table_idempotent);
RUN_TEST(test_make_crc_table_crc32_known_vector);
RUN_TEST(test_make_crc_table_crc32_combine_consistency);
return UNITY_END();
}