diff --git a/src/common/list_utils.h b/src/common/list_utils.h new file mode 100644 index 000000000..e4f9ac5be --- /dev/null +++ b/src/common/list_utils.h @@ -0,0 +1,78 @@ +/* + mkvmerge -- utility for splicing together matroska files + from component media subtypes + + Distributed under the GPL v2 + see the file COPYING for details + or visit http://www.gnu.org/copyleft/gpl.html + + list utility functions + + Written by Moritz Bunkus . +*/ + +#ifndef MTX_COMMON_LIST_UTILS_H +#define MTX_COMMON_LIST_UTILS_H + +namespace mtx { + +template +bool +includes(T const &needle, + T const &val) { + return needle == val; +} + +template +bool +includes(T const &needle, + T const &val, + Trest... rest) { + return (needle == val) || includes(needle, rest...); +} + +template +bool +any_of(std::function pred, + T const &val) { + return pred(val); +} + +template +bool +any_of(std::function pred, + T const &val, + Trest... rest) { + return pred(val) || any_of(pred, rest...); +} + +template +bool +all_of(std::function pred, + T const &val) { + return pred(val); +} + +template +bool +all_of(std::function pred, + T const &val, + Trest... rest) { + return pred(val) && all_of(pred, rest...); +} + +template +bool +none_of(std::function pred, + T const &val, + Trest... rest) { + return !any_of(pred, val, rest...); +} + +} + +#endif // MTX_COMMON_LIST_UTILS_H diff --git a/tests/unit/common/list_utils.cpp b/tests/unit/common/list_utils.cpp new file mode 100644 index 000000000..e00017f89 --- /dev/null +++ b/tests/unit/common/list_utils.cpp @@ -0,0 +1,57 @@ +#include "common/common_pch.h" + +#include "common/list_utils.h" + +#include "gtest/gtest.h" + +namespace { + +TEST(ListUtils, includes) { + EXPECT_TRUE(mtx::includes(42, 42)); + EXPECT_TRUE(mtx::includes(42, 54, 42, 48)); + EXPECT_TRUE(mtx::includes(42, 42, 54, 48)); + EXPECT_TRUE(mtx::includes(42, 54, 48, 42)); + + EXPECT_FALSE(mtx::includes(23, 42)); + EXPECT_FALSE(mtx::includes(23, 54, 42, 48)); + EXPECT_FALSE(mtx::includes(23, 42, 54, 48)); + EXPECT_FALSE(mtx::includes(23, 54, 48, 42)); +} + +TEST(ListUtils, any_of) { + EXPECT_TRUE(mtx::any_of([](int val) { return val < 43; }, 42)); + EXPECT_TRUE(mtx::any_of([](int val) { return val < 43; }, 54, 42, 48)); + EXPECT_TRUE(mtx::any_of([](int val) { return val < 43; }, 42, 54, 48)); + EXPECT_TRUE(mtx::any_of([](int val) { return val < 43; }, 54, 48, 42)); + + EXPECT_FALSE(mtx::any_of([](int val) { return val > 64; }, 42)); + EXPECT_FALSE(mtx::any_of([](int val) { return val > 64; }, 54, 42, 48)); + EXPECT_FALSE(mtx::any_of([](int val) { return val > 64; }, 42, 54, 48)); + EXPECT_FALSE(mtx::any_of([](int val) { return val > 64; }, 54, 48, 42)); +} + +TEST(ListUtils, all_of) { + EXPECT_TRUE(mtx::all_of([](int val) { return val < 64; }, 42)); + EXPECT_TRUE(mtx::all_of([](int val) { return val < 64; }, 54, 42, 48)); + EXPECT_TRUE(mtx::all_of([](int val) { return val < 64; }, 42, 54, 48)); + EXPECT_TRUE(mtx::all_of([](int val) { return val < 64; }, 54, 48, 42)); + + EXPECT_FALSE(mtx::all_of([](int val) { return val < 42; }, 42)); + EXPECT_FALSE(mtx::all_of([](int val) { return val < 42; }, 54, 42, 48)); + EXPECT_FALSE(mtx::all_of([](int val) { return val < 42; }, 42, 54, 48)); + EXPECT_FALSE(mtx::all_of([](int val) { return val < 42; }, 54, 48, 42)); +} + +TEST(ListUtils, none_of) { + EXPECT_TRUE(mtx::none_of([](int val) { return val > 64; }, 42)); + EXPECT_TRUE(mtx::none_of([](int val) { return val > 64; }, 54, 42, 48)); + EXPECT_TRUE(mtx::none_of([](int val) { return val > 64; }, 42, 54, 48)); + EXPECT_TRUE(mtx::none_of([](int val) { return val > 64; }, 54, 48, 42)); + + EXPECT_FALSE(mtx::none_of([](int val) { return val < 56; }, 42)); + EXPECT_FALSE(mtx::none_of([](int val) { return val < 56; }, 54, 42, 48)); + EXPECT_FALSE(mtx::none_of([](int val) { return val < 56; }, 42, 54, 48)); + EXPECT_FALSE(mtx::none_of([](int val) { return val < 56; }, 54, 48, 42)); +} + +}