From 82341966644a83b6fd0f8571067d14036f505f6c Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Tue, 5 Nov 2024 21:56:05 +0100 Subject: [PATCH 1/4] Fix negative zero string conversion --- src/scratch/value_functions_p.h | 4 ++-- test/scratch_classes/value_test.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/scratch/value_functions_p.h b/src/scratch/value_functions_p.h index ca497e99..01958ab5 100644 --- a/src/scratch/value_functions_p.h +++ b/src/scratch/value_functions_p.h @@ -50,7 +50,7 @@ extern "C" return v < 0 && std::isinf(v); } - inline long value_convert_int_str(const char *str, int n, bool *ok) + inline double value_convert_int_str(const char *str, int n, bool *ok) { if (ok) *ok = false; @@ -80,7 +80,7 @@ extern "C" *ok = true; if (isNegative) - return -ret; + return -static_cast(ret); // for negative zero else return ret; } diff --git a/test/scratch_classes/value_test.cpp b/test/scratch_classes/value_test.cpp index 359bc946..04cce470 100644 --- a/test/scratch_classes/value_test.cpp +++ b/test/scratch_classes/value_test.cpp @@ -938,6 +938,15 @@ TEST(ValueTest, ToDouble) v = "-0.15"; ASSERT_EQ(v.toDouble(), -0.15); + v = "0"; + ASSERT_EQ(v.toDouble(), 0.0); + v = "-0"; + ASSERT_EQ(v.toDouble(), -0.0); + v = "0.0"; + ASSERT_EQ(v.toDouble(), 0.0); + v = "-0.0"; + ASSERT_EQ(v.toDouble(), -0.0); + v = "+.15"; ASSERT_EQ(v.toDouble(), 0.15); v = ".15"; @@ -2879,6 +2888,11 @@ TEST(ValueTest, StringToDouble) ASSERT_EQ(value_stringToDouble("0.15"), 0.15); ASSERT_EQ(value_stringToDouble("-0.15"), -0.15); + ASSERT_EQ(value_stringToDouble("0"), 0.0); + ASSERT_EQ(value_stringToDouble("-0"), -0.0); + ASSERT_EQ(value_stringToDouble("0.0"), 0.0); + ASSERT_EQ(value_stringToDouble("-0.0"), -0.0); + ASSERT_EQ(value_stringToDouble("+.15"), 0.15); ASSERT_EQ(value_stringToDouble(".15"), 0.15); ASSERT_EQ(value_stringToDouble("-.15"), -0.15); From 015af2a52ff8830524015e9242e80b5505dc5498 Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Sun, 8 Dec 2024 10:49:33 +0100 Subject: [PATCH 2/4] RandomGenerator: Fix except = start and except = end edge cases --- src/engine/internal/randomgenerator.cpp | 17 ++++++++--------- test/randomgenerator/randomgenerator_test.cpp | 12 ++++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/engine/internal/randomgenerator.cpp b/src/engine/internal/randomgenerator.cpp index e9e7c572..9f441238 100644 --- a/src/engine/internal/randomgenerator.cpp +++ b/src/engine/internal/randomgenerator.cpp @@ -57,13 +57,12 @@ long RandomGenerator::randintExcept(long start, long end, long except) const return end; else return start; - } - - if (randint(0, 1) == 0) { - std::uniform_int_distribution distribution(start, except - 1); - return distribution(*m_generator); - } else { - std::uniform_int_distribution distribution(except + 1, end); - return distribution(*m_generator); - } + } else if (except == start) + return randint(except + 1, end); + else if (except == end) + return randint(start, except - 1); + else if (randint(0, 1) == 0) + return randint(start, except - 1); + else + return randint(except + 1, end); } diff --git a/test/randomgenerator/randomgenerator_test.cpp b/test/randomgenerator/randomgenerator_test.cpp index 06b9dda0..12032e95 100644 --- a/test/randomgenerator/randomgenerator_test.cpp +++ b/test/randomgenerator/randomgenerator_test.cpp @@ -96,4 +96,16 @@ TEST(RandomGeneratorTest, RandIntExcept) num = rng->randintExcept(1, 2, 2); ASSERT_EQ(num, 1); } + + for (int i = 0; i < 25; i++) { + num = rng->randintExcept(1, 5, 1); + ASSERT_GE(num, 2); + ASSERT_LE(num, 5); + } + + for (int i = 0; i < 25; i++) { + num = rng->randintExcept(1, 5, 5); + ASSERT_GE(num, 1); + ASSERT_LE(num, 4); + } } From e9d4bba357335f312e76f680c2eae8b13d3943a5 Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Sun, 8 Dec 2024 12:10:15 +0100 Subject: [PATCH 3/4] fix #601: Do not treat booleans as numbers in List::toString() --- include/scratchcpp/list.h | 2 +- test/scratch_classes/list_test.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/scratchcpp/list.h b/include/scratchcpp/list.h index 8ce30c85..6687e226 100644 --- a/include/scratchcpp/list.h +++ b/include/scratchcpp/list.h @@ -139,7 +139,7 @@ class LIBSCRATCHCPP_EXPORT List : public Entity strings.push_back(std::string()); value_toString(item, &strings.back()); - if (value_isValidNumber(item) && !strings.back().empty()) { + if (value_isValidNumber(item) && !value_isBool(item) && !strings.back().empty()) { double doubleNum = value_toDouble(item); long num = value_toLong(item); diff --git a/test/scratch_classes/list_test.cpp b/test/scratch_classes/list_test.cpp index a6574b5d..927189a6 100644 --- a/test/scratch_classes/list_test.cpp +++ b/test/scratch_classes/list_test.cpp @@ -278,6 +278,20 @@ TEST(ListTest, ToString) list.toString(s); ASSERT_EQ(s, "098"); ASSERT_EQ(list.toString(), "098"); + + list.clear(); + list.append("true"); + list.append("false"); + list.toString(s); + ASSERT_EQ(s, "true false"); + ASSERT_EQ(list.toString(), "true false"); + + list.clear(); + list.append(true); + list.append(false); + list.toString(s); + ASSERT_EQ(s, "true false"); + ASSERT_EQ(list.toString(), "true false"); } TEST(ListTest, Clone) From 70f2c7060fb116c8000f712fba1fc661343d7d1d Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Sun, 8 Dec 2024 12:16:24 +0100 Subject: [PATCH 4/4] Set version to 0.12.1 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 29f589fa..9f5f63aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.14) -project(libscratchcpp VERSION 0.12.0 LANGUAGES C CXX) +project(libscratchcpp VERSION 0.12.1 LANGUAGES C CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_CXX_STANDARD 17)