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