Module: xenomai-gch Branch: for-forge Commit: 5fbfaeb911cc590a82f5b713d1231e20ec86a6be URL: http://git.xenomai.org/?p=xenomai-gch.git;a=commit;h=5fbfaeb911cc590a82f5b713d1231e20ec86a6be
Author: Gilles Chanteperdrix <gilles.chanteperd...@xenomai.org> Date: Sun Jun 1 19:05:24 2014 +0200 regression: add clock_settime test (bootstrap needed) --- testsuite/regression/posix/Makefile.am | 1 + testsuite/regression/posix/clock_settime.c | 224 ++++++++++++++++++++++++++++ 2 files changed, 225 insertions(+) diff --git a/testsuite/regression/posix/Makefile.am b/testsuite/regression/posix/Makefile.am index 1a534a0..f975984 100644 --- a/testsuite/regression/posix/Makefile.am +++ b/testsuite/regression/posix/Makefile.am @@ -5,6 +5,7 @@ CCLD = $(top_srcdir)/scripts/wrap-link.sh $(CC) noinst_HEADERS = check.h test_PROGRAMS = \ + clock_settime \ leaks \ mq_select \ timerfd diff --git a/testsuite/regression/posix/clock_settime.c b/testsuite/regression/posix/clock_settime.c new file mode 100644 index 0000000..5168ea1 --- /dev/null +++ b/testsuite/regression/posix/clock_settime.c @@ -0,0 +1,224 @@ +/* + * Copyright (C) 2014 Gilles Chanteperdrix <g...@xenomai.org> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#undef NDEBUG +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <assert.h> + +#include <unistd.h> +#include <time.h> +#include <sys/timerfd.h> +#include "check.h" + +static void clock_increase_before_oneshot_timer_first_tick(void) +{ + unsigned long long ticks; + struct itimerspec timer; + struct timespec now; + int t; + + check_unix(t = timerfd_create(CLOCK_REALTIME, 0)); + check_unix(clock_gettime(CLOCK_REALTIME, &now)); + timer.it_value = now; + timer.it_value.tv_sec++; + timer.it_interval.tv_sec = 0; + timer.it_interval.tv_nsec = 0; + check_unix(timerfd_settime(t, TFD_TIMER_ABSTIME, &timer, NULL)); + now.tv_sec += 5; + check_unix(clock_settime(CLOCK_REALTIME, &now)); + check_unix(clock_gettime(CLOCK_MONOTONIC, &now)); + check_unix(read(t, &ticks, sizeof(ticks))); + assert(ticks == 1); + timer.it_value = now; + check_unix(clock_gettime(CLOCK_MONOTONIC, &now)); + assert(now.tv_sec * 1000000000ULL + now.tv_nsec - + (timer.it_value.tv_sec * 1000000000ULL + timer.it_value.tv_nsec) + < 1000000000); + check_unix(close(t)); +} + +static void clock_increase_before_periodic_timer_first_tick(void) +{ + unsigned long long ticks; + struct itimerspec timer; + struct timespec now; + int t; + + check_unix(t = timerfd_create(CLOCK_REALTIME, 0)); + check_unix(clock_gettime(CLOCK_REALTIME, &now)); + timer.it_value = now; + timer.it_value.tv_sec++; + timer.it_interval.tv_sec = 1; + timer.it_interval.tv_nsec = 0; + check_unix(timerfd_settime(t, TFD_TIMER_ABSTIME, &timer, NULL)); + now.tv_sec += 5; + check_unix(clock_settime(CLOCK_REALTIME, &now)); + check_unix(clock_gettime(CLOCK_MONOTONIC, &now)); + check_unix(read(t, &ticks, sizeof(ticks))); + assert(ticks == 5); + timer.it_value = now; + check_unix(clock_gettime(CLOCK_MONOTONIC, &now)); + assert(now.tv_sec * 1000000000ULL + now.tv_nsec - + (timer.it_value.tv_sec * 1000000000ULL + timer.it_value.tv_nsec) + < 1000000000); + check_unix(read(t, &ticks, sizeof(ticks))); + assert(ticks == 1); + check_unix(close(t)); +} + +static void clock_increase_after_periodic_timer_first_tick(void) +{ + unsigned long long ticks; + struct itimerspec timer; + struct timespec now; + int t; + + check_unix(t = timerfd_create(CLOCK_REALTIME, 0)); + check_unix(clock_gettime(CLOCK_REALTIME, &now)); + timer.it_value = now; + timer.it_value.tv_sec++; + timer.it_interval.tv_sec = 1; + timer.it_interval.tv_nsec = 0; + check_unix(timerfd_settime(t, TFD_TIMER_ABSTIME, &timer, NULL)); + check_unix(read(t, &ticks, sizeof(ticks))); + assert(ticks == 1); + check_unix(clock_gettime(CLOCK_REALTIME, &now)); + now.tv_sec += 5; + check_unix(clock_settime(CLOCK_REALTIME, &now)); + check_unix(clock_gettime(CLOCK_MONOTONIC, &now)); + check_unix(read(t, &ticks, sizeof(ticks))); + assert(ticks == 5); + timer.it_value = now; + check_unix(clock_gettime(CLOCK_MONOTONIC, &now)); + assert(now.tv_sec * 1000000000ULL + now.tv_nsec - + (timer.it_value.tv_sec * 1000000000ULL + timer.it_value.tv_nsec) + < 1000000000); + check_unix(read(t, &ticks, sizeof(ticks))); + assert(ticks == 1); + check_unix(close(t)); +} + +static void clock_decrease_before_oneshot_timer_first_tick(void) +{ + unsigned long long ticks; + struct itimerspec timer; + struct timespec now; + long long diff; + int t; + + check_unix(t = timerfd_create(CLOCK_REALTIME, 0)); + check_unix(clock_gettime(CLOCK_REALTIME, &now)); + timer.it_value = now; + timer.it_value.tv_sec++; + timer.it_interval.tv_sec = 0; + timer.it_interval.tv_nsec = 0; + check_unix(timerfd_settime(t, TFD_TIMER_ABSTIME, &timer, NULL)); + now.tv_sec -= 5; + check_unix(clock_settime(CLOCK_REALTIME, &now)); + check_unix(clock_gettime(CLOCK_MONOTONIC, &now)); + check_unix(read(t, &ticks, sizeof(ticks))); + assert(ticks == 1); + timer.it_value = now; + check_unix(clock_gettime(CLOCK_MONOTONIC, &now)); + diff = now.tv_sec * 1000000000ULL + now.tv_nsec - + (timer.it_value.tv_sec * 1000000000ULL + timer.it_value.tv_nsec); + assert(diff >= 5500000000 && diff <= 6500000000); + check_unix(close(t)); +} + +static void clock_decrease_before_periodic_timer_first_tick(void) +{ + unsigned long long ticks; + struct itimerspec timer; + struct timespec now; + long long diff; + int t; + + check_unix(t = timerfd_create(CLOCK_REALTIME, 0)); + check_unix(clock_gettime(CLOCK_REALTIME, &now)); + timer.it_value = now; + timer.it_value.tv_sec++; + timer.it_interval.tv_sec = 1; + timer.it_interval.tv_nsec = 0; + check_unix(timerfd_settime(t, TFD_TIMER_ABSTIME, &timer, NULL)); + now.tv_sec -= 5; + check_unix(clock_settime(CLOCK_REALTIME, &now)); + check_unix(clock_gettime(CLOCK_MONOTONIC, &now)); + check_unix(read(t, &ticks, sizeof(ticks))); + assert(ticks == 1); + timer.it_value = now; + check_unix(clock_gettime(CLOCK_MONOTONIC, &now)); + diff = now.tv_sec * 1000000000ULL + now.tv_nsec - + (timer.it_value.tv_sec * 1000000000ULL + timer.it_value.tv_nsec); + assert(diff >= 5500000000 && diff <= 6500000000); + check_unix(read(t, &ticks, sizeof(ticks))); + assert(ticks == 1); + check_unix(close(t)); +} + +static void clock_decrease_after_periodic_timer_first_tick(void) +{ + unsigned long long ticks; + struct itimerspec timer; + struct timespec now; + long long diff; + int t; + + check_unix(t = timerfd_create(CLOCK_REALTIME, 0)); + check_unix(clock_gettime(CLOCK_REALTIME, &now)); + timer.it_value = now; + timer.it_value.tv_sec++; + timer.it_interval.tv_sec = 1; + timer.it_interval.tv_nsec = 0; + check_unix(timerfd_settime(t, TFD_TIMER_ABSTIME, &timer, NULL)); + check_unix(read(t, &ticks, sizeof(ticks))); + assert(ticks == 1); + check_unix(clock_gettime(CLOCK_REALTIME, &now)); + now.tv_sec -= 5; + check_unix(clock_settime(CLOCK_REALTIME, &now)); + check_unix(clock_gettime(CLOCK_MONOTONIC, &now)); + check_unix(read(t, &ticks, sizeof(ticks))); + assert(ticks == 1); + timer.it_value = now; + check_unix(clock_gettime(CLOCK_MONOTONIC, &now)); + diff = now.tv_sec * 1000000000ULL + now.tv_nsec - + (timer.it_value.tv_sec * 1000000000ULL + timer.it_value.tv_nsec); + assert(diff < 1000000000); + check_unix(read(t, &ticks, sizeof(ticks))); + assert(ticks == 1); + check_unix(close(t)); +} + +int main(void) +{ + clock_increase_before_oneshot_timer_first_tick(); + clock_increase_before_periodic_timer_first_tick(); + clock_increase_after_periodic_timer_first_tick(); + clock_decrease_before_oneshot_timer_first_tick(); + clock_decrease_before_periodic_timer_first_tick(); + clock_decrease_after_periodic_timer_first_tick(); + return EXIT_SUCCESS; +} _______________________________________________ Xenomai-git mailing list Xenomai-git@xenomai.org http://www.xenomai.org/mailman/listinfo/xenomai-git