genesis 1 #include <vector>
genesis 2 #include <boost/test/unit_test.hpp>
genesis 3 #include <boost/foreach.hpp>
genesis 4
genesis 5 #include "../util.h"
genesis 6
genesis 7 using namespace std;
genesis 8
genesis 9 BOOST_AUTO_TEST_SUITE(util_tests)
genesis 10
genesis 11 BOOST_AUTO_TEST_CASE(util_criticalsection)
genesis 12 {
genesis 13 CCriticalSection cs;
genesis 14
genesis 15 do {
genesis 16 CRITICAL_BLOCK(cs)
genesis 17 break;
genesis 18
genesis 19 BOOST_ERROR("break was swallowed!");
genesis 20 } while(0);
genesis 21
genesis 22 do {
genesis 23 TRY_CRITICAL_BLOCK(cs)
genesis 24 break;
genesis 25
genesis 26 BOOST_ERROR("break was swallowed!");
genesis 27 } while(0);
genesis 28 }
genesis 29
genesis 30 BOOST_AUTO_TEST_CASE(util_MedianFilter)
genesis 31 {
genesis 32 CMedianFilter<int> filter(5, 15);
genesis 33
genesis 34 BOOST_CHECK_EQUAL(filter.median(), 15);
genesis 35
genesis 36 filter.input(20);
genesis 37 BOOST_CHECK_EQUAL(filter.median(), 17);
genesis 38
genesis 39 filter.input(30);
genesis 40 BOOST_CHECK_EQUAL(filter.median(), 20);
genesis 41
genesis 42 filter.input(3);
genesis 43 BOOST_CHECK_EQUAL(filter.median(), 17);
genesis 44
genesis 45 filter.input(7);
genesis 46 BOOST_CHECK_EQUAL(filter.median(), 15);
genesis 47
genesis 48 filter.input(18);
genesis 49 BOOST_CHECK_EQUAL(filter.median(), 18);
genesis 50
genesis 51 filter.input(0);
genesis 52 BOOST_CHECK_EQUAL(filter.median(), 7);
genesis 53 }
genesis 54
genesis 55 static const unsigned char ParseHex_expected[65] = {
genesis 56 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
genesis 57 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
genesis 58 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
genesis 59 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
genesis 60 0x5f
genesis 61 };
genesis 62 BOOST_AUTO_TEST_CASE(util_ParseHex)
genesis 63 {
genesis 64 std::vector<unsigned char> result;
genesis 65 std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
genesis 66
genesis 67 result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
genesis 68 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
genesis 69
genesis 70
genesis 71 result = ParseHex("12 34 56 78");
genesis 72 BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
genesis 73
genesis 74
genesis 75 result = ParseHex("1234 invalid 1234");
genesis 76 BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
genesis 77 }
genesis 78
genesis 79 BOOST_AUTO_TEST_CASE(util_HexStr)
genesis 80 {
genesis 81 BOOST_CHECK_EQUAL(
genesis 82 HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
genesis 83 "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
genesis 84
genesis 85 BOOST_CHECK_EQUAL(
genesis 86 HexStr(ParseHex_expected, ParseHex_expected + 5, true),
genesis 87 "04 67 8a fd b0");
genesis 88 }
genesis 89
genesis 90 BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)
genesis 91 {
genesis 92 BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 0), "01/01/70 00:00:00");
genesis 93 BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 0x7FFFFFFF), "01/19/38 03:14:07");
genesis 94
genesis 95 BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 1317425777), "09/30/11 23:36:17");
genesis 96 BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M", 1317425777), "09/30/11 23:36");
genesis 97 }
genesis 98
genesis 99 BOOST_AUTO_TEST_CASE(util_ParseParameters)
genesis 100 {
genesis 101 const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
genesis 102
genesis 103 ParseParameters(0, (char**)argv_test);
genesis 104 BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
genesis 105
genesis 106 ParseParameters(1, (char**)argv_test);
genesis 107 BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
genesis 108
genesis 109 ParseParameters(5, (char**)argv_test);
genesis 110
genesis 111
genesis 112
genesis 113 BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3);
genesis 114 BOOST_CHECK(mapArgs.count("-a") && mapArgs.count("-b") && mapArgs.count("-ccc")
genesis 115 && !mapArgs.count("f") && !mapArgs.count("-d"));
genesis 116 BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc")
genesis 117 && !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));
genesis 118
genesis 119 BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple");
genesis 120 BOOST_CHECK(mapMultiArgs["-ccc"].size() == 2);
genesis 121 }
genesis 122
genesis 123 BOOST_AUTO_TEST_CASE(util_GetArg)
genesis 124 {
genesis 125 mapArgs.clear();
genesis 126 mapArgs["strtest1"] = "string...";
genesis 127
genesis 128 mapArgs["inttest1"] = "12345";
genesis 129 mapArgs["inttest2"] = "81985529216486895";
genesis 130
genesis 131 mapArgs["booltest1"] = "";
genesis 132
genesis 133 mapArgs["booltest3"] = "0";
genesis 134 mapArgs["booltest4"] = "1";
genesis 135
genesis 136 BOOST_CHECK_EQUAL(GetArg("strtest1", "default"), "string...");
genesis 137 BOOST_CHECK_EQUAL(GetArg("strtest2", "default"), "default");
genesis 138 BOOST_CHECK_EQUAL(GetArg("inttest1", -1), 12345);
genesis 139 BOOST_CHECK_EQUAL(GetArg("inttest2", -1), 81985529216486895LL);
genesis 140 BOOST_CHECK_EQUAL(GetArg("inttest3", -1), -1);
genesis 141 BOOST_CHECK_EQUAL(GetBoolArg("booltest1"), true);
genesis 142 BOOST_CHECK_EQUAL(GetBoolArg("booltest2"), false);
genesis 143 BOOST_CHECK_EQUAL(GetBoolArg("booltest3"), false);
genesis 144 BOOST_CHECK_EQUAL(GetBoolArg("booltest4"), true);
genesis 145 }
genesis 146
genesis 147 BOOST_AUTO_TEST_CASE(util_WildcardMatch)
genesis 148 {
genesis 149 BOOST_CHECK(WildcardMatch("127.0.0.1", "*"));
genesis 150 BOOST_CHECK(WildcardMatch("127.0.0.1", "127.*"));
genesis 151 BOOST_CHECK(WildcardMatch("abcdef", "a?cde?"));
genesis 152 BOOST_CHECK(!WildcardMatch("abcdef", "a?cde??"));
genesis 153 BOOST_CHECK(WildcardMatch("abcdef", "a*f"));
genesis 154 BOOST_CHECK(!WildcardMatch("abcdef", "a*x"));
genesis 155 BOOST_CHECK(WildcardMatch("", "*"));
genesis 156 }
genesis 157
genesis 158 BOOST_AUTO_TEST_CASE(util_FormatMoney)
genesis 159 {
genesis 160 BOOST_CHECK_EQUAL(FormatMoney(0, false), "0.00");
genesis 161 BOOST_CHECK_EQUAL(FormatMoney((COIN/10000)*123456789, false), "12345.6789");
genesis 162 BOOST_CHECK_EQUAL(FormatMoney(COIN, true), "+1.00");
genesis 163 BOOST_CHECK_EQUAL(FormatMoney(-COIN, false), "-1.00");
genesis 164 BOOST_CHECK_EQUAL(FormatMoney(-COIN, true), "-1.00");
genesis 165
genesis 166 BOOST_CHECK_EQUAL(FormatMoney(COIN*100000000, false), "100000000.00");
genesis 167 BOOST_CHECK_EQUAL(FormatMoney(COIN*10000000, false), "10000000.00");
genesis 168 BOOST_CHECK_EQUAL(FormatMoney(COIN*1000000, false), "1000000.00");
genesis 169 BOOST_CHECK_EQUAL(FormatMoney(COIN*100000, false), "100000.00");
genesis 170 BOOST_CHECK_EQUAL(FormatMoney(COIN*10000, false), "10000.00");
genesis 171 BOOST_CHECK_EQUAL(FormatMoney(COIN*1000, false), "1000.00");
genesis 172 BOOST_CHECK_EQUAL(FormatMoney(COIN*100, false), "100.00");
genesis 173 BOOST_CHECK_EQUAL(FormatMoney(COIN*10, false), "10.00");
genesis 174 BOOST_CHECK_EQUAL(FormatMoney(COIN, false), "1.00");
genesis 175 BOOST_CHECK_EQUAL(FormatMoney(COIN/10, false), "0.10");
genesis 176 BOOST_CHECK_EQUAL(FormatMoney(COIN/100, false), "0.01");
genesis 177 BOOST_CHECK_EQUAL(FormatMoney(COIN/1000, false), "0.001");
genesis 178 BOOST_CHECK_EQUAL(FormatMoney(COIN/10000, false), "0.0001");
genesis 179 BOOST_CHECK_EQUAL(FormatMoney(COIN/100000, false), "0.00001");
genesis 180 BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000, false), "0.000001");
genesis 181 BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000, false), "0.0000001");
genesis 182 BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000, false), "0.00000001");
genesis 183 }
genesis 184
genesis 185 BOOST_AUTO_TEST_CASE(util_ParseMoney)
genesis 186 {
genesis 187 int64 ret = 0;
genesis 188 BOOST_CHECK(ParseMoney("0.0", ret));
genesis 189 BOOST_CHECK_EQUAL(ret, 0);
genesis 190
genesis 191 BOOST_CHECK(ParseMoney("12345.6789", ret));
genesis 192 BOOST_CHECK_EQUAL(ret, (COIN/10000)*123456789);
genesis 193
genesis 194 BOOST_CHECK(ParseMoney("100000000.00", ret));
genesis 195 BOOST_CHECK_EQUAL(ret, COIN*100000000);
genesis 196 BOOST_CHECK(ParseMoney("10000000.00", ret));
genesis 197 BOOST_CHECK_EQUAL(ret, COIN*10000000);
genesis 198 BOOST_CHECK(ParseMoney("1000000.00", ret));
genesis 199 BOOST_CHECK_EQUAL(ret, COIN*1000000);
genesis 200 BOOST_CHECK(ParseMoney("100000.00", ret));
genesis 201 BOOST_CHECK_EQUAL(ret, COIN*100000);
genesis 202 BOOST_CHECK(ParseMoney("10000.00", ret));
genesis 203 BOOST_CHECK_EQUAL(ret, COIN*10000);
genesis 204 BOOST_CHECK(ParseMoney("1000.00", ret));
genesis 205 BOOST_CHECK_EQUAL(ret, COIN*1000);
genesis 206 BOOST_CHECK(ParseMoney("100.00", ret));
genesis 207 BOOST_CHECK_EQUAL(ret, COIN*100);
genesis 208 BOOST_CHECK(ParseMoney("10.00", ret));
genesis 209 BOOST_CHECK_EQUAL(ret, COIN*10);
genesis 210 BOOST_CHECK(ParseMoney("1.00", ret));
genesis 211 BOOST_CHECK_EQUAL(ret, COIN);
genesis 212 BOOST_CHECK(ParseMoney("0.1", ret));
genesis 213 BOOST_CHECK_EQUAL(ret, COIN/10);
genesis 214 BOOST_CHECK(ParseMoney("0.01", ret));
genesis 215 BOOST_CHECK_EQUAL(ret, COIN/100);
genesis 216 BOOST_CHECK(ParseMoney("0.001", ret));
genesis 217 BOOST_CHECK_EQUAL(ret, COIN/1000);
genesis 218 BOOST_CHECK(ParseMoney("0.0001", ret));
genesis 219 BOOST_CHECK_EQUAL(ret, COIN/10000);
genesis 220 BOOST_CHECK(ParseMoney("0.00001", ret));
genesis 221 BOOST_CHECK_EQUAL(ret, COIN/100000);
genesis 222 BOOST_CHECK(ParseMoney("0.000001", ret));
genesis 223 BOOST_CHECK_EQUAL(ret, COIN/1000000);
genesis 224 BOOST_CHECK(ParseMoney("0.0000001", ret));
genesis 225 BOOST_CHECK_EQUAL(ret, COIN/10000000);
genesis 226 BOOST_CHECK(ParseMoney("0.00000001", ret));
genesis 227 BOOST_CHECK_EQUAL(ret, COIN/100000000);
genesis 228
genesis 229
genesis 230 BOOST_CHECK(!ParseMoney("92233720368.54775808", ret));
genesis 231 }
genesis 232
genesis 233 BOOST_AUTO_TEST_SUITE_END()