Fabcoin Core  0.16.2
P2P Digital Currency
boostTest.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #define BOOST_TEST_MODULE EthereumTests
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wunused-parameter"
25 //#define BOOST_DISABLE_WIN32 //disables SEH warning
26 #define BOOST_TEST_NO_MAIN
27 
28 #if defined(_MSC_VER)
29 #pragma warning(push)
30 #pragma warning(disable:4535) // calling _set_se_translator requires /EHa
31 #endif
32 #include <boost/test/included/unit_test.hpp>
33 #if defined(_MSC_VER)
34 #pragma warning(pop)
35 #endif
36 
37 #pragma GCC diagnostic pop
38 
39 #include <clocale>
40 #include <stdlib.h>
42 #include <boost/version.hpp>
43 
44 using namespace boost::unit_test;
45 
46 std::vector<char*> parameters;
47 static std::ostringstream strCout;
48 std::streambuf* oldCoutStreamBuf;
49 std::streambuf* oldCerrStreamBuf;
50 
51 //Custom Boost Initialization
52 test_suite* fake_init_func(int argc, char* argv[])
53 {
54  //Required for boost. -nowarning
55  (void) argc;
56  (void) argv;
57  return 0;
58 }
59 
61 {
62  //restore output for creating test
63  std::cout.rdbuf(oldCoutStreamBuf);
64  std::cerr.rdbuf(oldCerrStreamBuf);
65 
66  //For no reason BOOST tend to remove valuable arg -t "TestSuiteName"
67  //And can't hadle large input stream of data
68  //so the actual test suite and raw test input is read into Options
70  throw framework::internal_error("Create Random Test Error!");
71  else
72  {
73  //disable post output so the test json would be clean
74  if (dev::test::Options::get().rCheckTest.size() > 0)
75  std::cout << "correct" << std::endl;
76  exit(0);
77  }
78 }
79 
80 static std::atomic_bool stopTravisOut;
81 void travisOut()
82 {
83  int tickCounter = 0;
84  while (!stopTravisOut)
85  {
86  std::this_thread::sleep_for(std::chrono::seconds(1));
87  ++tickCounter;
88  if (tickCounter % 10 == 0)
89  std::cout << "." << std::endl; // Output dot every 10s.
90  }
91 }
92 
93 /*
94 The equivalent of setlocale(LC_ALL, ā€œCā€) is called before any user code is run.
95 If the user has an invalid environment setting then it is possible for the call
96 to set locale to fail, so there are only two possible actions, the first is to
97 throw a runtime exception and cause the program to quit (default behaviour),
98 or the second is to modify the environment to something sensible (least
99 surprising behaviour).
100 
101 The follow code produces the least surprising behaviour. It will use the user
102 specified default locale if it is valid, and if not then it will modify the
103 environment the process is running in to use a sensible default. This also means
104 that users do not need to install language packs for their OS.
105 */
107 {
108 #if __unix__
109  if (!std::setlocale(LC_ALL, ""))
110  {
111  setenv("LC_ALL", "C", 1);
112  }
113 #endif
114 }
115 
116 //Custom Boost Unit Test Main
117 int main( int argc, char* argv[] )
118 {
120  //Initialize options
121  dev::test::Options const& opt = dev::test::Options::get(argc, argv);
122 
123  if (opt.createRandomTest)
124  {
125  //disable initial output
126  oldCoutStreamBuf = std::cout.rdbuf();
127  oldCerrStreamBuf = std::cerr.rdbuf();
128  std::cout.rdbuf(strCout.rdbuf());
129  std::cerr.rdbuf(strCout.rdbuf());
130 
131  for (int i = 0; i < argc; i++)
132  {
133  std::string arg = std::string{argv[i]};
134 
135  //replace test suite to random tests
136  if (arg == "-t" && i+1 < argc)
137  argv[i+1] = (char*)std::string("RandomTestCreationSuite").c_str();
138 
139  //don't pass long raw test input to boost
140  if (arg == "--checktest")
141  {
142  argc = i + 1;
143  break;
144  }
145  }
146 
147  //add random tests suite
148  test_suite* ts1 = BOOST_TEST_SUITE("RandomTestCreationSuite");
149  ts1->add(BOOST_TEST_CASE(&createRandomTest));
150  framework::master_test_suite().add(ts1);
151  }
152 
153  for (int i = 0; i < argc; i++)
154  parameters.push_back(argv[i]);
155 
156  stopTravisOut = false;
157  std::future<int> ret = std::async(unit_test_main, fake_init_func, argc, argv);
158  std::thread outputThread(travisOut);
159  int result = ret.get();
160  stopTravisOut = true;
161  outputThread.join();
163  return result;
164 }
static Options const & get(int argc=0, char **argv=0)
Get reference to options The first time used, options are parsed with argc, argv. ...
Definition: Options.cpp:203
void createRandomTest()
Definition: boostTest.cpp:60
std::streambuf * oldCerrStreamBuf
Definition: boostTest.cpp:49
bool createRandomTest
Generate random test.
Definition: Options.h:50
int main(int argc, char *argv[])
Definition: boostTest.cpp:117
void setDefaultOrCLocale()
Definition: boostTest.cpp:106
void travisOut()
Definition: boostTest.cpp:81
test_suite * fake_init_func(int argc, char *argv[])
Definition: boostTest.cpp:52
std::streambuf * oldCoutStreamBuf
Definition: boostTest.cpp:48
std::vector< char * > parameters
Definition: boostTest.cpp:46
int createRandomTest(std::vector< char * > const &_parameters)
Helper functions to work with json::spirit and test files.