1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010-2020 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef UT_MISC_COMMON
18 #define UT_MISC_COMMON
21 #include <nel/misc/common.h>
23 struct CUTMiscCommon
: public Test::Suite
27 TEST_ADD(CUTMiscCommon::bytesToHumanReadableUnits
);
28 TEST_ADD(CUTMiscCommon::humanReadableToBytes
);
29 TEST_ADD(CUTMiscCommon::encodeURIComponent
);
30 TEST_ADD(CUTMiscCommon::decodeURIComponent
);
32 // Add a line here when adding a new test METHOD
35 void bytesToHumanReadableUnits()
37 std::vector
<std::string
> units
;
41 // no unit, returns an empty string
42 res
= NLMISC::bytesToHumanReadableUnits(0, units
);
43 TEST_ASSERT(res
.empty());
49 res
= NLMISC::bytesToHumanReadableUnits(0, units
);
50 TEST_ASSERT(res
== "0 B");
53 res
= NLMISC::bytesToHumanReadableUnits(1000, units
);
54 TEST_ASSERT(res
== "1000 B");
57 res
= NLMISC::bytesToHumanReadableUnits(1024, units
);
58 TEST_ASSERT(res
== "1024 B");
61 units
.push_back("KiB");
63 // 1000 bytes in B or KiB
64 res
= NLMISC::bytesToHumanReadableUnits(1000, units
);
65 TEST_ASSERT(res
== "1000 B");
67 // 1024 bytes in B or KiB
68 res
= NLMISC::bytesToHumanReadableUnits(1024, units
);
69 TEST_ASSERT(res
== "1024 B");
72 res
= NLMISC::bytesToHumanReadableUnits(1000 * 1000, units
);
73 TEST_ASSERT(res
== "976 KiB");
76 res
= NLMISC::bytesToHumanReadableUnits(1024 * 1024, units
);
77 TEST_ASSERT(res
== "1024 KiB");
80 res
= NLMISC::bytesToHumanReadableUnits(1000 * 1000 * 1000, units
);
81 TEST_ASSERT(res
== "976562 KiB");
84 res
= NLMISC::bytesToHumanReadableUnits(1024 * 1024 * 1024, units
);
85 TEST_ASSERT(res
== "1048576 KiB");
88 units
.push_back("MiB");
90 // 1 GB in B, KiB or MiB
91 res
= NLMISC::bytesToHumanReadableUnits(1000 * 1000 * 1000, units
);
92 TEST_ASSERT(res
== "953 MiB");
94 // 1 GiB in B, KiB or MiB
95 res
= NLMISC::bytesToHumanReadableUnits(1024 * 1024 * 1024, units
);
96 TEST_ASSERT(res
== "1024 MiB");
99 void humanReadableToBytes()
103 // kiB is a wrong unit
104 bytes
= NLMISC::humanReadableToBytes("1kiB");
105 TEST_ASSERT(bytes
== 1);
108 bytes
= NLMISC::humanReadableToBytes("1KiB");
109 TEST_ASSERT(bytes
== 1024);
112 bytes
= NLMISC::humanReadableToBytes("1MiB");
113 TEST_ASSERT(bytes
== 1024*1024);
116 bytes
= NLMISC::humanReadableToBytes("1KB");
117 TEST_ASSERT(bytes
== 1000);
120 bytes
= NLMISC::humanReadableToBytes("1kB");
121 TEST_ASSERT(bytes
== 1000);
124 bytes
= NLMISC::humanReadableToBytes("1MB");
125 TEST_ASSERT(bytes
== 1000*1000);
128 bytes
= NLMISC::humanReadableToBytes("1B");
129 TEST_ASSERT(bytes
== 1);
132 bytes
= NLMISC::humanReadableToBytes("1");
133 TEST_ASSERT(bytes
== 1);
135 // kiB is a wrong unit
136 bytes
= NLMISC::humanReadableToBytes("1 kiB");
137 TEST_ASSERT(bytes
== 1);
140 bytes
= NLMISC::humanReadableToBytes("1 KiB");
141 TEST_ASSERT(bytes
== 1024);
144 bytes
= NLMISC::humanReadableToBytes("1 MiB");
145 TEST_ASSERT(bytes
== 1024*1024);
148 bytes
= NLMISC::humanReadableToBytes("1 KB");
149 TEST_ASSERT(bytes
== 1000);
152 bytes
= NLMISC::humanReadableToBytes("1 kB");
153 TEST_ASSERT(bytes
== 1000);
156 bytes
= NLMISC::humanReadableToBytes("1 MB");
157 TEST_ASSERT(bytes
== 1000*1000);
160 bytes
= NLMISC::humanReadableToBytes("1 B");
161 TEST_ASSERT(bytes
== 1);
164 bytes
= NLMISC::humanReadableToBytes("AB");
165 TEST_ASSERT(bytes
== 0);
167 // not a positive number
168 bytes
= NLMISC::humanReadableToBytes("-1 B");
169 TEST_ASSERT(bytes
== 0);
172 void encodeURIComponent()
174 TEST_ASSERT("%00" == NLMISC::encodeURIComponent(std::string("\x00", 1)));
175 TEST_ASSERT("%0A" == NLMISC::encodeURIComponent(std::string("\x0A", 1)));
176 TEST_ASSERT("%A0" == NLMISC::encodeURIComponent(std::string("\xA0", 1)));
177 TEST_ASSERT("a%20b" == NLMISC::encodeURIComponent("a b"));
178 TEST_ASSERT("a%2Bb" == NLMISC::encodeURIComponent("a+b"));
181 void decodeURIComponent()
183 TEST_ASSERT(std::string("\x00", 1) == NLMISC::decodeURIComponent(std::string("\x00", 1)));
184 TEST_ASSERT(std::string("\x0A", 1) == NLMISC::decodeURIComponent(std::string("\x0A", 1)));
185 TEST_ASSERT(std::string("\xA0", 1) == NLMISC::decodeURIComponent(std::string("\xA0", 1)));
186 TEST_ASSERT("a b" == NLMISC::decodeURIComponent("a%20b"));
187 TEST_ASSERT("a+b" == NLMISC::decodeURIComponent("a%2Bb"));
189 TEST_ASSERT("a%A" == NLMISC::decodeURIComponent("a%A"));
190 TEST_ASSERT("a%AX" == NLMISC::decodeURIComponent("a%AX"));