Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
parse-util-test.cc
Go to the documentation of this file.
1 // Copyright 2014 Cloudera Inc.
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 <iostream>
16 #include <sstream>
17 #include <stdio.h>
18 #include <stdlib.h>
19 
20 #include <gtest/gtest.h>
21 
22 #include "common/init.h"
23 #include "util/mem-info.h"
24 #include "util/parse-util.h"
25 
26 #include "common/names.h"
27 
28 namespace impala {
29 
30 TEST(ParseMemSpecs, Basic) {
31  bool is_percent;
32  int64_t bytes;
33 
34  int64_t megabytes = 1024 * 1024;
35  int64_t gigabytes = 1024 * megabytes;
36 
37  bytes = ParseUtil::ParseMemSpec("1", &is_percent, MemInfo::physical_mem());
38  ASSERT_EQ(1, bytes);
39  ASSERT_FALSE(is_percent);
40 
41  bytes = ParseUtil::ParseMemSpec("100b", &is_percent, MemInfo::physical_mem());
42  ASSERT_EQ(100, bytes);
43  ASSERT_FALSE(is_percent);
44 
45  bytes = ParseUtil::ParseMemSpec("4MB", &is_percent, MemInfo::physical_mem());
46  ASSERT_EQ(4 * megabytes, bytes);
47  ASSERT_FALSE(is_percent);
48 
49  bytes = ParseUtil::ParseMemSpec("4m", &is_percent, MemInfo::physical_mem());
50  ASSERT_EQ(4 * megabytes, bytes);
51  ASSERT_FALSE(is_percent);
52 
53  bytes = ParseUtil::ParseMemSpec("8gb", &is_percent, MemInfo::physical_mem());
54  ASSERT_EQ(8 * gigabytes, bytes);
55  ASSERT_FALSE(is_percent);
56 
57  bytes = ParseUtil::ParseMemSpec("8G", &is_percent, MemInfo::physical_mem());
58  ASSERT_EQ(8 * gigabytes, bytes);
59  ASSERT_FALSE(is_percent);
60 
61  bytes = ParseUtil::ParseMemSpec("12Gb", &is_percent, MemInfo::physical_mem());
62  ASSERT_EQ(12 * gigabytes, bytes);
63  ASSERT_FALSE(is_percent);
64 
65  bytes = ParseUtil::ParseMemSpec("13%", &is_percent, MemInfo::physical_mem());
66  ASSERT_GT(bytes, 0);
67  ASSERT_TRUE(is_percent);
68 
69  ASSERT_GT(ParseUtil::ParseMemSpec("17%", &is_percent, MemInfo::physical_mem()), bytes);
70  ASSERT_EQ(ParseUtil::ParseMemSpec("17%", &is_percent, 100), 17);
71  ASSERT_TRUE(is_percent);
72 
73  vector<string> bad_values;
74  bad_values.push_back("1gib");
75  bad_values.push_back("1%b");
76  bad_values.push_back("1b%");
77  bad_values.push_back("gb");
78  bad_values.push_back("1GMb");
79  bad_values.push_back("1b1Mb");
80  bad_values.push_back("1Bb");
81  bad_values.push_back("1%%");
82  bad_values.push_back("1.1");
83  stringstream ss;
84  ss << UINT64_MAX;
85  bad_values.push_back(ss.str());
86  bad_values.push_back("%");
87  for (vector<string>::iterator it = bad_values.begin(); it != bad_values.end(); it++) {
88  bytes = ParseUtil::ParseMemSpec(*it, &is_percent, MemInfo::physical_mem());
89  ASSERT_EQ(-1, bytes);
90  }
91 
92  bytes = ParseUtil::ParseMemSpec("", &is_percent, MemInfo::physical_mem());
93  ASSERT_EQ(0, bytes);
94 
95  bytes = ParseUtil::ParseMemSpec("-1", &is_percent, MemInfo::physical_mem());
96  ASSERT_EQ(0, bytes);
97 
98  bytes = ParseUtil::ParseMemSpec("-2", &is_percent, MemInfo::physical_mem());
99  ASSERT_LT(bytes, 0);
100 
101  bytes = ParseUtil::ParseMemSpec("-2%", &is_percent, MemInfo::physical_mem());
102  ASSERT_LT(bytes, 0);
103 }
104 
105 }
106 
107 int main(int argc, char **argv) {
108  impala::InitCommonRuntime(argc, argv, false);
109  ::testing::InitGoogleTest(&argc, argv);
110  return RUN_ALL_TESTS();
111 }
static int64_t physical_mem()
Get total physical memory in bytes (ignores cgroups memory limits).
Definition: mem-info.h:36
void InitCommonRuntime(int argc, char **argv, bool init_jvm, TestInfo::Mode m=TestInfo::NON_TEST)
Definition: init.cc:122
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
static int64_t ParseMemSpec(const std::string &mem_spec_str, bool *is_percent, int64_t relative_reference)
Definition: parse-util.cc:23
int main(int argc, char **argv)