1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_time.c,v 1.3 2014/10/31 12:22:38 justin Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2011 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc * by Jukka Ruohonen.
911be35a1SLionel Sambuc *
1011be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc * are met:
1311be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc *
1911be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc */
3111be35a1SLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_time.c,v 1.3 2014/10/31 12:22:38 justin Exp $");
3311be35a1SLionel Sambuc
3411be35a1SLionel Sambuc #include <atf-c.h>
3511be35a1SLionel Sambuc #include <errno.h>
3611be35a1SLionel Sambuc #include <inttypes.h>
3711be35a1SLionel Sambuc #include <stdint.h>
3811be35a1SLionel Sambuc #include <stdio.h>
3911be35a1SLionel Sambuc #include <stdlib.h>
4011be35a1SLionel Sambuc #include <time.h>
4111be35a1SLionel Sambuc #include <unistd.h>
4211be35a1SLionel Sambuc
4311be35a1SLionel Sambuc ATF_TC(time_copy);
ATF_TC_HEAD(time_copy,tc)4411be35a1SLionel Sambuc ATF_TC_HEAD(time_copy, tc)
4511be35a1SLionel Sambuc {
4611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test the return values of time(3)");
4711be35a1SLionel Sambuc }
4811be35a1SLionel Sambuc
ATF_TC_BODY(time_copy,tc)4911be35a1SLionel Sambuc ATF_TC_BODY(time_copy, tc)
5011be35a1SLionel Sambuc {
5111be35a1SLionel Sambuc time_t t1, t2 = 0;
5211be35a1SLionel Sambuc
5311be35a1SLionel Sambuc t1 = time(&t2);
5411be35a1SLionel Sambuc
5511be35a1SLionel Sambuc if (t1 != t2)
5611be35a1SLionel Sambuc atf_tc_fail("incorrect return values from time(3)");
5711be35a1SLionel Sambuc }
5811be35a1SLionel Sambuc
5911be35a1SLionel Sambuc ATF_TC(time_mono);
ATF_TC_HEAD(time_mono,tc)6011be35a1SLionel Sambuc ATF_TC_HEAD(time_mono, tc)
6111be35a1SLionel Sambuc {
6211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test monotonicity of time(3)");
6311be35a1SLionel Sambuc }
6411be35a1SLionel Sambuc
ATF_TC_BODY(time_mono,tc)6511be35a1SLionel Sambuc ATF_TC_BODY(time_mono, tc)
6611be35a1SLionel Sambuc {
6711be35a1SLionel Sambuc const size_t maxiter = 10;
6811be35a1SLionel Sambuc time_t t1, t2;
6911be35a1SLionel Sambuc size_t i;
7011be35a1SLionel Sambuc
7111be35a1SLionel Sambuc for (i = 0; i < maxiter; i++) {
7211be35a1SLionel Sambuc
7311be35a1SLionel Sambuc t1 = time(NULL);
7411be35a1SLionel Sambuc (void)sleep(1);
7511be35a1SLionel Sambuc t2 = time(NULL);
7611be35a1SLionel Sambuc
7711be35a1SLionel Sambuc (void)fprintf(stderr, "%"PRId64" vs. %"PRId64"\n",
7811be35a1SLionel Sambuc (int64_t)t1, (int64_t)t2);
7911be35a1SLionel Sambuc
8011be35a1SLionel Sambuc if (t1 >= t2)
8111be35a1SLionel Sambuc atf_tc_fail("time(3) is not monotonic");
8211be35a1SLionel Sambuc }
8311be35a1SLionel Sambuc }
8411be35a1SLionel Sambuc
8511be35a1SLionel Sambuc ATF_TC(time_timeofday);
ATF_TC_HEAD(time_timeofday,tc)8611be35a1SLionel Sambuc ATF_TC_HEAD(time_timeofday, tc)
8711be35a1SLionel Sambuc {
8811be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test time(3) vs. gettimeofday(2)");
8911be35a1SLionel Sambuc }
9011be35a1SLionel Sambuc
ATF_TC_BODY(time_timeofday,tc)9111be35a1SLionel Sambuc ATF_TC_BODY(time_timeofday, tc)
9211be35a1SLionel Sambuc {
9311be35a1SLionel Sambuc struct timeval tv = { 0, 0 };
94*0a6a1f1dSLionel Sambuc time_t t1, t2;
9511be35a1SLionel Sambuc
96*0a6a1f1dSLionel Sambuc t1 = time(NULL);
9711be35a1SLionel Sambuc ATF_REQUIRE(gettimeofday(&tv, NULL) == 0);
98*0a6a1f1dSLionel Sambuc t2 = time(NULL);
9911be35a1SLionel Sambuc
10011be35a1SLionel Sambuc (void)fprintf(stderr, "%"PRId64" vs. %"PRId64"\n",
101*0a6a1f1dSLionel Sambuc (int64_t)t1, (int64_t)tv.tv_sec);
10211be35a1SLionel Sambuc
103*0a6a1f1dSLionel Sambuc if (t1 > tv.tv_sec || t2 < tv.tv_sec)
10411be35a1SLionel Sambuc atf_tc_fail("time(3) and gettimeofday(2) differ");
10511be35a1SLionel Sambuc }
10611be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)10711be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
10811be35a1SLionel Sambuc {
10911be35a1SLionel Sambuc
11011be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, time_copy);
11111be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, time_mono);
11211be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, time_timeofday);
11311be35a1SLionel Sambuc
11411be35a1SLionel Sambuc return atf_no_error();
11511be35a1SLionel Sambuc }
116