File: StringUtilTest.cpp

package info (click to toggle)
abyss 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,284 kB
  • sloc: cpp: 78,182; ansic: 6,512; makefile: 2,252; perl: 672; sh: 509; haskell: 412; python: 4
file content (192 lines) | stat: -rw-r--r-- 4,640 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "Common/StringUtil.h"
#include "gtest/gtest.h"
using namespace std;

TEST(chop_test, base_case_2)
{
	string myString = "me";
	EXPECT_EQ((unsigned)2, myString.length());
	EXPECT_EQ('e', chop(myString));
	EXPECT_EQ((unsigned)1, myString.length());
}

TEST(chop_test, trivial_gt_length2)
{
	string myString = "something";
	EXPECT_EQ((unsigned)9, myString.length());
	EXPECT_EQ('g', chop(myString));
	EXPECT_EQ((unsigned)8, myString.length());
	EXPECT_EQ('n', chop(myString));
	EXPECT_EQ((unsigned)7, myString.length());
}

TEST(chomp_test, base_cases)
{
	// test .length=1
	string myString = "a";
	EXPECT_TRUE(1 == myString.length());
	EXPECT_FALSE(chomp(myString));
	string anotherString = "\n";
	EXPECT_TRUE(1 == anotherString.length());
	EXPECT_TRUE(chomp(anotherString));

	// test .length=2
	string greatString = "ab";
	EXPECT_FALSE(chomp(greatString));
	EXPECT_EQ((unsigned)2, greatString.length());

	string badString = "a\n";
	EXPECT_TRUE(chomp(badString));
	EXPECT_EQ((unsigned)1, badString.length());
}

TEST(toSI_test, all_the_cases)
{
	// negative and zero values
	EXPECT_EQ ("-0.000123 ", toSI(-0.0001234));
	EXPECT_EQ("1e-13 ", toSI(0.0000000000001));
	EXPECT_EQ("-1.2e-13 ", toSI(-0.00000000000012));
	EXPECT_EQ("-1.23e-13 ", toSI(-0.000000000000123456));
	EXPECT_EQ("0 ", toSI(0));
	EXPECT_EQ("-0 ", toSI(-0.000));

	//trivially all posible values
	EXPECT_EQ("1.23 k", toSI(1234));
	EXPECT_EQ("123 M", toSI(123440111));
	EXPECT_EQ("23.4 M", toSI(23440111));
	EXPECT_EQ("1.23 G", toSI(1234123123));
	EXPECT_EQ("123 G", toSI(123440111222));
	EXPECT_EQ("23.4 G", toSI(23440222111));
}

TEST(fromSI_test, all_the_cases)
{
	// zero values
	EXPECT_EQ(0, fromSI("0"));
	EXPECT_EQ(0, fromSI("0G"));

	// negative values
	EXPECT_EQ(-1.23e3, fromSI("-1.23k"));
	EXPECT_EQ(-9.06e6, fromSI("-9.06M"));
	EXPECT_EQ(-1.234e9, fromSI("-1.234G"));

	// positive values
	EXPECT_EQ(1.23e3, fromSI("1.23k"));
	EXPECT_EQ(9.06e6, fromSI("9.06M"));
	EXPECT_EQ(1.234e9, fromSI("1.234G"));
}

TEST(bytesToSI_test, all_the_cases)
{
	// zero values
	EXPECT_EQ("0", bytesToSI(0));

	// unit conversion
	EXPECT_EQ("1", bytesToSI(1));
	EXPECT_EQ("1k", bytesToSI(1024));
	EXPECT_EQ("1M", bytesToSI(1048576UL));
	EXPECT_EQ("1G", bytesToSI(1073741824UL));
}

template <typename T>
class MultiTypes{
 public:
 	T myVar;
	void type_int() { ::testing::StaticAssertTypeEq<int, T>(); }
	void type_double() { ::testing::StaticAssertTypeEq<double, T>(); }
	void type_string() { ::testing::StaticAssertTypeEq<string, T>(); }
};

TEST(toEng_test, integer_cases)
{
	EXPECT_EQ("1234", toEng(1234));

	MultiTypes<int> temp;
	temp.type_int();
	temp.myVar = 1234;
	EXPECT_EQ(temp.myVar, 1234);
	EXPECT_EQ("1234", toEng(temp.myVar));
	temp.myVar = 12345678;

	EXPECT_EQ("12.35e6", toEng(temp.myVar));
	temp.myVar = 123456789;
	EXPECT_EQ ("123.5e6" , toEng(temp.myVar));
}

TEST(toEng_test, double_cases)
{
	MultiTypes<double> temp;
	temp.type_double();

	temp.myVar = 123.456;
	EXPECT_EQ (temp.myVar, 123.456);
	EXPECT_EQ ("123.5", toEng(temp.myVar));
	temp.myVar = 123456789.9;
	EXPECT_EQ ("123.5e6", toEng(temp.myVar));
}

TEST(toEng_test, string_cases)
{
	MultiTypes<string> temp;
	temp.type_string();
	temp.myVar = "123.456";
}

TEST (startsWith_test, trivial_cases)
{
	EXPECT_TRUE (startsWith("hello", "hell"));
	EXPECT_TRUE (startsWith("hello", "h"));
	EXPECT_FALSE (startsWith("hello", "hello"));
	EXPECT_TRUE (startsWith("hello", ""));
	EXPECT_FALSE (startsWith ("whatever", "who"));
}

TEST(endsWith_test, any_cases)
{
	// suffix should not be the string itself
	EXPECT_FALSE(endsWith("hello", "hello"));
	EXPECT_FALSE(endsWith("", ""));
	EXPECT_TRUE(endsWith("hello", ""));

	// EXPECT_FALSE(endsWith("hello", NULL));
	// NULL is not valid
	EXPECT_TRUE(endsWith("hello", "ello"));
	EXPECT_TRUE(endsWith("hello", "o"));
	EXPECT_FALSE(endsWith("hell", "hello"));
	EXPECT_FALSE(endsWith("hell", "heaven"));
}

TEST(SIToBytes_test, unit_conversions)
{
	EXPECT_EQ(1024u, SIToBytes("1024"));
	EXPECT_EQ(1024u, SIToBytes("1k"));
	EXPECT_EQ(1536u, SIToBytes("1.5k"));
	EXPECT_EQ(1048576u, SIToBytes("1M"));
	EXPECT_EQ(1073741824u, SIToBytes("1G"));
}

TEST(SIToBytes_test, error_handling)
{
	unsigned long long bytes;

	// non-number

	istringstream nonNumber("not-a-number");
	bytes = SIToBytes(nonNumber);
	EXPECT_EQ(0u, bytes);
	EXPECT_TRUE(nonNumber.fail());

	// unrecognized suffix

	istringstream illegalUnits("1024y");
	bytes = SIToBytes(nonNumber);
	EXPECT_EQ(0u, bytes);
	EXPECT_TRUE(nonNumber.fail());

	// valid string should set eof

	istringstream valid("500M");
	bytes = SIToBytes(valid);
	EXPECT_EQ(524288000u, bytes);
	EXPECT_TRUE(valid.eof());
}