Home | History | Annotate | Download | only in nanosleep
      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 nanosleep() on a variety of valid and invalid input parameters.
      9  *
     10  * For valid parameters, if the seconds spent is within OKSECERR, the
     11  * test is considered a pass (Note:  This is not too accurate since
     12  * accuracy is at the second level.).
     13  *
     14  * For invalid parameters, nanosleep should fail with -1 exit and
     15  * errno set to EINVAL.
     16  */
     17 #include <stdio.h>
     18 #include <time.h>
     19 #include <errno.h>
     20 #include "posixtest.h"
     21 
     22 #define NUMVALID 6
     23 #define NUMINVALID 7
     24 
     25 #define OKSECERR 1
     26 
     27 /*
     28  * Copyright (c) 2002, Intel Corporation. All rights reserved.
     29  * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
     30  * This file is licensed under the GPL license.  For the full content
     31  * of this license, see the COPYING file at the top level of this
     32  * source tree.
     33 
     34  * input tests
     35  */
     36 static int sleepvalid[NUMVALID][2] = { {0, 30000000}, {1, 0},
     37 {1, 30000000}, {2, 0},
     38 {10, 5000}, {13, 5}
     39 };
     40 static int sleepinvalid[NUMINVALID][2] = { {-1, -1}, {0, -1},
     41 {1, 1000000000}, {2, 1000000000},
     42 {-2147483647, -2147483647},
     43 {1, 2147483647},
     44 {0, 1075002478}
     45 };
     46 
     47 int main(void)
     48 {
     49 	struct timespec tssleepfor, tsstorage, tsbefore, tsafter;
     50 	int i;
     51 	int failure = 0;
     52 	int slepts = 0, sleptns = 0;
     53 
     54 	for (i = 0; i < NUMVALID; i++) {
     55 		tssleepfor.tv_sec = sleepvalid[i][0];
     56 		tssleepfor.tv_nsec = sleepvalid[i][1];
     57 		printf("sleep %d sec %d nsec\n",
     58 		       (int)tssleepfor.tv_sec, (int)tssleepfor.tv_nsec);
     59 		if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
     60 			perror("Error in clock_gettime()\n");
     61 			return PTS_UNRESOLVED;
     62 		}
     63 
     64 		if (nanosleep(&tssleepfor, &tsstorage) == 0) {
     65 			if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
     66 				perror("Error in clock_gettime()\n");
     67 				return PTS_UNRESOLVED;
     68 			}
     69 			/*
     70 			 * Generic alg for calculating slept time.
     71 			 */
     72 			slepts = tsafter.tv_sec - tsbefore.tv_sec;
     73 			sleptns = tsafter.tv_nsec - tsbefore.tv_nsec;
     74 			if (sleptns < 0) {
     75 				sleptns = sleptns + 1000000000;
     76 				slepts = slepts - 1;
     77 			}
     78 
     79 			if ((slepts - tssleepfor.tv_sec) > OKSECERR) {
     80 				printf("FAIL - slept %ds%dns >> %lds%ldns\n",
     81 				       slepts, sleptns,
     82 				       tssleepfor.tv_sec, tssleepfor.tv_nsec);
     83 				failure = 1;
     84 			} else {
     85 				printf("PASS - slept %ds%dns ~= %lds%ldns\n",
     86 				       slepts, sleptns,
     87 				       tssleepfor.tv_sec, tssleepfor.tv_nsec);
     88 			}
     89 		} else {
     90 			printf("nanosleep() did not return 0 on success\n");
     91 			failure = 1;
     92 		}
     93 	}
     94 
     95 	for (i = 0; i < NUMINVALID; i++) {
     96 		tssleepfor.tv_sec = sleepinvalid[i][0];
     97 		tssleepfor.tv_nsec = sleepinvalid[i][1];
     98 		printf("sleep %d sec %d nsec\n",
     99 		       (int)tssleepfor.tv_sec, (int)tssleepfor.tv_nsec);
    100 		if (nanosleep(&tssleepfor, &tsstorage) == -1) {
    101 			if (EINVAL != errno) {
    102 				printf("errno != EINVAL\n");
    103 				failure = 1;
    104 			}
    105 		} else {
    106 			printf("nanosleep() did not return -1 on failure\n");
    107 			failure = 1;
    108 		}
    109 	}
    110 
    111 	if (failure) {
    112 		printf("At least one test FAILED\n");
    113 		return PTS_FAIL;
    114 	} else {
    115 		printf("All tests PASSED\n");
    116 		return PTS_PASS;
    117 	}
    118 	return PTS_PASS;
    119 }
    120