DaveGamble / cJSON

Ultralightweight JSON parser in ANSI C
MIT License
10.72k stars 3.21k forks source link

It's a bug on "print_number.c" file! #475

Open MCU-LONG opened 4 years ago

MCU-LONG commented 4 years ago

On "print_number.c" file, add three test cass. They fail to run. My IDE is visual studio 2017.

static void print_number_should_print_positive_reals(void) { assert_print_number("0.123", 0.123); assert_print_number("1e-09", 10e-10); assert_print_number("1000000000000", 10e11); assert_print_number("1.23e+129", 123e+127); assert_print_number("1.23e-126", 123e-128); assert_print_number("3.1415926535897931", 3.1415926535897931);

assert_print_number("1e-009", 1e-009);      // 测试出现错误,1e-009在assert_print_number函数中输入为1e-09
assert_print_number("1e+017", 1e+017);      // 测试出现错误,1e+017在assert_print_number函数中输入为1e+17
assert_print_number("1e-101", 1e-101);      // 错误,按照转换最后的new_buffer为1e-11,即"1e-101"会转换为"1e-11"

}

static void assert_print_number(const char expected, double input) {
/
In MinGW or visual studio(before 2015),the exponten is represented using three digits,like:"1e-009","1e+017"

// have bug, sizeof(new_buffer) should be strlen(new_buffer) for(i = 0;i <sizeof(new_buffer);i++) { if(i >3 && new_buffer[i] =='0') // if input is 1e-101, new_buffer will be "1e-11", it's error { if((new_buffer[i-3] =='e' && new_buffer[i-2] == '-' && new_buffer[i] =='0') ||(new_buffer[i-2] =='e' && new_buffer[i-1] =='+')) { while(new_buffer[i] !='\0') { new_buffer[i] = new_buffer[i+1]; i++; } }
}
}
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, buffer.buffer, "Printed number is not as expected."); }

Alanscut commented 4 years ago

you are right, but it's not a bug of cJSON itself, actually we shouldn't modify the output of the test API in test file, see #327 . However, some exponent(1e09, 1e17) has different outputs in different environments, which will always fail in some environments. I think it is necessary to change a test method.