From 85b20449d7f54d0754445c71c0594e383350b4e4 Mon Sep 17 00:00:00 2001 From: Broccoli811 Date: Sun, 31 Aug 2025 20:29:38 +1000 Subject: [PATCH] Added new test cases for json_set_number # Description MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The goal was to implement missing test cases for a function called unit_test_json from the json.cpp file to the unit_test_json.cpp file. The added TEST_CASE is named: "json_set_number can store different number types". With the test tag being: "[json_set_number]" ## Type of change - [✓] Added new Test cases ## How Has This Been Tested? I have tested this using float and double data types. CMake operations were used to test if the function could retain positive/negative number types AND I have also tested if we overwrite existing keys/overwrite across data types ## Testing Checklist - [✓] Tested with sktest - [✓] Tested with skunit_tests ## Checklist - [✓] My code follows the style guidelines of this project - [✓] I have performed a self-review of my own code - [✓] I have made corresponding changes to the documentation - [✓] My changes generate no new warnings - [✓] I have requested a review from the teams chat on the Pull Request --- .../src/test/unit_tests/unit_test_json.cpp | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/coresdk/src/test/unit_tests/unit_test_json.cpp b/coresdk/src/test/unit_tests/unit_test_json.cpp index f6bc1b98..871dcf12 100644 --- a/coresdk/src/test/unit_tests/unit_test_json.cpp +++ b/coresdk/src/test/unit_tests/unit_test_json.cpp @@ -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); + } + + 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(); } \ No newline at end of file