Back to home page

Bitcoin sources

 
 

    


0001 #include <vector>
0002 #include <boost/test/unit_test.hpp>
0003 #include <boost/foreach.hpp>
0004 
0005 #include "../util.h"
0006 
0007 using namespace std;
0008 
0009 BOOST_AUTO_TEST_SUITE(util_tests)
0010 
0011 BOOST_AUTO_TEST_CASE(util_criticalsection)
0012 {
0013     CCriticalSection cs;
0014 
0015     do {
0016         CRITICAL_BLOCK(cs)
0017             break;
0018 
0019         BOOST_ERROR("break was swallowed!");
0020     } while(0);
0021 
0022     do {
0023         TRY_CRITICAL_BLOCK(cs)
0024             break;
0025 
0026         BOOST_ERROR("break was swallowed!");
0027     } while(0);
0028 }
0029 
0030 BOOST_AUTO_TEST_CASE(util_MedianFilter)
0031 {    
0032     CMedianFilter<int> filter(5, 15);
0033 
0034     BOOST_CHECK_EQUAL(filter.median(), 15);
0035 
0036     filter.input(20); // [15 20]
0037     BOOST_CHECK_EQUAL(filter.median(), 17);
0038 
0039     filter.input(30); // [15 20 30]
0040     BOOST_CHECK_EQUAL(filter.median(), 20);
0041 
0042     filter.input(3); // [3 15 20 30]
0043     BOOST_CHECK_EQUAL(filter.median(), 17);
0044 
0045     filter.input(7); // [3 7 15 20 30]
0046     BOOST_CHECK_EQUAL(filter.median(), 15);
0047 
0048     filter.input(18); // [3 7 18 20 30]
0049     BOOST_CHECK_EQUAL(filter.median(), 18);
0050 
0051     filter.input(0); // [0 3 7 18 30]
0052     BOOST_CHECK_EQUAL(filter.median(), 7);
0053 }
0054 
0055 static const unsigned char ParseHex_expected[65] = {
0056     0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 
0057     0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 
0058     0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 
0059     0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 
0060     0x5f
0061 };
0062 BOOST_AUTO_TEST_CASE(util_ParseHex)
0063 {
0064     std::vector<unsigned char> result;
0065     std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
0066     // Basic test vector
0067     result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
0068     BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
0069 
0070     // Spaces between bytes must be supported
0071     result = ParseHex("12 34 56 78");
0072     BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
0073 
0074     // Stop parsing at invalid value
0075     result = ParseHex("1234 invalid 1234");
0076     BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
0077 }
0078 
0079 BOOST_AUTO_TEST_CASE(util_HexStr)
0080 {
0081     BOOST_CHECK_EQUAL(
0082         HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
0083         "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
0084 
0085     BOOST_CHECK_EQUAL(
0086         HexStr(ParseHex_expected, ParseHex_expected + 5, true),
0087         "04 67 8a fd b0");
0088 }
0089 
0090 BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)
0091 {
0092     BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 0), "01/01/70 00:00:00");
0093     BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 0x7FFFFFFF), "01/19/38 03:14:07");
0094     // Formats used within bitcoin
0095     BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 1317425777), "09/30/11 23:36:17");
0096     BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M", 1317425777), "09/30/11 23:36");
0097 }
0098 
0099 BOOST_AUTO_TEST_CASE(util_ParseParameters)
0100 {
0101     const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
0102 
0103     ParseParameters(0, (char**)argv_test);
0104     BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
0105 
0106     ParseParameters(1, (char**)argv_test);
0107     BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
0108 
0109     ParseParameters(5, (char**)argv_test);
0110     // expectation: -ignored is ignored (program name argument), 
0111     // -a, -b and -ccc end up in map, -d ignored because it is after
0112     // a non-option argument (non-GNU option parsing)
0113     BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3);
0114     BOOST_CHECK(mapArgs.count("-a") && mapArgs.count("-b") && mapArgs.count("-ccc") 
0115                 && !mapArgs.count("f") && !mapArgs.count("-d"));
0116     BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc") 
0117                 && !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));
0118 
0119     BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple");
0120     BOOST_CHECK(mapMultiArgs["-ccc"].size() == 2);
0121 }
0122 
0123 BOOST_AUTO_TEST_CASE(util_GetArg)
0124 {
0125     mapArgs.clear();
0126     mapArgs["strtest1"] = "string...";
0127     // strtest2 undefined on purpose
0128     mapArgs["inttest1"] = "12345";
0129     mapArgs["inttest2"] = "81985529216486895";
0130     // inttest3 undefined on purpose
0131     mapArgs["booltest1"] = "";
0132     // booltest2 undefined on purpose
0133     mapArgs["booltest3"] = "0";
0134     mapArgs["booltest4"] = "1";
0135 
0136     BOOST_CHECK_EQUAL(GetArg("strtest1", "default"), "string...");
0137     BOOST_CHECK_EQUAL(GetArg("strtest2", "default"), "default");
0138     BOOST_CHECK_EQUAL(GetArg("inttest1", -1), 12345);
0139     BOOST_CHECK_EQUAL(GetArg("inttest2", -1), 81985529216486895LL);
0140     BOOST_CHECK_EQUAL(GetArg("inttest3", -1), -1);
0141     BOOST_CHECK_EQUAL(GetBoolArg("booltest1"), true);
0142     BOOST_CHECK_EQUAL(GetBoolArg("booltest2"), false);
0143     BOOST_CHECK_EQUAL(GetBoolArg("booltest3"), false);
0144     BOOST_CHECK_EQUAL(GetBoolArg("booltest4"), true);
0145 }
0146 
0147 BOOST_AUTO_TEST_CASE(util_WildcardMatch)
0148 {
0149     BOOST_CHECK(WildcardMatch("127.0.0.1", "*"));
0150     BOOST_CHECK(WildcardMatch("127.0.0.1", "127.*"));
0151     BOOST_CHECK(WildcardMatch("abcdef", "a?cde?"));
0152     BOOST_CHECK(!WildcardMatch("abcdef", "a?cde??"));
0153     BOOST_CHECK(WildcardMatch("abcdef", "a*f"));
0154     BOOST_CHECK(!WildcardMatch("abcdef", "a*x"));
0155     BOOST_CHECK(WildcardMatch("", "*"));
0156 }
0157 
0158 BOOST_AUTO_TEST_CASE(util_FormatMoney)
0159 {
0160     BOOST_CHECK_EQUAL(FormatMoney(0, false), "0.00");
0161     BOOST_CHECK_EQUAL(FormatMoney((COIN/10000)*123456789, false), "12345.6789");
0162     BOOST_CHECK_EQUAL(FormatMoney(COIN, true), "+1.00");
0163     BOOST_CHECK_EQUAL(FormatMoney(-COIN, false), "-1.00");
0164     BOOST_CHECK_EQUAL(FormatMoney(-COIN, true), "-1.00");
0165 
0166     BOOST_CHECK_EQUAL(FormatMoney(COIN*100000000, false), "100000000.00");
0167     BOOST_CHECK_EQUAL(FormatMoney(COIN*10000000, false), "10000000.00");
0168     BOOST_CHECK_EQUAL(FormatMoney(COIN*1000000, false), "1000000.00");
0169     BOOST_CHECK_EQUAL(FormatMoney(COIN*100000, false), "100000.00");
0170     BOOST_CHECK_EQUAL(FormatMoney(COIN*10000, false), "10000.00");
0171     BOOST_CHECK_EQUAL(FormatMoney(COIN*1000, false), "1000.00");
0172     BOOST_CHECK_EQUAL(FormatMoney(COIN*100, false), "100.00");
0173     BOOST_CHECK_EQUAL(FormatMoney(COIN*10, false), "10.00");
0174     BOOST_CHECK_EQUAL(FormatMoney(COIN, false), "1.00");
0175     BOOST_CHECK_EQUAL(FormatMoney(COIN/10, false), "0.10");
0176     BOOST_CHECK_EQUAL(FormatMoney(COIN/100, false), "0.01");
0177     BOOST_CHECK_EQUAL(FormatMoney(COIN/1000, false), "0.001");
0178     BOOST_CHECK_EQUAL(FormatMoney(COIN/10000, false), "0.0001");
0179     BOOST_CHECK_EQUAL(FormatMoney(COIN/100000, false), "0.00001");
0180     BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000, false), "0.000001");
0181     BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000, false), "0.0000001");
0182     BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000, false), "0.00000001");
0183 }
0184 
0185 BOOST_AUTO_TEST_CASE(util_ParseMoney)
0186 {
0187     int64 ret = 0;
0188     BOOST_CHECK(ParseMoney("0.0", ret));
0189     BOOST_CHECK_EQUAL(ret, 0);
0190 
0191     BOOST_CHECK(ParseMoney("12345.6789", ret));
0192     BOOST_CHECK_EQUAL(ret, (COIN/10000)*123456789);
0193 
0194     BOOST_CHECK(ParseMoney("100000000.00", ret));
0195     BOOST_CHECK_EQUAL(ret, COIN*100000000);
0196     BOOST_CHECK(ParseMoney("10000000.00", ret));
0197     BOOST_CHECK_EQUAL(ret, COIN*10000000);
0198     BOOST_CHECK(ParseMoney("1000000.00", ret));
0199     BOOST_CHECK_EQUAL(ret, COIN*1000000);
0200     BOOST_CHECK(ParseMoney("100000.00", ret));
0201     BOOST_CHECK_EQUAL(ret, COIN*100000);
0202     BOOST_CHECK(ParseMoney("10000.00", ret));
0203     BOOST_CHECK_EQUAL(ret, COIN*10000);
0204     BOOST_CHECK(ParseMoney("1000.00", ret));
0205     BOOST_CHECK_EQUAL(ret, COIN*1000);
0206     BOOST_CHECK(ParseMoney("100.00", ret));
0207     BOOST_CHECK_EQUAL(ret, COIN*100);
0208     BOOST_CHECK(ParseMoney("10.00", ret));
0209     BOOST_CHECK_EQUAL(ret, COIN*10);
0210     BOOST_CHECK(ParseMoney("1.00", ret));
0211     BOOST_CHECK_EQUAL(ret, COIN);
0212     BOOST_CHECK(ParseMoney("0.1", ret));
0213     BOOST_CHECK_EQUAL(ret, COIN/10);
0214     BOOST_CHECK(ParseMoney("0.01", ret));
0215     BOOST_CHECK_EQUAL(ret, COIN/100);
0216     BOOST_CHECK(ParseMoney("0.001", ret));
0217     BOOST_CHECK_EQUAL(ret, COIN/1000);
0218     BOOST_CHECK(ParseMoney("0.0001", ret));
0219     BOOST_CHECK_EQUAL(ret, COIN/10000);
0220     BOOST_CHECK(ParseMoney("0.00001", ret));
0221     BOOST_CHECK_EQUAL(ret, COIN/100000);
0222     BOOST_CHECK(ParseMoney("0.000001", ret));
0223     BOOST_CHECK_EQUAL(ret, COIN/1000000);
0224     BOOST_CHECK(ParseMoney("0.0000001", ret));
0225     BOOST_CHECK_EQUAL(ret, COIN/10000000);
0226     BOOST_CHECK(ParseMoney("0.00000001", ret));
0227     BOOST_CHECK_EQUAL(ret, COIN/100000000);
0228 
0229     // Attempted 63 bit overflow should fail
0230     BOOST_CHECK(!ParseMoney("92233720368.54775808", ret));
0231 }
0232 
0233 BOOST_AUTO_TEST_SUITE_END()