xref: /netbsd-src/tests/lib/libc/gen/t_time.c (revision 91da77105793f837264f8248a0573ba82610b7c0)
1*91da7710Schristos /*	$NetBSD: t_time.c,v 1.4 2017/01/10 15:32:46 christos Exp $ */
206b5587bSjruoho 
306b5587bSjruoho /*-
406b5587bSjruoho  * Copyright (c) 2011 The NetBSD Foundation, Inc.
506b5587bSjruoho  * All rights reserved.
606b5587bSjruoho  *
706b5587bSjruoho  * This code is derived from software contributed to The NetBSD Foundation
806b5587bSjruoho  * by Jukka Ruohonen.
906b5587bSjruoho  *
1006b5587bSjruoho  * Redistribution and use in source and binary forms, with or without
1106b5587bSjruoho  * modification, are permitted provided that the following conditions
1206b5587bSjruoho  * are met:
1306b5587bSjruoho  * 1. Redistributions of source code must retain the above copyright
1406b5587bSjruoho  *    notice, this list of conditions and the following disclaimer.
1506b5587bSjruoho  * 2. Redistributions in binary form must reproduce the above copyright
1606b5587bSjruoho  *    notice, this list of conditions and the following disclaimer in the
1706b5587bSjruoho  *    documentation and/or other materials provided with the distribution.
1806b5587bSjruoho  *
1906b5587bSjruoho  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2006b5587bSjruoho  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2106b5587bSjruoho  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2206b5587bSjruoho  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2306b5587bSjruoho  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2406b5587bSjruoho  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2506b5587bSjruoho  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2606b5587bSjruoho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2706b5587bSjruoho  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2806b5587bSjruoho  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2906b5587bSjruoho  * POSSIBILITY OF SUCH DAMAGE.
3006b5587bSjruoho  */
3106b5587bSjruoho #include <sys/cdefs.h>
32*91da7710Schristos __RCSID("$NetBSD: t_time.c,v 1.4 2017/01/10 15:32:46 christos Exp $");
3306b5587bSjruoho 
3406b5587bSjruoho #include <atf-c.h>
3506b5587bSjruoho #include <errno.h>
3606b5587bSjruoho #include <inttypes.h>
3706b5587bSjruoho #include <stdint.h>
3806b5587bSjruoho #include <stdio.h>
3906b5587bSjruoho #include <stdlib.h>
4006b5587bSjruoho #include <time.h>
41*91da7710Schristos #include <sys/time.h>
4206b5587bSjruoho #include <unistd.h>
4306b5587bSjruoho 
4406b5587bSjruoho ATF_TC(time_copy);
ATF_TC_HEAD(time_copy,tc)4506b5587bSjruoho ATF_TC_HEAD(time_copy, tc)
4606b5587bSjruoho {
4706b5587bSjruoho 	atf_tc_set_md_var(tc, "descr", "Test the return values of time(3)");
4806b5587bSjruoho }
4906b5587bSjruoho 
ATF_TC_BODY(time_copy,tc)5006b5587bSjruoho ATF_TC_BODY(time_copy, tc)
5106b5587bSjruoho {
5206b5587bSjruoho 	time_t t1, t2 = 0;
5306b5587bSjruoho 
5406b5587bSjruoho 	t1 = time(&t2);
5506b5587bSjruoho 
5606b5587bSjruoho 	if (t1 != t2)
5706b5587bSjruoho 		atf_tc_fail("incorrect return values from time(3)");
5806b5587bSjruoho }
5906b5587bSjruoho 
6006b5587bSjruoho ATF_TC(time_mono);
ATF_TC_HEAD(time_mono,tc)6106b5587bSjruoho ATF_TC_HEAD(time_mono, tc)
6206b5587bSjruoho {
6306b5587bSjruoho 	atf_tc_set_md_var(tc, "descr", "Test monotonicity of time(3)");
6406b5587bSjruoho }
6506b5587bSjruoho 
ATF_TC_BODY(time_mono,tc)6606b5587bSjruoho ATF_TC_BODY(time_mono, tc)
6706b5587bSjruoho {
6806b5587bSjruoho 	const size_t maxiter = 10;
6906b5587bSjruoho 	time_t t1, t2;
7006b5587bSjruoho 	size_t i;
7106b5587bSjruoho 
7206b5587bSjruoho 	for (i = 0; i < maxiter; i++) {
7306b5587bSjruoho 
7406b5587bSjruoho 		t1 = time(NULL);
7506b5587bSjruoho 		(void)sleep(1);
7606b5587bSjruoho 		t2 = time(NULL);
7706b5587bSjruoho 
7806b5587bSjruoho 		(void)fprintf(stderr, "%"PRId64" vs. %"PRId64"\n",
7906b5587bSjruoho 		    (int64_t)t1, (int64_t)t2);
8006b5587bSjruoho 
8106b5587bSjruoho 		if (t1 >= t2)
8206b5587bSjruoho 			atf_tc_fail("time(3) is not monotonic");
8306b5587bSjruoho 	}
8406b5587bSjruoho }
8506b5587bSjruoho 
8606b5587bSjruoho ATF_TC(time_timeofday);
ATF_TC_HEAD(time_timeofday,tc)8706b5587bSjruoho ATF_TC_HEAD(time_timeofday, tc)
8806b5587bSjruoho {
8906b5587bSjruoho 	atf_tc_set_md_var(tc, "descr", "Test time(3) vs. gettimeofday(2)");
9006b5587bSjruoho }
9106b5587bSjruoho 
ATF_TC_BODY(time_timeofday,tc)9206b5587bSjruoho ATF_TC_BODY(time_timeofday, tc)
9306b5587bSjruoho {
9406b5587bSjruoho 	struct timeval tv = { 0, 0 };
954a3dd593Sjustin 	time_t t1, t2;
9606b5587bSjruoho 
974a3dd593Sjustin 	t1 = time(NULL);
9806b5587bSjruoho 	ATF_REQUIRE(gettimeofday(&tv, NULL) == 0);
994a3dd593Sjustin 	t2 = time(NULL);
10006b5587bSjruoho 
101074fcac0Sjruoho 	(void)fprintf(stderr, "%"PRId64" vs. %"PRId64"\n",
1024a3dd593Sjustin 	    (int64_t)t1, (int64_t)tv.tv_sec);
10306b5587bSjruoho 
1044a3dd593Sjustin 	if (t1 > tv.tv_sec || t2 < tv.tv_sec)
10506b5587bSjruoho 		atf_tc_fail("time(3) and gettimeofday(2) differ");
10606b5587bSjruoho }
10706b5587bSjruoho 
ATF_TP_ADD_TCS(tp)10806b5587bSjruoho ATF_TP_ADD_TCS(tp)
10906b5587bSjruoho {
11006b5587bSjruoho 
11106b5587bSjruoho 	ATF_TP_ADD_TC(tp, time_copy);
11206b5587bSjruoho 	ATF_TP_ADD_TC(tp, time_mono);
11306b5587bSjruoho 	ATF_TP_ADD_TC(tp, time_timeofday);
11406b5587bSjruoho 
11506b5587bSjruoho 	return atf_no_error();
11606b5587bSjruoho }
117