blob: 61a92c9c0728e398af7d1ecc9ab4cc87948263ea [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
4*
5* This software is distributed under the terms of the GNU Affero Public License.
6* See the COPYING file in the main directory for details.
7*
8* This use of this software may be subject to additional restrictions.
9* See the LEGAL file in the main directory for details.
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Affero General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Affero General Public License for more details.
20
21 You should have received a copy of the GNU Affero General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24*/
25
26
27
28
29#include "Timeval.h"
30#include <iostream>
Pau Espin Pedrol2652f2b2018-01-09 19:12:21 +010031#include <assert.h>
Pau Espin Pedrol28ce3152018-01-15 11:46:26 +010032#include <sys/time.h>
dburgess82c46ff2011-10-07 02:40:51 +000033
34using namespace std;
35
36int main(int argc, char *argv[])
37{
Pau Espin Pedrol2652f2b2018-01-09 19:12:21 +010038 long last_remaining = 10000;
Pau Espin Pedrol28ce3152018-01-15 11:46:26 +010039 Timeval then(last_remaining);
40 assert(then.elapsed() == -last_remaining);
41 cerr << then << " elapsed: " << then.elapsed() << endl;
42
43 /* Check that last_remaining parameter affects setting time in the future */
44 usleep(10000);
45 double increased_time_secs = Timeval().seconds();
46 assert(increased_time_secs <= then.seconds());
47
48 struct timespec invariant_time = then.timespec();
Pau Espin Pedrol2652f2b2018-01-09 19:12:21 +010049 int loops = 0;
dburgess82c46ff2011-10-07 02:40:51 +000050
51 while (!then.passed()) {
Pau Espin Pedrol28ce3152018-01-15 11:46:26 +010052 struct timespec tspecnow = then.timespec();
53 cerr << "now: " << Timeval().seconds() << " then: " << then << " remaining: " << then.remaining() << endl;
54 assert(last_remaining >= then.remaining());
55 assert(tspecnow.tv_sec == invariant_time.tv_sec && tspecnow.tv_nsec == invariant_time.tv_nsec);
dburgess82c46ff2011-10-07 02:40:51 +000056 usleep(500000);
Pau Espin Pedrol2652f2b2018-01-09 19:12:21 +010057 loops++;
dburgess82c46ff2011-10-07 02:40:51 +000058 }
Pau Espin Pedrol2652f2b2018-01-09 19:12:21 +010059 cerr << "now: " << Timeval() << " then: " << then << " remaining: " << then.remaining() << endl;
60 assert(then.remaining() <= 0);
61 assert(loops >= 18);
62
63 printf("Done\n");
dburgess82c46ff2011-10-07 02:40:51 +000064}