]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/lib/json/util/json_util_ut.c
4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #include "spdk_cunit.h"
36 #include "json_util.c"
41 #define NUM_SETUP(x) \
42 snprintf(buf, sizeof(buf), "%s", x); \
43 v.type = SPDK_JSON_VAL_NUMBER; \
47 #define NUM_INT32_PASS(s, i) \
49 CU_ASSERT(spdk_json_number_to_int32(&v, &i32) == 0); \
52 #define NUM_INT32_FAIL(s) \
54 CU_ASSERT(spdk_json_number_to_int32(&v, &i32) != 0)
59 struct spdk_json_val v
;
61 v
.type
= SPDK_JSON_VAL_STRING
;
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);
69 v
.type
= SPDK_JSON_VAL_NAME
;
70 CU_ASSERT(spdk_json_strequal(&v
, "test") == true);
72 v
.type
= SPDK_JSON_VAL_NUMBER
;
73 CU_ASSERT(spdk_json_strequal(&v
, "test") == false);
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);
82 test_num_to_int32(void)
84 struct spdk_json_val v
;
89 CU_ASSERT(spdk_json_number_to_int32(&v
, &i32
) == 0);
90 CU_ASSERT(i32
== 1234);
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);
101 NUM_INT32_FAIL("1.2");
102 NUM_INT32_FAIL("1.2E0");
103 NUM_INT32_FAIL("1.234e1");
104 NUM_INT32_FAIL("12341e-1");
107 int main(int argc
, char **argv
)
109 CU_pSuite suite
= NULL
;
110 unsigned int num_failures
;
112 if (CU_initialize_registry() != CUE_SUCCESS
) {
113 return CU_get_error();
116 suite
= CU_add_suite("json", NULL
, NULL
);
118 CU_cleanup_registry();
119 return CU_get_error();
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();
129 CU_basic_set_mode(CU_BRM_VERBOSE
);
131 CU_basic_run_tests();
133 num_failures
= CU_get_number_of_failures();
134 CU_cleanup_registry();