Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
string-buffer-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 
18 #include "runtime/mem-pool.h"
19 #include "runtime/mem-tracker.h"
20 #include "runtime/string-buffer.h"
21 
22 #include "common/names.h"
23 
24 namespace impala {
25 
26 void ValidateString(const string& std_str, const StringBuffer& str) {
27  EXPECT_EQ(std_str.empty(), str.Empty());
28  EXPECT_EQ((int)std_str.size(), str.Size());
29  if (std_str.size() > 0) {
30  EXPECT_EQ(strncmp(std_str.c_str(), str.str().ptr, std_str.size()), 0);
31  }
32 }
33 
34 TEST(StringBufferTest, Basic) {
36  MemPool pool(&tracker);
37  StringBuffer str(&pool);
38  string std_str;
39 
40  // Empty string
41  ValidateString(std_str, str);
42 
43  // Clear empty string
44  std_str.clear();
45  str.Clear();
46  ValidateString(std_str, str);
47 
48  // Append to empty
49  std_str.append("Hello");
50  str.Append("Hello", strlen("Hello"));
51  ValidateString(std_str, str);
52 
53  // Append some more
54  std_str.append("World");
55  str.Append("World", strlen("World"));
56  ValidateString(std_str, str);
57 
58  // Assign
59  std_str.assign("foo");
60  str.Assign("foo", strlen("foo"));
61  ValidateString(std_str, str);
62 
63  // Clear
64  std_str.clear();
65  str.Clear();
66  ValidateString(std_str, str);
67 
68  // Underlying buffer size should be the length of the max string during the test.
69  EXPECT_EQ(str.buffer_size(), strlen("HelloWorld"));
70 
71  pool.FreeAll();
72 }
73 
74 }
75 
76 int main(int argc, char **argv) {
77  ::testing::InitGoogleTest(&argc, argv);
78  return RUN_ALL_TESTS();
79 }
80 
const StringValue & str() const
Returns the underlying StringValue.
Definition: string-buffer.h:86
int Size() const
Returns the length of the current string.
Definition: string-buffer.h:81
void Append(const char *str, int len)
Append 'str' to the current string, allocating a new buffer as necessary.
Definition: string-buffer.h:44
MemTracker tracker
void Assign(const char *str, int len)
Assigns contents to StringBuffer.
Definition: string-buffer.h:59
int buffer_size() const
Returns the buffer size.
Definition: string-buffer.h:91
void Clear()
Clear the underlying StringValue. The allocated buffer can be reused.
Definition: string-buffer.h:65
bool Empty() const
Returns whether the current string is empty.
Definition: string-buffer.h:76
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
void FreeAll()
Definition: mem-pool.cc:73
ObjectPool pool
This class is thread-safe.
Definition: mem-tracker.h:61
int main(int argc, char **argv)
void ValidateString(const string &std_str, const StringBuffer &str)