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
31 static cJSON item[1];
32
assert_is_number(cJSON * number_item)33 static void assert_is_number(cJSON *number_item)
34 {
35 TEST_ASSERT_NOT_NULL_MESSAGE(number_item, "Item is NULL.");
36
37 assert_not_in_list(number_item);
38 assert_has_no_child(number_item);
39 assert_has_type(number_item, cJSON_Number);
40 assert_has_no_reference(number_item);
41 assert_has_no_const_string(number_item);
42 assert_has_no_valuestring(number_item);
43 assert_has_no_string(number_item);
44 }
45
assert_parse_number(const char * string,int integer,double real)46 static void assert_parse_number(const char *string, int integer, double real)
47 {
48 parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
49 buffer.content = (const unsigned char*)string;
50 buffer.length = strlen(string) + sizeof("");
51
52 TEST_ASSERT_TRUE(parse_number(item, &buffer));
53 assert_is_number(item);
54 TEST_ASSERT_EQUAL_INT(integer, item->valueint);
55 TEST_ASSERT_EQUAL_DOUBLE(real, item->valuedouble);
56 }
57
parse_number_should_parse_zero(void)58 static void parse_number_should_parse_zero(void)
59 {
60 assert_parse_number("0", 0, 0);
61 assert_parse_number("0.0", 0, 0.0);
62 assert_parse_number("-0", 0, -0.0);
63 }
64
parse_number_should_parse_negative_integers(void)65 static void parse_number_should_parse_negative_integers(void)
66 {
67 assert_parse_number("-1", -1, -1);
68 assert_parse_number("-32768", -32768, -32768.0);
69 assert_parse_number("-2147483648", (int)-2147483648.0, -2147483648.0);
70 }
71
parse_number_should_parse_positive_integers(void)72 static void parse_number_should_parse_positive_integers(void)
73 {
74 assert_parse_number("1", 1, 1);
75 assert_parse_number("32767", 32767, 32767.0);
76 assert_parse_number("2147483647", (int)2147483647.0, 2147483647.0);
77 }
78
parse_number_should_parse_positive_reals(void)79 static void parse_number_should_parse_positive_reals(void)
80 {
81 assert_parse_number("0.001", 0, 0.001);
82 assert_parse_number("10e-10", 0, 10e-10);
83 assert_parse_number("10E-10", 0, 10e-10);
84 assert_parse_number("10e10", INT_MAX, 10e10);
85 assert_parse_number("123e+127", INT_MAX, 123e127);
86 assert_parse_number("123e-128", 0, 123e-128);
87 }
88
parse_number_should_parse_negative_reals(void)89 static void parse_number_should_parse_negative_reals(void)
90 {
91 assert_parse_number("-0.001", 0, -0.001);
92 assert_parse_number("-10e-10", 0, -10e-10);
93 assert_parse_number("-10E-10", 0, -10e-10);
94 assert_parse_number("-10e20", INT_MIN, -10e20);
95 assert_parse_number("-123e+127", INT_MIN, -123e127);
96 assert_parse_number("-123e-128", 0, -123e-128);
97 }
98
main(void)99 int CJSON_CDECL main(void)
100 {
101 /* initialize cJSON item */
102 memset(item, 0, sizeof(cJSON));
103 UNITY_BEGIN();
104 RUN_TEST(parse_number_should_parse_zero);
105 RUN_TEST(parse_number_should_parse_negative_integers);
106 RUN_TEST(parse_number_should_parse_positive_integers);
107 RUN_TEST(parse_number_should_parse_positive_reals);
108 RUN_TEST(parse_number_should_parse_negative_reals);
109 return UNITY_END();
110 }
111