Ticket #10110: test.2.cpp

File test.2.cpp, 3.2 KB (added by Maximilian Kleinert <kleinert.max@…>, 8 years ago)

Test paramter assertions

Line 
1#include <iostream>
2#include <random>
3#include <typeinfo>
4
5#define BOOST_ENABLE_ASSERT_HANDLER
6#include <boost/assert.hpp>
7#include <boost/random.hpp>
8
9#include <setjmp.h>
10#include <csignal>
11#include <cxxabi.h>
12
13jmp_buf env;
14void signal_handler(int signal)
15{
16 longjmp(env,2);
17}
18
19namespace boost {
20inline void assertion_failed(char const * expr, char const * function, char
21 const * file, long line)
22{
23 std::cout << "BOOST_ASSERT: " << file << ":" << line << ": " << function << ": Assertion `" << expr << "' failed." << std::endl;
24 throw std::runtime_error("BOOST_ASSERT failed.");
25}
26}
27
28
29template <typename T>
30void test(const T k, const double p)
31{
32 int status;
33 char * realname = abi::__cxa_demangle(typeid(k).name(), 0, 0, &status);
34 std::cout << "Called with type for parameter k: " << realname << std::endl;
35 std::cout << "-----------------------------------------------------------" << std::endl;
36 std::random_device rd;
37 std::mt19937 gen(rd());
38
39 std::cout << std::endl << "STD RANDOM TEST" << std::endl;
40 std::cout << "Trying: std::negative_binomial_distribution (k = " << k << ", p = " << p << ")" << std::endl;
41 int val = setjmp (env);
42 if (!val) {
43 std::negative_binomial_distribution<> nb(k, p);
44 std::cout << "**************************" << std::endl;
45 std::cout << "*** Constructor passed ***" << std::endl;
46 std::cout << "**************************" << std::endl;
47 std::cout << "Sample: " << nb(gen) << std::endl;
48 std::cout << "**************************" << std::endl;
49 std::cout << "**** Sampling passed *****" << std::endl;
50 std::cout << "**************************" << std::endl;
51 }
52
53 std::cout << "-----------------------------------------------------------" << std::endl;
54 std::cout << std::endl << "BOOST TEST" << std::endl;
55 boost::random::mt19937 rng;
56 std::cout << "Trying: boost::random::negative_binomial_distribution (k = " << k << ", p = " << p << ")" << std::endl;
57 try
58 {
59 boost::random::negative_binomial_distribution<> nb_boost(k, p);
60 try
61 {
62 std::cout << "**************************" << std::endl;
63 std::cout << "*** Constructor passed ***" << std::endl;
64 std::cout << "**************************" << std::endl;
65 std::cout << "Sample: " << nb_boost(rng) << std::endl;
66 std::cout << "**************************" << std::endl;
67 std::cout << "**** Sampling passed *****" << std::endl;
68 std::cout << "**************************" << std::endl;
69 }
70 catch (const std::runtime_error& error) {
71 std::cout << "Creating random samples: Assertion failed!" << std::endl;
72 }
73 }
74 catch (const std::runtime_error& error) {
75 std::cout << "Constructor: Assertion failed!" << std::endl;
76 }
77 std::cout << "===========================================================" << std::endl << std::endl;
78}
79int main()
80{
81 std::signal(SIGABRT, &signal_handler);
82
83 const int k = 1;
84 const double stepsize = 0.5;
85 double p = -0.5;
86
87 for (int i=0; i <= 4; ++i)
88 {
89 test<int>(k, p);
90 p += stepsize;
91 }
92 test<double>(0.99, 0.5);
93}