From 40426fe405dc795668351cb41bd50d8d08e5b094 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 27 Jul 2017 14:43:55 +0300 Subject: Complete progress test --- tests/progress/driver.cxx | 67 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) (limited to 'tests/progress') diff --git a/tests/progress/driver.cxx b/tests/progress/driver.cxx index 702319a..64e84fd 100644 --- a/tests/progress/driver.cxx +++ b/tests/progress/driver.cxx @@ -3,22 +3,57 @@ // license : MIT; see accompanying LICENSE file #ifndef _WIN32 +# include // write() + # include // this_thread::sleep_for() #else # include + +# include //_write() #endif #include // size_t #include +#include +#include // fdnull(), stderr_fd() #include using namespace std; using namespace butl; +// Usage: +// +// argv[0] [-n] [-c] +// +// -n +// Do not run child process. By default the program runs itself with -c +// option (see below). +// +// -c +// Run as a child process that just prints lines with a small but varying +// delay. +// int -main () +main (int argc, const char* argv[]) { + bool child (false); + bool no_child (false); + + assert (argc > 0); + + for (int i (1); i != argc; ++i) + { + string v (argv[i]); + + if (v == "-c") + child = true; + else if (v == "-n") + no_child = true; + else + assert (false); + } + auto sleep = [] (size_t ms = 100) { // MINGW GCC 4.9 doesn't implement this_thread so use Win32 Sleep(). @@ -30,6 +65,34 @@ main () #endif }; + if (child) + { + auto print = [] (const string& s) + { +#ifndef _WIN32 + write (stderr_fd(), s.c_str (), s.size ()); +#else + _write (stderr_fd(), s.c_str (), static_cast (s.size ())); +#endif + }; + + for (size_t i (50); i != 0; --i) + { + print ("Child line " + to_string (i) + '\n'); + sleep (200 - i); + } + + return 0; + } + + // @@ Can't compile unless convert to process_env() explicitly (GCC still + // warns about calls ambiguity). + // + process pr (!no_child + ? process_start (fdnull (), fdnull (), stderr_fd (), + process_env (argv[0]), "-c") + : process (process_exit (0))); // Exited normally. + for (size_t i (100); i != 0; --i) { if (i % 10 == 0) @@ -59,4 +122,6 @@ main () } sleep (1000); + + assert (pr.wait ()); } -- cgit v1.1