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 31static cJSON item[1]; 32 33static 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 46static 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 58#ifdef __CJSON_USE_INT64 59static void assert_is_int64(cJSON *int64_number_item) 60{ 61 assert_is_number(int64_number_item); 62 TEST_ASSERT_BITS_MESSAGE(cJSON_IsInt64, cJSON_IsInt64, int64_number_item->type, "Item should be a int64 integer"); 63} 64 65static void assert_parse_int64_number(const char *string, long long integer, double real) 66{ 67 parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; 68 buffer.content = (const unsigned char*)string; 69 buffer.length = strlen(string) + sizeof(""); 70 71 TEST_ASSERT_TRUE(parse_number(item, &buffer)); 72 TEST_ASSERT_EQUAL_INT64(integer, item->valueint); 73 TEST_ASSERT_EQUAL_DOUBLE(real, item->valuedouble); 74} 75 76static void assert_parse_int64_number_with_type(const char *string, long long integer, double real) 77{ 78 assert_parse_int64_number(string, integer, real); 79 assert_is_int64(item); 80} 81#endif /* __CJSON_USE_INT64 */ 82 83static void parse_number_should_parse_zero(void) 84{ 85 assert_parse_number("0", 0, 0); 86 assert_parse_number("0.0", 0, 0.0); 87 assert_parse_number("-0", 0, -0.0); 88} 89 90static void parse_number_should_parse_negative_integers(void) 91{ 92 assert_parse_number("-1", -1, -1); 93 assert_parse_number("-32768", -32768, -32768.0); 94 assert_parse_number("-2147483648", (int)-2147483648.0, -2147483648.0); 95} 96 97static void parse_number_should_parse_positive_integers(void) 98{ 99 assert_parse_number("1", 1, 1); 100 assert_parse_number("32767", 32767, 32767.0); 101 assert_parse_number("2147483647", (int)2147483647.0, 2147483647.0); 102} 103 104static void parse_number_should_parse_positive_reals(void) 105{ 106 assert_parse_number("0.001", 0, 0.001); 107 assert_parse_number("10e-10", 0, 10e-10); 108 assert_parse_number("10E-10", 0, 10e-10); 109#ifdef __CJSON_USE_INT64 110 assert_parse_int64_number("10e10", 100000000000LL, 10e10); 111 assert_parse_int64_number("123e+127", LLONG_MAX, 123e127); 112#else 113 assert_parse_number("10e10", INT_MAX, 10e10); 114 assert_parse_number("123e+127", INT_MAX, 123e127); 115#endif /* __CJSON_USE_INT64 */ 116 assert_parse_number("123e-128", 0, 123e-128); 117} 118 119static void parse_number_should_parse_negative_reals(void) 120{ 121 assert_parse_number("-0.001", 0, -0.001); 122 assert_parse_number("-10e-10", 0, -10e-10); 123 assert_parse_number("-10E-10", 0, -10e-10); 124#ifdef __CJSON_USE_INT64 125 assert_parse_int64_number("-10e20", LLONG_MIN, -10e20); 126 assert_parse_int64_number("-123e+127", LLONG_MIN, -123e127); 127#else 128 assert_parse_number("-10e20", INT_MIN, -10e20); 129 assert_parse_number("-123e+127", INT_MIN, -123e127); 130#endif /* __CJSON_USE_INT64 */ 131 assert_parse_number("-123e-128", 0, -123e-128); 132} 133 134#ifdef __CJSON_USE_INT64 135static void parse_number_should_parse_int64_numbers(void) 136{ 137 assert_parse_int64_number_with_type("0", 0LL, 0); 138 assert_parse_int64_number_with_type("-1", -1LL, -1); 139 assert_parse_int64_number_with_type("-32768", -32768LL, -32768.0); 140 assert_parse_int64_number_with_type("-2147483648", -2147483648LL, -2147483648.0); 141 assert_parse_int64_number_with_type("2147483648", (long long)INT_MAX + 1, 2147483648.0); 142 assert_parse_int64_number_with_type("-2147483649", -2147483649LL, -2147483649.0); 143 assert_parse_int64_number_with_type("9223372036854775807", LLONG_MAX, 9223372036854775807.0); 144 assert_parse_int64_number_with_type("-9223372036854775808", LLONG_MIN, -9223372036854775808.0); 145} 146#endif /* __CJSON_USE_INT64 */ 147 148int CJSON_CDECL main(void) 149{ 150 /* initialize cJSON item */ 151 memset(item, 0, sizeof(cJSON)); 152 UNITY_BEGIN(); 153 RUN_TEST(parse_number_should_parse_zero); 154 RUN_TEST(parse_number_should_parse_negative_integers); 155 RUN_TEST(parse_number_should_parse_positive_integers); 156 RUN_TEST(parse_number_should_parse_positive_reals); 157 RUN_TEST(parse_number_should_parse_negative_reals); 158#ifdef __CJSON_USE_INT64 159 RUN_TEST(parse_number_should_parse_int64_numbers); 160#endif 161 return UNITY_END(); 162} 163