Brad Bishop | 6826840 | 2018-12-13 11:52:42 -0500 | [diff] [blame] | 1 | #include "../utils.hpp" |
| 2 | |
| 3 | #include <gtest/gtest.h> |
| 4 | |
Brad Bishop | 6826840 | 2018-12-13 11:52:42 -0500 | [diff] [blame] | 5 | using namespace phosphor::inventory::manager; |
| 6 | using namespace std::string_literals; |
| 7 | |
| 8 | TEST(UtilsTest, TestVariantVisitor) |
| 9 | { |
Patrick Williams | 26f8668 | 2020-05-13 11:36:19 -0500 | [diff] [blame] | 10 | std::variant<int, std::string> ib1(100); |
| 11 | auto converted1 = convertVariant<std::variant<int>>(ib1); |
| 12 | EXPECT_TRUE(std::get<int>(converted1) == 100); |
Brad Bishop | 6826840 | 2018-12-13 11:52:42 -0500 | [diff] [blame] | 13 | |
Patrick Williams | 26f8668 | 2020-05-13 11:36:19 -0500 | [diff] [blame] | 14 | std::variant<int, std::string> ib2(100); |
| 15 | EXPECT_THROW(convertVariant<std::variant<std::string>>(ib2), |
| 16 | std::runtime_error); |
Brad Bishop | 6826840 | 2018-12-13 11:52:42 -0500 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | TEST(UtilsTest, TestCompareFirst) |
| 20 | { |
| 21 | auto c = compareFirst(std::less<int>()); |
| 22 | auto p1 = std::make_pair(1, 2); |
| 23 | auto p2 = std::make_pair(3, 4); |
| 24 | |
| 25 | EXPECT_TRUE(c(p1, p2)); |
| 26 | EXPECT_TRUE(c(1, p2)); |
| 27 | EXPECT_TRUE(c(p1, 2)); |
| 28 | EXPECT_FALSE(c(p2, p1)); |
| 29 | EXPECT_FALSE(c(p2, 1)); |
| 30 | EXPECT_FALSE(c(2, p1)); |
| 31 | |
| 32 | auto p3 = std::make_pair(1, 100); |
| 33 | auto p4 = std::make_pair(1, 200); |
| 34 | |
| 35 | EXPECT_FALSE(c(p3, p4)); |
| 36 | EXPECT_FALSE(c(1, p4)); |
| 37 | EXPECT_TRUE(c(p3, 2)); |
| 38 | EXPECT_FALSE(c(p4, p3)); |
| 39 | EXPECT_FALSE(c(p4, 1)); |
| 40 | EXPECT_FALSE(c(2, p3)); |
| 41 | } |
| 42 | |
| 43 | TEST(UtilsTest, TestRelPathComparePrefix) |
| 44 | { |
| 45 | auto s1 = "prefixfoo"s; |
| 46 | auto s2 = "prefixbar"s; |
| 47 | RelPathCompare comp("prefix"); |
| 48 | |
| 49 | EXPECT_TRUE(comp(s2, s1)); |
| 50 | EXPECT_FALSE(comp(s1, s2)); |
| 51 | |
| 52 | auto s3 = "bar"s; |
| 53 | auto s4 = "foo"s; |
| 54 | |
| 55 | EXPECT_FALSE(comp(s4, s3)); |
| 56 | EXPECT_TRUE(comp(s3, s4)); |
| 57 | |
| 58 | auto s5 = "prefixbar"s; |
| 59 | auto s6 = "foo"s; |
| 60 | |
| 61 | EXPECT_FALSE(comp(s6, s5)); |
| 62 | EXPECT_TRUE(comp(s5, s6)); |
| 63 | |
| 64 | auto s7 = "bar"s; |
| 65 | auto s8 = "prefixfoo"s; |
| 66 | |
| 67 | EXPECT_FALSE(comp(s8, s7)); |
| 68 | EXPECT_TRUE(comp(s7, s8)); |
| 69 | } |
| 70 | |
| 71 | TEST(UtilsTest, TestRelPathCompareNoPrefix) |
| 72 | { |
| 73 | auto s1 = "prefixfoo"s; |
| 74 | auto s2 = "prefixbar"s; |
| 75 | RelPathCompare comp(""); |
| 76 | |
| 77 | EXPECT_TRUE(comp(s2, s1)); |
| 78 | EXPECT_FALSE(comp(s1, s2)); |
| 79 | |
| 80 | auto s3 = "bar"s; |
| 81 | auto s4 = "foo"s; |
| 82 | |
| 83 | EXPECT_FALSE(comp(s4, s3)); |
| 84 | EXPECT_TRUE(comp(s3, s4)); |
| 85 | |
| 86 | auto s5 = "prefixbar"s; |
| 87 | auto s6 = "foo"s; |
| 88 | |
| 89 | EXPECT_TRUE(comp(s6, s5)); |
| 90 | EXPECT_FALSE(comp(s5, s6)); |
| 91 | |
| 92 | auto s7 = "bar"s; |
| 93 | auto s8 = "prefixfoo"s; |
| 94 | |
| 95 | EXPECT_FALSE(comp(s8, s7)); |
| 96 | EXPECT_TRUE(comp(s7, s8)); |
| 97 | } |