Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MCU-LONG opened this issue May 17, 2020 · 1 comment
Open

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

MCU-LONG opened this issue May 17, 2020 · 1 comment

Comments

@MCU-LONG
Copy link

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"
* remove extra "0" to output "1e-09" or "1e 17",which makes testcase PASS */

// 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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants