Spdk/test/unit/lib/util/crc32c.c/crc32c_ut.c
Jim Harris 488570ebd4 Replace most BSD 3-clause license text with SPDX identifier.
Many open source projects have moved to using SPDX identifiers
to specify license information, reducing the amount of
boilerplate code in every source file.  This patch replaces
the bulk of SPDK .c, .cpp and Makefiles with the BSD-3-Clause
identifier.

Almost all of these files share the exact same license text,
and this patch only modifies the files that contain the
most common license text.  There can be slight variations
because the third clause contains company names - most say
"Intel Corporation", but there are instances for Nvidia,
Samsung, Eideticom and even "the copyright holder".

Used a bash script to automate replacement of the license text
with SPDX identifier which is checked into scripts/spdx.sh.

Signed-off-by: Jim Harris <james.r.harris@intel.com>
Change-Id: Iaa88ab5e92ea471691dc298cfe41ebfb5d169780
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/12904
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Community-CI: Mellanox Build Bot
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Aleksey Marchuk <alexeymar@nvidia.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Dong Yi <dongx.yi@intel.com>
Reviewed-by: Konrad Sztyber <konrad.sztyber@intel.com>
Reviewed-by: Paul Luse <paul.e.luse@intel.com>
Reviewed-by: <qun.wan@intel.com>
2022-06-09 07:35:12 +00:00

139 lines
3.4 KiB
C

/* SPDX-License-Identifier: BSD-3-Clause
* Copyright (c) Intel Corporation.
* All rights reserved.
*/
#include "spdk/stdinc.h"
#include "spdk_cunit.h"
#include "util/crc32.c"
#include "util/crc32c.c"
static void
test_crc32c(void)
{
uint32_t crc;
char buf[1024], buf1[1024];
struct iovec iov[2] = {};
/* Verify a string's CRC32-C value against the known correct result. */
snprintf(buf, sizeof(buf), "%s", "Hello world!");
crc = 0xFFFFFFFFu;
crc = spdk_crc32c_update(buf, strlen(buf), crc);
crc ^= 0xFFFFFFFFu;
CU_ASSERT(crc == 0x7b98e751);
crc = 0xFFFFFFFFu;
iov[0].iov_base = buf;
iov[0].iov_len = strlen(buf);
crc = spdk_crc32c_iov_update(iov, 1, crc);
crc ^= 0xFFFFFFFFu;
CU_ASSERT(crc == 0x7b98e751);
crc = 0xFFFFFFFFu;
snprintf(buf, sizeof(buf), "%s", "Hello");
iov[0].iov_base = buf;
iov[0].iov_len = strlen(buf);
snprintf(buf1, sizeof(buf1), "%s", " world!");
iov[1].iov_base = buf1;
iov[1].iov_len = strlen(buf1);
crc = spdk_crc32c_iov_update(iov, 2, crc);
crc ^= 0xFFFFFFFFu;
CU_ASSERT(crc == 0x7b98e751);
/*
* The main loop of the optimized CRC32-C implementation processes data in 8-byte blocks,
* followed by a loop to handle the 0-7 trailing bytes.
* Test all buffer sizes from 0 to 7 in order to hit all possible trailing byte counts.
*/
/* 0-byte buffer should not modify CRC at all, so final result should be ~0 ^ ~0 == 0 */
snprintf(buf, sizeof(buf), "%s", "");
crc = 0xFFFFFFFFu;
crc = spdk_crc32c_update(buf, strlen(buf), crc);
crc ^= 0xFFFFFFFFu;
CU_ASSERT(crc == 0);
/* 1-byte buffer */
snprintf(buf, sizeof(buf), "%s", "1");
crc = 0xFFFFFFFFu;
crc = spdk_crc32c_update(buf, strlen(buf), crc);
crc ^= 0xFFFFFFFFu;
CU_ASSERT(crc == 0x90F599E3);
/* 2-byte buffer */
snprintf(buf, sizeof(buf), "%s", "12");
crc = 0xFFFFFFFFu;
crc = spdk_crc32c_update(buf, strlen(buf), crc);
crc ^= 0xFFFFFFFFu;
CU_ASSERT(crc == 0x7355C460);
/* 3-byte buffer */
snprintf(buf, sizeof(buf), "%s", "123");
crc = 0xFFFFFFFFu;
crc = spdk_crc32c_update(buf, strlen(buf), crc);
crc ^= 0xFFFFFFFFu;
CU_ASSERT(crc == 0x107B2FB2);
/* 4-byte buffer */
snprintf(buf, sizeof(buf), "%s", "1234");
crc = 0xFFFFFFFFu;
crc = spdk_crc32c_update(buf, strlen(buf), crc);
crc ^= 0xFFFFFFFFu;
CU_ASSERT(crc == 0xF63AF4EE);
/* 5-byte buffer */
snprintf(buf, sizeof(buf), "%s", "12345");
crc = 0xFFFFFFFFu;
crc = spdk_crc32c_update(buf, strlen(buf), crc);
crc ^= 0xFFFFFFFFu;
CU_ASSERT(crc == 0x18D12335);
/* 6-byte buffer */
snprintf(buf, sizeof(buf), "%s", "123456");
crc = 0xFFFFFFFFu;
crc = spdk_crc32c_update(buf, strlen(buf), crc);
crc ^= 0xFFFFFFFFu;
CU_ASSERT(crc == 0x41357186);
/* 7-byte buffer */
snprintf(buf, sizeof(buf), "%s", "1234567");
crc = 0xFFFFFFFFu;
crc = spdk_crc32c_update(buf, strlen(buf), crc);
crc ^= 0xFFFFFFFFu;
CU_ASSERT(crc == 0x124297EA);
/* Test a buffer of exactly 8 bytes (one block in the main CRC32-C loop). */
snprintf(buf, sizeof(buf), "%s", "12345678");
crc = 0xFFFFFFFFu;
crc = spdk_crc32c_update(buf, strlen(buf), crc);
crc ^= 0xFFFFFFFFu;
CU_ASSERT(crc == 0x6087809A);
}
int
main(int argc, char **argv)
{
CU_pSuite suite = NULL;
unsigned int num_failures;
CU_set_error_action(CUEA_ABORT);
CU_initialize_registry();
suite = CU_add_suite("crc32c", NULL, NULL);
CU_ADD_TEST(suite, test_crc32c);
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
num_failures = CU_get_number_of_failures();
CU_cleanup_registry();
return num_failures;
}