Update checkRpItemsPosition and getTeam
[ryzomcore.git] / nel / tools / nel_unit_test / ut_misc_common.h
blobb21fa22f36d3520e69e34707151e46f30f1009f5
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010-2020 Winch Gate Property Limited
3 //
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.
8 //
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
20 #include <limits>
21 #include <nel/misc/common.h>
23 struct CUTMiscCommon : public Test::Suite
25 CUTMiscCommon()
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;
39 std::string res;
41 // no unit, returns an empty string
42 res = NLMISC::bytesToHumanReadableUnits(0, units);
43 TEST_ASSERT(res.empty());
45 // support bytes
46 units.push_back("B");
48 // 0 bytes
49 res = NLMISC::bytesToHumanReadableUnits(0, units);
50 TEST_ASSERT(res == "0 B");
52 // 1000 bytes in B
53 res = NLMISC::bytesToHumanReadableUnits(1000, units);
54 TEST_ASSERT(res == "1000 B");
56 // 1024 bytes in B
57 res = NLMISC::bytesToHumanReadableUnits(1024, units);
58 TEST_ASSERT(res == "1024 B");
60 // support kibibytes
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");
71 // 1 MB in B or KiB
72 res = NLMISC::bytesToHumanReadableUnits(1000 * 1000, units);
73 TEST_ASSERT(res == "976 KiB");
75 // 1 MiB in B or KiB
76 res = NLMISC::bytesToHumanReadableUnits(1024 * 1024, units);
77 TEST_ASSERT(res == "1024 KiB");
79 // 1 GB in B or KiB
80 res = NLMISC::bytesToHumanReadableUnits(1000 * 1000 * 1000, units);
81 TEST_ASSERT(res == "976562 KiB");
83 // 1 GiB in B or KiB
84 res = NLMISC::bytesToHumanReadableUnits(1024 * 1024 * 1024, units);
85 TEST_ASSERT(res == "1048576 KiB");
87 // support mebibytes
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()
101 uint64 bytes = 0;
103 // kiB is a wrong unit
104 bytes = NLMISC::humanReadableToBytes("1kiB");
105 TEST_ASSERT(bytes == 1);
107 // 1 kibibyte
108 bytes = NLMISC::humanReadableToBytes("1KiB");
109 TEST_ASSERT(bytes == 1024);
111 // 1 mebibyte
112 bytes = NLMISC::humanReadableToBytes("1MiB");
113 TEST_ASSERT(bytes == 1024*1024);
115 // 1 kilobyte
116 bytes = NLMISC::humanReadableToBytes("1KB");
117 TEST_ASSERT(bytes == 1000);
119 // 1 kilobyte
120 bytes = NLMISC::humanReadableToBytes("1kB");
121 TEST_ASSERT(bytes == 1000);
123 // 1 megabyte
124 bytes = NLMISC::humanReadableToBytes("1MB");
125 TEST_ASSERT(bytes == 1000*1000);
127 // 1 byte
128 bytes = NLMISC::humanReadableToBytes("1B");
129 TEST_ASSERT(bytes == 1);
131 // 1 byte
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);
139 // 1 kibibyte
140 bytes = NLMISC::humanReadableToBytes("1 KiB");
141 TEST_ASSERT(bytes == 1024);
143 // 1 mebibyte
144 bytes = NLMISC::humanReadableToBytes("1 MiB");
145 TEST_ASSERT(bytes == 1024*1024);
147 // 1 kilobyte
148 bytes = NLMISC::humanReadableToBytes("1 KB");
149 TEST_ASSERT(bytes == 1000);
151 // 1 kilobyte
152 bytes = NLMISC::humanReadableToBytes("1 kB");
153 TEST_ASSERT(bytes == 1000);
155 // 1 megabyte
156 bytes = NLMISC::humanReadableToBytes("1 MB");
157 TEST_ASSERT(bytes == 1000*1000);
159 // 1 byte
160 bytes = NLMISC::humanReadableToBytes("1 B");
161 TEST_ASSERT(bytes == 1);
163 // not a number
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"));
194 #endif