tst: Provide test for difftime

Message ID 20210125120155.17966-1-lukma@denx.de
State Superseded
Headers
Series tst: Provide test for difftime |

Commit Message

Lukasz Majewski Jan. 25, 2021, 12:01 p.m. UTC
  This change adds new test to assess difftime's functionality by
adding some arbitrary offsets to current time_t value (read via
time).

If 64 bit time_t is supported, the same procedure is applied around
the threshold of Y2038 time overflow.
---
 time/Makefile       |  2 +-
 time/tst-difftime.c | 57 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 time/tst-difftime.c
  

Patch

diff --git a/time/Makefile b/time/Makefile
index 486fb02ecb..7de2ce0196 100644
--- a/time/Makefile
+++ b/time/Makefile
@@ -51,7 +51,7 @@  tests	:= test_time clocktest tst-posixtz tst-strptime tst_wcsftime \
 	   tst-clock tst-clock2 tst-clock_nanosleep tst-cpuclock1 \
 	   tst-adjtime tst-clock-y2038 tst-clock2-y2038 \
 	   tst-cpuclock1-y2038 tst-clock_nanosleep-y2038 tst-clock_settime \
-	   tst-clock_adjtime tst-ctime
+	   tst-clock_adjtime tst-ctime tst-difftime
 
 include ../Rules
 
diff --git a/time/tst-difftime.c b/time/tst-difftime.c
new file mode 100644
index 0000000000..eb3024c949
--- /dev/null
+++ b/time/tst-difftime.c
@@ -0,0 +1,57 @@ 
+/* Test for difftime
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <time.h>
+#include <support/check.h>
+
+static void test_difftime_helper (time_t start, time_t t1,
+                                  time_t t0, double exp_val)
+{
+  time_t time1, time0;
+
+  time1 = time0 = start;
+  time1 += t1;
+  time0 += t0;
+
+  double sub = difftime (time1, time0);
+  if (sub != exp_val)
+    FAIL_EXIT1 ("*** Difftime returned %f (expected %f)\n", sub, exp_val);
+}
+
+static int
+do_test (void)
+{
+  /* Check if difftime works with current time.  */
+  test_difftime_helper (time (NULL), +1800, -1800, 3600.0);
+  test_difftime_helper (time (NULL), -1800, +1800, -3600.0);
+
+  /* Check if difftime works correctly near 32 bit time_t overflow.  */
+  if (sizeof (time_t) > 4)
+    {
+      test_difftime_helper (0x7FFFFFFF, +1800, -1800, 3600.0);
+      test_difftime_helper (0x80000000, +1800, -1800, 3600.0);
+      test_difftime_helper (0x80000000, -1800, +1800, -3600.0);
+      test_difftime_helper (0x7FFFFFFF, -1800, +1800, -3600.0);
+    }
+  else
+    FAIL_UNSUPPORTED ("32-bit time_t");
+
+  return 0;
+}
+
+#include <support/test-driver.c>