blob: ab8627fa7d2ed8a9d06037f056cc3be2968d2d13 [file] [log] [blame]
David Sehrc431b9d2018-03-02 12:01:51 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "utils.h"
Alex Light60117ae2021-02-08 17:46:15 -080018#include "stl_util.h"
David Sehrc431b9d2018-03-02 12:01:51 -080019
Roland Levillainf0409142021-03-22 15:45:03 +000020#include "gmock/gmock.h"
David Sehrc431b9d2018-03-02 12:01:51 -080021#include "gtest/gtest.h"
22
23namespace art {
24
25class UtilsTest : public testing::Test {};
26
27TEST_F(UtilsTest, PrettySize) {
David Srbeckyde6c7142019-01-09 11:27:40 +000028 EXPECT_EQ("1024MB", PrettySize(1 * GB));
29 EXPECT_EQ("2048MB", PrettySize(2 * GB));
David Sehrc431b9d2018-03-02 12:01:51 -080030 if (sizeof(size_t) > sizeof(uint32_t)) {
31 EXPECT_EQ("100GB", PrettySize(100 * GB));
32 }
33 EXPECT_EQ("1024KB", PrettySize(1 * MB));
34 EXPECT_EQ("10MB", PrettySize(10 * MB));
35 EXPECT_EQ("100MB", PrettySize(100 * MB));
36 EXPECT_EQ("1024B", PrettySize(1 * KB));
37 EXPECT_EQ("10KB", PrettySize(10 * KB));
38 EXPECT_EQ("100KB", PrettySize(100 * KB));
39 EXPECT_EQ("0B", PrettySize(0));
40 EXPECT_EQ("1B", PrettySize(1));
41 EXPECT_EQ("10B", PrettySize(10));
42 EXPECT_EQ("100B", PrettySize(100));
43 EXPECT_EQ("512B", PrettySize(512));
44}
45
Alex Light60117ae2021-02-08 17:46:15 -080046void Split(const char* arr, char s, std::vector<std::string_view>* sv) {
47 Split<std::string_view>(std::string_view(arr), s, sv);
48}
49
David Sehrc431b9d2018-03-02 12:01:51 -080050TEST_F(UtilsTest, Split) {
Alex Light60117ae2021-02-08 17:46:15 -080051 std::vector<std::string_view> actual;
52 std::vector<std::string_view> expected;
David Sehrc431b9d2018-03-02 12:01:51 -080053
54 expected.clear();
55
56 actual.clear();
57 Split("", ':', &actual);
58 EXPECT_EQ(expected, actual);
59
60 actual.clear();
61 Split(":", ':', &actual);
62 EXPECT_EQ(expected, actual);
63
64 expected.clear();
65 expected.push_back("foo");
66
67 actual.clear();
68 Split(":foo", ':', &actual);
69 EXPECT_EQ(expected, actual);
70
71 actual.clear();
72 Split("foo:", ':', &actual);
73 EXPECT_EQ(expected, actual);
74
75 actual.clear();
76 Split(":foo:", ':', &actual);
77 EXPECT_EQ(expected, actual);
78
79 expected.push_back("bar");
80
81 actual.clear();
82 Split("foo:bar", ':', &actual);
83 EXPECT_EQ(expected, actual);
84
85 actual.clear();
86 Split(":foo:bar", ':', &actual);
87 EXPECT_EQ(expected, actual);
88
89 actual.clear();
90 Split("foo:bar:", ':', &actual);
91 EXPECT_EQ(expected, actual);
92
93 actual.clear();
94 Split(":foo:bar:", ':', &actual);
95 EXPECT_EQ(expected, actual);
96
97 expected.push_back("baz");
98
99 actual.clear();
100 Split("foo:bar:baz", ':', &actual);
101 EXPECT_EQ(expected, actual);
102
103 actual.clear();
104 Split(":foo:bar:baz", ':', &actual);
105 EXPECT_EQ(expected, actual);
106
107 actual.clear();
108 Split("foo:bar:baz:", ':', &actual);
109 EXPECT_EQ(expected, actual);
110
111 actual.clear();
112 Split(":foo:bar:baz:", ':', &actual);
113 EXPECT_EQ(expected, actual);
114}
115
Wei Li8991ad02018-09-13 16:43:39 +0800116TEST_F(UtilsTest, GetProcessStatus) {
Roland Levillainf0409142021-03-22 15:45:03 +0000117 EXPECT_THAT(GetProcessStatus("Name"),
118 testing::AnyOf(
119 "art_libartbase_", // Test binary name: `art_libartbase_test`.
120 "art_standalone_")); // Test binary name: `art_standalone_libartbase_test`.
Wei Li8991ad02018-09-13 16:43:39 +0800121 EXPECT_EQ("R (running)", GetProcessStatus("State"));
122 EXPECT_EQ("<unknown>", GetProcessStatus("tate"));
123 EXPECT_EQ("<unknown>", GetProcessStatus("e"));
Vladimir Marko8ebd86e2020-07-27 12:49:24 +0000124 EXPECT_EQ("<unknown>", GetProcessStatus("InvalidFieldName"));
Wei Li8991ad02018-09-13 16:43:39 +0800125}
126
Alex Light60117ae2021-02-08 17:46:15 -0800127TEST_F(UtilsTest, StringSplit) {
128 auto range = SplitString("[ab[c[[d[e[", '[');
129 auto it = range.begin();
130 EXPECT_FALSE(it == range.end());
131 EXPECT_EQ(*it++, "");
132 EXPECT_FALSE(it == range.end());
133 EXPECT_EQ(*it++, "ab");
134 EXPECT_FALSE(it == range.end());
135 EXPECT_EQ(*it++, "c");
136 EXPECT_FALSE(it == range.end());
137 EXPECT_EQ(*it++, "");
138 EXPECT_FALSE(it == range.end());
139 EXPECT_EQ(*it++, "d");
140 EXPECT_FALSE(it == range.end());
141 EXPECT_EQ(*it++, "e");
142 EXPECT_FALSE(it == range.end());
143 EXPECT_EQ(*it++, "");
144 EXPECT_TRUE(it == range.end());
145}
146
147TEST_F(UtilsTest, StringSplit2) {
148 auto range = SplitString("ab[c[[d[e", '[');
149 auto it = range.begin();
150 EXPECT_FALSE(it == range.end());
151 EXPECT_EQ(*it++, "ab");
152 EXPECT_FALSE(it == range.end());
153 EXPECT_EQ(*it++, "c");
154 EXPECT_FALSE(it == range.end());
155 EXPECT_EQ(*it++, "");
156 EXPECT_FALSE(it == range.end());
157 EXPECT_EQ(*it++, "d");
158 EXPECT_FALSE(it == range.end());
159 EXPECT_EQ(*it++, "e");
160 EXPECT_TRUE(it == range.end());
161}
162
163TEST_F(UtilsTest, StringSplit3) {
164 auto range = SplitString("", '[');
165 auto it = range.begin();
166 EXPECT_FALSE(it == range.end());
167 EXPECT_EQ(*it++, "");
168 EXPECT_TRUE(it == range.end());
169}
170
David Sehrc431b9d2018-03-02 12:01:51 -0800171} // namespace art