]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/lib/json/util/json_util_ut.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / spdk / test / lib / json / util / json_util_ut.c
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include "spdk_cunit.h"
35
36 #include "json_util.c"
37
38 #include <stdio.h>
39 #include <string.h>
40
41 #define NUM_SETUP(x) \
42 snprintf(buf, sizeof(buf), "%s", x); \
43 v.type = SPDK_JSON_VAL_NUMBER; \
44 v.start = buf; \
45 v.len = sizeof(x) - 1
46
47 #define NUM_INT32_PASS(s, i) \
48 NUM_SETUP(s); \
49 CU_ASSERT(spdk_json_number_to_int32(&v, &i32) == 0); \
50 CU_ASSERT(i32 == i)
51
52 #define NUM_INT32_FAIL(s) \
53 NUM_SETUP(s); \
54 CU_ASSERT(spdk_json_number_to_int32(&v, &i32) != 0)
55
56 static void
57 test_strequal(void)
58 {
59 struct spdk_json_val v;
60
61 v.type = SPDK_JSON_VAL_STRING;
62 v.start = "test";
63 v.len = sizeof("test") - 1;
64 CU_ASSERT(spdk_json_strequal(&v, "test") == true);
65 CU_ASSERT(spdk_json_strequal(&v, "TEST") == false);
66 CU_ASSERT(spdk_json_strequal(&v, "hello") == false);
67 CU_ASSERT(spdk_json_strequal(&v, "t") == false);
68
69 v.type = SPDK_JSON_VAL_NAME;
70 CU_ASSERT(spdk_json_strequal(&v, "test") == true);
71
72 v.type = SPDK_JSON_VAL_NUMBER;
73 CU_ASSERT(spdk_json_strequal(&v, "test") == false);
74
75 v.type = SPDK_JSON_VAL_STRING;
76 v.start = "test\0hello";
77 v.len = sizeof("test\0hello") - 1;
78 CU_ASSERT(spdk_json_strequal(&v, "test") == false);
79 }
80
81 static void
82 test_num_to_int32(void)
83 {
84 struct spdk_json_val v;
85 char buf[100];
86 int32_t i32 = 0;
87
88 NUM_SETUP("1234");
89 CU_ASSERT(spdk_json_number_to_int32(&v, &i32) == 0);
90 CU_ASSERT(i32 == 1234);
91
92
93 NUM_INT32_PASS("0", 0);
94 NUM_INT32_PASS("1234", 1234);
95 NUM_INT32_PASS("-1234", -1234);
96 NUM_INT32_PASS("1234.00000", 1234);
97 NUM_INT32_PASS("1.2e1", 12);
98 NUM_INT32_PASS("12340e-1", 1234);
99 NUM_INT32_PASS("-0", 0);
100
101 NUM_INT32_FAIL("1.2");
102 NUM_INT32_FAIL("1.2E0");
103 NUM_INT32_FAIL("1.234e1");
104 NUM_INT32_FAIL("12341e-1");
105 }
106
107 int main(int argc, char **argv)
108 {
109 CU_pSuite suite = NULL;
110 unsigned int num_failures;
111
112 if (CU_initialize_registry() != CUE_SUCCESS) {
113 return CU_get_error();
114 }
115
116 suite = CU_add_suite("json", NULL, NULL);
117 if (suite == NULL) {
118 CU_cleanup_registry();
119 return CU_get_error();
120 }
121
122 if (
123 CU_add_test(suite, "strequal", test_strequal) == NULL ||
124 CU_add_test(suite, "num_to_int32", test_num_to_int32) == NULL) {
125 CU_cleanup_registry();
126 return CU_get_error();
127 }
128
129 CU_basic_set_mode(CU_BRM_VERBOSE);
130
131 CU_basic_run_tests();
132
133 num_failures = CU_get_number_of_failures();
134 CU_cleanup_registry();
135
136 return num_failures;
137 }