souffle  2.0.2-371-g6315b36
profile_util_test.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2019, The Souffle Developers. All rights reserved.
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file profile_util_test.cpp
12  *
13  * Test cases for the profile utilities.
14  *
15  ***********************************************************************/
16 
17 #include "tests/test.h"
18 
21 #include <chrono>
22 #include <cmath>
23 #include <iosfwd>
24 #include <string>
25 #include <vector>
26 
27 using namespace souffle;
28 using namespace souffle::profile;
29 
30 TEST(StringUtil, formatNum) {
31  EXPECT_EQ("0", Tools::formatNum(0));
32 
33  EXPECT_EQ("12", Tools::formatNum(12.0));
34  EXPECT_EQ("12.45", Tools::formatNum(12.45));
35 
36  EXPECT_EQ("12", Tools::formatNum(1, 12));
37  EXPECT_EQ("12", Tools::formatNum(2, 12));
38  EXPECT_EQ("123", Tools::formatNum(2, 123));
39  EXPECT_EQ("1.23K", Tools::formatNum(3, 1234));
40 
41  EXPECT_EQ("1.2K", Tools::formatNum(2, 1200));
42  EXPECT_EQ("12K", Tools::formatNum(2, 12345));
43  EXPECT_EQ("100K", Tools::formatNum(1, 123456));
44  EXPECT_EQ("120K", Tools::formatNum(2, 123456));
45  EXPECT_EQ("123K", Tools::formatNum(3, 123456));
46  EXPECT_EQ("1.2B", Tools::formatNum(2, 1234560000));
47  EXPECT_EQ("1.23B", Tools::formatNum(3, 1234560000));
48  EXPECT_EQ("1.234B", Tools::formatNum(4, 1234560000));
49  EXPECT_EQ("1.2t", Tools::formatNum(2, 1234560000000));
50  EXPECT_EQ("1.23q", Tools::formatNum(3, 1234560000000000));
51 }
52 
53 TEST(StringUtil, formatMemory) {
54  EXPECT_EQ("0kB", Tools::formatMemory(0));
55  EXPECT_EQ("1kB", Tools::formatMemory(1));
56  EXPECT_EQ("1000kB", Tools::formatMemory(1000));
57  EXPECT_EQ("1900kB", Tools::formatMemory(1900));
58 
59  EXPECT_EQ("2MB", Tools::formatMemory(2 * 1024));
60  EXPECT_EQ("1000MB", Tools::formatMemory(1000 * 1024));
61  EXPECT_EQ("1900MB", Tools::formatMemory(1900 * 1024));
62 
63  EXPECT_EQ("2GB", Tools::formatMemory(2 * 1024 * 1024));
64  EXPECT_EQ("1000GB", Tools::formatMemory(1000 * 1024 * 1024));
65  EXPECT_EQ("1900GB", Tools::formatMemory(1900 * 1024 * 1024));
66 
67  EXPECT_EQ("2TB", Tools::formatMemory(2L * 1024 * 1024 * 1024));
68  EXPECT_EQ("1000TB", Tools::formatMemory(1000L * 1024 * 1024 * 1024));
69  EXPECT_EQ("1900TB", Tools::formatMemory(1900L * 1024 * 1024 * 1024));
70 }
71 
72 TEST(StringUtil, formatTime) {
73  std::chrono::microseconds time{0};
74  EXPECT_EQ(".000s", Tools::formatTime(time));
75  time = std::chrono::microseconds(1);
76  EXPECT_EQ(".000s", Tools::formatTime(time));
77  time = std::chrono::microseconds(1000);
78  EXPECT_EQ(".001s", Tools::formatTime(time));
79  time = std::chrono::microseconds(12340);
80  EXPECT_EQ(".012s", Tools::formatTime(time));
81  time = std::chrono::microseconds(123400);
82  EXPECT_EQ(".123s", Tools::formatTime(time));
83  time = std::chrono::microseconds(1234000);
84  EXPECT_EQ("1.23s", Tools::formatTime(time));
85  time = std::chrono::microseconds(12340000);
87 
88  time = std::chrono::microseconds(123400000);
90 
91  time = std::chrono::hours(25);
93 
94  time = std::chrono::hours(25 * 24);
96 }
97 
98 TEST(StringUtil, split) {
99  std::vector<std::string> expected{"a", "b", "c"};
100  EXPECT_EQ(expected, Tools::split("a b c", " "));
101  EXPECT_EQ(expected, Tools::split("a,b,c", ","));
102  EXPECT_EQ(expected, Tools::split("aTESTbTESTc", "TEST"));
103 
104  expected = {"", "a", "b", "c", ""};
105  EXPECT_EQ(expected, Tools::split(" a b c ", " "));
106  EXPECT_EQ(expected, Tools::split("TESTTESTaTESTbTESTcTESTTEST", "TEST"));
107 
108  expected = {"a", "b", "", "c"};
109  EXPECT_EQ(expected, Tools::split(" a b c ", " "));
110  EXPECT_EQ(expected, Tools::split("TESTaTESTbTESTTESTcTEST", "TEST"));
111 }
112 
113 TEST(StringUtil, trimWhitespace) {
114  EXPECT_EQ("a b c", Tools::trimWhitespace("\t \t a b c"));
115  EXPECT_EQ("a b c", Tools::trimWhitespace("\t \t a b c\t\t\t"));
116  EXPECT_EQ("a b c", Tools::trimWhitespace("\t \t a b c\t \t\t "));
117  EXPECT_EQ("a b c", Tools::trimWhitespace("\t \t a b c "));
118  EXPECT_EQ("a b c", Tools::trimWhitespace(" a b c "));
119 }
120 
121 TEST(StringUtil, cleanString) {
122  EXPECT_EQ(" a b c", Tools::cleanString("\t\n\ta\nb\\nc"));
123  EXPECT_EQ(" a b c", Tools::cleanString("\"\t\n\ta\nb\\nc\""));
124 }
125 
126 TEST(StringUtil, cleanJsonOutString) {
127  EXPECT_EQ("ab", Tools::cleanJsonOut("ab"));
128  EXPECT_EQ("abc", Tools::cleanJsonOut("abc"));
129 
130  EXPECT_EQ("ab", Tools::cleanJsonOut("\"ab\""));
131 
132  EXPECT_EQ("\\\\", Tools::cleanJsonOut("\\"));
133 
134  EXPECT_EQ("\\\"", Tools::cleanJsonOut("\""));
135 }
136 
137 TEST(StringUtil, cleanJsonOutDouble) {
138  EXPECT_EQ("NaN", Tools::cleanJsonOut(NAN));
139  EXPECT_EQ("1.234567e+02", Tools::cleanJsonOut(123.4567));
140 }
CellInterface.h
souffle::profile::Tools::cleanString
std::string cleanString(std::string val)
Remove and \t characters, and \t sequence of two chars, and wrapping quotes.
Definition: StringUtils.h:206
souffle::profile::Tools::trimWhitespace
std::string trimWhitespace(std::string str)
Definition: StringUtils.h:182
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: test.h:191
souffle::profile::Tools::formatMemory
std::string formatMemory(uint64_t kbytes)
Definition: StringUtils.h:97
souffle::profile::Tools::formatNum
std::string formatNum(double amount)
Definition: StringUtils.h:40
souffle::TEST
TEST(SparseArray, Basic)
Definition: brie_test.cpp:45
souffle::profile
Definition: Cell.h:20
souffle::profile::Tools::cleanJsonOut
std::string cleanJsonOut(std::string value)
escape escapes and quotes, and remove surrounding quotes
Definition: StringUtils.h:232
test.h
souffle::profile::Tools::split
std::vector< std::string > split(std::string toSplit, std::string delimiter)
split on the delimiter
Definition: StringUtils.h:162
souffle::split
std::vector< std::string > split(const std::string &s, char delim, int times=-1)
utility function to split a string
Definition: ExplainProvenance.h:146
StringUtils.h
souffle::profile::Tools::formatTime
std::string formatTime(std::chrono::microseconds number)
Definition: StringUtils.h:108
souffle::test::time
long time(const std::string &name, const Op &operation)
Definition: btree_multiset_test.cpp:411
souffle
Definition: AggregateOp.h:25