CommonJS.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file CommonJS.cpp
  15. * @author Lefteris Karapetsas <lefteris@refu.co>
  16. * @date 2015
  17. * Tests for functions in CommonJS.h
  18. */
  19. #include <boost/test/unit_test.hpp>
  20. #include <libdevcore/CommonJS.h>
  21. #include <test/TestHelper.h>
  22. using namespace dev;
  23. using namespace std;
  24. using namespace test;
  25. BOOST_FIXTURE_TEST_SUITE(CommonJSTests, TestOutputHelper)
  26. BOOST_AUTO_TEST_CASE(test_toJS)
  27. {
  28. h64 a("0xbaadf00ddeadbeef");
  29. u64 b("0xffff0000bbbaaaa");
  30. uint64_t c = 38990234243;
  31. bytes d = {0xff, 0x0, 0xef, 0xbc};
  32. BOOST_CHECK(toJS(a) == "0xbaadf00ddeadbeef");
  33. BOOST_CHECK(toJS(b) == "0xffff0000bbbaaaa");
  34. BOOST_CHECK(toJS(c) == "0x913ffc283");
  35. BOOST_CHECK(toJS(d) == "0xff00efbc");
  36. }
  37. BOOST_AUTO_TEST_CASE(test_jsToBytes)
  38. {
  39. bytes a = {0xff, 0xaa, 0xbb, 0xcc};
  40. bytes b = {0x03, 0x89, 0x90, 0x23, 0x42, 0x43};
  41. BOOST_CHECK(a == jsToBytes("0xffaabbcc"));
  42. BOOST_CHECK(b == jsToBytes("38990234243"));
  43. BOOST_CHECK(bytes() == jsToBytes(""));
  44. BOOST_CHECK(bytes() == jsToBytes("Invalid hex chars"));
  45. }
  46. BOOST_AUTO_TEST_CASE(test_padded)
  47. {
  48. bytes a = {0xff, 0xaa};
  49. BOOST_CHECK(bytes({0x00, 0x00, 0xff, 0xaa}) == padded(a, 4));
  50. bytes b = {};
  51. BOOST_CHECK(bytes({0x00, 0x00, 0x00, 0x00}) == padded(b, 4));
  52. bytes c = {0xff, 0xaa, 0xbb, 0xcc};
  53. BOOST_CHECK(bytes{0xcc} == padded(c, 1));
  54. }
  55. BOOST_AUTO_TEST_CASE(test_paddedRight)
  56. {
  57. bytes a = {0xff, 0xaa};
  58. BOOST_CHECK(bytes({0xff, 0xaa, 0x00, 0x00}) == paddedRight(a, 4));
  59. bytes b = {};
  60. BOOST_CHECK(bytes({0x00, 0x00, 0x00, 0x00}) == paddedRight(b, 4));
  61. bytes c = {0xff, 0xaa, 0xbb, 0xcc};
  62. BOOST_CHECK(bytes{0xff} == paddedRight(c, 1));
  63. }
  64. BOOST_AUTO_TEST_CASE(test_unpadded)
  65. {
  66. bytes a = {0xff, 0xaa, 0x00, 0x00, 0x00};
  67. BOOST_CHECK(bytes({0xff, 0xaa}) == unpadded(a));
  68. bytes b = {0x00, 0x00};
  69. BOOST_CHECK(bytes() == unpadded(b));
  70. bytes c = {};
  71. BOOST_CHECK(bytes() == unpadded(c));
  72. }
  73. BOOST_AUTO_TEST_CASE(test_unpaddedLeft)
  74. {
  75. bytes a = {0x00, 0x00, 0x00, 0xff, 0xaa};
  76. BOOST_CHECK(bytes({0xff, 0xaa}) == unpadLeft(a));
  77. bytes b = {0x00, 0x00};
  78. BOOST_CHECK(bytes() == unpadLeft(b));
  79. bytes c = {};
  80. BOOST_CHECK(bytes() == unpadLeft(c));
  81. }
  82. BOOST_AUTO_TEST_CASE(test_fromRaw)
  83. {
  84. //non ascii characters means empty string
  85. h256 a("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  86. BOOST_CHECK("" == fromRaw(a));
  87. h256 b("");
  88. BOOST_CHECK("" == fromRaw(b));
  89. h256 c("0x4173636969436861726163746572730000000000000000000000000000000000");
  90. BOOST_CHECK("AsciiCharacters" == fromRaw(c));
  91. }
  92. BOOST_AUTO_TEST_CASE(test_jsToFixed)
  93. {
  94. h256 a("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  95. BOOST_CHECK(a == jsToFixed<32>("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  96. h256 b("0x000000000000000000000000000000000000000000000000000000740c54b42f");
  97. BOOST_CHECK(b == jsToFixed<32>("498423084079"));
  98. BOOST_CHECK(h256() == jsToFixed<32>("NotAHexadecimalOrDecimal"));
  99. }
  100. BOOST_AUTO_TEST_CASE(test_jsToInt)
  101. {
  102. BOOST_CHECK(43832124 == jsToInt("43832124"));
  103. BOOST_CHECK(1342356623 == jsToInt("0x5002bc8f"));
  104. BOOST_CHECK(3483942 == jsToInt("015224446"));
  105. BOOST_CHECK(0 == jsToInt("NotAHexadecimalOrDecimal"));
  106. BOOST_CHECK(u256("983298932490823474234") == jsToInt<32>("983298932490823474234"));
  107. BOOST_CHECK(u256("983298932490823474234") == jsToInt<32>("0x354e03915c00571c3a"));
  108. BOOST_CHECK(u256() == jsToInt<32>("NotAHexadecimalOrDecimal"));
  109. BOOST_CHECK(u128("228273101986715476958866839113050921216") == jsToInt<16>("0xabbbccddeeff11223344556677889900"));
  110. BOOST_CHECK(u128() == jsToInt<16>("NotAHexadecimalOrDecimal"));
  111. }
  112. BOOST_AUTO_TEST_CASE(test_jsToU256)
  113. {
  114. BOOST_CHECK(u256("983298932490823474234") == jsToU256("983298932490823474234"));
  115. BOOST_CHECK(u256() == jsToU256("NotAHexadecimalOrDecimal"));
  116. }
  117. BOOST_AUTO_TEST_SUITE_END()