From 55a2bacc0e29d8ebf7a78aa1fec03d2a26b39829 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Wed, 2 Mar 2022 11:08:14 +0300 Subject: [PATCH] add within --- bin/main.cpp | 12 +++++++++--- bin/meson.build | 1 + src/meson.build | 1 + src/range/meson.build | 14 ++++++++++++++ src/range/range.cpp | 5 +++++ src/range/range.hpp | 15 +++++++++++++++ tests/meson.build | 11 ++++++++++- tests/range.cpp | 9 +++++++++ tests/{split_str.cpp => string.cpp} | 0 9 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/range/meson.build create mode 100644 src/range/range.cpp create mode 100644 src/range/range.hpp create mode 100644 tests/range.cpp rename tests/{split_str.cpp => string.cpp} (100%) diff --git a/bin/main.cpp b/bin/main.cpp index c4ac633..3d9fc4e 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -1,11 +1,17 @@ #include #include "string/string.hpp" +#include "range/range.hpp" int main(int argc, char *argv[]) { - std::string str { "asdf,qwer,zxcv" }; - hack::v_str v = hack::split_str(str, ','); + {// ex: split_str + std::string str { "asdf,qwer,zxcv" }; + hack::v_str v = hack::split_str(str, ','); + for (const auto& c : v) std::cout << c << std::endl; + } - for (const auto& c : v) std::cout << c << std::endl; + {// ex: within + std::cout << hack::within(12, 34, 12, 23, 31) << std::endl; + } } diff --git a/bin/meson.build b/bin/meson.build index ddaf903..6baa655 100644 --- a/bin/meson.build +++ b/bin/meson.build @@ -1,4 +1,5 @@ deps += string_dep +deps += range_dep executable( 'hack', 'main.cpp', diff --git a/src/meson.build b/src/meson.build index 0cb5b45..325ba16 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,3 +1,4 @@ inc += include_directories('.') subdir('string') +subdir('range') diff --git a/src/range/meson.build b/src/range/meson.build new file mode 100644 index 0000000..94717ab --- /dev/null +++ b/src/range/meson.build @@ -0,0 +1,14 @@ +headers = ['range.hpp'] +sources = ['range.cpp'] + +lib = library( + 'range', + include_directories : inc, + install : true, + sources: [headers, sources] +) + +range_dep = declare_dependency( + include_directories: inc, + link_with: lib +) diff --git a/src/range/range.cpp b/src/range/range.cpp new file mode 100644 index 0000000..614e456 --- /dev/null +++ b/src/range/range.cpp @@ -0,0 +1,5 @@ +#include "range.hpp" + +namespace hack +{ +} diff --git a/src/range/range.hpp b/src/range/range.hpp new file mode 100644 index 0000000..7c2cd6e --- /dev/null +++ b/src/range/range.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +namespace hack +{ + template + bool within(T min, T max, Args... args) + { + return ((min <= args && max >= args) && ...); + // 1, 5, 2, 3, 4 + // ( (1 <= 2 && 5 >= 2) && (1 <= 3 && 5 >= 3) && (1 <= 4 && 5 >= 4) ) + } +} diff --git a/tests/meson.build b/tests/meson.build index fb6c145..9773ccb 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -5,7 +5,16 @@ test( 'split_str', executable( 'split_str', - 'split_str.cpp', + 'string.cpp', dependencies: [ string_dep, gtest_dep ] ) ) + +test( + 'within', + executable( + 'within', + 'range.cpp', + dependencies: [ range_dep, gtest_dep ] + ) +) diff --git a/tests/range.cpp b/tests/range.cpp new file mode 100644 index 0000000..10edac8 --- /dev/null +++ b/tests/range.cpp @@ -0,0 +1,9 @@ +#include + +#include "range/range.hpp" + +TEST(within, check) +{ + ASSERT_EQ(hack::within(23, 123, 34, 44, 55, 66), true); + ASSERT_EQ(hack::within(23, 123, 134, 44, 55, 66), false); +} diff --git a/tests/split_str.cpp b/tests/string.cpp similarity index 100% rename from tests/split_str.cpp rename to tests/string.cpp