memorydb.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 MemoryDB.cpp
  15. * @author Christoph Jentzsch <cj@ethdev.com>
  16. * @date 2015
  17. * memDB test functions.
  18. */
  19. #include <boost/test/unit_test.hpp>
  20. #include <iostream>
  21. #include <libdevcore/MemoryDB.h>
  22. #include <test/TestHelper.h>
  23. using namespace std;
  24. using namespace dev;
  25. using namespace dev::test;
  26. namespace dev { namespace test {
  27. } }// Namespace Close
  28. BOOST_FIXTURE_TEST_SUITE(memDB, TestOutputHelper)
  29. BOOST_AUTO_TEST_CASE(kill)
  30. {
  31. MemoryDB myDB;
  32. BOOST_CHECK(myDB.get().empty());
  33. bytes value = fromHex("43");
  34. myDB.insert(h256(42), &value);
  35. BOOST_CHECK(myDB.exists(h256(42)));
  36. BOOST_CHECK_EQUAL(myDB.get().size(), 1);
  37. BOOST_CHECK(!myDB.kill(h256(43)));
  38. BOOST_CHECK(myDB.kill(h256(42)));
  39. }
  40. BOOST_AUTO_TEST_CASE(purgeMainMem)
  41. {
  42. MemoryDB myDB;
  43. BOOST_CHECK(myDB.get().empty());
  44. bytes value = fromHex("43");
  45. myDB.insert(h256(42), &value);
  46. MemoryDB copy;
  47. copy = myDB;
  48. BOOST_CHECK(myDB.exists(h256(42)));
  49. BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), toString(value[0]));
  50. BOOST_CHECK_EQUAL(myDB.get().size(), 1);
  51. BOOST_CHECK(myDB.kill(h256(42)));
  52. BOOST_CHECK(myDB.get() == copy.get());
  53. BOOST_CHECK(myDB.exists(h256(42)));
  54. BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), toString(value[0]));
  55. BOOST_CHECK_EQUAL(myDB.get().size(), 1);
  56. myDB.purge();
  57. BOOST_CHECK_EQUAL(myDB.get().size(), 0);
  58. myDB.insert(h256(43), &value);
  59. BOOST_CHECK_EQUAL(myDB.get().size(), 1);
  60. myDB.clear();
  61. BOOST_CHECK_EQUAL(myDB.get().size(), 0);
  62. }
  63. BOOST_AUTO_TEST_CASE(purgeMainMem_Refs)
  64. {
  65. MemoryDB myDB;
  66. {
  67. EnforceRefs enforceRefs(myDB, true);
  68. BOOST_CHECK(myDB.get().empty());
  69. bytes value = fromHex("43");
  70. myDB.insert(h256(42), &value);
  71. MemoryDB copy;
  72. copy = myDB;
  73. BOOST_CHECK(myDB.exists(h256(42)));
  74. BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), toString(value[0]));
  75. BOOST_CHECK_EQUAL(myDB.get().size(), 1);
  76. BOOST_CHECK(myDB.kill(h256(42)));
  77. BOOST_CHECK(myDB.get() != copy.get());
  78. BOOST_CHECK(!myDB.exists(h256(42)));
  79. BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), std::string());
  80. BOOST_CHECK_EQUAL(myDB.get().size(), 0);
  81. myDB.purge();
  82. BOOST_CHECK_EQUAL(myDB.get().size(), 0);
  83. myDB.insert(h256(43), &value);
  84. BOOST_CHECK_EQUAL(myDB.get().size(), 1);
  85. myDB.clear();
  86. BOOST_CHECK_EQUAL(myDB.get().size(), 0);
  87. // call EnforceRefs destructor
  88. }
  89. // do same tests again without EnforceRefs
  90. BOOST_CHECK(myDB.get().empty());
  91. bytes value = fromHex("43");
  92. myDB.insert(h256(42), &value);
  93. MemoryDB copy;
  94. copy = myDB;
  95. BOOST_CHECK(myDB.exists(h256(42)));
  96. BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), toString(value[0]));
  97. BOOST_CHECK_EQUAL(myDB.get().size(), 1);
  98. BOOST_CHECK(myDB.kill(h256(42)));
  99. BOOST_CHECK(myDB.get() == copy.get());
  100. BOOST_CHECK(myDB.exists(h256(42)));
  101. BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), toString(value[0]));
  102. BOOST_CHECK_EQUAL(myDB.get().size(), 1);
  103. myDB.purge();
  104. BOOST_CHECK_EQUAL(myDB.get().size(), 0);
  105. myDB.insert(h256(43), &value);
  106. BOOST_CHECK_EQUAL(myDB.get().size(), 1);
  107. myDB.clear();
  108. BOOST_CHECK_EQUAL(myDB.get().size(), 0);
  109. }
  110. BOOST_AUTO_TEST_CASE(purgeAuxMem)
  111. {
  112. class AuxMemDB : public MemoryDB
  113. {
  114. public:
  115. std::unordered_map<h256, std::pair<bytes, bool>> getAux() { return m_aux;}
  116. };
  117. AuxMemDB myDB;
  118. BOOST_CHECK(myDB.get().empty());
  119. bytes value = fromHex("43");
  120. myDB.insertAux(h256(42), &value);
  121. BOOST_CHECK(myDB.lookupAux(h256(42)) == value);
  122. BOOST_CHECK_EQUAL(myDB.get().size(), 0);
  123. myDB.removeAux(h256(42));
  124. BOOST_CHECK(myDB.lookupAux(h256(42)) == value);
  125. BOOST_CHECK_EQUAL(myDB.getAux().size(), 1);
  126. myDB.purge();
  127. BOOST_CHECK(myDB.lookupAux(h256(42)) == bytes());
  128. BOOST_CHECK_EQUAL(myDB.getAux().size(), 0);
  129. myDB.insertAux(h256(43), &value);
  130. BOOST_CHECK_EQUAL(myDB.getAux().size(), 1);
  131. myDB.clear();
  132. BOOST_CHECK_EQUAL(myDB.getAux().size(), 0);
  133. }
  134. BOOST_AUTO_TEST_CASE(copy)
  135. {
  136. MemoryDB myDB;
  137. BOOST_CHECK(myDB.get().empty());
  138. bytes value = fromHex("43");
  139. myDB.insert(h256(42), &value);
  140. BOOST_CHECK(myDB.exists(h256(42)));
  141. BOOST_CHECK_EQUAL(myDB.get().size(), 1);
  142. MemoryDB copyToDB;
  143. copyToDB = myDB;
  144. BOOST_CHECK(copyToDB.exists(h256(42)));
  145. BOOST_CHECK_EQUAL(copyToDB.get().size(), 1);
  146. BOOST_CHECK(myDB.keys() == copyToDB.keys());
  147. BOOST_CHECK(myDB.get() == copyToDB.get());
  148. myDB.insert(h256(43), &value);
  149. BOOST_CHECK(myDB.keys() != copyToDB.keys());
  150. }
  151. BOOST_AUTO_TEST_CASE(lookUp)
  152. {
  153. MemoryDB myDB;
  154. BOOST_CHECK(myDB.get().empty());
  155. bytes value = fromHex("43");
  156. myDB.insert(h256(42), &value);
  157. BOOST_CHECK(myDB.exists(h256(42)));
  158. BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), toString(value[0]));
  159. BOOST_CHECK_EQUAL(myDB.get().size(), 1);
  160. myDB.insert(h256(0), &value);
  161. BOOST_CHECK(myDB.exists(h256(0)));
  162. BOOST_CHECK_EQUAL(myDB.lookup(h256(0)), toString(value[0]));
  163. myDB.insert(h256(std::numeric_limits<u256>::max()), &value);
  164. BOOST_CHECK(myDB.exists(h256(std::numeric_limits<u256>::max())));
  165. BOOST_CHECK_EQUAL(myDB.lookup(h256(std::numeric_limits<u256>::max())), toString(value[0]));
  166. BOOST_CHECK_EQUAL(myDB.get().size(), 3);
  167. }
  168. BOOST_AUTO_TEST_CASE(stream)
  169. {
  170. MemoryDB myDB;
  171. BOOST_CHECK(myDB.get().empty());
  172. bytes value = fromHex("43");
  173. myDB.insert(h256(42), &value);
  174. myDB.insert(h256(43), &value);
  175. std::ostringstream stream;
  176. stream << myDB;
  177. BOOST_CHECK_EQUAL(stream.str(), "000000000000000000000000000000000000000000000000000000000000002a: 0x43 43\n000000000000000000000000000000000000000000000000000000000000002b: 0x43 43\n");
  178. }
  179. BOOST_AUTO_TEST_SUITE_END()