-
-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
|
||
#define exprtk_disable_string_capabilities | ||
#define exprtk_disable_rtl_io | ||
#define exprtk_disable_rtl_io_file | ||
#define exprtk_disable_rtl_vecops | ||
|
||
#include <exprtk/exprtk.hpp> | ||
|
||
namespace krbn { | ||
namespace exprtk_utility { | ||
inline double eval(const std::string& expression_string, | ||
std::vector<std::pair<std::string, double>> constants) { | ||
exprtk::symbol_table<double> symbol_table; | ||
symbol_table.add_constants(); // pi, epsilon and inf | ||
for (auto&& [name, value] : constants) { | ||
symbol_table.add_constant(name, value); | ||
} | ||
|
||
exprtk::expression<double> expression; | ||
expression.register_symbol_table(symbol_table); | ||
|
||
exprtk::parser<double> parser; | ||
parser.compile(expression_string, expression); | ||
|
||
return expression.value(); | ||
} | ||
|
||
inline exprtk::expression<double> compile(const std::string& expression_string, | ||
std::vector<std::pair<std::string, double>> constants, | ||
std::vector<std::pair<std::string, double&>> variables) { | ||
exprtk::symbol_table<double> symbol_table; | ||
symbol_table.add_constants(); // pi, epsilon and inf | ||
for (auto&& [name, value] : constants) { | ||
symbol_table.add_constant(name, value); | ||
} | ||
for (auto&& [name, value] : variables) { | ||
symbol_table.add_variable(name, value); | ||
} | ||
|
||
exprtk::expression<double> expression; | ||
expression.register_symbol_table(symbol_table); | ||
|
||
exprtk::parser<double> parser; | ||
parser.compile(expression_string, expression); | ||
|
||
return expression; | ||
} | ||
} // namespace exprtk_utility | ||
} // namespace krbn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
cmake_minimum_required(VERSION 3.9) | ||
|
||
include(../../tests.cmake) | ||
|
||
project(karabiner_test) | ||
|
||
include_directories(${CMAKE_CURRENT_LIST_DIR}/../../../src/vendor/duktape-2.7.0/src) | ||
include_directories(${CMAKE_CURRENT_LIST_DIR}/../../../src/vendor/duktape-2.7.0/extras/console) | ||
include_directories(${CMAKE_CURRENT_LIST_DIR}/../../../src/vendor/duktape-2.7.0/extras/module-node) | ||
|
||
# Suppress duktape warnings | ||
add_compile_options(-Wno-deprecated-declarations) | ||
add_compile_options(-Wno-unused-but-set-variable) | ||
|
||
add_executable( | ||
karabiner_test | ||
src/test.cpp | ||
../../../src/vendor/duktape-2.7.0/src/duktape.c | ||
../../../src/vendor/duktape-2.7.0/extras/console/duk_console.c | ||
../../../src/vendor/duktape-2.7.0/extras/module-node/duk_module_node.c | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
all: build_make | ||
./build/karabiner_test | ||
|
||
clean: clean_builds | ||
|
||
include ../Makefile.rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "exprtk_utility.hpp" | ||
#include <boost/ut.hpp> | ||
|
||
int main(void) { | ||
using namespace boost::ut; | ||
using namespace boost::ut::literals; | ||
using namespace std::literals; | ||
|
||
"exprtk_utility::eval"_test = [] { | ||
auto actual = krbn::exprtk_utility::eval("cos(radian) * magnitude", | ||
{ | ||
{"radian", 0.78539816339}, | ||
{"magnitude", 3.5}, | ||
}); | ||
expect(2.47487_d == actual); | ||
}; | ||
|
||
"exprtk_utility::compile"_test = [] { | ||
double radian = 0.0; | ||
double magnitude = 1.0; | ||
|
||
auto expression = krbn::exprtk_utility::compile("cos(radian) * magnitude", | ||
{}, | ||
{ | ||
{"radian", radian}, | ||
{"magnitude", magnitude}, | ||
}); | ||
expect(1.0_d == expression.value()); | ||
|
||
radian = 0.78539816339; | ||
magnitude = 3.5; | ||
expect(2.47487_d == expression.value()); | ||
}; | ||
|
||
return 0; | ||
} |