File indexing completed on 2026-07-26 08:22:23
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "traccc/utils/algorithm.hpp"
0011
0012
0013 #include <gtest/gtest.h>
0014
0015
0016 #include <string>
0017
0018 class double_int : public traccc::algorithm<int(const int &)> {
0019 public:
0020 virtual int operator()(const int &i) const override { return 2 * i; }
0021 };
0022
0023 class double_int_affine : public traccc::algorithm<int(int &&)> {
0024 public:
0025 virtual int operator()(int &&i) const override { return 2 * i; }
0026 };
0027
0028 class double_string_affine
0029 : public traccc::algorithm<std::string(std::string &&)> {
0030 public:
0031 virtual std::string operator()(std::string &&i) const override {
0032 return i + i;
0033 }
0034 };
0035
0036 class double_string_regular
0037 : public traccc::algorithm<std::string(const std::string &)> {
0038 public:
0039 virtual std::string operator()(const std::string &i) const override {
0040 return i + i;
0041 }
0042 };
0043
0044 class add : public traccc::algorithm<int(const int &, const int &)> {
0045 public:
0046 virtual int operator()(const int &i, const int &j) const override {
0047 return i + j;
0048 }
0049 };
0050
0051 TEST(algorithm, basic) {
0052 double_int i;
0053
0054 ASSERT_EQ(i(1), 2);
0055 ASSERT_EQ(i(-1), -2);
0056 ASSERT_EQ(i(5), 10);
0057 }
0058
0059 TEST(algorithm, basic_affine) {
0060 double_int_affine i;
0061
0062 ASSERT_EQ(i(1), 2);
0063 ASSERT_EQ(i(-1), -2);
0064 ASSERT_EQ(i(5), 10);
0065 }
0066
0067 TEST(algorithm, string_affine) {
0068 double_string_affine i;
0069
0070 std::string s = "test";
0071
0072 ASSERT_EQ(i("hello"), "hellohello");
0073 ASSERT_EQ(i("bye"), "byebye");
0074 ASSERT_EQ(i(std::move(s)), "testtest");
0075 }
0076
0077 TEST(algorithm, string_regular) {
0078 double_string_regular i;
0079
0080 std::string s = "test";
0081
0082 ASSERT_EQ(i("hello"), "hellohello");
0083 ASSERT_EQ(i("bye"), "byebye");
0084 ASSERT_EQ(i(s), "testtest");
0085 }
0086
0087 TEST(algorithm, compose_double_int_affine_1) {
0088 double_int_affine i;
0089
0090 std::function<int(int &&)> f = traccc::compose(std::function<int(int &&)>(i),
0091 std::function<int(int &&)>(i));
0092
0093 ASSERT_EQ(f(1), 4);
0094 ASSERT_EQ(f(-1), -4);
0095 ASSERT_EQ(f(5), 20);
0096 }
0097
0098 TEST(algorithm, compose_double_int_affine_2) {
0099 double_int_affine i;
0100 double_int_affine j;
0101
0102 std::function<int(int &&)> f = traccc::compose(std::function<int(int &&)>(i),
0103 std::function<int(int &&)>(j));
0104
0105 ASSERT_EQ(f(1), 4);
0106 ASSERT_EQ(f(-1), -4);
0107 ASSERT_EQ(f(5), 20);
0108 }
0109
0110 TEST(algorithm, compose_double_int_regular_1) {
0111 double_int i;
0112
0113 std::function<int(const int &)> f = traccc::compose(
0114 std::function<int(const int &)>(i), std::function<int(const int &)>(i));
0115
0116 ASSERT_EQ(f(1), 4);
0117 ASSERT_EQ(f(-1), -4);
0118 ASSERT_EQ(f(5), 20);
0119 }
0120
0121 TEST(algorithm, compose_double_int_regular_2) {
0122 double_int i;
0123 double_int j;
0124
0125 std::function<int(const int &)> f = traccc::compose(
0126 std::function<int(const int &)>(i), std::function<int(const int &)>(j));
0127
0128 ASSERT_EQ(f(1), 4);
0129 ASSERT_EQ(f(-1), -4);
0130 ASSERT_EQ(f(5), 20);
0131 }
0132
0133 TEST(algorithm, compose_string_lvalue_1) {
0134 double_string_regular i;
0135
0136 std::string s = "test";
0137
0138 std::function<std::string(const std::string &)> f =
0139 traccc::compose(std::function<std::string(const std::string &)>(i),
0140 std::function<std::string(const std::string &)>(i));
0141
0142 ASSERT_EQ(f("hello"), "hellohellohellohello");
0143 ASSERT_EQ(f("bye"), "byebyebyebye");
0144 ASSERT_EQ(f(s), "testtesttesttest");
0145 }
0146
0147 TEST(algorithm, compose_string_rvalue_1) {
0148 double_string_affine i;
0149
0150 std::string s = "test";
0151
0152 std::function<std::string(std::string &&)> f =
0153 traccc::compose(std::function<std::string(std::string &&)>(i),
0154 std::function<std::string(std::string &&)>(i));
0155
0156 ASSERT_EQ(f("hello"), "hellohellohellohello");
0157 ASSERT_EQ(f("bye"), "byebyebyebye");
0158 ASSERT_EQ(f(std::move(s)), "testtesttesttest");
0159 }
0160
0161 TEST(algorithm, compose_double_int_many_times) {
0162 double_int i;
0163
0164 std::function<int(const int &)> f = traccc::compose(
0165 std::function<int(const int &)>(i), std::function<int(const int &)>(i),
0166 std::function<int(const int &)>(i), std::function<int(const int &)>(i),
0167 std::function<int(const int &)>(i), std::function<int(const int &)>(i),
0168 std::function<int(const int &)>(i), std::function<int(const int &)>(i));
0169
0170 ASSERT_EQ(f(1), 256);
0171 ASSERT_EQ(f(-1), -256);
0172 ASSERT_EQ(f(5), 1280);
0173 }
0174
0175 TEST(algorithm, side_effect) {
0176 double_int i;
0177 int j = 0;
0178
0179 std::function<int(const int &)> f = traccc::compose(
0180 std::function<int(const int &)>(i), std::function<int(const int &)>(i),
0181 traccc::side_effect(
0182 std::function<int(const int &)>(i),
0183 std::function<void(const int &)>([&j](const int &q) { j = q + 5; })));
0184
0185 ASSERT_EQ(f(1), 8);
0186 ASSERT_EQ(j, 9);
0187 }
0188
0189 TEST(algorithm, partial_application_bind) {
0190 add i;
0191 std::function<int(const int &)> f = std::bind(i, std::placeholders::_1, 5);
0192
0193 ASSERT_EQ(f(1), 6);
0194 ASSERT_EQ(f(-1), 4);
0195 ASSERT_EQ(f(5), 10);
0196 }
0197
0198 TEST(algorithm, partial_application_lambda) {
0199 add i;
0200 std::function<int(const int &)> f = [i](const int &j) { return i(j, 5); };
0201
0202 ASSERT_EQ(f(1), 6);
0203 ASSERT_EQ(f(-1), 4);
0204 ASSERT_EQ(f(5), 10);
0205 }
0206
0207 TEST(algorithm, partial_application_compose) {
0208 add i;
0209 std::function<int(const int &)> j = std::bind(i, std::placeholders::_1, 5);
0210
0211 std::function<int(const int &)> f = traccc::compose(j, j, j);
0212
0213 ASSERT_EQ(f(1), 16);
0214 ASSERT_EQ(f(-1), 14);
0215 ASSERT_EQ(f(5), 20);
0216 }