xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_time.c (revision 8a6fe8ce60ab99778558c4951d23615a0141daf0)
1*ddba0402SEnji Cooper /*	$NetBSD: t_time.c,v 1.4 2017/01/10 15:32:46 christos Exp $ */
257718be8SEnji Cooper 
357718be8SEnji Cooper /*-
457718be8SEnji Cooper  * Copyright (c) 2011 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper  * by Jukka Ruohonen.
957718be8SEnji Cooper  *
1057718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper  * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper  * are met:
1357718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper  *
1957718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper  */
3157718be8SEnji Cooper #include <sys/cdefs.h>
32*ddba0402SEnji Cooper __RCSID("$NetBSD: t_time.c,v 1.4 2017/01/10 15:32:46 christos Exp $");
3357718be8SEnji Cooper 
3457718be8SEnji Cooper #include <atf-c.h>
3557718be8SEnji Cooper #include <errno.h>
3657718be8SEnji Cooper #include <inttypes.h>
3757718be8SEnji Cooper #include <stdint.h>
3857718be8SEnji Cooper #include <stdio.h>
3957718be8SEnji Cooper #include <stdlib.h>
4057718be8SEnji Cooper #include <time.h>
41*ddba0402SEnji Cooper #include <sys/time.h>
4257718be8SEnji Cooper #include <unistd.h>
4357718be8SEnji Cooper 
4457718be8SEnji Cooper ATF_TC(time_copy);
ATF_TC_HEAD(time_copy,tc)4557718be8SEnji Cooper ATF_TC_HEAD(time_copy, tc)
4657718be8SEnji Cooper {
4757718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test the return values of time(3)");
4857718be8SEnji Cooper }
4957718be8SEnji Cooper 
ATF_TC_BODY(time_copy,tc)5057718be8SEnji Cooper ATF_TC_BODY(time_copy, tc)
5157718be8SEnji Cooper {
5257718be8SEnji Cooper 	time_t t1, t2 = 0;
5357718be8SEnji Cooper 
5457718be8SEnji Cooper 	t1 = time(&t2);
5557718be8SEnji Cooper 
5657718be8SEnji Cooper 	if (t1 != t2)
5757718be8SEnji Cooper 		atf_tc_fail("incorrect return values from time(3)");
5857718be8SEnji Cooper }
5957718be8SEnji Cooper 
6057718be8SEnji Cooper ATF_TC(time_mono);
ATF_TC_HEAD(time_mono,tc)6157718be8SEnji Cooper ATF_TC_HEAD(time_mono, tc)
6257718be8SEnji Cooper {
6357718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test monotonicity of time(3)");
6457718be8SEnji Cooper }
6557718be8SEnji Cooper 
ATF_TC_BODY(time_mono,tc)6657718be8SEnji Cooper ATF_TC_BODY(time_mono, tc)
6757718be8SEnji Cooper {
6857718be8SEnji Cooper 	const size_t maxiter = 10;
6957718be8SEnji Cooper 	time_t t1, t2;
7057718be8SEnji Cooper 	size_t i;
7157718be8SEnji Cooper 
7257718be8SEnji Cooper 	for (i = 0; i < maxiter; i++) {
7357718be8SEnji Cooper 
7457718be8SEnji Cooper 		t1 = time(NULL);
7557718be8SEnji Cooper 		(void)sleep(1);
7657718be8SEnji Cooper 		t2 = time(NULL);
7757718be8SEnji Cooper 
7857718be8SEnji Cooper 		(void)fprintf(stderr, "%"PRId64" vs. %"PRId64"\n",
7957718be8SEnji Cooper 		    (int64_t)t1, (int64_t)t2);
8057718be8SEnji Cooper 
8157718be8SEnji Cooper 		if (t1 >= t2)
8257718be8SEnji Cooper 			atf_tc_fail("time(3) is not monotonic");
8357718be8SEnji Cooper 	}
8457718be8SEnji Cooper }
8557718be8SEnji Cooper 
8657718be8SEnji Cooper ATF_TC(time_timeofday);
ATF_TC_HEAD(time_timeofday,tc)8757718be8SEnji Cooper ATF_TC_HEAD(time_timeofday, tc)
8857718be8SEnji Cooper {
8957718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test time(3) vs. gettimeofday(2)");
9057718be8SEnji Cooper }
9157718be8SEnji Cooper 
ATF_TC_BODY(time_timeofday,tc)9257718be8SEnji Cooper ATF_TC_BODY(time_timeofday, tc)
9357718be8SEnji Cooper {
9457718be8SEnji Cooper 	struct timeval tv = { 0, 0 };
95640235e2SEnji Cooper 	time_t t1, t2;
9657718be8SEnji Cooper 
97640235e2SEnji Cooper 	t1 = time(NULL);
9857718be8SEnji Cooper 	ATF_REQUIRE(gettimeofday(&tv, NULL) == 0);
99640235e2SEnji Cooper 	t2 = time(NULL);
10057718be8SEnji Cooper 
10157718be8SEnji Cooper 	(void)fprintf(stderr, "%"PRId64" vs. %"PRId64"\n",
102640235e2SEnji Cooper 	    (int64_t)t1, (int64_t)tv.tv_sec);
10357718be8SEnji Cooper 
104640235e2SEnji Cooper 	if (t1 > tv.tv_sec || t2 < tv.tv_sec)
10557718be8SEnji Cooper 		atf_tc_fail("time(3) and gettimeofday(2) differ");
10657718be8SEnji Cooper }
10757718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)10857718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
10957718be8SEnji Cooper {
11057718be8SEnji Cooper 
11157718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, time_copy);
11257718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, time_mono);
11357718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, time_timeofday);
11457718be8SEnji Cooper 
11557718be8SEnji Cooper 	return atf_no_error();
11657718be8SEnji Cooper }
117