1/* 2 * Copyright (c) 2015 ETH Zurich. 3 * All rights reserved. 4 * 5 * This file is distributed under the terms in the attached LICENSE file. 6 * If you do not find this file, copies can be found by writing to: 7 * ETH Zurich D-INFK, Universitaetsstrasse 4, CH-8092 Zurich. Attn: Systems Group. 8 */ 9 10#include <iostream> 11#include <chrono> 12#include <thread> 13#include <string> 14 15#include "cxxtest.hpp" 16 17static void func(int d) 18{ 19 using namespace std::chrono; 20 steady_clock::time_point start = steady_clock::now(); 21 steady_clock::time_point current, notified; 22 while (true) { 23 current = steady_clock::now(); 24 if (duration_cast<std::chrono::duration<double>> 25 (current - start).count() >= d){ 26 break; 27 } 28 else { 29 // notify user how much time of the experiment has passed 30 long diff = duration_cast<std::chrono::duration 31 <double>>(current - notified).count(); 32 if (diff >= (d/100.0)) { 33 notified = high_resolution_clock::now(); 34 diff = duration_cast<std::chrono::duration 35 <double>>(notified - start).count(); 36 std::cout << (int) (100.0 * diff / d) 37 << "% completed." << std::endl; 38 } 39 // sleep for a while 40 std::this_thread::sleep_for(std::chrono::milliseconds(500)); 41 } 42 } 43} 44 45static void timeout_test(void) { 46 auto s = std::chrono::steady_clock::now(); 47 std::thread t(func, 10); 48 t.join(); 49 auto d = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - s); 50 std::cout << ((d.count() >= 10) ? "PASS" : "FAIL") << std::endl; 51} 52 53void stl_chrono_test(void) 54{ 55 timeout_test(); 56} 57