Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libc/time Add some tests for timegm(3) close to th...



details:   https://anonhg.NetBSD.org/src/rev/5e3574bfd1e7
branches:  trunk
changeset: 772087:5e3574bfd1e7
user:      apb <apb%NetBSD.org@localhost>
date:      Sat Dec 17 19:04:07 2011 +0000

description:
Add some tests for timegm(3) close to the epoch.  In
particular, 1969-12-31 23:59:59 should convert to (time_t)-1
with errno = 0.

diffstat:

 tests/lib/libc/time/t_mktime.c |  76 +++++++++++++++++++++++++++++++++++++++--
 1 files changed, 71 insertions(+), 5 deletions(-)

diffs (106 lines):

diff -r 00dcb87e40ac -r 5e3574bfd1e7 tests/lib/libc/time/t_mktime.c
--- a/tests/lib/libc/time/t_mktime.c    Sat Dec 17 15:40:22 2011 +0000
+++ b/tests/lib/libc/time/t_mktime.c    Sat Dec 17 19:04:07 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: t_mktime.c,v 1.2 2011/04/07 18:14:09 jruoho Exp $ */
+/* $NetBSD: t_mktime.c,v 1.3 2011/12/17 19:04:07 apb Exp $ */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -33,15 +33,15 @@
 #include <string.h>
 #include <time.h>
 
-ATF_TC(mktime);
+ATF_TC(mktime_negyear);
 
-ATF_TC_HEAD(mktime, tc)
+ATF_TC_HEAD(mktime_negyear, tc)
 {
 
        atf_tc_set_md_var(tc, "descr", "Test mktime(3) with negative year");
 }
 
-ATF_TC_BODY(mktime, tc)
+ATF_TC_BODY(mktime_negyear, tc)
 {
        struct tm tms;
 
@@ -53,10 +53,76 @@
        ATF_REQUIRE_ERRNO(errno, mktime(&tms) != (time_t)-1);
 }
 
+ATF_TC(timegm_epoch);
+
+ATF_TC_HEAD(timegm_epoch, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr", "Test timegm(3) close to the epoch");
+}
+
+ATF_TC_BODY(timegm_epoch, tc)
+{
+       struct tm tms;
+
+       /* midnight on 1 Jan 1970 */
+       (void)memset(&tms, 0, sizeof(tms));
+       errno = 0;
+       tms.tm_year = 1970 - 1900;
+       tms.tm_mday = 1;
+       ATF_REQUIRE_ERRNO(errno, timegm(&tms) == (time_t)0);
+
+       /* one second after midnight on 1 Jan 1970 */
+       (void)memset(&tms, 0, sizeof(tms));
+       errno = 0;
+       tms.tm_year = 1970 - 1900;
+       tms.tm_mday = 1;
+       tms.tm_sec = 1;
+       ATF_REQUIRE_ERRNO(errno, timegm(&tms) == (time_t)1);
+
+       /*
+        * 1969-12-31 23:59:59 = one second before the epoch.
+        * Result should be -1 with errno = 0.
+        */
+       (void)memset(&tms, 0, sizeof(tms));
+       errno = 0;
+       tms.tm_year = 1969 - 1900;
+       tms.tm_mon = 12 - 1;
+       tms.tm_mday = 31;
+       tms.tm_hour = 23;
+       tms.tm_min = 59;
+       tms.tm_sec = 59;
+       ATF_REQUIRE_ERRNO(errno, timegm(&tms) == (time_t)-1);
+
+       /*
+        * Another way of getting one second before the epoch:
+        * Set date to 1 Jan 1970, and time to -1 second.
+        */
+       (void)memset(&tms, 0, sizeof(tms));
+       errno = 0;
+       tms.tm_year = 1970 - 1900;
+       tms.tm_mday = 1;
+       tms.tm_sec = -1;
+       ATF_REQUIRE_ERRNO(errno, timegm(&tms) == (time_t)-1);
+
+       /*
+        * Two seconds before the epoch.
+        */
+       (void)memset(&tms, 0, sizeof(tms));
+       errno = 0;
+       tms.tm_year = 1970 - 1900;
+       tms.tm_mday = 1;
+       tms.tm_sec = -2;
+       ATF_REQUIRE_ERRNO(errno, timegm(&tms) == (time_t)-2);
+
+}
+
+
 ATF_TP_ADD_TCS(tp)
 {
 
-       ATF_TP_ADD_TC(tp, mktime);
+       ATF_TP_ADD_TC(tp, mktime_negyear);
+       ATF_TP_ADD_TC(tp, timegm_epoch);
 
        return atf_no_error();
 }



Home | Main Index | Thread Index | Old Index