Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hs2-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 "common/init.h"
16 
17 #include <string>
18 #include <gtest/gtest.h>
19 
20 #include "service/hs2-util.h"
21 
22 #include "common/names.h"
23 
24 using namespace impala;
25 
26 // Test that a single byte can be stitched to an empty string at all offsets.
27 TEST(StitchNullsTest, OneByteStitch) {
28  string from;
29  from += 0xF;
30  for (int i = 0; i < 8; ++i) {
31  string to;
32  StitchNulls(0, 8 - i, i, from, &to);
33  ASSERT_TRUE(to.size() == 1);
34  ASSERT_EQ(to[0], 0xF >> i);
35  }
36 }
37 
38 // Test that a bit string of more than one byte is stitched in correctly.
39 TEST(StitchNullsTest, MultiByteStitch) {
40  string from;
41  from += 0xFF;
42  from += 0xFF;
43  for (int i = 0; i < 16; ++i) {
44  string to;
45  // Stitch any from 1-16 bits, starting at the i'th bit in from.
46  StitchNulls(0, 16 - i, i, from, &to);
47  if (i < 8) {
48  // For more than 8 bits added, the result should be two bytes long.
49  ASSERT_EQ(to.size(), 2);
50  ASSERT_EQ(to[0], (char)0xFF);
51  ASSERT_EQ(to[1], (char)(0xFF >> i));
52  } else {
53  // For the first 8 bits, the result should be less than one byte long.
54  ASSERT_EQ(to.size(), 1);
55  ASSERT_EQ(to[0], (char)(0xFF >> (i - 8)));
56  }
57  }
58 }
59 
60 // Test stitching two bitstrings whose combined length is still less than a byte.
61 TEST(StitchNullsTest, StitchOverlapping) {
62  string from;
63  from += 0x1;
64  for (int i = 1; i < 9; ++i) {
65  string to;
66  to += 0x1;
67  // Result's first byte should always be 1100 0000 (LSB->MSB). Once i is larger than 7,
68  // an extra byte will be needed.
69  StitchNulls(1, i, 0, from, &to);
70  ASSERT_EQ(to[0], 0x3);
71  if (i < 8) {
72  ASSERT_EQ(to.size(), 1);
73  } else {
74  ASSERT_EQ(to.size(), 2);
75  ASSERT_EQ(to[1], 0x0);
76  }
77  }
78 }
79 
80 // Test stitching in a multi-byte bit string with an offset; i.e. not starting at the 0'th
81 // bit.
82 TEST(StitchNullsTest, StitchWithOffset) {
83  string from;
84  from += 0x1;
85  from += 0x2;
86  from += 0x4;
87  from += 0x8;
88 
89  for (int i = 0; i < 4; ++i) {
90  string to;
91  to += 0x1;
92  StitchNulls(8, 8, 8 * i, from, &to);
93  ASSERT_EQ(to.size(), 2);
94  ASSERT_EQ(to[1], from[i]);
95  }
96 
97  for (int i = 0; i < 4; ++i) {
98  string to;
99  to += 0x1;
100  // Add one bit, starting at the least-significant set-bit in the i'th byte of
101  // 'from'. The effect is to always append exactly one bit.
102  StitchNulls(1, 1, (8 * i) + i, from, &to);
103  ASSERT_EQ(to.size(), 1);
104  // Result is always 1100 0000 (LSB->MSB)
105  ASSERT_EQ(to[0], 0x3);
106  }
107 }
108 
109 int main(int argc, char** argv) {
110  ::testing::InitGoogleTest(&argc, argv);
111  return RUN_ALL_TESTS();
112 }
void StitchNulls(uint32_t num_rows_before, uint32_t num_rows_added, uint32_t start_idx, const std::string &from, std::string *to)
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
int main(int argc, char **argv)