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