-
+ 1360397AC0C494D5003656DEB41494DE4480C56BBE46254A09F193682A9E7813FE133A45C93699835E6DE89951C350AD2A7E59BCF8B1307A7670CC1DB5F22E24
bitcoin/src/test/util_tests.cpp
(0 . 0)(1 . 233)
21664 #include <vector>
21665 #include <boost/test/unit_test.hpp>
21666 #include <boost/foreach.hpp>
21667
21668 #include "../util.h"
21669
21670 using namespace std;
21671
21672 BOOST_AUTO_TEST_SUITE(util_tests)
21673
21674 BOOST_AUTO_TEST_CASE(util_criticalsection)
21675 {
21676 CCriticalSection cs;
21677
21678 do {
21679 CRITICAL_BLOCK(cs)
21680 break;
21681
21682 BOOST_ERROR("break was swallowed!");
21683 } while(0);
21684
21685 do {
21686 TRY_CRITICAL_BLOCK(cs)
21687 break;
21688
21689 BOOST_ERROR("break was swallowed!");
21690 } while(0);
21691 }
21692
21693 BOOST_AUTO_TEST_CASE(util_MedianFilter)
21694 {
21695 CMedianFilter<int> filter(5, 15);
21696
21697 BOOST_CHECK_EQUAL(filter.median(), 15);
21698
21699 filter.input(20); // [15 20]
21700 BOOST_CHECK_EQUAL(filter.median(), 17);
21701
21702 filter.input(30); // [15 20 30]
21703 BOOST_CHECK_EQUAL(filter.median(), 20);
21704
21705 filter.input(3); // [3 15 20 30]
21706 BOOST_CHECK_EQUAL(filter.median(), 17);
21707
21708 filter.input(7); // [3 7 15 20 30]
21709 BOOST_CHECK_EQUAL(filter.median(), 15);
21710
21711 filter.input(18); // [3 7 18 20 30]
21712 BOOST_CHECK_EQUAL(filter.median(), 18);
21713
21714 filter.input(0); // [0 3 7 18 30]
21715 BOOST_CHECK_EQUAL(filter.median(), 7);
21716 }
21717
21718 static const unsigned char ParseHex_expected[65] = {
21719 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
21720 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
21721 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
21722 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
21723 0x5f
21724 };
21725 BOOST_AUTO_TEST_CASE(util_ParseHex)
21726 {
21727 std::vector<unsigned char> result;
21728 std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
21729 // Basic test vector
21730 result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
21731 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
21732
21733 // Spaces between bytes must be supported
21734 result = ParseHex("12 34 56 78");
21735 BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
21736
21737 // Stop parsing at invalid value
21738 result = ParseHex("1234 invalid 1234");
21739 BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
21740 }
21741
21742 BOOST_AUTO_TEST_CASE(util_HexStr)
21743 {
21744 BOOST_CHECK_EQUAL(
21745 HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
21746 "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
21747
21748 BOOST_CHECK_EQUAL(
21749 HexStr(ParseHex_expected, ParseHex_expected + 5, true),
21750 "04 67 8a fd b0");
21751 }
21752
21753 BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)
21754 {
21755 BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 0), "01/01/70 00:00:00");
21756 BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 0x7FFFFFFF), "01/19/38 03:14:07");
21757 // Formats used within bitcoin
21758 BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 1317425777), "09/30/11 23:36:17");
21759 BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M", 1317425777), "09/30/11 23:36");
21760 }
21761
21762 BOOST_AUTO_TEST_CASE(util_ParseParameters)
21763 {
21764 const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
21765
21766 ParseParameters(0, (char**)argv_test);
21767 BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
21768
21769 ParseParameters(1, (char**)argv_test);
21770 BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
21771
21772 ParseParameters(5, (char**)argv_test);
21773 // expectation: -ignored is ignored (program name argument),
21774 // -a, -b and -ccc end up in map, -d ignored because it is after
21775 // a non-option argument (non-GNU option parsing)
21776 BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3);
21777 BOOST_CHECK(mapArgs.count("-a") && mapArgs.count("-b") && mapArgs.count("-ccc")
21778 && !mapArgs.count("f") && !mapArgs.count("-d"));
21779 BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc")
21780 && !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));
21781
21782 BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple");
21783 BOOST_CHECK(mapMultiArgs["-ccc"].size() == 2);
21784 }
21785
21786 BOOST_AUTO_TEST_CASE(util_GetArg)
21787 {
21788 mapArgs.clear();
21789 mapArgs["strtest1"] = "string...";
21790 // strtest2 undefined on purpose
21791 mapArgs["inttest1"] = "12345";
21792 mapArgs["inttest2"] = "81985529216486895";
21793 // inttest3 undefined on purpose
21794 mapArgs["booltest1"] = "";
21795 // booltest2 undefined on purpose
21796 mapArgs["booltest3"] = "0";
21797 mapArgs["booltest4"] = "1";
21798
21799 BOOST_CHECK_EQUAL(GetArg("strtest1", "default"), "string...");
21800 BOOST_CHECK_EQUAL(GetArg("strtest2", "default"), "default");
21801 BOOST_CHECK_EQUAL(GetArg("inttest1", -1), 12345);
21802 BOOST_CHECK_EQUAL(GetArg("inttest2", -1), 81985529216486895LL);
21803 BOOST_CHECK_EQUAL(GetArg("inttest3", -1), -1);
21804 BOOST_CHECK_EQUAL(GetBoolArg("booltest1"), true);
21805 BOOST_CHECK_EQUAL(GetBoolArg("booltest2"), false);
21806 BOOST_CHECK_EQUAL(GetBoolArg("booltest3"), false);
21807 BOOST_CHECK_EQUAL(GetBoolArg("booltest4"), true);
21808 }
21809
21810 BOOST_AUTO_TEST_CASE(util_WildcardMatch)
21811 {
21812 BOOST_CHECK(WildcardMatch("127.0.0.1", "*"));
21813 BOOST_CHECK(WildcardMatch("127.0.0.1", "127.*"));
21814 BOOST_CHECK(WildcardMatch("abcdef", "a?cde?"));
21815 BOOST_CHECK(!WildcardMatch("abcdef", "a?cde??"));
21816 BOOST_CHECK(WildcardMatch("abcdef", "a*f"));
21817 BOOST_CHECK(!WildcardMatch("abcdef", "a*x"));
21818 BOOST_CHECK(WildcardMatch("", "*"));
21819 }
21820
21821 BOOST_AUTO_TEST_CASE(util_FormatMoney)
21822 {
21823 BOOST_CHECK_EQUAL(FormatMoney(0, false), "0.00");
21824 BOOST_CHECK_EQUAL(FormatMoney((COIN/10000)*123456789, false), "12345.6789");
21825 BOOST_CHECK_EQUAL(FormatMoney(COIN, true), "+1.00");
21826 BOOST_CHECK_EQUAL(FormatMoney(-COIN, false), "-1.00");
21827 BOOST_CHECK_EQUAL(FormatMoney(-COIN, true), "-1.00");
21828
21829 BOOST_CHECK_EQUAL(FormatMoney(COIN*100000000, false), "100000000.00");
21830 BOOST_CHECK_EQUAL(FormatMoney(COIN*10000000, false), "10000000.00");
21831 BOOST_CHECK_EQUAL(FormatMoney(COIN*1000000, false), "1000000.00");
21832 BOOST_CHECK_EQUAL(FormatMoney(COIN*100000, false), "100000.00");
21833 BOOST_CHECK_EQUAL(FormatMoney(COIN*10000, false), "10000.00");
21834 BOOST_CHECK_EQUAL(FormatMoney(COIN*1000, false), "1000.00");
21835 BOOST_CHECK_EQUAL(FormatMoney(COIN*100, false), "100.00");
21836 BOOST_CHECK_EQUAL(FormatMoney(COIN*10, false), "10.00");
21837 BOOST_CHECK_EQUAL(FormatMoney(COIN, false), "1.00");
21838 BOOST_CHECK_EQUAL(FormatMoney(COIN/10, false), "0.10");
21839 BOOST_CHECK_EQUAL(FormatMoney(COIN/100, false), "0.01");
21840 BOOST_CHECK_EQUAL(FormatMoney(COIN/1000, false), "0.001");
21841 BOOST_CHECK_EQUAL(FormatMoney(COIN/10000, false), "0.0001");
21842 BOOST_CHECK_EQUAL(FormatMoney(COIN/100000, false), "0.00001");
21843 BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000, false), "0.000001");
21844 BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000, false), "0.0000001");
21845 BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000, false), "0.00000001");
21846 }
21847
21848 BOOST_AUTO_TEST_CASE(util_ParseMoney)
21849 {
21850 int64 ret = 0;
21851 BOOST_CHECK(ParseMoney("0.0", ret));
21852 BOOST_CHECK_EQUAL(ret, 0);
21853
21854 BOOST_CHECK(ParseMoney("12345.6789", ret));
21855 BOOST_CHECK_EQUAL(ret, (COIN/10000)*123456789);
21856
21857 BOOST_CHECK(ParseMoney("100000000.00", ret));
21858 BOOST_CHECK_EQUAL(ret, COIN*100000000);
21859 BOOST_CHECK(ParseMoney("10000000.00", ret));
21860 BOOST_CHECK_EQUAL(ret, COIN*10000000);
21861 BOOST_CHECK(ParseMoney("1000000.00", ret));
21862 BOOST_CHECK_EQUAL(ret, COIN*1000000);
21863 BOOST_CHECK(ParseMoney("100000.00", ret));
21864 BOOST_CHECK_EQUAL(ret, COIN*100000);
21865 BOOST_CHECK(ParseMoney("10000.00", ret));
21866 BOOST_CHECK_EQUAL(ret, COIN*10000);
21867 BOOST_CHECK(ParseMoney("1000.00", ret));
21868 BOOST_CHECK_EQUAL(ret, COIN*1000);
21869 BOOST_CHECK(ParseMoney("100.00", ret));
21870 BOOST_CHECK_EQUAL(ret, COIN*100);
21871 BOOST_CHECK(ParseMoney("10.00", ret));
21872 BOOST_CHECK_EQUAL(ret, COIN*10);
21873 BOOST_CHECK(ParseMoney("1.00", ret));
21874 BOOST_CHECK_EQUAL(ret, COIN);
21875 BOOST_CHECK(ParseMoney("0.1", ret));
21876 BOOST_CHECK_EQUAL(ret, COIN/10);
21877 BOOST_CHECK(ParseMoney("0.01", ret));
21878 BOOST_CHECK_EQUAL(ret, COIN/100);
21879 BOOST_CHECK(ParseMoney("0.001", ret));
21880 BOOST_CHECK_EQUAL(ret, COIN/1000);
21881 BOOST_CHECK(ParseMoney("0.0001", ret));
21882 BOOST_CHECK_EQUAL(ret, COIN/10000);
21883 BOOST_CHECK(ParseMoney("0.00001", ret));
21884 BOOST_CHECK_EQUAL(ret, COIN/100000);
21885 BOOST_CHECK(ParseMoney("0.000001", ret));
21886 BOOST_CHECK_EQUAL(ret, COIN/1000000);
21887 BOOST_CHECK(ParseMoney("0.0000001", ret));
21888 BOOST_CHECK_EQUAL(ret, COIN/10000000);
21889 BOOST_CHECK(ParseMoney("0.00000001", ret));
21890 BOOST_CHECK_EQUAL(ret, COIN/100000000);
21891
21892 // Attempted 63 bit overflow should fail
21893 BOOST_CHECK(!ParseMoney("92233720368.54775808", ret));
21894 }
21895
21896 BOOST_AUTO_TEST_SUITE_END()