xref: /openbmc/entity-manager/test/test_utils.cpp (revision ecf1a31636a65f3eb2aaa7e45c8b51b900bedf7b)
1 #include "utils.hpp"
2 
3 #include <gtest/gtest.h>
4 
5 constexpr std::string_view helloWorld = "Hello World";
6 
7 TEST(IfindFirstTest, BasicMatch)
8 {
9     auto match = iFindFirst(helloWorld, "World");
10     EXPECT_TRUE(match);
11     EXPECT_EQ(std::distance(helloWorld.begin(), match.begin()), 6);
12     EXPECT_EQ(std::distance(helloWorld.begin(), match.end()), 11);
13 }
14 
15 TEST(IfindFirstTest, CaseInsensitiveMatch)
16 {
17     auto match = iFindFirst(helloWorld, "world");
18     EXPECT_TRUE(match);
19     EXPECT_EQ(std::distance(helloWorld.begin(), match.begin()), 6);
20     EXPECT_EQ(std::distance(helloWorld.begin(), match.end()), 11);
21 }
22 
23 TEST(IfindFirstTest, NoMatch)
24 {
25     auto match = iFindFirst(helloWorld, "Planet");
26     EXPECT_FALSE(match);
27 }
28 
29 TEST(IfindFirstTest, MatchAtStart)
30 {
31     auto match = iFindFirst(helloWorld, "HeLLo");
32     EXPECT_TRUE(match);
33     EXPECT_EQ(std::distance(helloWorld.begin(), match.begin()), 0);
34     EXPECT_EQ(std::distance(helloWorld.begin(), match.end()), 5);
35 }
36 
37 TEST(IfindFirstTest, MatchAtEnd)
38 {
39     auto match = iFindFirst(helloWorld, "LD");
40     EXPECT_TRUE(match);
41     EXPECT_EQ(std::distance(helloWorld.begin(), match.begin()), 9);
42     EXPECT_EQ(std::distance(helloWorld.begin(), match.end()), 11);
43 }
44 
45 TEST(IfindFirstTest, EmptySubstring)
46 {
47     auto match = iFindFirst(helloWorld, "");
48     EXPECT_FALSE(match);
49 }
50 
51 TEST(IfindFirstTest, EmptyString)
52 {
53     auto match = iFindFirst("", "Hello");
54     EXPECT_FALSE(match);
55 }
56 
57 TEST(SplitTest, NormalSplit)
58 {
59     auto result = split("a,b,c", ',');
60     std::vector<std::string> expected = {"a", "b", "c"};
61     EXPECT_EQ(result, expected);
62 }
63 
64 TEST(SplitTest, ConsecutiveDelimiters)
65 {
66     auto result = split("a,,b", ',');
67     std::vector<std::string> expected = {"a", "", "b"};
68     EXPECT_EQ(result, expected);
69 }
70 
71 TEST(SplitTest, LeadingDelimiter)
72 {
73     auto result = split(",a,b", ',');
74     std::vector<std::string> expected = {"", "a", "b"};
75     EXPECT_EQ(result, expected);
76 }
77 
78 TEST(SplitTest, TrailingDelimiter)
79 {
80     auto result = split("a,b,", ',');
81     std::vector<std::string> expected = {"a", "b", ""};
82     EXPECT_EQ(result, expected);
83 }
84 
85 TEST(SplitTest, NoDelimiter)
86 {
87     auto result = split("abc", ',');
88     std::vector<std::string> expected = {"abc"};
89     EXPECT_EQ(result, expected);
90 }
91 
92 TEST(SplitTest, EmptyString)
93 {
94     auto result = split("", ',');
95     std::vector<std::string> expected = {""};
96     EXPECT_EQ(result, expected);
97 }
98