Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions coresdk/src/test/unit_tests/unit_test_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,83 @@ TEST_CASE("json can be created and read", "[json]")

free_json(person);
free_all_json();
}

TEST_CASE("json_set_number can store different number types", "[json_set_number]")
{
json j = create_json();

SECTION("with positive float")
{
float float_test = 3.14f;

json_set_number(j, "float_value", float_test);

REQUIRE(json_has_key(j, "float_value"));
REQUIRE(json_read_number(j, "float_value") == 3.14f);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, some float comparisons use == directly like the above line of code but actually this is risky because of floating-point precision issues. Try to replace with Approx maybe something like REQUIRE(json_read_number(j, "float_value") == Approx(3.14f));. would be great. Same applies for -120.38f, 0.0f, 1.23f, and 4.56f.

}

SECTION("with negative float")
{
float float_test = -120.38f;

json_set_number(j, "float_value", float_test);

REQUIRE(json_has_key(j, "float_value"));
REQUIRE(json_read_number(j, "float_value") == -120.38f);
}

SECTION("Can store zero")
{
float zero_test = 0.0f;

json_set_number(j, "float_value", zero_test);

REQUIRE(json_has_key(j, "float_value"));
REQUIRE(json_read_number(j, "float_value") == 0.0f);
}

SECTION("with positive double")
{
double double_test = 2.718281828;

json_set_number(j, "double_value", double_test);

REQUIRE(json_has_key(j, "double_value"));
REQUIRE(json_read_number(j, "double_value") == Approx(2.718281828));
}

SECTION("with negative double")
{
double double_test = -7.84352;

json_set_number(j, "double_value", double_test);

REQUIRE(json_has_key(j, "double_value"));
REQUIRE(json_read_number(j, "double_value") == Approx(-7.84352));
}

SECTION("can overwrite existing key")
{
json_set_number(j, "float_value", 1.23f);
REQUIRE(json_read_number(j, "float_value") == 1.23f);

json_set_number(j, "float_value", 4.56f);
REQUIRE(json_read_number(j, "float_value") == 4.56f);
}

SECTION("can overwrite across int/float/double")
{
json_set_number(j, "value", 42);
REQUIRE(json_read_number(j, "value") == 42);

json_set_number(j, "value", 3.14f);
REQUIRE(json_read_number(j, "value") == Approx(3.14f));

json_set_number(j, "value", 2.718281828);
REQUIRE(json_read_number(j, "value") == Approx(2.718281828));
}

free_json(j);
free_all_json();
}