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 
      8    Test that parameter CLOCK_MONOTONIC returns seconds since the
      9    an unspecified point that cannot change.
     10    Validity is checked by ensuring that the time returned is always
     11    increasing.
     12  */
     13 #include <stdio.h>
     14 #include <time.h>
     15 #include <sys/time.h>
     16 #include <unistd.h>
     17 #include "posixtest.h"
     18 
     19 int main(void)
     20 {
     21 #ifdef CLOCK_MONOTONIC
     22 
     23 	struct timespec ts1, ts2, ts3, ts4;
     24 
     25 	/* Test that MONOTONIC CLOCK functionality really exists */
     26 	if (sysconf(_SC_MONOTONIC_CLOCK) == -1) {
     27 		printf("CLOCK_MONOTONIC unsupported\n");
     28 		return PTS_UNSUPPORTED;
     29 	}
     30 
     31 	if (clock_gettime(CLOCK_MONOTONIC, &ts1) != 0) {
     32 		printf("clock_gettime() failed\n");
     33 		return PTS_UNRESOLVED;
     34 	}
     35 
     36 	if (clock_gettime(CLOCK_MONOTONIC, &ts2) != 0) {
     37 		printf("clock_gettime() failed\n");
     38 		return PTS_UNRESOLVED;
     39 	}
     40 
     41 	sleep(1);
     42 
     43 	if (clock_gettime(CLOCK_MONOTONIC, &ts3) != 0) {
     44 		printf("clock_gettime() failed\n");
     45 		return PTS_UNRESOLVED;
     46 	}
     47 
     48 	sleep(3);
     49 	if (clock_gettime(CLOCK_MONOTONIC, &ts4) != 0) {
     50 		printf("clock_gettime() failed\n");
     51 		return PTS_UNRESOLVED;
     52 	}
     53 
     54 	if ((ts1.tv_sec <= ts2.tv_sec) &&
     55 	    (ts2.tv_sec <= ts3.tv_sec) && (ts3.tv_sec <= ts4.tv_sec)) {
     56 		printf("Test PASSED\n");
     57 		return PTS_PASS;
     58 	}
     59 
     60 	printf("Test FAILED - ts1=%ld,ts2=%ld,ts3=%ld,ts4=%ld\n",
     61 	       ts1.tv_sec, ts2.tv_sec, ts3.tv_sec, ts4.tv_sec);
     62 	return PTS_FAIL;
     63 #else
     64 	printf("CLOCK_MONOTONIC unsupported\n");
     65 	return PTS_UNSUPPORTED;
     66 #endif
     67 }
     68