1 /*
2 Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "unity/examples/unity_config.h"
28 #include "unity/src/unity.h"
29 #include "common.h"
30
assert_is_int64(cJSON * int64_number_item)31 static void assert_is_int64(cJSON *int64_number_item)
32 {
33 assert_has_type(int64_number_item, cJSON_Number);
34 TEST_ASSERT_BITS_MESSAGE(cJSON_IsInt64, cJSON_IsInt64, int64_number_item->type, "Item should be a int64 integer.");
35 }
36
cjson_get_object_item_should_get_object_items_with_int64(void)37 static void cjson_get_object_item_should_get_object_items_with_int64(void)
38 {
39 cJSON *item = NULL;
40 cJSON *found = NULL;
41
42 item = cJSON_Parse("{\"one\":1, \"Two\":2, \"tHree\":3}");
43
44 found = cJSON_GetObjectItem(NULL, "test");
45 TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL pointer.");
46
47 found = cJSON_GetObjectItem(item, NULL);
48 TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL string.");
49
50 found = cJSON_GetObjectItem(item, "one");
51 TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
52 TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 1);
53 assert_is_int64(found);
54
55 found = cJSON_GetObjectItem(item, "tWo");
56 TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
57 TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 2);
58 assert_is_int64(found);
59
60 found = cJSON_GetObjectItem(item, "three");
61 TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find item.");
62 TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 3);
63 assert_is_int64(found);
64
65 found = cJSON_GetObjectItem(item, "four");
66 TEST_ASSERT_NULL_MESSAGE(found, "Should not find something that isn't there.");
67
68 cJSON_Delete(item);
69 }
70
cjson_get_object_item_case_sensitive_should_get_object_items_with_int64(void)71 static void cjson_get_object_item_case_sensitive_should_get_object_items_with_int64(void)
72 {
73 cJSON *item = NULL;
74 cJSON *found = NULL;
75
76 item = cJSON_Parse("{\"one\":1, \"Two\":2, \"tHree\":3}");
77
78 found = cJSON_GetObjectItemCaseSensitive(NULL, "test");
79 TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL pointer.");
80
81 found = cJSON_GetObjectItemCaseSensitive(item, NULL);
82 TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL string.");
83
84 found = cJSON_GetObjectItemCaseSensitive(item, "one");
85 TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
86 TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 1);
87 assert_is_int64(found);
88
89 found = cJSON_GetObjectItemCaseSensitive(item, "Two");
90 TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
91 TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 2);
92 assert_is_int64(found);
93
94 found = cJSON_GetObjectItemCaseSensitive(item, "tHree");
95 TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find item.");
96 TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 3);
97 assert_is_int64(found);
98
99 found = cJSON_GetObjectItemCaseSensitive(item, "One");
100 TEST_ASSERT_NULL_MESSAGE(found, "Should not find something that isn't there.");
101
102 cJSON_Delete(item);
103 }
104
cjson_set_number_value_should_set_numbers_with_int64(void)105 static void cjson_set_number_value_should_set_numbers_with_int64(void)
106 {
107 cJSON number[1] = {{NULL, NULL, NULL, cJSON_Number | cJSON_IsInt64, NULL, 0, 0, NULL}};
108
109 cJSON_SetInt64NumberValue(number, 1LL);
110 TEST_ASSERT_EQUAL_INT64(1LL, number->valueint);
111 TEST_ASSERT_EQUAL_DOUBLE(1.0, number->valuedouble);
112
113 cJSON_SetInt64NumberValue(number, -1LL);
114 TEST_ASSERT_EQUAL_INT64(-1LL, number->valueint);
115 TEST_ASSERT_EQUAL_DOUBLE(-1.0, number->valuedouble);
116
117 cJSON_SetInt64NumberValue(number, 0LL);
118 TEST_ASSERT_EQUAL_INT64(0LL, number->valueint);
119 TEST_ASSERT_EQUAL_DOUBLE(0.0, number->valuedouble);
120
121 cJSON_SetInt64NumberValue(number, LLONG_MAX);
122 TEST_ASSERT_EQUAL_INT64(LLONG_MAX, number->valueint);
123 TEST_ASSERT_EQUAL_DOUBLE((double)LLONG_MAX, number->valuedouble);
124
125 cJSON_SetInt64NumberValue(number, LLONG_MIN);
126 TEST_ASSERT_EQUAL_INT64(LLONG_MIN, number->valueint);
127 TEST_ASSERT_EQUAL_DOUBLE((double)LLONG_MIN, number->valuedouble);
128 }
129
typecheck_functions_should_check_type_with_int64(void)130 static void typecheck_functions_should_check_type_with_int64(void)
131 {
132 cJSON item[1];
133 item->type = cJSON_Number;
134 TEST_ASSERT_FALSE(cJSON_IsInt64Number(item));
135
136 item->type = cJSON_IsInt64;
137 TEST_ASSERT_FALSE(cJSON_IsInt64Number(item));
138
139 item->type = cJSON_Number | cJSON_IsInt64;
140 TEST_ASSERT_TRUE(cJSON_IsInt64Number(item));
141
142 item->type = cJSON_False;
143 TEST_ASSERT_FALSE(cJSON_IsInt64Number(item));
144 }
145
cjson_functions_should_not_crash_with_null_pointers_with_int64(void)146 static void cjson_functions_should_not_crash_with_null_pointers_with_int64(void)
147 {
148 cJSON *item = cJSON_CreateString("item");
149
150 TEST_ASSERT_FALSE(cJSON_IsInt64Number(NULL));
151 cJSON_AddInt64NumberToObject(NULL, "item", 0LL);
152 cJSON_AddInt64NumberToObject(item, NULL, 0LL);
153 cJSON_AddInt64NumberToObject(NULL, NULL, 0LL);
154 TEST_ASSERT_NULL(cJSON_GetInt64NumberValue(NULL));
155 TEST_ASSERT_EQUAL_INT64(0LL, cJSON_SetInt64NumberValue(NULL, 0LL));
156
157 cJSON_Delete(item);
158 }
159
cjson_get_number_value_should_get_a_number_with_int64(void)160 static void cjson_get_number_value_should_get_a_number_with_int64(void)
161 {
162 cJSON *string = cJSON_CreateString("test");
163 cJSON *number = cJSON_CreateInt64Number(1LL);
164
165 TEST_ASSERT_EQUAL_INT64(*cJSON_GetInt64NumberValue(number), number->valueint);
166 TEST_ASSERT_NULL(cJSON_GetInt64NumberValue(string));
167 TEST_ASSERT_NULL(cJSON_GetInt64NumberValue(NULL));
168
169 cJSON_Delete(number);
170 cJSON_Delete(string);
171 }
172
main(void)173 int CJSON_CDECL main(void)
174 {
175 UNITY_BEGIN();
176
177 RUN_TEST(cjson_get_object_item_should_get_object_items_with_int64);
178 RUN_TEST(cjson_get_object_item_case_sensitive_should_get_object_items_with_int64);
179 RUN_TEST(cjson_set_number_value_should_set_numbers_with_int64);
180 RUN_TEST(typecheck_functions_should_check_type_with_int64);
181 RUN_TEST(cjson_functions_should_not_crash_with_null_pointers_with_int64);
182 RUN_TEST(cjson_get_number_value_should_get_a_number_with_int64);
183
184 return UNITY_END();
185 }
186