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 "unity/examples/unity_config.h"
24 #include "unity/src/unity.h"
25 #include "common.h"
26
27 #ifdef __CJSON_USE_INT64
assert_print_int64_number(const char * expected,long long input)28 static void assert_print_int64_number(const char *expected, long long input)
29 {
30 unsigned char printed[1024];
31 cJSON item[1] = {{ NULL, NULL, NULL, cJSON_Number | cJSON_IsInt64, NULL, 0, 0, NULL }};
32 printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
33 buffer.buffer = printed;
34 buffer.length = sizeof(printed);
35 buffer.offset = 0;
36 buffer.noalloc = true;
37 buffer.hooks = global_hooks;
38
39 cJSON_SetInt64NumberValue(item, input);
40 TEST_ASSERT_TRUE_MESSAGE(print_number(item, &buffer), "Failed to print number.");
41
42 TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, buffer.buffer, "Printed number is not as expected.");
43 }
44 #endif /* __CJSON_USE_INT64 */
45
assert_print_number(const char * expected,double input)46 static void assert_print_number(const char *expected, double input)
47 {
48 unsigned char printed[1024];
49 unsigned char new_buffer[26];
50 unsigned int i = 0;
51 cJSON item[1];
52 printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
53 buffer.buffer = printed;
54 buffer.length = sizeof(printed);
55 buffer.offset = 0;
56 buffer.noalloc = true;
57 buffer.hooks = global_hooks;
58 buffer.buffer = new_buffer;
59
60 memset(item, 0, sizeof(item));
61 memset(new_buffer, 0, sizeof(new_buffer));
62 cJSON_SetNumberValue(item, input);
63 TEST_ASSERT_TRUE_MESSAGE(print_number(item, &buffer), "Failed to print number.");
64
65 /* In MinGW or visual studio(before 2015),the exponten is represented using three digits,like:"1e-009","1e+017"
66 * remove extra "0" to output "1e-09" or "1e+17",which makes testcase PASS */
67 for(i = 0;i <sizeof(new_buffer);i++)
68 {
69 if(i >3 && new_buffer[i] =='0')
70 {
71 if((new_buffer[i-3] =='e' && new_buffer[i-2] == '-' && new_buffer[i] =='0') ||(new_buffer[i-2] =='e' && new_buffer[i-1] =='+'))
72 {
73 while(new_buffer[i] !='\0')
74 {
75 new_buffer[i] = new_buffer[i+1];
76 i++;
77 }
78 }
79 }
80 }
81 TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, buffer.buffer, "Printed number is not as expected.");
82 }
83
print_number_should_print_zero(void)84 static void print_number_should_print_zero(void)
85 {
86 assert_print_number("0", 0);
87 }
88
print_number_should_print_negative_integers(void)89 static void print_number_should_print_negative_integers(void)
90 {
91 assert_print_number("-1", -1.0);
92 assert_print_number("-32768", -32768.0);
93 assert_print_number("-2147483648", -2147483648.0);
94 #ifdef __CJSON_USE_INT64
95 assert_print_int64_number("-1", -1LL);
96 assert_print_int64_number("-32768", -32768LL);
97 assert_print_int64_number("-2147483647", -2147483647LL);
98 assert_print_int64_number("-9223372036854775808", LLONG_MIN);
99 #endif /* __CJSON_USE_INT64 */
100 }
101
print_number_should_print_positive_integers(void)102 static void print_number_should_print_positive_integers(void)
103 {
104 assert_print_number("1", 1.0);
105 assert_print_number("32767", 32767.0);
106 assert_print_number("2147483647", 2147483647.0);
107 #ifdef __CJSON_USE_INT64
108 assert_print_int64_number("1", 1LL);
109 assert_print_int64_number("32767", 32767LL);
110 assert_print_int64_number("2147483647", 2147483647LL);
111 assert_print_int64_number("9223372036854775807", LLONG_MAX);
112 #endif /* __CJSON_USE_INT64 */
113 }
114
print_number_should_print_positive_reals(void)115 static void print_number_should_print_positive_reals(void)
116 {
117 assert_print_number("0.123", 0.123);
118 assert_print_number("1e-09", 10e-10);
119 assert_print_number("1000000000000", 10e11);
120 assert_print_number("1.23e+129", 123e+127);
121 assert_print_number("1.23e-126", 123e-128);
122 assert_print_number("3.1415926535897931", 3.1415926535897931);
123 }
124
print_number_should_print_negative_reals(void)125 static void print_number_should_print_negative_reals(void)
126 {
127 assert_print_number("-0.0123", -0.0123);
128 assert_print_number("-1e-09", -10e-10);
129 assert_print_number("-1e+21", -10e20);
130 assert_print_number("-1.23e+129", -123e+127);
131 assert_print_number("-1.23e-126", -123e-128);
132 }
133
print_number_should_print_non_number(void)134 static void print_number_should_print_non_number(void)
135 {
136 TEST_IGNORE();
137 /* FIXME: Cannot test this easily in C89! */
138 /* assert_print_number("null", NaN); */
139 /* assert_print_number("null", INFTY); */
140 /* assert_print_number("null", -INFTY); */
141 }
142
main(void)143 int CJSON_CDECL main(void)
144 {
145 /* initialize cJSON item */
146 UNITY_BEGIN();
147
148 RUN_TEST(print_number_should_print_zero);
149 RUN_TEST(print_number_should_print_negative_integers);
150 RUN_TEST(print_number_should_print_positive_integers);
151 RUN_TEST(print_number_should_print_positive_reals);
152 RUN_TEST(print_number_should_print_negative_reals);
153 RUN_TEST(print_number_should_print_non_number);
154
155 return UNITY_END();
156 }
157