-
+ 6B89864EE98914179BD2DBE3B1BACB6F4DA638DB7A04125D1AC4BC1AB2FA0D739C0FD3CBF2B95352715133A8F74E8A36B025E20D9C606CEE1403ED05B9D9FAFBbitcoin/src/test/util_tests.cpp(0 . 0)(1 . 264)
23586 // /****************************\
23587 // * EXPERIMENTAL BRANCH. *
23588 // * FOR LABORATORY USE ONLY. *
23589 // ********************************
23590 // ************
23591 // **************
23592 // ****************
23593 // **** **** ****
23594 // *** *** ***
23595 // *** *** ***
23596 // *** * * **
23597 // ******** ********
23598 // ******* ******
23599 // *** **
23600 // * ******* **
23601 // ** * * * * *
23602 // ** * * ***
23603 // **** * * * * ****
23604 // **** *** * * ** ***
23605 // **** ********* ******
23606 // ******* ***** *******
23607 // ********* ****** **
23608 // ** ****** ******
23609 // ** ******* **
23610 // ** ******* ***
23611 // **** ******** ************
23612 // ************ ************
23613 // ******** *******
23614 // ****** ****
23615 // *** ***
23616 // ********************************
23617 #include <vector>
23618 #include <boost/test/unit_test.hpp>
23619 #include <boost/foreach.hpp>
23620
23621 #include "../util.h"
23622
23623 using namespace std;
23624
23625 BOOST_AUTO_TEST_SUITE(util_tests)
23626
23627 BOOST_AUTO_TEST_CASE(util_criticalsection)
23628 {
23629 CCriticalSection cs;
23630
23631 do {
23632 CRITICAL_BLOCK(cs)
23633 break;
23634
23635 BOOST_ERROR("break was swallowed!");
23636 } while(0);
23637
23638 do {
23639 TRY_CRITICAL_BLOCK(cs)
23640 break;
23641
23642 BOOST_ERROR("break was swallowed!");
23643 } while(0);
23644 }
23645
23646 BOOST_AUTO_TEST_CASE(util_MedianFilter)
23647 {
23648 CMedianFilter<int> filter(5, 15);
23649
23650 BOOST_CHECK_EQUAL(filter.median(), 15);
23651
23652 filter.input(20); // [15 20]
23653 BOOST_CHECK_EQUAL(filter.median(), 17);
23654
23655 filter.input(30); // [15 20 30]
23656 BOOST_CHECK_EQUAL(filter.median(), 20);
23657
23658 filter.input(3); // [3 15 20 30]
23659 BOOST_CHECK_EQUAL(filter.median(), 17);
23660
23661 filter.input(7); // [3 7 15 20 30]
23662 BOOST_CHECK_EQUAL(filter.median(), 15);
23663
23664 filter.input(18); // [3 7 18 20 30]
23665 BOOST_CHECK_EQUAL(filter.median(), 18);
23666
23667 filter.input(0); // [0 3 7 18 30]
23668 BOOST_CHECK_EQUAL(filter.median(), 7);
23669 }
23670
23671 static const unsigned char ParseHex_expected[65] = {
23672 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
23673 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
23674 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
23675 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
23676 0x5f
23677 };
23678 BOOST_AUTO_TEST_CASE(util_ParseHex)
23679 {
23680 std::vector<unsigned char> result;
23681 std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
23682 // Basic test vector
23683 result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
23684 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
23685
23686 // Spaces between bytes must be supported
23687 result = ParseHex("12 34 56 78");
23688 BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
23689
23690 // Stop parsing at invalid value
23691 result = ParseHex("1234 invalid 1234");
23692 BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
23693 }
23694
23695 BOOST_AUTO_TEST_CASE(util_HexStr)
23696 {
23697 BOOST_CHECK_EQUAL(
23698 HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
23699 "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
23700
23701 BOOST_CHECK_EQUAL(
23702 HexStr(ParseHex_expected, ParseHex_expected + 5, true),
23703 "04 67 8a fd b0");
23704 }
23705
23706 BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)
23707 {
23708 BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 0), "01/01/70 00:00:00");
23709 BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 0x7FFFFFFF), "01/19/38 03:14:07");
23710 // Formats used within bitcoin
23711 BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 1317425777), "09/30/11 23:36:17");
23712 BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M", 1317425777), "09/30/11 23:36");
23713 }
23714
23715 BOOST_AUTO_TEST_CASE(util_ParseParameters)
23716 {
23717 const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
23718
23719 ParseParameters(0, (char**)argv_test);
23720 BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
23721
23722 ParseParameters(1, (char**)argv_test);
23723 BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
23724
23725 ParseParameters(5, (char**)argv_test);
23726 // expectation: -ignored is ignored (program name argument),
23727 // -a, -b and -ccc end up in map, -d ignored because it is after
23728 // a non-option argument (non-GNU option parsing)
23729 BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3);
23730 BOOST_CHECK(mapArgs.count("-a") && mapArgs.count("-b") && mapArgs.count("-ccc")
23731 && !mapArgs.count("f") && !mapArgs.count("-d"));
23732 BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc")
23733 && !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));
23734
23735 BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple");
23736 BOOST_CHECK(mapMultiArgs["-ccc"].size() == 2);
23737 }
23738
23739 BOOST_AUTO_TEST_CASE(util_GetArg)
23740 {
23741 mapArgs.clear();
23742 mapArgs["strtest1"] = "string...";
23743 // strtest2 undefined on purpose
23744 mapArgs["inttest1"] = "12345";
23745 mapArgs["inttest2"] = "81985529216486895";
23746 // inttest3 undefined on purpose
23747 mapArgs["booltest1"] = "";
23748 // booltest2 undefined on purpose
23749 mapArgs["booltest3"] = "0";
23750 mapArgs["booltest4"] = "1";
23751
23752 BOOST_CHECK_EQUAL(GetArg("strtest1", "default"), "string...");
23753 BOOST_CHECK_EQUAL(GetArg("strtest2", "default"), "default");
23754 BOOST_CHECK_EQUAL(GetArg("inttest1", -1), 12345);
23755 BOOST_CHECK_EQUAL(GetArg("inttest2", -1), 81985529216486895LL);
23756 BOOST_CHECK_EQUAL(GetArg("inttest3", -1), -1);
23757 BOOST_CHECK_EQUAL(GetBoolArg("booltest1"), true);
23758 BOOST_CHECK_EQUAL(GetBoolArg("booltest2"), false);
23759 BOOST_CHECK_EQUAL(GetBoolArg("booltest3"), false);
23760 BOOST_CHECK_EQUAL(GetBoolArg("booltest4"), true);
23761 }
23762
23763 BOOST_AUTO_TEST_CASE(util_WildcardMatch)
23764 {
23765 BOOST_CHECK(WildcardMatch("127.0.0.1", "*"));
23766 BOOST_CHECK(WildcardMatch("127.0.0.1", "127.*"));
23767 BOOST_CHECK(WildcardMatch("abcdef", "a?cde?"));
23768 BOOST_CHECK(!WildcardMatch("abcdef", "a?cde??"));
23769 BOOST_CHECK(WildcardMatch("abcdef", "a*f"));
23770 BOOST_CHECK(!WildcardMatch("abcdef", "a*x"));
23771 BOOST_CHECK(WildcardMatch("", "*"));
23772 }
23773
23774 BOOST_AUTO_TEST_CASE(util_FormatMoney)
23775 {
23776 BOOST_CHECK_EQUAL(FormatMoney(0, false), "0.00");
23777 BOOST_CHECK_EQUAL(FormatMoney((COIN/10000)*123456789, false), "12345.6789");
23778 BOOST_CHECK_EQUAL(FormatMoney(COIN, true), "+1.00");
23779 BOOST_CHECK_EQUAL(FormatMoney(-COIN, false), "-1.00");
23780 BOOST_CHECK_EQUAL(FormatMoney(-COIN, true), "-1.00");
23781
23782 BOOST_CHECK_EQUAL(FormatMoney(COIN*100000000, false), "100000000.00");
23783 BOOST_CHECK_EQUAL(FormatMoney(COIN*10000000, false), "10000000.00");
23784 BOOST_CHECK_EQUAL(FormatMoney(COIN*1000000, false), "1000000.00");
23785 BOOST_CHECK_EQUAL(FormatMoney(COIN*100000, false), "100000.00");
23786 BOOST_CHECK_EQUAL(FormatMoney(COIN*10000, false), "10000.00");
23787 BOOST_CHECK_EQUAL(FormatMoney(COIN*1000, false), "1000.00");
23788 BOOST_CHECK_EQUAL(FormatMoney(COIN*100, false), "100.00");
23789 BOOST_CHECK_EQUAL(FormatMoney(COIN*10, false), "10.00");
23790 BOOST_CHECK_EQUAL(FormatMoney(COIN, false), "1.00");
23791 BOOST_CHECK_EQUAL(FormatMoney(COIN/10, false), "0.10");
23792 BOOST_CHECK_EQUAL(FormatMoney(COIN/100, false), "0.01");
23793 BOOST_CHECK_EQUAL(FormatMoney(COIN/1000, false), "0.001");
23794 BOOST_CHECK_EQUAL(FormatMoney(COIN/10000, false), "0.0001");
23795 BOOST_CHECK_EQUAL(FormatMoney(COIN/100000, false), "0.00001");
23796 BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000, false), "0.000001");
23797 BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000, false), "0.0000001");
23798 BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000, false), "0.00000001");
23799 }
23800
23801 BOOST_AUTO_TEST_CASE(util_ParseMoney)
23802 {
23803 int64 ret = 0;
23804 BOOST_CHECK(ParseMoney("0.0", ret));
23805 BOOST_CHECK_EQUAL(ret, 0);
23806
23807 BOOST_CHECK(ParseMoney("12345.6789", ret));
23808 BOOST_CHECK_EQUAL(ret, (COIN/10000)*123456789);
23809
23810 BOOST_CHECK(ParseMoney("100000000.00", ret));
23811 BOOST_CHECK_EQUAL(ret, COIN*100000000);
23812 BOOST_CHECK(ParseMoney("10000000.00", ret));
23813 BOOST_CHECK_EQUAL(ret, COIN*10000000);
23814 BOOST_CHECK(ParseMoney("1000000.00", ret));
23815 BOOST_CHECK_EQUAL(ret, COIN*1000000);
23816 BOOST_CHECK(ParseMoney("100000.00", ret));
23817 BOOST_CHECK_EQUAL(ret, COIN*100000);
23818 BOOST_CHECK(ParseMoney("10000.00", ret));
23819 BOOST_CHECK_EQUAL(ret, COIN*10000);
23820 BOOST_CHECK(ParseMoney("1000.00", ret));
23821 BOOST_CHECK_EQUAL(ret, COIN*1000);
23822 BOOST_CHECK(ParseMoney("100.00", ret));
23823 BOOST_CHECK_EQUAL(ret, COIN*100);
23824 BOOST_CHECK(ParseMoney("10.00", ret));
23825 BOOST_CHECK_EQUAL(ret, COIN*10);
23826 BOOST_CHECK(ParseMoney("1.00", ret));
23827 BOOST_CHECK_EQUAL(ret, COIN);
23828 BOOST_CHECK(ParseMoney("0.1", ret));
23829 BOOST_CHECK_EQUAL(ret, COIN/10);
23830 BOOST_CHECK(ParseMoney("0.01", ret));
23831 BOOST_CHECK_EQUAL(ret, COIN/100);
23832 BOOST_CHECK(ParseMoney("0.001", ret));
23833 BOOST_CHECK_EQUAL(ret, COIN/1000);
23834 BOOST_CHECK(ParseMoney("0.0001", ret));
23835 BOOST_CHECK_EQUAL(ret, COIN/10000);
23836 BOOST_CHECK(ParseMoney("0.00001", ret));
23837 BOOST_CHECK_EQUAL(ret, COIN/100000);
23838 BOOST_CHECK(ParseMoney("0.000001", ret));
23839 BOOST_CHECK_EQUAL(ret, COIN/1000000);
23840 BOOST_CHECK(ParseMoney("0.0000001", ret));
23841 BOOST_CHECK_EQUAL(ret, COIN/10000000);
23842 BOOST_CHECK(ParseMoney("0.00000001", ret));
23843 BOOST_CHECK_EQUAL(ret, COIN/100000000);
23844
23845 // Attempted 63 bit overflow should fail
23846 BOOST_CHECK(!ParseMoney("92233720368.54775808", ret));
23847 }
23848
23849 BOOST_AUTO_TEST_SUITE_END()