xref: /spdk/test/unit/lib/util/math.c/math_ut.c (revision ea941caeaf896fdf2aef7685f86f37023060faed)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2020 Intel Corporation.
3  *   Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES.
4  *   All rights reserved.
5  */
6 
7 #include "spdk/stdinc.h"
8 
9 #include "spdk_internal/cunit.h"
10 
11 #include "util/math.c"
12 
13 static void
test_serial_number_arithmetic(void)14 test_serial_number_arithmetic(void)
15 {
16 	CU_ASSERT(spdk_sn32_add(0, 1) == 1);
17 	CU_ASSERT(spdk_sn32_add(1, 1) == 2);
18 	CU_ASSERT(spdk_sn32_add(1, 2) == 3);
19 	CU_ASSERT(spdk_sn32_add(1, UINT32_MAX) == 0);
20 	CU_ASSERT(spdk_sn32_add(UINT32_MAX, UINT32_MAX) == UINT32_MAX - 1);
21 	CU_ASSERT(spdk_sn32_gt(1, 0) == true);
22 	CU_ASSERT(spdk_sn32_gt(2, 1) == true);
23 	CU_ASSERT(spdk_sn32_gt(UINT32_MAX, UINT32_MAX - 1) == true);
24 	CU_ASSERT(spdk_sn32_gt(0, UINT32_MAX) == true);
25 	CU_ASSERT(spdk_sn32_gt(100, UINT32_MAX - 100) == true);
26 	CU_ASSERT(spdk_sn32_lt(1, 0) == false);
27 	CU_ASSERT(spdk_sn32_lt(2, 1) == false);
28 	CU_ASSERT(spdk_sn32_lt(UINT32_MAX, UINT32_MAX - 1) == false);
29 	CU_ASSERT(spdk_sn32_lt(0, UINT32_MAX) == false);
30 	CU_ASSERT(spdk_sn32_lt(100, UINT32_MAX - 100) == false);
31 }
32 
33 static void
test_memset_s(void)34 test_memset_s(void)
35 {
36 	char secret[] = "0123456789abcdef";
37 
38 	/* Zero length, nothing should be changed */
39 	spdk_memset_s(secret, sizeof(secret), 'b', 0);
40 	CU_ASSERT_EQUAL(memcmp(secret, "0123456789abcdef", sizeof(secret)), 0);
41 
42 	/* Fill digits */
43 	spdk_memset_s(secret, sizeof(secret), 'x', 10);
44 	CU_ASSERT_EQUAL(memcmp(secret, "xxxxxxxxxxabcdef", sizeof(secret)), 0);
45 
46 	/* Fill the whole string except of the NULL char */
47 	spdk_memset_s(secret, sizeof(secret), 'y', sizeof(secret) - 1);
48 	CU_ASSERT_EQUAL(memcmp(secret, "yyyyyyyyyyyyyyyy", sizeof(secret) - 1), 0);
49 }
50 
51 int
main(int argc,char ** argv)52 main(int argc, char **argv)
53 {
54 	CU_pSuite	suite_math = NULL, suite_erase = NULL;
55 	unsigned int	num_failures;
56 
57 	CU_initialize_registry();
58 
59 	suite_math = CU_add_suite("math", NULL, NULL);
60 	CU_ADD_TEST(suite_math, test_serial_number_arithmetic);
61 
62 	suite_erase = CU_add_suite("erase", NULL, NULL);
63 	CU_ADD_TEST(suite_erase, test_memset_s);
64 
65 
66 	num_failures = spdk_ut_run_tests(argc, argv, NULL);
67 
68 	CU_cleanup_registry();
69 
70 	return num_failures;
71 }
72