Tester Stable
This commit is contained in:
parent
f25d3cc8da
commit
2fba0b1d53
17 changed files with 769 additions and 113 deletions
1
src/Renderer.cpp
Normal file
1
src/Renderer.cpp
Normal file
|
@ -0,0 +1 @@
|
|||
#include "Renderer.h"
|
7
src/Renderer.h
Normal file
7
src/Renderer.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
class Renderer
|
||||
{
|
||||
public:
|
||||
|
||||
};
|
42
src/SortingTester.cpp
Normal file
42
src/SortingTester.cpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
#include "SortingTester.h"
|
||||
|
||||
|
||||
SortingTester::SortingTester() : list(10)
|
||||
{
|
||||
lua.lua.open_libraries(sol::lib::coroutine, sol::lib::table);
|
||||
lua.lua.new_usertype<SortingTester>("List",
|
||||
"size", &SortingTester::list_size,
|
||||
"swap", &SortingTester::list_swap,
|
||||
"greater", &SortingTester::list_greater,
|
||||
"less", &SortingTester::list_less,
|
||||
"equal", &SortingTester::list_equal
|
||||
);
|
||||
|
||||
lua.lua["list"] = this;
|
||||
}
|
||||
|
||||
void SortingTester::load(const std::string& script)
|
||||
{
|
||||
if(lua.load(script) != safesol::LuaResultType::SUCCESS)
|
||||
{
|
||||
std::cerr << "Error loading file" << std::endl;
|
||||
std::terminate();
|
||||
}
|
||||
}
|
||||
|
||||
#include "internal_script.hpp"
|
||||
|
||||
void SortingTester::load_internal()
|
||||
{
|
||||
if (lua.script(std_intern_sorting_script) != safesol::LuaResultType::SUCCESS)
|
||||
{
|
||||
std::cerr << "Error loading internal script" << std::endl;
|
||||
std::terminate();
|
||||
}
|
||||
}
|
||||
|
||||
void SortingTester::populate(const populate_function& f)
|
||||
{
|
||||
for (size_t i = 0; i < list.size(); i++)
|
||||
f(i, list[i]);
|
||||
}
|
152
src/SortingTester.h
Normal file
152
src/SortingTester.h
Normal file
|
@ -0,0 +1,152 @@
|
|||
#pragma once
|
||||
#define SVA_LIST_CALLBACKS
|
||||
#include <std.hpp>
|
||||
|
||||
#include <functional>
|
||||
#include "lua/safesol.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
class SortingTester
|
||||
{
|
||||
List<u64> list;
|
||||
|
||||
safesol lua;
|
||||
|
||||
Timer timer = Timer(Timer::State::paused);
|
||||
|
||||
using populate_function = std::function<void(size_t, u64&)>;
|
||||
|
||||
template<typename ...Args>
|
||||
sol::protected_function_result run_function(const sol::protected_function& function, Args&&... args)
|
||||
{
|
||||
timer.set_state(Timer::State::running);
|
||||
sol::protected_function_result result = function(std::forward<Args>(args)...);
|
||||
timer.set_state(Timer::State::paused);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
bool list_greater(size_t index1, size_t index2)
|
||||
{
|
||||
if (active_result)
|
||||
++active_result->count_greater;
|
||||
return list[index1] > list[index2];
|
||||
}
|
||||
|
||||
bool list_less(size_t index1, size_t index2)
|
||||
{
|
||||
if (active_result)
|
||||
++active_result->count_less;
|
||||
return list[index1] < list[index2];
|
||||
}
|
||||
|
||||
bool list_equal(size_t index1, size_t index2)
|
||||
{
|
||||
if (active_result)
|
||||
++active_result->count_equal;
|
||||
return list[index1] == list[index2];
|
||||
}
|
||||
|
||||
void list_swap(size_t index1, size_t index2)
|
||||
{
|
||||
if (active_result)
|
||||
++active_result->count_swaps;
|
||||
// list[index1] ^= list[index2];
|
||||
// list[index2] ^= list[index1];
|
||||
// list[index1] ^= list[index2];
|
||||
auto tmp = list[index1];
|
||||
list[index1] = list[index2];
|
||||
list[index2] = tmp;
|
||||
}
|
||||
|
||||
size_t list_size() const
|
||||
{
|
||||
return list.size();
|
||||
}
|
||||
|
||||
bool is_sorted()
|
||||
{
|
||||
if (list.size() <= 1)
|
||||
return true;
|
||||
for (size_t i = 1; i < list.size(); ++i)
|
||||
{
|
||||
if (list[i - 1] > list[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public:
|
||||
struct run_result
|
||||
{
|
||||
timer_result timer;
|
||||
// counts
|
||||
u64 count_swaps = 0;
|
||||
u64 count_greater = 0;
|
||||
u64 count_less = 0;
|
||||
u64 count_equal = 0;
|
||||
// counts for comparisons
|
||||
u64 count_comparisons = 0;
|
||||
};
|
||||
private:
|
||||
run_result* active_result = nullptr;
|
||||
|
||||
public:
|
||||
|
||||
SortingTester();
|
||||
|
||||
void load(const std::string& script);
|
||||
void load_internal();
|
||||
|
||||
template<typename ...Args>
|
||||
run_result run(const std::string& function_name, Args&&... args)
|
||||
{
|
||||
run_result result;
|
||||
timer.result.emplace(&result.timer);
|
||||
timer.reset(Timer::State::paused);
|
||||
active_result = &result;
|
||||
|
||||
// Create a bound member function using std::bind
|
||||
auto bound_run_function = std::bind(&SortingTester::run_function<Args...>,
|
||||
this,
|
||||
std::placeholders::_1,
|
||||
std::forward<Args>(args)...);
|
||||
|
||||
safesol::LuaResultType runstate = lua.run_on_caller(function_name, bound_run_function);
|
||||
|
||||
if (runstate != safesol::LuaResultType::SUCCESS)
|
||||
{
|
||||
std::cerr << "Error running function \"" << function_name << "\": " << static_cast<u16>(runstate) << std::endl;
|
||||
exit(static_cast<int>(runstate));
|
||||
}
|
||||
|
||||
if (!is_sorted())
|
||||
{
|
||||
std::cerr << "[ERROR] The algorithm \"" << function_name << "\" did not sort the list" << std::endl;
|
||||
std::cerr << "[List] [ ";
|
||||
for (size_t i = 0; i < list.size(); i++)
|
||||
{
|
||||
if(i == list.size()-1)
|
||||
{
|
||||
std::cerr << list[i] << " ]";
|
||||
continue;
|
||||
}
|
||||
std::cerr << list[i] << ", ";
|
||||
}
|
||||
std::flush(std::cerr);
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
result.count_comparisons = result.count_equal + result.count_greater + result.count_less;
|
||||
|
||||
active_result = nullptr;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void populate(const populate_function& f);
|
||||
};
|
|
@ -1,13 +0,0 @@
|
|||
#include "View.h"
|
||||
#include <raylibs/raygui.h>
|
||||
|
||||
void View::draw()
|
||||
{
|
||||
BeginDrawing();
|
||||
ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
void View::update()
|
||||
{
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
#pragma once
|
||||
#include "raylib.h"
|
||||
#include <types.hpp>
|
||||
|
||||
class View
|
||||
{
|
||||
friend class ViewManager;
|
||||
public:
|
||||
View(rectu rect) : m_rect(rect) {}
|
||||
virtual ~View() = default;
|
||||
virtual void draw();
|
||||
virtual void update();
|
||||
virtual void onMouseDown(vec2d mousePos) {}
|
||||
virtual void onMouseUp(vec2d mousePos) {}
|
||||
virtual void onMouseMove(vec2d mousePos) {}
|
||||
virtual void onKeyDown(int key) {}
|
||||
virtual void onKeyUp(int key) {}
|
||||
virtual void onKeyPress(int key) {}
|
||||
virtual void onFocus() {}
|
||||
virtual void onBlur() {}
|
||||
|
||||
virtual void onResize(vec2d factor)
|
||||
{
|
||||
m_rect.width = static_cast<rectu::type>(static_cast<float>(m_rect.width) * factor.x);
|
||||
m_rect.height = static_cast<rectu::type>(static_cast<float>(m_rect.height) * factor.y);
|
||||
}
|
||||
|
||||
|
||||
|
||||
vec2u getPosition() const { return {m_rect.x, m_rect.y}; }
|
||||
vec2u getSize() const { return {m_rect.width, m_rect.height}; }
|
||||
const rectu& getRect() const { return m_rect; }
|
||||
void setPosition(vec2u pos) { m_rect.x = pos.x; m_rect.y = pos.y; }
|
||||
void setSize(vec2u size) { m_rect.width = size.x; m_rect.height = size.y; }
|
||||
void setRect(rectu &rect) { m_rect = rect; }
|
||||
virtual void Resize(float multiplier)
|
||||
{
|
||||
m_rect.width = static_cast<rectu::type>(static_cast<float>(m_rect.width) * multiplier);
|
||||
m_rect.height = static_cast<rectu::type>(static_cast<float>(m_rect.height) * multiplier);
|
||||
}
|
||||
private:
|
||||
rectu m_rect;
|
||||
};
|
|
@ -1,21 +0,0 @@
|
|||
#include "ViewManager.h"
|
||||
|
||||
ViewManager::ViewManager()
|
||||
{
|
||||
m_viewportSize = { GetScreenWidth(), GetScreenHeight() };
|
||||
m_mainView = new View({ 0, 0, m_viewportSize.x, m_viewportSize.y });
|
||||
}
|
||||
|
||||
void ViewManager::update()
|
||||
{
|
||||
if (IsWindowResized())
|
||||
{
|
||||
m_viewportSize = { GetScreenWidth(), GetScreenHeight() };
|
||||
m_mainView->onResize({ static_cast<float>(m_viewportSize.x), static_cast<float>(m_viewportSize.y) });
|
||||
}
|
||||
m_mainView->update();
|
||||
}
|
||||
|
||||
void ViewManager::draw()
|
||||
{
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
#pragma once
|
||||
#include "View.h"
|
||||
#include <vector>
|
||||
#include <types.hpp>
|
||||
|
||||
class ViewManager
|
||||
{
|
||||
vec2u m_viewportSize;
|
||||
public:
|
||||
ViewManager();
|
||||
~ViewManager();
|
||||
void update();
|
||||
void draw();
|
||||
private:
|
||||
View* m_mainView;
|
||||
};
|
27
src/internal_script.hpp
Normal file
27
src/internal_script.hpp
Normal file
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
inline const char* std_intern_sorting_script = R"(
|
||||
-- bubble sort
|
||||
function bubble_sort()
|
||||
isSorted = false
|
||||
while isSorted == false do
|
||||
movedElements = 0
|
||||
for x=1, list:size(), 1 do
|
||||
if list:greater(x, x+1) then
|
||||
movedElements = movedElements + 1
|
||||
list:swap(x, x+1)
|
||||
end
|
||||
end
|
||||
if movedElements == 0 then
|
||||
isSorted = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function do_nothing()
|
||||
end
|
||||
|
||||
-- other functions
|
||||
|
||||
|
||||
)";
|
61
src/lua/safesol.cpp
Normal file
61
src/lua/safesol.cpp
Normal file
|
@ -0,0 +1,61 @@
|
|||
#include "safesol.h"
|
||||
|
||||
safesol::LuaResultType safesol::script(const std::string& script)
|
||||
{
|
||||
sol::load_result result = lua.load(script);
|
||||
if (!result.valid()) {
|
||||
sol::error err = result;
|
||||
std::cout << "Error loading script: " << err.what() << std::endl;
|
||||
return LuaResultType::LOAD_ERROR;
|
||||
}
|
||||
|
||||
sol::protected_function_result script_result = result();
|
||||
|
||||
if (!script_result.valid()) {
|
||||
sol::error err = script_result;
|
||||
std::cout << "Error running script: " << err.what() << std::endl;
|
||||
return LuaResultType::SCRIPT_ERROR;
|
||||
}
|
||||
|
||||
return LuaResultType::SUCCESS;
|
||||
}
|
||||
|
||||
safesol::LuaResultType safesol::script(const char* script)
|
||||
{
|
||||
sol::load_result result = lua.load(script);
|
||||
if (!result.valid()) {
|
||||
sol::error err = result;
|
||||
std::cout << "Error loading script: " << err.what() << std::endl;
|
||||
return LuaResultType::LOAD_ERROR;
|
||||
}
|
||||
|
||||
sol::protected_function_result script_result = result();
|
||||
|
||||
if (!script_result.valid()) {
|
||||
sol::error err = script_result;
|
||||
std::cout << "Error running script: " << err.what() << std::endl;
|
||||
return LuaResultType::SCRIPT_ERROR;
|
||||
}
|
||||
|
||||
return LuaResultType::SUCCESS;
|
||||
}
|
||||
|
||||
safesol::LuaResultType safesol::load(const std::string& file)
|
||||
{
|
||||
sol::load_result result = lua.load_file(file);
|
||||
if (!result.valid()) {
|
||||
sol::error err = result;
|
||||
std::cout << "Error loading file: " << err.what() << std::endl;
|
||||
return LuaResultType::LOAD_ERROR;
|
||||
}
|
||||
|
||||
sol::protected_function_result script_result = result();
|
||||
|
||||
if (!script_result.valid()) {
|
||||
sol::error err = script_result;
|
||||
std::cout << "Error running script: " << err.what() << std::endl;
|
||||
return LuaResultType::SCRIPT_ERROR;
|
||||
}
|
||||
|
||||
return LuaResultType::SUCCESS;
|
||||
}
|
55
src/lua/safesol.h
Normal file
55
src/lua/safesol.h
Normal file
|
@ -0,0 +1,55 @@
|
|||
#pragma once
|
||||
#include <std.hpp>
|
||||
#include <sol/sol.hpp>
|
||||
|
||||
class safesol
|
||||
{
|
||||
public:
|
||||
enum class LuaResultType : u8
|
||||
{
|
||||
SUCCESS = 0,
|
||||
LOAD_ERROR = 1,
|
||||
SCRIPT_ERROR,
|
||||
RUN_ERROR,
|
||||
UNKNOWN_ERROR
|
||||
};
|
||||
|
||||
sol::state lua;
|
||||
|
||||
|
||||
LuaResultType script(const std::string& script);
|
||||
LuaResultType script(const char* script);
|
||||
|
||||
LuaResultType load(const std::string& file);
|
||||
|
||||
template<typename ...Args>
|
||||
sol::protected_function_result default_caller(const sol::protected_function& function, Args&&... args)
|
||||
{
|
||||
return function(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename ...Args>
|
||||
LuaResultType run(const std::string& function_name, Args&&... args)
|
||||
{
|
||||
return run_on_caller(function_name, default_caller, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename ...Args, typename Caller = std::function<sol::protected_function_result(const sol::protected_function&, Args&&...)>>
|
||||
LuaResultType run_on_caller(const std::string& function_name, const Caller& caller, Args&&... args)
|
||||
{
|
||||
auto function = lua[function_name];
|
||||
if (!function.valid()) {
|
||||
std::cout << "Error: function " << function_name << " not found" << std::endl;
|
||||
return LuaResultType::RUN_ERROR;
|
||||
}
|
||||
|
||||
auto result = caller(function, std::forward<Args>(args)...);
|
||||
if (!result.valid()) {
|
||||
sol::error err = result;
|
||||
std::cout << "Error running function " << function_name << ": " << err.what() << std::endl;
|
||||
return LuaResultType::RUN_ERROR;
|
||||
}
|
||||
|
||||
return LuaResultType::SUCCESS;
|
||||
}
|
||||
};
|
80
src/main.cpp
80
src/main.cpp
|
@ -6,8 +6,14 @@
|
|||
|
||||
#include "gui/drawing_helper.hpp"
|
||||
|
||||
#define RESOURCES_PATH "G:\\School\\Belegarbeit\\sortiva\\res\\"
|
||||
#define RESOURCES_PATH "G:/School/Belegarbeit/sortiva/res/"
|
||||
|
||||
#define SETTINGS_PATH "./.config.cfg"
|
||||
|
||||
#define NO_GUI
|
||||
|
||||
|
||||
#ifndef NO_GUI
|
||||
|
||||
int screenWidth = 1280;
|
||||
int screenHeight = 720;
|
||||
|
@ -23,13 +29,77 @@ GAME_STATES gameState = SVA_STATE_TITLE;
|
|||
void UpdateDrawFrame(); // Update and Draw one frame
|
||||
|
||||
#include "gui/themes/dark.h"
|
||||
#include <filesystem>
|
||||
#include "gui/Views/ViewManager.h"
|
||||
#endif
|
||||
#include <filesystem>
|
||||
|
||||
|
||||
#ifdef NO_GUI
|
||||
#include "SortingTester.h"
|
||||
#endif
|
||||
|
||||
|
||||
#include <random>
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<u64> dis(0, 10);
|
||||
|
||||
void populate(size_t index, u64& value)
|
||||
{
|
||||
value = dis(gen);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
sva_console_init();
|
||||
|
||||
#ifdef NO_GUI
|
||||
|
||||
std::cout << "Sortiva - Sorting Algorithm Visualizer" << std::endl;
|
||||
std::cout << "No GUI mode" << std::endl;
|
||||
|
||||
SortingTester tester;
|
||||
tester.populate( populate );
|
||||
|
||||
tester.load_internal();
|
||||
|
||||
SortingTester::run_result result = tester.run("bubble_sort");
|
||||
|
||||
std::cout << "Time: " << result.timer.seconds << " seconds" << std::endl;
|
||||
std::cout << "Difftime: " << result.timer.difftime << std::endl;
|
||||
|
||||
|
||||
std::cout << "Count swaps: " << result.count_swaps << std::endl;
|
||||
std::cout << "Count comparisons: " << result.count_comparisons << std::endl;
|
||||
std::cout << "\tCount greater: " << result.count_greater << std::endl;
|
||||
std::cout << "\tCount less: " << result.count_less << std::endl;
|
||||
std::cout << "\tCount equal: " << result.count_equal << std::endl;
|
||||
|
||||
std::cout << "\n\n";
|
||||
|
||||
|
||||
result = tester.run("do_nothing");
|
||||
|
||||
std::cout << "Time: " << result.timer.seconds << " seconds" << std::endl;
|
||||
std::cout << "Difftime: " << result.timer.difftime << std::endl;
|
||||
|
||||
|
||||
std::cout << "Count swaps: " << result.count_swaps << std::endl;
|
||||
std::cout << "Count comparisons: " << result.count_comparisons << std::endl;
|
||||
std::cout << "\tCount greater: " << result.count_greater << std::endl;
|
||||
std::cout << "\tCount less: " << result.count_less << std::endl;
|
||||
std::cout << "\tCount equal: " << result.count_equal << "\n\n";
|
||||
|
||||
std::cout << "\nPress any key to exit..." << std::endl;
|
||||
getchar();
|
||||
return 0;
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef NO_GUI
|
||||
if(!DirectoryExists(RESOURCES_PATH))
|
||||
{
|
||||
std::cerr << "Resources folder not found!" << std::endl;
|
||||
|
@ -62,9 +132,11 @@ int main(int argc, char** argv)
|
|||
UpdateDrawFrame();
|
||||
}
|
||||
CloseWindow();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef NO_GUI
|
||||
//----------------------------------------------------------------------------------
|
||||
// Render Functions
|
||||
//----------------------------------------------------------------------------------
|
||||
|
@ -149,4 +221,6 @@ void RenderGameplayState()
|
|||
);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue