From 1b53756fea5764be931d86d6cbba9e0c7fe79f3b Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Sun, 4 Aug 2024 19:13:38 -0400 Subject: [PATCH] revert changes from yesterday, order was fine. --- CMakeLists.txt | 3 +- include/blt/gp/operations.h | 2 +- tests/evaluation_tests.cpp | 6 +- tests/order_tests.cpp | 115 ++++++++++++++++++++++++++++++++++++ tests/stack_tests.cpp | 48 +++++++-------- 5 files changed, 145 insertions(+), 29 deletions(-) create mode 100644 tests/order_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e15defd..10f81d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.112) +project(blt-gp VERSION 0.0.113) include(CTest) @@ -95,6 +95,7 @@ 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-order tests/order_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/include/blt/gp/operations.h b/include/blt/gp/operations.h index c52451f..b80d9fe 100644 --- a/include/blt/gp/operations.h +++ b/include/blt/gp/operations.h @@ -78,7 +78,7 @@ namespace blt::gp { blt::size_t offset = 0; blt::size_t current_index = 0; - ((offset += (current_index++ < index ? stack_allocator::aligned_size>() : 0)), ...); + ((offset += (current_index++ > index ? stack_allocator::aligned_size>() : 0)), ...); //BLT_INFO("offset %ld for index %ld", offset, index); return offset; } diff --git a/tests/evaluation_tests.cpp b/tests/evaluation_tests.cpp index 78bd155..4d88428 100644 --- a/tests/evaluation_tests.cpp +++ b/tests/evaluation_tests.cpp @@ -91,7 +91,7 @@ blt::gp::operation_t cross_large_type([](const large_18290& input, const float a }); blt::gp::operation_t f_literal([]() { - return 0.0f; + return 555.0f; }); blt::gp::operation_t b_literal([]() { @@ -110,8 +110,8 @@ void basic_tree() tree.get_operations().push_back(make_container(sub.id)); 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(120.0f); tree.get_values().push(50.0f); + tree.get_values().push(120.0f); auto val = tree.get_evaluation_value(nullptr); BLT_TRACE(val); @@ -125,11 +125,11 @@ void large_cross_type_tree() auto& vals = tree.get_values(); ops.push_back(make_container(cross_large_type.id)); - ops.push_back(make_container(large_literal.id)); ops.push_back(make_container(sub.id)); ops.push_back(make_value(type_system.get_type())); ops.push_back(make_value(type_system.get_type())); ops.push_back(make_value(type_system.get_type())); + ops.push_back(make_container(large_literal.id)); vals.push(50.0f); vals.push(120.0f); diff --git a/tests/order_tests.cpp b/tests/order_tests.cpp new file mode 100644 index 0000000..131f675 --- /dev/null +++ b/tests/order_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 +#include + +const blt::u64 SEED = std::random_device()(); +//const blt::u64 SEED = 3495535167; +blt::gp::random_t b_rand {SEED}; +blt::gp::type_provider type_system; + +struct context +{ + blt::gp::gp_program* program; +}; + +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::operation_t basic_sub([](float a, float b, bool choice) { + if (choice) + { + BLT_TRACE("Choice Taken! a: %lf b: %lf", a, b); + return b - a; + }else + { + BLT_TRACE("Choice Not Taken! a: %lf b: %lf", a, b); + return a - b; + } +}, "sub"); + +blt::gp::operation_t basic_lit_f([]() { + return b_rand.choice() ? 5.0f : 10.0f; +}); + +blt::gp::operation_t basic_lit_b([]() { + return false; +}); + +void basic_test() +{ + blt::gp::gp_program program{type_system, SEED}; + + blt::gp::operator_builder builder{type_system}; + + builder.add_operator(basic_sub); + builder.add_operator(basic_lit_f, true); + builder.add_operator(basic_lit_b, true); + + program.set_operations(builder.build()); + + blt::gp::grow_generator_t gen; + blt::gp::generator_arguments args{program, type_system.get_type().id(), 1, 1}; + auto tree = gen.generate(args); + + context ctx{&program}; + auto result = tree.get_evaluation_value(&ctx); + BLT_TRACE(result); + BLT_ASSERT(result == -5.0f || result == 5.0f || result == 0.0f); + tree.print(program, std::cout, true, true); +} + +int main() +{ + BLT_INFO("Starting with seed %ld", SEED); + + type_system.register_type(); + type_system.register_type(); + type_system.register_type(); + + basic_test(); +} \ No newline at end of file diff --git a/tests/stack_tests.cpp b/tests/stack_tests.cpp index d931826..effdf1c 100644 --- a/tests/stack_tests.cpp +++ b/tests/stack_tests.cpp @@ -298,8 +298,8 @@ void test_basic() BLT_INFO("Testing basic with stack"); { blt::gp::stack_allocator stack; - stack.push(10.0f); stack.push(50.0f); + stack.push(10.0f); BLT_TRACE_STREAM << stack.size() << "\n"; basic_2.make_callable()(nullptr, stack, stack); BLT_TRACE_STREAM << stack.size() << "\n"; @@ -312,8 +312,8 @@ void test_basic() { blt::gp::stack_allocator stack; stack.push(std::array{}); - stack.push(10.0f); stack.push(50.0f); + stack.push(10.0f); auto size = stack.size(); BLT_TRACE_STREAM << size << "\n"; BLT_ASSERT(size.blocks > 1 && "Stack doesn't have more than one block!"); @@ -332,10 +332,10 @@ void test_mixed() BLT_INFO("Testing mixed with stack"); { blt::gp::stack_allocator stack; - stack.push(false); - stack.push(true); - stack.push(10.0f); stack.push(50.0f); + stack.push(10.0f); + stack.push(true); + stack.push(false); BLT_TRACE_STREAM << stack.size() << "\n"; basic_mixed_4.make_callable()(nullptr, stack, stack); @@ -349,10 +349,10 @@ void test_mixed() { blt::gp::stack_allocator stack; stack.push(std::array{}); - stack.push(false); - stack.push(true); - stack.push(10.0f); stack.push(50.0f); + stack.push(10.0f); + stack.push(true); + stack.push(false); auto size = stack.size(); BLT_TRACE_STREAM << size << "\n"; BLT_ASSERT(size.blocks > 1 && "Stack doesn't have more than one block!"); @@ -371,9 +371,9 @@ void test_large_256() BLT_INFO("Testing large 256 with stack"); { blt::gp::stack_allocator stack; - stack.push(69.420f); - stack.push(691.0f); stack.push(base_256); + stack.push(691.0f); + stack.push(69.420f); BLT_TRACE_STREAM << stack.size() << "\n"; large_256_basic_3.make_callable()(nullptr, stack, stack); @@ -387,9 +387,9 @@ void test_large_256() { blt::gp::stack_allocator stack; stack.push(std::array{}); - stack.push(69.420f); - stack.push(691.0f); stack.push(base_256); + stack.push(691.0f); + stack.push(69.420f); auto size = stack.size(); BLT_TRACE_STREAM << size << "\n"; BLT_ASSERT(size.blocks > 1 && "Stack doesn't have more than one block!"); @@ -408,9 +408,9 @@ void test_large_4096() BLT_INFO("Testing large 4096 with stack"); { blt::gp::stack_allocator stack; - stack.push(true); - stack.push(33.0f); stack.push(base_4096); + stack.push(33.0f); + stack.push(true); BLT_TRACE_STREAM << stack.size() << "\n"; large_4096_basic_3b.make_callable()(nullptr, stack, stack); BLT_TRACE_STREAM << stack.size() << "\n"; @@ -422,9 +422,9 @@ void test_large_4096() BLT_INFO("Testing large 4096 with stack over boundary"); { blt::gp::stack_allocator stack; - stack.push(true); - stack.push(33.0f); stack.push(base_4096); + stack.push(33.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!"); @@ -442,9 +442,9 @@ void test_large_18290() BLT_INFO("Testing large 18290 with stack"); { blt::gp::stack_allocator stack; - stack.push(true); - stack.push(-2543.0f); 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"; @@ -457,9 +457,9 @@ void test_large_18290() { blt::gp::stack_allocator stack; stack.push(std::array()); - stack.push(true); - stack.push(-2543.0f); 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!"); @@ -474,15 +474,15 @@ void test_large_18290() BLT_INFO("Testing large 18290 with stack over multiple boundaries"); { blt::gp::stack_allocator stack; - stack.push(true); - stack.push(-2543.0f); - stack.push(true); - stack.push(-2543.0f); 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";