Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
pretty-printer-test.cc
Go to the documentation of this file.
1 // Copyright 2015 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 <gtest/gtest.h>
19 
20 #include "util/pretty-printer.h"
21 
22 #include "common/names.h"
23 
24 namespace impala {
25 
26 TEST(PrettyPrinterTest, Unit) {
27  // Note no restriction to 2 decimal places with UNIT < 1000.
28  EXPECT_EQ(PrettyPrinter::Print(1.2345678f, TUnit::UNIT), "1.234568");
29  EXPECT_EQ(PrettyPrinter::Print(1, TUnit::UNIT), "1");
30  EXPECT_EQ(PrettyPrinter::Print("string", TUnit::UNIT), "string");
31 
32  EXPECT_EQ(PrettyPrinter::Print(1000, TUnit::UNIT), "1.00K");
33  EXPECT_EQ(PrettyPrinter::Print(1000 * 1000, TUnit::UNIT), "1.00M");
34  EXPECT_EQ(PrettyPrinter::Print(1000 * 1000 * 1000, TUnit::UNIT), "1.00B");
35 }
36 
37 TEST(PrettyPrinterTest, UnitPerSecond) {
38  EXPECT_EQ(PrettyPrinter::Print(1.2345678f, TUnit::UNIT_PER_SECOND), "1.23 /sec");
39  EXPECT_EQ(PrettyPrinter::Print(1, TUnit::UNIT_PER_SECOND), "1.00 /sec");
40  EXPECT_EQ(PrettyPrinter::Print("string", TUnit::UNIT_PER_SECOND), "string");
41 
42  EXPECT_EQ(PrettyPrinter::Print(1000, TUnit::UNIT_PER_SECOND), "1.00 K/sec");
43  EXPECT_EQ(PrettyPrinter::Print(1000 * 1000, TUnit::UNIT_PER_SECOND),
44  "1.00 M/sec");
45  EXPECT_EQ(PrettyPrinter::Print(1000 * 1000 * 1000, TUnit::UNIT_PER_SECOND),
46  "1.00 B/sec");
47 }
48 
49 TEST(PrettyPrinterTest, CpuTicks) {
50  EXPECT_EQ(PrettyPrinter::Print(1000, TUnit::CPU_TICKS), "1.00K clock cycles");
51  EXPECT_EQ(PrettyPrinter::Print(1500, TUnit::CPU_TICKS), "1.50K clock cycles");
52  EXPECT_EQ(PrettyPrinter::Print(1000.1, TUnit::CPU_TICKS), "1.00K clock cycles");
53 }
54 
55 TEST(PrettyPrinterTest, Bytes) {
56  EXPECT_EQ(PrettyPrinter::Print(1.2345678f, TUnit::BYTES), "1.23 B");
57  EXPECT_EQ(PrettyPrinter::Print(1, TUnit::BYTES), "1.00 B");
58 
59  EXPECT_EQ(PrettyPrinter::Print(1024, TUnit::BYTES), "1.00 KB");
60  EXPECT_EQ(PrettyPrinter::Print(1024 * 1024, TUnit::BYTES), "1.00 MB");
61  EXPECT_EQ(PrettyPrinter::Print(1024 * 1024 * 1024, TUnit::BYTES), "1.00 GB");
62 
63  EXPECT_EQ(PrettyPrinter::Print(1536, TUnit::BYTES), "1.50 KB");
64  EXPECT_EQ(PrettyPrinter::Print(1536.0, TUnit::BYTES), "1.50 KB");
65 
66  // Units are ignored for strings
67  EXPECT_EQ(PrettyPrinter::Print("string", TUnit::BYTES), "string");
68 }
69 
70 TEST(PrettyPrinterTest, BytesPerSecond) {
71  EXPECT_EQ(PrettyPrinter::Print(1.2345678f, TUnit::BYTES_PER_SECOND),
72  "1.23 B/sec");
73  EXPECT_EQ(PrettyPrinter::Print(1, TUnit::BYTES_PER_SECOND), "1.00 B/sec");
74 
75  EXPECT_EQ(PrettyPrinter::Print(1024, TUnit::BYTES_PER_SECOND), "1.00 KB/sec");
76  EXPECT_EQ(PrettyPrinter::Print(1024 * 1024, TUnit::BYTES_PER_SECOND),
77  "1.00 MB/sec");
78  EXPECT_EQ(PrettyPrinter::Print(1024 * 1024 * 1024, TUnit::BYTES_PER_SECOND),
79  "1.00 GB/sec");
80 
81  EXPECT_EQ(PrettyPrinter::Print(1536, TUnit::BYTES_PER_SECOND), "1.50 KB/sec");
82  EXPECT_EQ(PrettyPrinter::Print(1536.0, TUnit::BYTES_PER_SECOND), "1.50 KB/sec");
83 }
84 
85 TEST(PrettyPrinterTest, Seconds) {
86  EXPECT_EQ(PrettyPrinter::Print(1.5, TUnit::TIME_S), "1s500ms");
87 
88  EXPECT_EQ(PrettyPrinter::Print(60, TUnit::TIME_S), "1m");
89  EXPECT_EQ(PrettyPrinter::Print(60 * 60, TUnit::TIME_S), "1h");
90  EXPECT_EQ(PrettyPrinter::Print(60 * 60 * 24, TUnit::TIME_S), "24h");
91 
92  EXPECT_EQ(PrettyPrinter::Print(1000, TUnit::TIME_S), "16m40s");
93 
94  // Only three decimal places of precision are supported for seconds.
95  // TODO: Fix buggy rounding behaviour. IMPALA-1648
96  EXPECT_EQ(PrettyPrinter::Print(1.5009, TUnit::TIME_S), "1s500ms");
97 }
98 
99 TEST(PrettyPrinterTest, NanoSeconds) {
100  EXPECT_EQ(PrettyPrinter::Print(1, TUnit::TIME_NS), "1ns");
101  EXPECT_EQ(PrettyPrinter::Print(1.5009, TUnit::TIME_NS), "1.500900ns");
102  EXPECT_EQ(PrettyPrinter::Print(1000 * 1000 * 1000, TUnit::TIME_NS), "1s000ms");
103 }
104 
105 TEST(PrettyPrinterTest, DoubleValue) {
106  EXPECT_EQ(PrettyPrinter::Print(1.0, TUnit::DOUBLE_VALUE), "1.00 ");
107 }
108 
109 TEST(PrettyPrinterTest, StringList) {
110  vector<int> vals;
111  stringstream ss1;
112  PrettyPrinter::PrintStringList(vals, TUnit::UNIT, &ss1);
113  EXPECT_EQ(ss1.str(), "[]");
114 
115  vals.push_back(1);
116  vals.push_back(1000);
117  vals.push_back(1000 * 1000);
118  stringstream ss2;
119  PrettyPrinter::PrintStringList(vals, TUnit::UNIT, &ss2);
120  EXPECT_EQ(ss2.str(), "[1, 1.00K, 1.00M]");
121 }
122 
123 }
124 
125 int main(int argc, char **argv) {
126  google::InitGoogleLogging(argv[0]);
127  ::testing::InitGoogleTest(&argc, argv);
129  return RUN_ALL_TESTS();
130 }
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
static std::string Print(bool value, TUnit::type ignored, bool verbose=false)
int main(int argc, char **argv)
static void Init()
Initialize CpuInfo.
Definition: cpu-info.cc:75