From 9893e1743e4891d7d7aaa20053a83baaab074cfd Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 23 Jul 2024 21:33:20 -0400 Subject: [PATCH] do tree tests --- CMakeLists.txt | 3 +- tests/evaluation_tests.cpp | 115 +++++++++++++++++++++++++++++++++++++ tests/stack_tests.cpp | 60 ++++++++++++++++++- 3 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 tests/evaluation_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e03b5b..0cbb83a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.107) +project(blt-gp VERSION 0.0.108) include(CTest) @@ -94,6 +94,7 @@ endif () if (${BUILD_GP_TESTS}) blt_add_project(blt-stack tests/stack_tests.cpp test) + blt_add_project(blt-eval tests/evaluation_tests.cpp test) blt_add_project(blt-gp1 tests/gp_test_1.cpp test) blt_add_project(blt-gp2 tests/gp_test_2.cpp test) blt_add_project(blt-gp3 tests/gp_test_3.cpp test) diff --git a/tests/evaluation_tests.cpp b/tests/evaluation_tests.cpp new file mode 100644 index 0000000..32bbedf --- /dev/null +++ b/tests/evaluation_tests.cpp @@ -0,0 +1,115 @@ +/* + * + * Copyright (C) 2024 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include +#include +#include + +const blt::u64 SEED = std::random_device()(); + +struct large_256 +{ + blt::u8 data[256]; +}; + +struct large_2048 +{ + blt::u8 data[2048]; +}; + +// not actually 4096 but will fill the whole page (4096) +struct large_4096 +{ + blt::u8 data[blt::gp::stack_allocator::page_size_no_block()]; +}; + +struct large_6123 +{ + blt::u8 data[6123]; +}; + +struct large_18290 +{ + blt::u8 data[18290]; +}; + +blt::gp::type_provider type_system; +blt::gp::gp_program program{type_system, SEED}; + +blt::gp::op_container_t make_container(blt::gp::operator_id id) +{ + auto& info = program.get_operator_info(id); + return {info.function, type_system.get_type(info.return_type).size(), id, false}; +} + +blt::gp::op_container_t make_value(const blt::gp::type& id) +{ + static blt::gp::detail::callable_t empty([](void*, blt::gp::stack_allocator&, blt::gp::stack_allocator&) {}); + return {empty, id.size(), 0, true}; +} + +blt::gp::operation_t basic_2([](float a, float b) { + return a + b; +}); + +blt::gp::operation_t basic_2t([](float a, bool b) { + return b ? a : 0.0f; +}); + +blt::gp::operation_t f_literal([]() { + return 0.0f; +}); + +blt::gp::operation_t b_literal([]() { + return false; +}); + +void basic_tree() +{ + BLT_INFO("Testing if we can get a basic tree going."); + blt::gp::tree_t tree; + + tree.get_operations().push_back(make_container(2)); + tree.get_operations().push_back(make_value(type_system.get_type())); + tree.get_operations().push_back(make_value(type_system.get_type())); + tree.get_values().push(50.0f); + tree.get_values().push(120.0f); + + auto val = tree.get_evaluation_value(nullptr); + BLT_TRACE(val); + BLT_ASSERT(val == (50 + 120)); +} + +int main() +{ + type_system.register_type(); + type_system.register_type(); + + blt::gp::operator_builder builder{type_system}; + + builder.add_operator(f_literal); // 0 + builder.add_operator(b_literal); // 1 + builder.add_operator(basic_2); // 2 + builder.add_operator(basic_2t); // 3 + + program.set_operations(builder.build()); + + basic_tree(); +} \ No newline at end of file diff --git a/tests/stack_tests.cpp b/tests/stack_tests.cpp index 7b12260..f4a1673 100644 --- a/tests/stack_tests.cpp +++ b/tests/stack_tests.cpp @@ -91,7 +91,7 @@ struct large_2048 // not actually 4096 but will fill the whole page (4096) struct large_4096 { - blt::u8 data[4096 - blt::gp::stack_allocator::page_size_no_block()]; + blt::u8 data[blt::gp::stack_allocator::page_size_no_block()]; }; struct large_6123 @@ -426,6 +426,62 @@ void test_large_4096() } } +void test_large_18290() +{ + BLT_INFO("Testing large 18290 with stack"); + { + blt::gp::stack_allocator stack; + stack.push(base_18290); + stack.push(-2543.0f); + stack.push(true); + BLT_TRACE_STREAM << stack.size() << "\n"; + large_18290_basic_3b.make_callable()(nullptr, stack, stack); + BLT_TRACE_STREAM << stack.size() << "\n"; + auto val = stack.pop(); + RUN_TEST(!compare(val, base_18290), stack, "Large 18290 3 Test Passed", "Large 4096 3 Test Failed. Unexpected value produced '%lf'", val); + BLT_TRACE_STREAM << stack.size() << "\n"; + BLT_ASSERT(stack.empty() && "Stack was not empty after evaluation."); + } + BLT_INFO("Testing large 18290 with stack over boundary"); + { + blt::gp::stack_allocator stack; + stack.push(std::array()); + stack.push(base_18290); + stack.push(-2543.0f); + stack.push(true); + auto size = stack.size(); + BLT_TRACE_STREAM << size << "\n"; + BLT_ASSERT(size.blocks > 1 && "Stack doesn't have more than one block!"); + large_18290_basic_3b.make_callable()(nullptr, stack, stack); + BLT_TRACE_STREAM << stack.size() << "\n"; + auto val = stack.pop(); + stack.pop>(); + RUN_TEST(!compare(val, base_18290), stack, "Large 18290 3 Boundary Test Passed", "Large 4096 3 Test Failed. Unexpected value produced '%lf'", val); + BLT_TRACE_STREAM << stack.size() << "\n"; + BLT_ASSERT(stack.empty() && "Stack was not empty after evaluation over stack boundary"); + } + BLT_INFO("Testing large 18290 with stack over multiple boundaries"); + { + blt::gp::stack_allocator stack; + stack.push(base_18290); + stack.push(-2543.0f); + stack.push(true); + auto size = stack.size(); + BLT_TRACE_STREAM << size << "\n"; + large_18290_basic_3b.make_callable()(nullptr, stack, stack); + BLT_TRACE_STREAM << stack.size() << "\n"; + stack.push(-2543.0f); + stack.push(true); + BLT_TRACE_STREAM << stack.size() << "\n"; + large_18290_basic_3b.make_callable()(nullptr, stack, stack); + BLT_TRACE_STREAM << stack.size() << "\n"; + auto val = stack.pop(); + RUN_TEST(!compare(val, base_18290), stack, "Large 18290 3 Boundary Test Passed", "Large 4096 3 Test Failed. Unexpected value produced '%lf'", val); + BLT_TRACE_STREAM << stack.size() << "\n"; + BLT_ASSERT(stack.empty() && "Stack was not empty after evaluation over multiple stack boundary"); + } +} + void test_operators() { log_box box("-----------------------{Operator Testing}-----------------------", BLT_INFO_STREAM); @@ -436,6 +492,8 @@ void test_operators() test_large_256(); BLT_NEWLINE(); test_large_4096(); + BLT_NEWLINE(); + test_large_18290(); } int main()