// Copyright 2022 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source // License, and you may not use this file except in compliance with the Business Source License. // // As of the Change Date specified in that file, in accordance with // the Business Source License, use of this software will be governed // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. #include #include #include #include #include #include "gtest/gtest.h" #include "utils/algorithm.hpp" using vec = std::vector; using namespace std::string_literals; using namespace memgraph::utils; TEST(Algorithm, PrintIterable) { // Checkss both variants of the function (using iterators and collections) auto check = [](const std::vector &iterable, const std::string &expected_output) { { std::ostringstream oss; PrintIterable(oss, iterable, ", "); EXPECT_EQ(oss.str(), expected_output); } { auto streamer = [](auto &stream, const auto &item) { stream << item; }; std::ostringstream oss; PrintIterable(&oss, iterable.begin(), iterable.end(), ", ", streamer); EXPECT_EQ(oss.str(), expected_output); } }; check(std::vector{1, 2, 3, 4}, "1, 2, 3, 4"); check(std::vector{1}, "1"); check(std::vector{}, ""); { // Use custom streamer auto map_streamer = [](auto &stream, const auto &item) { stream << item.first << ": " << item.second; }; std::ostringstream oss; std::map map; map.emplace("a", "x"); map.emplace("b", "y"); PrintIterable(&oss, map.begin(), map.end(), ", ", map_streamer); EXPECT_EQ(oss.str(), "a: x, b: y"); } } TEST(Algorithm, Reversed) { EXPECT_EQ(Reversed(""s), ""s); EXPECT_EQ(Reversed("abc"s), "cba"s); EXPECT_EQ(Reversed(std::vector({1, 2, 3, 4})), std::vector({4, 3, 2, 1})); EXPECT_EQ(Reversed(std::list({"ab"s, "cd"s})), std::list({"cd"s, "ab"s})); }