1 // Copyright 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/string_utils.h"
16
17 #include <cstdint>
18 #include <string>
19
20 #include "gtest/gtest.h"
21
22 namespace netsim {
23 namespace stringutils {
24 namespace testing {
25 namespace {
26
TEST(StringUtilsTest,ToHexTest)27 TEST(StringUtilsTest, ToHexTest) {
28 EXPECT_EQ(ToHexString(0x12), "0x12");
29 EXPECT_EQ(ToHexString(0xBE, 0xEF), "0xBEEF");
30 const std::vector<uint8_t> a = {0xDE, 0xAD, 0xBE, 0xEF};
31 EXPECT_EQ(ToHexString(a, 8), "DE AD BE EF");
32 EXPECT_EQ(ToHexString(a, 3), "DE AD BE");
33 }
34
TEST(StringUtilsTest,TrimTest)35 TEST(StringUtilsTest, TrimTest) {
36 std::string s = "\n\tHello World \r\n";
37 std::string_view trimmed = Trim(s);
38 EXPECT_EQ(trimmed, "Hello World");
39 }
40
TEST(StringUtilsTest,SplitTest)41 TEST(StringUtilsTest, SplitTest) {
42 std::string s = "a=b=c=d==";
43 auto r = Split(s, "=");
44 EXPECT_EQ(r.size(), 4);
45 EXPECT_EQ(r[0], "a");
46 }
47
TEST(StringUtilsTest,AsStringTest)48 TEST(StringUtilsTest, AsStringTest) {
49 std::string str = "test-string";
50 std::string_view sv = str;
51 EXPECT_EQ(AsString(sv), str);
52 }
53
54 } // namespace
55 } // namespace testing
56 } // namespace stringutils
57 } // namespace netsim
58