Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
string-value-test.cc
Go to the documentation of this file.
1 // Copyright 2012 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 <string>
16 #include <gtest/gtest.h>
17 
19 #include "util/cpu-info.h"
20 
21 #include "common/names.h"
22 
23 namespace impala {
24 
25 StringValue FromStdString(const string& str) {
26  char* ptr = const_cast<char*>(str.c_str());
27  int len = str.size();
28  return StringValue(ptr, len);
29 }
30 
31 TEST(StringValueTest, TestCompare) {
32  string empty_str = "";
33  string str1_str("\0", 1);
34  string str2_str("\0xy", 3);
35  string str3_str = "abc";
36  string str4_str("abc\0def", 7);
37  string str5_str = "abcdef";
38  string str6_str = "xyz";
39  string str7_str("xyz\0", 4);
40  // Include a few long strings so we test the SSE path
41  string str8_str("yyyyyyyyyyyyyyyy\0yyyyyyyyyyyyyyyyyy", 35);
42  string str9_str("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", 34);
43  string char0_str("hi", 2);
44  string char1_str("hi ", 4);
45  string char2_str(" hi ", 5);
46  string char3_str("12345", 5);
47  string char4_str(" ", 1);
48  string char5_str("", 0);
49 
50  const int NUM_STRINGS = 10;
51  const int NUM_CHARS = 6;
52 
53  // Must be in lexical order
55  svs[0] = FromStdString(empty_str);
56  svs[1] = FromStdString(str1_str);
57  svs[2] = FromStdString(str2_str);
58  svs[3] = FromStdString(str3_str);
59  svs[4] = FromStdString(str4_str);
60  svs[5] = FromStdString(str5_str);
61  svs[6] = FromStdString(str6_str);
62  svs[7] = FromStdString(str7_str);
63  svs[8] = FromStdString(str8_str);
64  svs[9] = FromStdString(str9_str);
65 
66  for (int i = 0; i < NUM_STRINGS; ++i) {
67  for (int j = 0; j < NUM_STRINGS; ++j) {
68  if (i == j) {
69  // Same string
70  EXPECT_TRUE(svs[i].Eq(svs[j])) << "i=" << i << " j=" << j;
71  EXPECT_FALSE(svs[i].Ne(svs[j])) << "i=" << i << " j=" << j;
72  EXPECT_FALSE(svs[i].Lt(svs[j])) << "i=" << i << " j=" << j;
73  EXPECT_FALSE(svs[i].Gt(svs[j])) << "i=" << i << " j=" << j;
74  EXPECT_TRUE(svs[i].Le(svs[j])) << "i=" << i << " j=" << j;
75  EXPECT_TRUE(svs[i].Ge(svs[j])) << "i=" << i << " j=" << j;
76  EXPECT_TRUE(svs[i].Compare(svs[j]) == 0) << "i=" << i << " j=" << j;
77  } else if (i < j) {
78  // svs[i] < svs[j]
79  EXPECT_FALSE(svs[i].Eq(svs[j])) << "i=" << i << " j=" << j;
80  EXPECT_TRUE(svs[i].Ne(svs[j])) << "i=" << i << " j=" << j;
81  EXPECT_TRUE(svs[i].Lt(svs[j])) << "i=" << i << " j=" << j;
82  EXPECT_FALSE(svs[i].Gt(svs[j])) << "i=" << i << " j=" << j;
83  EXPECT_TRUE(svs[i].Le(svs[j])) << "i=" << i << " j=" << j;
84  EXPECT_FALSE(svs[i].Gt(svs[j])) << "i=" << i << " j=" << j;
85  EXPECT_TRUE(svs[i].Compare(svs[j]) < 0) << "i=" << i << " j=" << j;
86  } else {
87  // svs[i] > svs[j]
88  EXPECT_FALSE(svs[i].Eq(svs[j])) << "i=" << i << " j=" << j;
89  EXPECT_TRUE(svs[i].Ne(svs[j])) << "i=" << i << " j=" << j;
90  EXPECT_FALSE(svs[i].Lt(svs[j])) << "i=" << i << " j=" << j;
91  EXPECT_TRUE(svs[i].Gt(svs[j])) << "i=" << i << " j=" << j;
92  EXPECT_FALSE(svs[i].Le(svs[j])) << "i=" << i << " j=" << j;
93  EXPECT_TRUE(svs[i].Gt(svs[j])) << "i=" << i << " j=" << j;
94  EXPECT_TRUE(svs[i].Compare(svs[j]) > 0) << "i=" << i << " j=" << j;
95  }
96  }
97  }
98 
99  StringValue chars[NUM_CHARS];
100  chars[0] = FromStdString(char0_str);
101  chars[1] = FromStdString(char1_str);
102  chars[2] = FromStdString(char2_str);
103  chars[3] = FromStdString(char3_str);
104  chars[4] = FromStdString(char4_str);
105  chars[5] = FromStdString(char5_str);
106 
107  EXPECT_EQ(StringValue::UnpaddedCharLength(chars[0].ptr, 2), 2);
108  EXPECT_EQ(StringValue::UnpaddedCharLength(chars[1].ptr, 4), 2);
109  EXPECT_EQ(StringValue::UnpaddedCharLength(chars[2].ptr, 5), 3);
110  EXPECT_EQ(StringValue::UnpaddedCharLength(chars[3].ptr, 5), 5);
111  EXPECT_EQ(StringValue::UnpaddedCharLength(chars[4].ptr, 1), 0);
112  EXPECT_EQ(StringValue::UnpaddedCharLength(chars[5].ptr, 0), 0);
113 
114  StringValue::PadWithSpaces(chars[3].ptr, 5, 4);
115  EXPECT_EQ(chars[3].ptr[4], ' ');
116  EXPECT_EQ(chars[3].ptr[3], '4');
117 }
118 
119 }
120 
121 int main(int argc, char **argv) {
122  ::testing::InitGoogleTest(&argc, argv);
124  return RUN_ALL_TESTS();
125 }
126 
StringValue FromStdString(const string &str)
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
static void PadWithSpaces(char *cptr, int64_t cptr_len, int64_t num_chars)
static const int NUM_STRINGS
static int64_t UnpaddedCharLength(const char *cptr, int64_t len)
Returns number of characters in a char array (ignores trailing spaces)
int main(int argc, char **argv)
static void Init()
Initialize CpuInfo.
Definition: cpu-info.cc:75