void test_promise_set_exception_ref() { boost::fibers::promise< int& > p1; boost::fibers::future< int& > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); p1.set_exception( std::make_exception_ptr( my_exception() ) ); // set exception a second time bool thrown = false; try { p1.set_exception( std::make_exception_ptr( my_exception() ) ); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); // set value thrown = false; int i = 11; try { p1.set_value( i); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); }
void test_promise_set_exception_void() { boost::fibers::promise< void > p1; BOOST_CHECK( p1); boost::fibers::future< void > f1 = p1.get_future(); BOOST_CHECK( f1); BOOST_CHECK( f1.valid() ); p1.set_exception( boost::copy_exception( my_exception() ) ); // set exception a second time bool thrown = false; try { p1.set_exception( boost::copy_exception( my_exception() ) ); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); // set value thrown = false; try { p1.set_value(); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); }
int main() { boost::throw_exception(my_exception()); BOOST_TEST(called); return boost::report_errors(); }
//////////////////////////////////////////////////////////////////////////////// // norm_ppf // // calculates the percent point function (inverse of the cdf) for the normal // // distribution at a a given probability 'p' (0 < p < 1). // // // // Implementation algorithm translated from FORTRAN code in DATAPAC // // http://gams.nist.gov/serve.cgi/ModuleComponent/2674/Documentation/ITL // // /normppf // //////////////////////////////////////////////////////////////////////////////// double norm_ppf(double p) { const double p0 = -0.322232431088; const double p1 = -1.0; const double p2 = -0.342242088547; const double p3 = -0.204231210245e-1; const double p4 = -0.453642210148e-4; const double q0 = 0.993484626060e-1; const double q1 = 0.588581570495; const double q2 = 0.531103462366; const double q3 = 0.103537752850; const double q4 = 0.38560700634e-2; if (p <= 0 || p >= 1.0) { throw(my_exception("invalid argument in function norm_ppf")); return 0; } if (p == .5) return 0; double r = p; if (p > .5) r = 1.0 - r; double t = sqrt(-2.0*log(r)); double num = ((((t*p4 + p3) * t + p2) * t + p1) * t + p0); double den = ((((t*q4 + q3) * t + q2) * t + q1) * t + q0); double ppf = t + num/den; if (p < .5) ppf = - ppf; return ppf; }
int main() { BOOST_THROW_EXCEPTION( my_exception() << my_int(42) ); BOOST_TEST(called); return boost::report_errors(); }
//////////////////////////////////////////////////////////////////////////////// // MobileStation::get_connection_AC // // // // returns access category of the connection with a terminal. If the // // connection does not exist then an exception is thrown. // //////////////////////////////////////////////////////////////////////////////// accCat MobileStation::get_connection_AC(Terminal* t) { if (t != connected.first) throw(my_exception(GENERAL, "unknown terminal in MobileStation::get_connection_AC")); return connected.second; }
ScalarWithExceptions operator+(const ScalarWithExceptions& other) const { countdown--; if(countdown<=0) throw my_exception(); return ScalarWithExceptions(*v+*other.v); }
void echo( push_coro_t & sink, int j) { for ( int i = 0; i < j; ++i) { if ( i == 5) boost::throw_exception( my_exception("abc") ); sink( i); } }
//////////////////////////////////////////////////////////////////////////////// // AccessPoint::get_current_mode // //////////////////////////////////////////////////////////////////////////////// transmission_mode AccessPoint::get_current_mode(Terminal* t,unsigned pl) { map<Terminal*, tuple<link_adapt, Traffic*, accCat> >::iterator it = connection.find(t); if (it == connection.end()) throw(my_exception(GENERAL, "unknown terminal in AccessPoint::get_current_mode")); return (get<0>(it->second)).get_current_mode(pl); }
//////////////////////////////////////////////////////////////////////////////// // AccessPoint::get_connection_AC // // // // returns access category of the connection with a terminal. If the // // connection does not exist then an exception is thrown. // //////////////////////////////////////////////////////////////////////////////// accCat AccessPoint::get_connection_AC(Terminal* t) { map<Terminal*, tuple<link_adapt, Traffic*, accCat> >::const_iterator it = connection.find(t); if (it == connection.end()) throw(my_exception(GENERAL, "unknown terminal in AccessPoint::get_connection_AC")); return get<2>(it->second); }
void operator()() const { boost::unique_lock<boost::mutex> lock(m); ++pass_counter; if(pass_counter<3) { throw my_exception(); } }
//////////////////////////////////////////////////////////////////////////////// // Simulation::run // // // // starts all simulations, iterates over all parameter combinations // //////////////////////////////////////////////////////////////////////////////// void Simulation::run() { int n_it = 0; do { n_it++; if(sim_par.get_partResults()) { cout << "\n\nIteration " << n_it << "\n " << sim_par << "\n" << endl; out << "\n\nIteration " << n_it << "\n " << sim_par << "\n" << endl; } if (log(log_type::setup)) log << "\n\nIteration " << n_it << "\n\t" << sim_par << "\n" << endl; /* #ifdef _SAVE_RATE_ADAPT rate_adapt_file_ch << "\n\nIteration " << n_it << "\n\t" << sim_par << "\n" << "Time ,term 1,term 2,path loss ," << "fading(R) ,fading(I)\n"; rate_adapt_file_ch.setf(ios::left); rate_adapt_file_rt << "\n\nIteration " << n_it << "\n\t" << sim_par << "\n" << "Time ,sender,target,data rate\n"; rate_adapt_file_rt.setf(ios::left); #endif */ main_sch.init(); randgent.seed(sim_par.get_Seed()); Standard::set_standard(sim_par.get_standard(),sim_par.get_bandwidth(),sim_par.get_shortGI()); if(sim_par.get_TxMode() > Standard::get_maxMCS()) throw (my_exception("MCS not supported by standard.")); channel_struct ch_par(sim_par.get_LossExponent(), sim_par.get_RefLoss(), sim_par.get_DopplerSpread(), sim_par.get_NumberSinus()); ch = new Channel(&main_sch, &randgent, ch_par, &log); init_terminals(); start_sim(); wrap_up(); delete ch; for (vector<Terminal*>::iterator it = term_vector.begin(); it != term_vector.end(); ++it) delete *it; term_vector.clear(); } while (sim_par.new_iteration()); final_results(); }
//////////////////////////////////////////////////////////////////////////////// // MobileStation::connect // // // // creates connection to another terminal // //////////////////////////////////////////////////////////////////////////////// void MobileStation::connect(Terminal* t, adapt_struct ad, traffic_struct ts, accCat AC) { if (connected.first != this) { throw(my_exception("second connection attempted to a Mobile Station")); } connected = make_pair(t,AC); tr = new Traffic(ptr2sch, randgen, mylog, this, t, ts); la = link_adapt(this, t, ad, mylog); }
//////////////////////////////////////////////////////////////////////////////// // Simulation constructor // //////////////////////////////////////////////////////////////////////////////// Simulation::Simulation(string dir, string par) : wdir(dir) { if (!sim_par.read_param(wdir)) throw (my_exception(CONFIG)); log.open (wdir,sim_par.get_Log()); string filename = wdir + '\\' + OUTPUT_FILE_NAME + OUTPUT_FILE_EXTENSION; out.open(filename.c_str()); run(); }
void test_ontop_exception() { value1 = 0; value2 = ""; void * ignored; ctx::captured_context ctx( fn6); std::tie( ctx, ignored) = ctx(); BOOST_CHECK_EQUAL( 3, value1); const char * what = "hello world"; ctx( ctx::exec_ontop_arg, [what](ctx::captured_context ctx, void * data){ throw my_exception( what, std::move( ctx) ); return std::make_tuple( std::move( ctx), data); }); BOOST_CHECK_EQUAL( 3, value1); BOOST_CHECK_EQUAL( std::string( what), value2); }
int main() { try { errno=1; throw my_exception() << info_errno(errno); BOOST_TEST(false); } catch( my_exception & x ) { BOOST_TEST(1==*boost::get_error_info<info_errno>(x)); } return boost::report_errors(); }
int main() { try { boost::throw_exception(my_exception()); BOOST_ERROR("boost::throw_exception failed to throw."); } catch( my_exception & ) { } catch( ... ) { BOOST_ERROR("boost::throw_exception malfunction."); } return boost::report_errors(); }
int main() { ctx::fiber f{[](ctx::fiber && f) ->ctx::fiber { std::cout << "entered" << std::endl; try { f = std::move( f).resume(); } catch ( my_exception & ex) { std::cerr << "my_exception: " << ex.what() << std::endl; return std::move( ex.f); } return {}; }}; f = std::move( f).resume(); f = std::move( f).resume_with([](ctx::fiber && f) ->ctx::fiber { throw my_exception(std::move( f), "abc"); return {}; }); std::cout << "main: done" << std::endl; return EXIT_SUCCESS; }
int main() { try { throw my_exception(); } catch( std::exception & e ) { try { throw; } catch( ...) { BOOST_TEST(boost::current_exception_cast<std::exception>()==&e); BOOST_TEST(!boost::current_exception_cast<polymorphic>()); } } return boost::report_errors(); }
int main() { try { throw my_exception(); } catch( boost::exception & e ) { e.set<tag1>(42); e.set<tag2>("42"); e.set<tag_error_code>(42); //Implicit conversion try { throw; } catch( my_exception & e ) { BOOST_TEST(e.get<tag1>() && *e.get<tag1>()==42); BOOST_TEST(e.get<tag2>() && *e.get<tag2>()=="42"); BOOST_TEST(!e.get<tag3>()); BOOST_TEST(e.get<tag_error_code>() && e.get<tag_error_code>()->ec==42); //Below we're verifying that an error code wrapped in a user-defined type //invokes the correct op<< to convert the error code to string. //Note that N3757 diagnostic_information uses different syntax, it is a //member of of std::exception. std::string di=boost::diagnostic_information(e); BOOST_TEST(!di.empty()); BOOST_TEST(my_error_code_to_string_called); } } return boost::report_errors(); }
void some_func( int i ) { if( i>0 ) throw my_exception( i ); }
void set_promise_exception_thread(boost::promise<int>* p) { p->set_exception(boost::copy_exception(my_exception())); }
//////////////////////////////////////////////////////////////////////////////// // Simulation::wrap_up // // // // ends one iteration and collect performance results, // // outputs them if required (if 'it_file_flag == true') // //////////////////////////////////////////////////////////////////////////////// void Simulation::wrap_up () { res_stats res; if(main_sch.now() <= sim_par.get_TransientTime()) throw (my_exception("transient time was longer than simulation time")); double ellapsed_time = double(main_sch.now() - sim_par.get_TransientTime()); if(sim_par.get_partResults()) { out.setf(ios::right | ios::fixed); out << Standard::get_standard() << endl; out << "Term Position dist. AC throughput transfer_t tx_time packets" << " kbytes pack_loss overflow queue_l tx_rate(PHY) tx_power" << endl; out << " m m Mbps ms ms " << " Mbps mW" << endl; } for (vector<Terminal*>::iterator it = term_vector.begin(); it != term_vector.end(); ++it) { double tp = (*it)->get_n_bytes()*8.0/ellapsed_time/1e6; res.add (res_struct(tp,(*it)->get_transfer_delay(), (*it)->get_transfer_delay_std(), (*it)->get_transmission_delay(), (*it)->get_transmission_delay_std(), (*it)->get_packet_loss_rate(), (*it)->get_overflow_rate(), (*it)->get_queue_length(), (*it)->get_average_power())); if(sim_par.get_partResults()) { out << setw(4) << (*it)->get_id() << " "; out.precision(0); out.width(3); out << (*it)->get_pos(); out.precision(1); out << setw(5) << ((*it)->get_pos()).distance(); out << " " << (*it)->get_term_ACs(); out.precision(3); out << " " << setw(8) << tp; out.precision(2); out << setw(12) << (*it)->get_transfer_delay() * 1000; out << setw(8) << (*it)->get_transmission_delay() * 1000; out << setw(8) << (*it)->get_n_packets(); out << setw(8) << (*it)->get_n_bytes()/1000; out.precision(4); out << setw(8) << (*it)->get_packet_loss_rate(); out << setw(9) << (*it)->get_overflow_rate(); out.precision(1); out << setw(8) << (*it)->get_queue_length(); out.precision(2); out << setw(10) << (*it)->get_tx_data_rate(); out.precision(1); out << setw(9) << (*it)->get_average_power(); out << endl; } } if(sim_par.get_partResults()) { out.unsetf(ios::right | ios::fixed); out.precision(6); } res_struct restotal(res.throughput.sum(), res.transfer_time.mean(), res.transfer_time_std.mean(), res.tx_time.mean(), res.tx_time_std.mean(), res.packet_loss_rate.mean(), res.overflow_rate.mean(), res.queue_length.mean(), res.average_power.mean()); if(sim_par.get_partResults()) { out << "\n Total throughput = " << restotal.throughput << " Mbps\n"; cout << "\nTotal throughput = " << restotal.throughput << " Mbps\n"; out << " Average transfer time = " << restotal.transfer_time << "s\n"; cout << "\nAverage transfer time = " << restotal.transfer_time << "s\n"; out << " Average transmission time = " << restotal.tx_time << "s\n"; cout << "\nAverage transmission time = " << restotal.tx_time << "s\n"; out << " Packet loss rate = " << restotal.packet_loss_rate << "\n"; cout << "\nPacket loss rate = " << restotal.packet_loss_rate << "\n"; out << " Overflow rate = " << restotal.overflow_rate << "\n"; cout << "\nOverflow rate = " << restotal.overflow_rate << "\n"; } results.push_back(restotal); }
int main() { std::cout << "*** Testing ThreadPool ***" << std::endl; doTest("post job", []() { ThreadPool pool; std::packaged_task<int()> t([](){ std::this_thread::sleep_for(std::chrono::milliseconds(1)); return 42; }); std::future<int> r = t.get_future(); pool.post(t); ASSERT(42 == r.get()); }); doTest("process job", []() { ThreadPool pool; std::future<int> r = pool.process([]() { std::this_thread::sleep_for(std::chrono::milliseconds(1)); return 42; }); ASSERT(42 == r.get()); }); struct my_exception {}; doTest("process job with exception", []() { ThreadPool pool; std::future<int> r = pool.process([]() { std::this_thread::sleep_for(std::chrono::milliseconds(1)); throw my_exception(); return 42; }); try { ASSERT(r.get() == 42 && !"should not be called, exception expected"); } catch (const my_exception &e) { } }); doTest("multiple compilation units", []() { extern size_t getWorkerIdForCurrentThread(); extern size_t getWorkerIdForCurrentThread2(); ThreadPool pool; std::future<std::tuple<size_t, size_t, size_t, size_t>> r = pool.process([]() { return std::make_tuple(Worker::getWorkerIdForCurrentThread(), *detail::thread_id(), getWorkerIdForCurrentThread(), getWorkerIdForCurrentThread2()); }); const auto t = r.get(); const auto id0 = std::get<0>(t); const auto id1 = std::get<1>(t); const auto id2 = std::get<2>(t); const auto id3 = std::get<3>(t); std::cout << " " << id0 << " " << id1 << " " << id2 << " " << id3; ASSERT(id0 == id1); ASSERT(id1 == id2); ASSERT(id2 == id3); }); }
//////////////////////////////////////////////////////////////////////////////// // student_ppf // // // // calculates the percent point function (inverse of the cdf) for student's t // // distribution at a a given probability 'p' for 'nu' degrees of freedom. // // 'nu' must be strictly positive and 0 < p < 1. // // // // Implementation algorithm translated from FORTRAN code in DATAPAC // // http://gams.nist.gov/serve.cgi/ModuleComponent/2732/Documentation/ITL/tppf // //////////////////////////////////////////////////////////////////////////////// double student_ppf(double p, int nu) { const double b21 = .25; const double b31 = .01041666666667; const double b32 = 5.0; const double b33 = 16.0; const double b34 = 3.0; const double b41 = 0.00260416666667; const double b42 = 3.0; const double b43 = 19.0; const double b44 = 17.0; const double b45 = -15.0; const double b51 = 0.00001085069444; const double b52 = 79.0; const double b53 = 776.0; const double b54 = 1482.0; const double b55 = -1920.0; const double b56 = -945.0; const int maxit = 5; if (p <= 0 || p >= 1.0 || nu < 1) { throw(my_exception("invalid argument in function student_ppf")); return 0; } if (nu == 1) { double arg = M_PI * p; return -cos(arg) / sin(arg); } else if (nu == 2) { double term1 = M_SQRT2 / 2.0; double term2 = 2.0 * p - 1.0; double term3 = sqrt(p*(1.0-p)); return term1 * term2 / term3; } else { double ppfn = norm_ppf(p); double d1 = ppfn; double d3 = ppfn * ppfn * ppfn; double d5 = d3 * ppfn * ppfn; double d7 = d5 * ppfn * ppfn; double d9 = d7 * ppfn * ppfn; double term1 = d1; double term2 = b21 * (d3 + d1) / double(nu); double term3 = b31 * (b32*d5 + b33*d3 + b34*d1) / double(nu*nu); double term4 = b41 * (b42*d7 + b43*d5 + b44*d3 + b45*d1) / double(nu*nu*nu); double term5 = b51 * (b52*d9 + b53*d7 + b54*d5 + b55*d3 + b56*d1) / double(nu*nu*nu*nu); double ppf = term1 + term2 + term3 + term4 + term5; if (nu == 3) { double con = M_PI * (p - .5); double arg = ppf / sqrt(double(nu)); double z = atan(arg); double s, c; for (int i = 1; i <= maxit; ++i) { s = sin(z); c = cos(z); z -= (z + s*c - con) / (2.0*c*c); } ppf = sqrt(double(nu)) * s / c; } else if (nu == 4) { double con = 2.0 * (p - .5); double arg = ppf / sqrt(double(nu)); double z = atan(arg); double s, c; for (int i = 1; i <= maxit; ++i) { s = sin(z); c = cos(z); z -= ((1.0 + .5*c*c) * s - con) / (1.5*c*c*c); } ppf = sqrt(double(nu)) * s / c; } else if (nu == 5) { double con = M_PI * (p - .5); double arg = ppf / sqrt(double(nu)); double z = atan(arg); double s, c; for (int i = 1; i <= maxit; ++i) { s = sin(z); c = cos(z); z -= (z + (c + (2.0/3.0)*c*c*c) * s - con) / ((8.0/3.0)*c*c*c*c); } ppf = sqrt(double(nu)) * s / c; } else if (nu == 6) { double con = 2.0 * (p - .5); double arg = ppf / sqrt(double(nu)); double z = atan(arg); double s, c; for (int i = 1; i <= maxit; ++i) { s = sin(z); c = cos(z); z -= ((1.0 + .5*c*c + .375*c*c*c*c) * s - con) / ((15.0/8.0)*c*c*c*c*c); } ppf = sqrt(double(nu)) * s / c; } return ppf; } }
void fn6() { boost::throw_exception( my_exception() ); }
int fn5() { boost::throw_exception( my_exception() ); return 3; }
A fn10() { boost::throw_exception( my_exception() ); return A(); }
int main() { std::cout << "*** Testing ThreadPool ***" << std::endl; doTest("post job", []() { ThreadPool pool; std::packaged_task<int()> t([](){ std::this_thread::sleep_for(std::chrono::milliseconds(1)); return 42; }); std::future<int> r = t.get_future(); pool.post(t); ASSERT(42 == r.get()); }); doTest("process job", []() { ThreadPool pool; std::future<int> r = pool.process([]() { std::this_thread::sleep_for(std::chrono::milliseconds(1)); return 42; }); ASSERT(42 == r.get()); }); struct my_exception {}; doTest("process job with exception", []() { ThreadPool pool; std::future<int> r = pool.process([]() { std::this_thread::sleep_for(std::chrono::milliseconds(1)); throw my_exception(); return 42; }); try { ASSERT(r.get() == 42 && !"should not be called, exception expected"); } catch (const my_exception &e) { } }); doTest("post job to threadpool with onStart/onStop", []() { std::atomic<int> someValue{0}; ThreadPoolOptions options; options.onStart = [&someValue](){ ++someValue; }; options.onStop = [&someValue](){ --someValue; }; if (true) { ThreadPool pool{options}; std::packaged_task<int()> t([&someValue](){ std::this_thread::sleep_for(std::chrono::milliseconds(1)); return someValue.load(); }); std::future<int> r = t.get_future(); pool.post(t); const auto result = r.get(); ASSERT(0 < result); ASSERT(pool.getWorkerCount() == result); } ASSERT(0 == someValue); }); }
int throw_an_exception() { printf("throw_an_exception()\n"); // std::cout << "throw_an_exception()" << std::endl; throw my_exception("my exception"); }