45#include <gtest/gtest.h>
67 std::vector<int> v = {1, 2, 3, 4, 5};
68 auto squares =
uni_map([](
int x) {
return x * x; }, v);
70 EXPECT_EQ(squares.size(), 5);
71 EXPECT_EQ(squares[0], 1);
72 EXPECT_EQ(squares[4], 25);
77 std::vector<int> v = {1, 2, 3, 4, 5, 6};
78 auto evens =
uni_filter([](
int x) {
return x % 2 == 0; }, v);
80 EXPECT_EQ(evens.size(), 3);
81 EXPECT_EQ(evens[0], 2);
82 EXPECT_EQ(evens[2], 6);
87 std::vector<int> v = {1, 2, 3, 4, 5};
88 int sum =
uni_foldl(0, [](
int acc,
int x) {
return acc + x; }, v);
94 std::vector<int> v = {2, 4, 6, 8};
95 EXPECT_TRUE(
uni_all([](
int x) {
return x % 2 == 0; }, v));
98TEST(UniExistsStl, SomeTrue)
100 std::vector<int> v = {1, 2, 3, 4, 5};
101 EXPECT_TRUE(
uni_exists([](
int x) {
return x == 3; }, v));
111 auto squares =
uni_map([](
int x) {
return x * x; },
l);
113 EXPECT_EQ(squares.size(), 5);
114 EXPECT_EQ(squares[0], 1);
115 EXPECT_EQ(squares[4], 25);
121 auto evens =
uni_filter([](
int x) {
return x % 2 == 0; },
l);
123 EXPECT_EQ(evens.size(), 3);
124 EXPECT_EQ(evens[0], 2);
125 EXPECT_EQ(evens[2], 6);
131 int sum =
uni_foldl(0, [](
int acc,
int x) {
return acc + x; },
l);
138 EXPECT_TRUE(
uni_all([](
int x) {
return x % 2 == 0; },
l));
144 EXPECT_TRUE(
uni_exists([](
int x) {
return x == 3; },
l));
154 auto squares =
uni_map([](
int x) {
return x * x; }, arr);
156 EXPECT_EQ(squares.size(), 3);
157 EXPECT_EQ(squares[0], 1);
158 EXPECT_EQ(squares[2], 9);
171 auto evens =
uni_filter([](
int x) {
return x % 2 == 0; },
l);
173 EXPECT_EQ(evens.size(), 3);
180TEST(UniMapMixed, SameResultBothContainerTypes)
182 std::vector<int> stl = {1, 2, 3, 4, 5};
185 auto stl_result =
uni_map([](
int x) {
return x * 2; }, stl);
186 auto aleph_result =
uni_map([](
int x) {
return x * 2; }, aleph);
188 EXPECT_EQ(stl_result, aleph_result);
191TEST(UniFilterMixed, SameResultBothContainerTypes)
193 std::vector<int> stl = {1, 2, 3, 4, 5, 6};
196 auto stl_result =
uni_filter([](
int x) {
return x > 3; }, stl);
197 auto aleph_result =
uni_filter([](
int x) {
return x > 3; }, aleph);
199 EXPECT_EQ(stl_result, aleph_result);
202TEST(UniFoldlMixed, SameResultBothContainerTypes)
204 std::vector<int> stl = {1, 2, 3, 4, 5};
208 int aleph_sum =
uni_foldl(0, [](
int acc,
int x) {
return acc + x; }, aleph);
220 std::vector<std::string> stl = {
"a",
"b",
"c"};
223 auto stl_result =
uni_mapi([](
size_t i,
const std::string& s) {
224 return std::to_string(i) + s;
227 auto aleph_result =
uni_mapi([](
size_t i,
const std::string& s) {
228 return std::to_string(i) + s;
231 EXPECT_EQ(stl_result, aleph_result);
232 EXPECT_EQ(stl_result[0],
"0a");
233 EXPECT_EQ(stl_result[2],
"2c");
238 std::vector<int> stl = {10, 20, 30, 40, 50};
241 auto stl_evens =
uni_filteri([](
size_t i,
int) {
return i % 2 == 0; }, stl);
242 auto aleph_evens =
uni_filteri([](
size_t i,
int) {
return i % 2 == 0; }, aleph);
244 EXPECT_EQ(stl_evens, aleph_evens);
245 EXPECT_EQ(stl_evens.size(), 3);
250 std::vector<int> stl = {1, 2, 3, 4};
253 auto stl_sums =
uni_scan_left(0, [](
int acc,
int x) {
return acc + x; }, stl);
254 auto aleph_sums =
uni_scan_left(0, [](
int acc,
int x) {
return acc + x; }, aleph);
256 EXPECT_EQ(stl_sums, aleph_sums);
257 EXPECT_EQ(stl_sums.size(), 5);
258 EXPECT_EQ(stl_sums[0], 0);
259 EXPECT_EQ(stl_sums[4], 10);
268 std::vector<int> stl = {1, 2, 3, 4, 5};
271 auto stl_result =
uni_find([](
int x) {
return x > 3; }, stl);
272 auto aleph_result =
uni_find([](
int x) {
return x > 3; }, aleph);
274 ASSERT_TRUE(stl_result.has_value());
275 ASSERT_TRUE(aleph_result.has_value());
276 EXPECT_EQ(*stl_result, *aleph_result);
277 EXPECT_EQ(*stl_result, 4);
282 std::vector<std::string> stl = {
"a",
"b",
"c",
"d"};
285 auto stl_idx =
uni_find_index([](
const std::string& s) {
return s ==
"c"; }, stl);
286 auto aleph_idx =
uni_find_index([](
const std::string& s) {
return s ==
"c"; }, aleph);
288 ASSERT_TRUE(stl_idx.has_value());
289 ASSERT_TRUE(aleph_idx.has_value());
290 EXPECT_EQ(*stl_idx, *aleph_idx);
291 EXPECT_EQ(*stl_idx, 2);
296 std::vector<int> stl = {1, 2, 3, 4, 5};
300 EXPECT_TRUE(
uni_mem(3, aleph));
301 EXPECT_FALSE(
uni_mem(10, stl));
302 EXPECT_FALSE(
uni_mem(10, aleph));
311 std::vector<int> stl = {1, 2, 3, 4, 5, 6};
315 size_t aleph_count =
uni_count([](
int x) {
return x % 2 == 0; }, aleph);
323 std::vector<int> stl = {1, 2, 3, 4, 5};
336 std::vector<int> stl = {1, 2, 3, 4, 5};
340 auto aleph_result =
uni_take(3, aleph);
342 EXPECT_EQ(stl_result, aleph_result);
343 EXPECT_EQ(stl_result.size(), 3);
344 EXPECT_EQ(stl_result[2], 3);
349 std::vector<int> stl = {1, 2, 3, 4, 5};
353 auto aleph_result =
uni_drop(2, aleph);
355 EXPECT_EQ(stl_result, aleph_result);
356 EXPECT_EQ(stl_result.size(), 3);
357 EXPECT_EQ(stl_result[0], 3);
362 std::vector<int> stl = {1, 2, 3, 10, 4, 5};
365 auto stl_result =
uni_take_while([](
int x) {
return x < 10; }, stl);
366 auto aleph_result =
uni_take_while([](
int x) {
return x < 10; }, aleph);
368 EXPECT_EQ(stl_result, aleph_result);
369 EXPECT_EQ(stl_result.size(), 3);
374 std::vector<int> stl = {1, 2, 3, 10, 4, 5};
377 auto stl_result =
uni_drop_while([](
int x) {
return x < 10; }, stl);
378 auto aleph_result =
uni_drop_while([](
int x) {
return x < 10; }, aleph);
380 EXPECT_EQ(stl_result, aleph_result);
381 EXPECT_EQ(stl_result.size(), 3);
382 EXPECT_EQ(stl_result[0], 10);
391 std::vector<int> stl = {10, 20, 30};
398 ASSERT_TRUE(aleph_first.has_value());
405 std::vector<int> stl = {10, 20, 30};
412 ASSERT_TRUE(aleph_last.has_value());
419 std::vector<int> stl = {10, 20, 30, 40, 50};
423 auto aleph_nth =
uni_nth(2, aleph);
425 ASSERT_TRUE(
stl_nth.has_value());
426 ASSERT_TRUE(aleph_nth.has_value());
427 EXPECT_EQ(*
stl_nth, *aleph_nth);
437 std::vector<int> stl = {3, 1, 4, 1, 5, 9};
441 auto aleph_min =
uni_min(aleph);
443 ASSERT_TRUE(
stl_min.has_value());
444 ASSERT_TRUE(aleph_min.has_value());
445 EXPECT_EQ(*
stl_min, *aleph_min);
451 std::vector<int> stl = {3, 1, 4, 1, 5, 9};
455 auto aleph_max =
uni_max(aleph);
457 ASSERT_TRUE(
stl_max.has_value());
458 ASSERT_TRUE(aleph_max.has_value());
459 EXPECT_EQ(*
stl_max, *aleph_max);
465 std::vector<int> stl = {3, 1, 4, 1, 5, 9};
471 ASSERT_TRUE(stl_mm.has_value());
472 ASSERT_TRUE(aleph_mm.has_value());
473 EXPECT_EQ(stl_mm->first, aleph_mm->first);
474 EXPECT_EQ(stl_mm->second, aleph_mm->second);
475 EXPECT_EQ(stl_mm->first, 1);
476 EXPECT_EQ(stl_mm->second, 9);
485 std::vector<int> stl = {1, 2, 3, 4, 5};
494 std::vector<int> stl = {1, 2, 3, 4};
507 std::vector<int> stl = {1, 2, 3, 4, 5, 6};
510 auto [stl_evens, stl_odds] =
uni_partition([](
int x) {
return x % 2 == 0; }, stl);
511 auto [aleph_evens, aleph_odds] =
uni_partition([](
int x) {
return x % 2 == 0; }, aleph);
513 EXPECT_EQ(stl_evens, aleph_evens);
514 EXPECT_EQ(stl_odds, aleph_odds);
515 EXPECT_EQ(stl_evens.size(), 3);
516 EXPECT_EQ(stl_odds.size(), 3);
529 EXPECT_EQ(vec.size(), 5);
530 EXPECT_EQ(vec[0], 1);
531 EXPECT_EQ(vec[4], 5);
540 std::vector<int> stl = {1, 2, 3};
545 EXPECT_FALSE(
uni_equal(stl, aleph_diff));
550 std::vector<int> stl = {1, 2, 3};
566 std::list<int>
l = {1, 2, 3, 4, 5};
568 auto squares =
uni_map([](
int x) {
return x * x; },
l);
569 EXPECT_EQ(squares.size(), 5);
570 EXPECT_EQ(squares[4], 25);
575 std::deque<int> d = {1, 2, 3, 4, 5, 6};
577 auto evens =
uni_filter([](
int x) {
return x % 2 == 0; }, d);
578 EXPECT_EQ(evens.size(), 3);
587 std::vector<int> empty;
589 auto mapped =
uni_map([](
int x) {
return x * 2; }, empty);
590 EXPECT_TRUE(mapped.empty());
593 EXPECT_FALSE(first.has_value());
603 auto mapped =
uni_map([](
int x) {
return x * 2; }, empty);
604 EXPECT_TRUE(mapped.empty());
607 EXPECT_FALSE(first.has_value());
613TEST(UniEdgeCases, SingleElement)
615 std::vector<int> stl = {42};
627 ::testing::InitGoogleTest(&argc, argv);
628 return RUN_ALL_TESTS();
Unified functional programming utilities for both STL and Aleph containers.
T & append()
Allocate a new entry to the end of array.
Dynamic doubly linked list with O(1) size and bidirectional access.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
auto stl_first(const Container &c)
Get first element.
auto uni_filter(Pred &&pred, const Container &c)
Filter elements satisfying predicate.
auto uni_nth(size_t n, const Container &c)
Get n-th element.
auto stl_last(const Container &c)
Get last element.
auto stl_max(const Container &c)
Get maximum element.
int uni_compare(const Container1 &c1, const Container2 &c2)
Compare two containers lexicographically.
bool uni_equal(const Container1 &c1, const Container2 &c2)
Check equality of two containers.
std::vector< T > uni_scan_left(T init, Op &&op, const Container &c)
Scan left - fold with all intermediate results.
size_t uni_length(const Container &c)
Get container length.
auto stl_nth(const size_t n, const Container &c)
Get n-th element.
auto uni_drop_while(Pred &&pred, const Container &c)
Drop elements while predicate is true, return the rest.
auto uni_product(const Container &c)
Product of all elements.
auto uni_find(Pred &&pred, const Container &c)
Find first element satisfying predicate.
bool uni_mem(const T &target, const Container &c)
Check if element exists in container (mem in ML).
auto uni_filteri(Pred &&pred, const Container &c)
Filter with index (filteri in ML).
auto uni_map(Op &&op, const Container &c)
Map operation - transform each element.
size_t stl_count(Pred &&pred, const Container &c)
Count elements satisfying predicate.
auto stl_sum(const Container &c)
Sum all elements.
auto uni_last(const Container &c)
Get last element.
auto uni_min_max(const Container &c)
Get both min and max in a single pass.
auto uni_min(const Container &c)
Get minimum element.
size_t uni_count(Pred &&pred, const Container &c)
Count elements satisfying predicate.
auto stl_min(const Container &c)
Get minimum element.
T uni_foldl(T init, Op &&op, const Container &c)
Left fold (foldl) - reduce from left to right.
auto uni_drop(size_t n, const Container &c)
Drop first n elements, return the rest.
auto uni_max(const Container &c)
Get maximum element.
auto uni_take_while(Pred &&pred, const Container &c)
Take elements while predicate is true.
bool uni_all(Pred &&pred, const Container &c)
Check if all elements satisfy predicate.
auto uni_first(const Container &c)
Get first element.
auto uni_partition(Pred &&pred, const Container &c)
Partition elements by predicate.
auto uni_to_vector(const Container &c)
Convert container to std::vector.
auto uni_mapi(Op &&op, const Container &c)
Map with index (mapi in ML).
auto uni_take(size_t n, const Container &c)
Take first n elements.
auto uni_sum(const Container &c)
Sum all elements.
bool uni_exists(Pred &&pred, const Container &c)
Check if any element satisfies predicate.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
std::optional< size_t > uni_find_index(Pred &&pred, const Container &c)
Find index of first element satisfying predicate.
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.