Home | History | Annotate | Download | only in clock_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  * Test that parameter CLOCK_REALTIME returns seconds since the
      8  * Epoch.  (This test is similar to other tests written, but rewritten
      9  * here for completeness.)
     10  * Validity is compared with gettimeofday().  See rationale.txt for
     11  * more info.
     12  * If the clocks are within a few seconds of each other, the test is
     13  * a pass.
     14  */
     15 #include <stdio.h>
     16 #include <time.h>
     17 #include <sys/time.h>
     18 #include <stdlib.h>
     19 #include "posixtest.h"
     20 
     21 #define ACCEPTABLEDELTA 1
     22 
     23 int main(void)
     24 {
     25 	struct timespec tpundertest;
     26 	struct timeval tvstandard;
     27 	int delta;
     28 
     29 	if (clock_gettime(CLOCK_REALTIME, &tpundertest) == 0) {
     30 		if (gettimeofday(&tvstandard, NULL) == 0) {
     31 			delta = (int)tvstandard.tv_sec -
     32 			    (int)tpundertest.tv_sec;
     33 			if (abs(delta) <= ACCEPTABLEDELTA) {
     34 				printf("Test PASSED\n");
     35 				return PTS_PASS;
     36 			} else {
     37 				printf("FAIL:  expected %d, received %d\n",
     38 				       (int)tvstandard.tv_sec,
     39 				       (int)tpundertest.tv_sec);
     40 				return PTS_FAIL;
     41 			}
     42 		} else {
     43 			perror("Error calling gettimeofday()\n");
     44 			return PTS_UNRESOLVED;
     45 		}
     46 	}
     47 
     48 	printf("clock_gettime() failed\n");
     49 	return PTS_UNRESOLVED;
     50 }
     51