xref: /spdk/test/unit/lib/util/crc16.c/crc16_ut.c (revision ea941caeaf896fdf2aef7685f86f37023060faed)
1488570ebSJim Harris /*   SPDX-License-Identifier: BSD-3-Clause
2a6dbe372Spaul luse  *   Copyright (C) 2017 Intel Corporation.
39ca670acSChangpeng Liu  *   All rights reserved.
49ca670acSChangpeng Liu  */
59ca670acSChangpeng Liu 
69ca670acSChangpeng Liu #include "spdk/stdinc.h"
79ca670acSChangpeng Liu 
8ae431e31SKonrad Sztyber #include "spdk_internal/cunit.h"
99ca670acSChangpeng Liu 
107ffe586aSJim Harris #include "util/crc16.c"
119ca670acSChangpeng Liu 
129ca670acSChangpeng Liu static void
test_crc16_t10dif(void)139ca670acSChangpeng Liu test_crc16_t10dif(void)
149ca670acSChangpeng Liu {
159ca670acSChangpeng Liu 	uint16_t crc;
169ca670acSChangpeng Liu 	char buf[] = "123456789";
179ca670acSChangpeng Liu 
18e303567bSShuhei Matsumoto 	crc = spdk_crc16_t10dif(0, buf, strlen(buf));
19e303567bSShuhei Matsumoto 	CU_ASSERT(crc == 0xd0db);
20e303567bSShuhei Matsumoto }
21e303567bSShuhei Matsumoto 
22e303567bSShuhei Matsumoto static void
test_crc16_t10dif_seed(void)23e303567bSShuhei Matsumoto test_crc16_t10dif_seed(void)
24e303567bSShuhei Matsumoto {
25e303567bSShuhei Matsumoto 	uint16_t crc = 0;
26e303567bSShuhei Matsumoto 	char buf1[] = "1234";
27e303567bSShuhei Matsumoto 	char buf2[] = "56789";
28e303567bSShuhei Matsumoto 
29e303567bSShuhei Matsumoto 	crc = spdk_crc16_t10dif(crc, buf1, strlen(buf1));
30e303567bSShuhei Matsumoto 	crc = spdk_crc16_t10dif(crc, buf2, strlen(buf2));
319ca670acSChangpeng Liu 	CU_ASSERT(crc == 0xd0db);
329ca670acSChangpeng Liu }
339ca670acSChangpeng Liu 
343947bc24SShuhei Matsumoto static void
test_crc16_t10dif_copy(void)353947bc24SShuhei Matsumoto test_crc16_t10dif_copy(void)
363947bc24SShuhei Matsumoto {
373947bc24SShuhei Matsumoto 	uint16_t crc1 = 0, crc2;
383947bc24SShuhei Matsumoto 	char buf1[] = "1234";
393947bc24SShuhei Matsumoto 	char buf2[] = "56789";
403947bc24SShuhei Matsumoto 	char *buf3 = calloc(1, strlen(buf1) + strlen(buf2) + 1);
413947bc24SShuhei Matsumoto 	SPDK_CU_ASSERT_FATAL(buf3 != NULL);
423947bc24SShuhei Matsumoto 
433947bc24SShuhei Matsumoto 	crc1 = spdk_crc16_t10dif_copy(crc1, buf3, buf1, strlen(buf1));
443947bc24SShuhei Matsumoto 	crc1 = spdk_crc16_t10dif_copy(crc1, buf3 + strlen(buf1), buf2, strlen(buf2));
453947bc24SShuhei Matsumoto 	CU_ASSERT(crc1 == 0xd0db);
463947bc24SShuhei Matsumoto 
473947bc24SShuhei Matsumoto 	crc2  = spdk_crc16_t10dif(0, buf3, strlen(buf3));
483947bc24SShuhei Matsumoto 	CU_ASSERT(crc2 == 0xd0db);
493947bc24SShuhei Matsumoto 
503947bc24SShuhei Matsumoto 	free(buf3);
513947bc24SShuhei Matsumoto }
523947bc24SShuhei Matsumoto 
539ca670acSChangpeng Liu int
main(int argc,char ** argv)549ca670acSChangpeng Liu main(int argc, char **argv)
559ca670acSChangpeng Liu {
569ca670acSChangpeng Liu 	CU_pSuite	suite = NULL;
579ca670acSChangpeng Liu 	unsigned int	num_failures;
589ca670acSChangpeng Liu 
5978b696bcSVitaliy Mysak 	CU_initialize_registry();
609ca670acSChangpeng Liu 
619ca670acSChangpeng Liu 	suite = CU_add_suite("crc16", NULL, NULL);
629ca670acSChangpeng Liu 
63dcf0ca15SVitaliy Mysak 	CU_ADD_TEST(suite, test_crc16_t10dif);
64dcf0ca15SVitaliy Mysak 	CU_ADD_TEST(suite, test_crc16_t10dif_seed);
65dcf0ca15SVitaliy Mysak 	CU_ADD_TEST(suite, test_crc16_t10dif_copy);
669ca670acSChangpeng Liu 
679ca670acSChangpeng Liu 
68*ea941caeSKonrad Sztyber 	num_failures = spdk_ut_run_tests(argc, argv, NULL);
699ca670acSChangpeng Liu 
709ca670acSChangpeng Liu 	CU_cleanup_registry();
719ca670acSChangpeng Liu 
729ca670acSChangpeng Liu 	return num_failures;
739ca670acSChangpeng Liu }
74