Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/cJSON/blob/master/tests/parse_number.c # cJSON [Ported from github.com/DaveGamble/cJSON] Fix: heap-use-after-free in merge_patch when patch is subtree of target (#1065) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `cJSON / tests / parse_number.c` 130 lines ยท 4673 bytes Raw Blame History 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 24 #include 25 #include 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 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 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 buffer.hooks = global_hooks; 52 53 TEST_ASSERT_TRUE(parse_number(item, &buffer)); 54 assert_is_number(item); 55 TEST_ASSERT_EQUAL_INT(integer, item->valueint); 56 TEST_ASSERT_EQUAL_DOUBLE(real, item->valuedouble); 57 } 58 59 static void assert_parse_big_number(const char *string) 60 { 61 parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; 62 buffer.content = (const unsigned char*)string; 63 buffer.length = strlen(string) + sizeof(""); 64 buffer.hooks = global_hooks; 65 66 TEST_ASSERT_TRUE(parse_number(item, &buffer)); 67 assert_is_number(item); 68 } 69 70 static void parse_number_should_parse_zero(void) 71 { 72 assert_parse_number("0", 0, 0); 73 assert_parse_number("0.0", 0, 0.0); 74 assert_parse_number("-0", 0, -0.0); 75 } 76 77 static void parse_number_should_parse_negative_integers(void) 78 { 79 assert_parse_number("-1", -1, -1); 80 assert_parse_number("-32768", -32768, -32768.0); 81 assert_parse_number("-`2147483648`", (int)-`2147483648`.0, -`2147483648`.0); 82 } 83 84 static void parse_number_should_parse_positive_integers(void) 85 { 86 assert_parse_number("1", 1, 1); 87 assert_parse_number("32767", 32767, 32767.0); 88 assert_parse_number("`2147483647`", (int)`2147483647`.0, `2147483647`.0); 89 } 90 91 static void parse_number_should_parse_positive_reals(void) 92 { 93 assert_parse_number("0.001", 0, 0.001); 94 assert_parse_number("10e-10", 0, 10e-10); 95 assert_parse_number("10E-10", 0, 10e-10); 96 assert_parse_number("10e10", INT_MAX, 10e10); 97 assert_parse_number("123e+127", INT_MAX, `123e127`); 98 assert_parse_number("123e-128", 0, 123e-128); 99 } 100 101 static void parse_number_should_parse_negative_reals(void) 102 { 103 assert_parse_number("-0.001", 0, -0.001); 104 assert_parse_number("-10e-10", 0, -10e-10); 105 assert_parse_number("-10E-10", 0, -10e-10); 106 assert_parse_number("-10e20", INT_MIN, -10e20); 107 assert_parse_number("-123e+127", INT_MIN, -`123e127`); 108 assert_parse_number("-123e-128", 0, -123e-128); 109 } 110 111 static void parse_number_should_parse_big_numbers(void) 112 { 113 assert_parse_big_number("`9999999999999999999999999999999999999999999999912345678901234567`"); 114 assert_parse_big_number("9999999999999999999999999999999999999999999999912345678901234567E10"); 115 assert_parse_big_number("`999999999999999999999999999999999999999999999991234567890`.`1234567`"); 116 } 117 118 int CJSON_CDECL main(void) 119 { 120 /* initialize cJSON item */ 121 memset(item, 0, sizeof(cJSON)); 122 UNITY_BEGIN(); 123 RUN_TEST(parse_number_should_parse_zero); 124 RUN_TEST(parse_number_should_parse_negative_integers); 125 RUN_TEST(parse_number_should_parse_positive_integers); 126 RUN_TEST(parse_number_should_parse_positive_reals); 127 RUN_TEST(parse_number_should_parse_negative_reals); 128 RUN_TEST(parse_number_should_parse_big_numbers); 129 return UNITY_END(); 130 }