Common.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 Common.cpp
  15. * @author Marek Kotewicz <marek@ethdev.com>
  16. * @date 2015
  17. */
  18. #include <random>
  19. #include <boost/filesystem.hpp>
  20. #include <libdevcore/CommonData.h>
  21. #include <libdevcore/CommonIO.h>
  22. #include <libdevcore/FileSystem.h>
  23. #include "Common.h"
  24. using namespace std;
  25. using namespace dev;
  26. using namespace dev::test;
  27. const char* TestChannel::name() { return "TST"; }
  28. std::string dev::test::getTestPath()
  29. {
  30. string testPath;
  31. const char* ptestPath = getenv("ETHEREUM_TEST_PATH");
  32. if (ptestPath == NULL)
  33. {
  34. ctest << " could not find environment variable ETHEREUM_TEST_PATH \n";
  35. testPath = "../../../tests";
  36. }
  37. else
  38. testPath = ptestPath;
  39. return testPath;
  40. }
  41. int dev::test::randomNumber()
  42. {
  43. static std::mt19937 randomGenerator(utcTime());
  44. randomGenerator.seed(std::random_device()());
  45. return std::uniform_int_distribution<int>(1)(randomGenerator);
  46. }
  47. Json::Value dev::test::loadJsonFromFile(std::string const& _path)
  48. {
  49. Json::Reader reader;
  50. Json::Value result;
  51. string s = dev::contentsString(_path);
  52. if (!s.length())
  53. ctest << "Contents of " + _path + " is empty. Have you cloned the 'tests' repo branch develop and set ETHEREUM_TEST_PATH to its path?";
  54. else
  55. ctest << "FIXTURE: loaded test from file: " << _path;
  56. reader.parse(s, result);
  57. return result;
  58. }
  59. std::string dev::test::toTestFilePath(std::string const& _filename)
  60. {
  61. return getTestPath() + "/" + _filename + ".json";
  62. }
  63. std::string dev::test::getFolder(std::string const& _file)
  64. {
  65. return boost::filesystem::path(_file).parent_path().string();
  66. }
  67. std::string dev::test::getRandomPath()
  68. {
  69. std::stringstream stream;
  70. stream << getDataDir("EthereumTests") << "/" << randomNumber();
  71. return stream.str();
  72. }