Home | History | Annotate | Download | only in timer_gettime
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7 
      8  * Test that timer_gettime() sets sets itimerspec.it_value to the
      9  * amount of time remaining.
     10  * - Create and arm a timer.
     11  * - Call timer_gettime().
     12  * - Ensure the value returned is within ACCEPTABLEDELTA less than
     13  *   the value set.
     14  *
     15  * Signal SIGCONT will be used so that it will not affect the test if
     16  * the timer expires.
     17  * Clock CLOCK_REALTIME will be used.
     18  */
     19 
     20 #include <time.h>
     21 #include <signal.h>
     22 #include <stdio.h>
     23 #include <unistd.h>
     24 #include <stdlib.h>
     25 #include "posixtest.h"
     26 
     27 #define TIMERSEC 5
     28 #define ACCEPTABLEDELTA 1
     29 
     30 int main(void)
     31 {
     32 	struct sigevent ev;
     33 	timer_t tid;
     34 	struct itimerspec itsset, itsget;
     35 	int delta;
     36 
     37 	ev.sigev_notify = SIGEV_SIGNAL;
     38 	ev.sigev_signo = SIGCONT;
     39 
     40 	itsset.it_interval.tv_sec = 0;
     41 	itsset.it_interval.tv_nsec = 0;
     42 	itsset.it_value.tv_sec = TIMERSEC;
     43 	itsset.it_value.tv_nsec = 0;
     44 
     45 	if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
     46 		perror("timer_create() did not return success\n");
     47 		return PTS_UNRESOLVED;
     48 	}
     49 
     50 	if (timer_settime(tid, 0, &itsset, NULL) != 0) {
     51 		perror("timer_settime() did not return success\n");
     52 		return PTS_UNRESOLVED;
     53 	}
     54 
     55 	if (timer_gettime(tid, &itsget) != 0) {
     56 		perror("timer_gettime() did not return success\n");
     57 		return PTS_UNRESOLVED;
     58 	}
     59 
     60 	delta = itsset.it_value.tv_sec - itsget.it_value.tv_sec;
     61 
     62 	if (delta < 0) {
     63 		printf("FAIL:  timer_gettime() value > timer_settime()\n");
     64 		printf("%d > %d\n", (int)itsget.it_value.tv_sec,
     65 		       (int)itsset.it_value.tv_sec);
     66 		return PTS_FAIL;
     67 	}
     68 
     69 	if (delta <= ACCEPTABLEDELTA) {
     70 		printf("Test PASSED\n");
     71 		return PTS_PASS;
     72 	}
     73 
     74 	printf("FAIL:  timer_gettime() value !~= timer_settime()\n");
     75 	printf("%d !~= %d\n", (int)itsget.it_value.tv_sec,
     76 	       (int)itsset.it_value.tv_sec);
     77 	return PTS_FAIL;
     78 }
     79