xref: /spdk/test/unit/lib/util/math.c/math_ut.c (revision ea941caeaf896fdf2aef7685f86f37023060faed)
1488570ebSJim Harris /*   SPDX-License-Identifier: BSD-3-Clause
2a6dbe372Spaul luse  *   Copyright (C) 2020 Intel Corporation.
3415fa164SAlexey Marchuk  *   Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES.
4ded6d2c7SShuhei Matsumoto  *   All rights reserved.
5ded6d2c7SShuhei Matsumoto  */
6ded6d2c7SShuhei Matsumoto 
7ded6d2c7SShuhei Matsumoto #include "spdk/stdinc.h"
8ded6d2c7SShuhei Matsumoto 
9ae431e31SKonrad Sztyber #include "spdk_internal/cunit.h"
10ded6d2c7SShuhei Matsumoto 
11ded6d2c7SShuhei Matsumoto #include "util/math.c"
12ded6d2c7SShuhei Matsumoto 
13ded6d2c7SShuhei Matsumoto static void
test_serial_number_arithmetic(void)14ded6d2c7SShuhei Matsumoto test_serial_number_arithmetic(void)
15ded6d2c7SShuhei Matsumoto {
16ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_add(0, 1) == 1);
17ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_add(1, 1) == 2);
18ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_add(1, 2) == 3);
19ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_add(1, UINT32_MAX) == 0);
20ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_add(UINT32_MAX, UINT32_MAX) == UINT32_MAX - 1);
21ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_gt(1, 0) == true);
22ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_gt(2, 1) == true);
23ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_gt(UINT32_MAX, UINT32_MAX - 1) == true);
24ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_gt(0, UINT32_MAX) == true);
25ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_gt(100, UINT32_MAX - 100) == true);
26ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_lt(1, 0) == false);
27ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_lt(2, 1) == false);
28ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_lt(UINT32_MAX, UINT32_MAX - 1) == false);
29ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_lt(0, UINT32_MAX) == false);
30ded6d2c7SShuhei Matsumoto 	CU_ASSERT(spdk_sn32_lt(100, UINT32_MAX - 100) == false);
31ded6d2c7SShuhei Matsumoto }
32ded6d2c7SShuhei Matsumoto 
33415fa164SAlexey Marchuk static void
test_memset_s(void)34415fa164SAlexey Marchuk test_memset_s(void)
35415fa164SAlexey Marchuk {
36415fa164SAlexey Marchuk 	char secret[] = "0123456789abcdef";
37415fa164SAlexey Marchuk 
38415fa164SAlexey Marchuk 	/* Zero length, nothing should be changed */
39415fa164SAlexey Marchuk 	spdk_memset_s(secret, sizeof(secret), 'b', 0);
40415fa164SAlexey Marchuk 	CU_ASSERT_EQUAL(memcmp(secret, "0123456789abcdef", sizeof(secret)), 0);
41415fa164SAlexey Marchuk 
42415fa164SAlexey Marchuk 	/* Fill digits */
43415fa164SAlexey Marchuk 	spdk_memset_s(secret, sizeof(secret), 'x', 10);
44415fa164SAlexey Marchuk 	CU_ASSERT_EQUAL(memcmp(secret, "xxxxxxxxxxabcdef", sizeof(secret)), 0);
45415fa164SAlexey Marchuk 
46415fa164SAlexey Marchuk 	/* Fill the whole string except of the NULL char */
47415fa164SAlexey Marchuk 	spdk_memset_s(secret, sizeof(secret), 'y', sizeof(secret) - 1);
48415fa164SAlexey Marchuk 	CU_ASSERT_EQUAL(memcmp(secret, "yyyyyyyyyyyyyyyy", sizeof(secret) - 1), 0);
49415fa164SAlexey Marchuk }
50415fa164SAlexey Marchuk 
51ded6d2c7SShuhei Matsumoto int
main(int argc,char ** argv)52ded6d2c7SShuhei Matsumoto main(int argc, char **argv)
53ded6d2c7SShuhei Matsumoto {
54415fa164SAlexey Marchuk 	CU_pSuite	suite_math = NULL, suite_erase = NULL;
55ded6d2c7SShuhei Matsumoto 	unsigned int	num_failures;
56ded6d2c7SShuhei Matsumoto 
5778b696bcSVitaliy Mysak 	CU_initialize_registry();
58ded6d2c7SShuhei Matsumoto 
59415fa164SAlexey Marchuk 	suite_math = CU_add_suite("math", NULL, NULL);
60415fa164SAlexey Marchuk 	CU_ADD_TEST(suite_math, test_serial_number_arithmetic);
61ded6d2c7SShuhei Matsumoto 
62415fa164SAlexey Marchuk 	suite_erase = CU_add_suite("erase", NULL, NULL);
63415fa164SAlexey Marchuk 	CU_ADD_TEST(suite_erase, test_memset_s);
64ded6d2c7SShuhei Matsumoto 
65ded6d2c7SShuhei Matsumoto 
66*ea941caeSKonrad Sztyber 	num_failures = spdk_ut_run_tests(argc, argv, NULL);
67ded6d2c7SShuhei Matsumoto 
68ded6d2c7SShuhei Matsumoto 	CU_cleanup_registry();
69ded6d2c7SShuhei Matsumoto 
70ded6d2c7SShuhei Matsumoto 	return num_failures;
71ded6d2c7SShuhei Matsumoto }
72