Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
zigzag-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 <stdlib.h>
16 #include <stdio.h>
17 #include <iostream>
18 #include <limits.h>
19 #include <gtest/gtest.h>
20 #include "common/status.h"
21 #include "exec/read-write-util.h"
22 #include "util/cpu-info.h"
23 #include "util/hash-util.h"
24 
25 #include "common/names.h"
26 
27 namespace impala {
28 
29 void TestZInt(int32_t value) {
30  uint8_t buf[ReadWriteUtil::MAX_ZINT_LEN];
31  int plen = ReadWriteUtil::PutZInt(value, static_cast<uint8_t*>(buf));
32  EXPECT_TRUE(plen <= ReadWriteUtil::MAX_ZINT_LEN);
33 
34  uint8_t* buf_ptr = static_cast<uint8_t*>(buf);
35  int32_t val = ReadWriteUtil::ReadZInt(&buf_ptr);
36  EXPECT_EQ(value, val);
37  int len = buf_ptr - buf;
38  EXPECT_GT(len, 0);
39  EXPECT_LE(len, sizeof(buf));
40 }
41 
42 void TestZLong(int64_t value) {
43  uint8_t buf[ReadWriteUtil::MAX_ZLONG_LEN];
44  int plen = ReadWriteUtil::PutZLong(value, static_cast<uint8_t*>(buf));
45  EXPECT_TRUE(plen <= ReadWriteUtil::MAX_ZLONG_LEN);
46 
47  uint8_t* buf_ptr = static_cast<uint8_t*>(buf);
48  int64_t val = ReadWriteUtil::ReadZLong(&buf_ptr);
49  EXPECT_EQ(value, val);
50  int len = buf_ptr - buf;
51  EXPECT_GT(len, 0);
52  EXPECT_LE(len, sizeof(buf));
53 }
54 
55 // Test put and get of zigzag integers and longs.
56 TEST(ZigzagTest, Basic) {
57  // Test min/max of all sizes.
58  TestZInt(0);
59  TestZInt(INT_MAX);
60  TestZInt(INT_MIN);
61  TestZInt(SHRT_MIN);
62  TestZInt(SHRT_MAX);
63  TestZInt(0);
64  TestZLong(LONG_MAX);
65  TestZLong(LONG_MIN);
66  TestZLong(INT_MAX);
67  TestZLong(INT_MIN);
68  TestZLong(SHRT_MIN);
69  TestZLong(SHRT_MAX);
70  TestZLong(SCHAR_MIN);
71  TestZLong(SCHAR_MAX);
72  // Test somewhat random bit patterns.
73  int32_t value = 0xa2a2a2a2;
74  for (int i = 0; i < 1000; ++i) {
75  value = HashUtil::Hash(&value, sizeof (value), i);
76  TestZInt(value);
77  TestZLong(value);
78  TestZLong((static_cast<int64_t>(value) << 32) | value);
79  }
80 }
81 }
82 
83 int main(int argc, char **argv) {
85  ::testing::InitGoogleTest(&argc, argv);
86  return RUN_ALL_TESTS();
87 }
static const int MAX_ZINT_LEN
Maximum lengths for Zigzag encodings.
static int PutZInt(int32_t integer, uint8_t *buf)
Put a zigzag encoded integer into a buffer and return its length.
void TestZInt(int32_t value)
Definition: zigzag-test.cc:29
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
static int64_t ReadZLong(uint8_t **buf)
static uint32_t Hash(const void *data, int32_t bytes, uint32_t seed)
Definition: hash-util.h:135
void TestZLong(int64_t value)
Definition: zigzag-test.cc:42
static const int MAX_ZLONG_LEN
static int32_t ReadZInt(uint8_t **buf)
Read a zig-zag encoded int.
int main(int argc, char **argv)
Definition: zigzag-test.cc:83
static void Init()
Initialize CpuInfo.
Definition: cpu-info.cc:75
static int PutZLong(int64_t longint, uint8_t *buf)
Put a zigzag encoded long integer into a buffer and return its length.