blob: 6a45d3b502e79943046bb35bf857d342e2338102 [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02004* SPDX-License-Identifier: AGPL-3.0+
dburgess82c46ff2011-10-07 02:40:51 +00005*
6* This software is distributed under the terms of the GNU Affero Public License.
7* See the COPYING file in the main directory for details.
8*
9* This use of this software may be subject to additional restrictions.
10* See the LEGAL file in the main directory for details.
11
12 This program is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Affero General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Affero General Public License for more details.
21
22 You should have received a copy of the GNU Affero General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
24
25*/
26
27
28
29
30#include "Timeval.h"
31#include <iostream>
Pau Espin Pedrol2652f2b2018-01-09 19:12:21 +010032#include <assert.h>
Pau Espin Pedrol28ce3152018-01-15 11:46:26 +010033#include <sys/time.h>
dburgess82c46ff2011-10-07 02:40:51 +000034
Pau Espin Pedrol25185882018-12-12 17:39:08 +010035extern "C" {
36#include <osmocom/core/timer.h>
37}
38
dburgess82c46ff2011-10-07 02:40:51 +000039using namespace std;
40
41int main(int argc, char *argv[])
42{
Pau Espin Pedrol25185882018-12-12 17:39:08 +010043
44 osmo_clock_override_enable(CLOCK_REALTIME, true);
45
46 struct timespec *clk = osmo_clock_override_gettimespec(CLOCK_REALTIME);
47 clk->tv_sec = 0;
48 clk->tv_nsec = 1000;
49
50 long last_remaining = 10000; /*10 sec */
Pau Espin Pedrol28ce3152018-01-15 11:46:26 +010051 Timeval then(last_remaining);
52 assert(then.elapsed() == -last_remaining);
53 cerr << then << " elapsed: " << then.elapsed() << endl;
54
55 /* Check that last_remaining parameter affects setting time in the future */
Pau Espin Pedrol25185882018-12-12 17:39:08 +010056 osmo_clock_override_add(CLOCK_REALTIME, 0, 10*1000*1000);
Pau Espin Pedrol28ce3152018-01-15 11:46:26 +010057 double increased_time_secs = Timeval().seconds();
Pau Espin Pedrol25185882018-12-12 17:39:08 +010058 assert(increased_time_secs < then.seconds());
Pau Espin Pedrol28ce3152018-01-15 11:46:26 +010059
60 struct timespec invariant_time = then.timespec();
Pau Espin Pedrol2652f2b2018-01-09 19:12:21 +010061 int loops = 0;
dburgess82c46ff2011-10-07 02:40:51 +000062
63 while (!then.passed()) {
Pau Espin Pedrol28ce3152018-01-15 11:46:26 +010064 struct timespec tspecnow = then.timespec();
Pau Espin Pedrol25185882018-12-12 17:39:08 +010065 cerr << "["<< loops << "] now: " << Timeval().seconds() << " then: " << then << " remaining: " << then.remaining() << endl;
Pau Espin Pedrol28ce3152018-01-15 11:46:26 +010066 assert(last_remaining >= then.remaining());
67 assert(tspecnow.tv_sec == invariant_time.tv_sec && tspecnow.tv_nsec == invariant_time.tv_nsec);
Pau Espin Pedrol25185882018-12-12 17:39:08 +010068 osmo_clock_override_add(CLOCK_REALTIME, 0, 500000*1000);
Pau Espin Pedrol2652f2b2018-01-09 19:12:21 +010069 loops++;
dburgess82c46ff2011-10-07 02:40:51 +000070 }
Pau Espin Pedrol2652f2b2018-01-09 19:12:21 +010071 cerr << "now: " << Timeval() << " then: " << then << " remaining: " << then.remaining() << endl;
Pau Espin Pedrol25185882018-12-12 17:39:08 +010072 assert(then.remaining() == -10);
73 assert(loops == 20);
Pau Espin Pedrol2652f2b2018-01-09 19:12:21 +010074
75 printf("Done\n");
dburgess82c46ff2011-10-07 02:40:51 +000076}