test_timestamp.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /*! \file
3  * \brief Test of \c enc_timestamp_decode() implementation
4  *
5  * Copyright (c) 2012-2022 by the developers. See the LICENSE file for details.
6  */
7 
8 
9 /* ========================================================================== */
10 /* Include headers */
11 
12 #include "posix.h" /* Include this first because of feature test macros */
13 
14 #include <stdio.h>
15 
16 #include "encoding.h"
17 #include "test.h"
18 #include "test_timestamp.h"
19 
20 
21 /* ========================================================================== */
22 /*! \addtogroup TEST */
23 /*! @{ */
24 
25 
26 /* ========================================================================== */
27 /*! \brief Test \c enc_timestamp_decode() implementation
28  *
29  * The following cases are tested:
30  * - One second after epoche with timezone "+0000"
31  * - One second after epoche with timezone "GMT"
32  * - First second of Y2K using timezone "Z"
33  * - Optional day of week and timezone "-0400"
34  * - Optional day of week and timezone "+0200"
35  * - Obsolete timezone "PDT"
36  * - Leap year, leap second and timezone "+0131" (with nonzero minutes)
37  * - Time of day without optional seconds and obsolete timezone "UT"
38  *
39  * \return
40  * - \c EXIT_SUCCESS on success
41  * - \c EXIT_FAILURE on error
42  */
43 
44 int test_timestamp(void)
45 {
46 #define TS_NUM (size_t) 8 /* Number of timestamps to test */
47  static const char* ts[TS_NUM] =
48  {
49  "1 Jan 1970 00:00:01 +0000",
50  "1 Jan 1970 00:00:01 GMT",
51  "1 Mar 2000 00:00:00 Z",
52  "Wed, 12 Jun 2013 21:56:34 -0400",
53  "Thu, 27 Jun 2013 00:01:26 +0200",
54  "01 Jan 1999 22:30:59 PDT",
55  "29 Feb 2036 22:30:60 +0131",
56  "31 Dec 1980 23:30 UT"
57  };
58  static const core_time_t t[TS_NUM] = {
59  1U,
60  1U,
61  951868800U,
62  1371088594U,
63  1372284086U,
64  915255059U,
65  2087931600U,
66  347153400U
67  };
68  int res = POSIX_EXIT_SUCCESS;
69  int rv;
70  size_t i;
71 
72  for(i = 0; i < TS_NUM; ++i)
73  {
74  rv = enc_timestamp_decode(ts[i]);
75  if(!rv)
76  {
77  print_error("Timestamp decoder failed");
78  res = POSIX_EXIT_FAILURE;
79  break;
80  }
81  if(t[i] != rv)
82  {
83  print_error("Result is not correct");
84  /* For debugging */
85  fprintf(stderr, TEST_TAB "Timestamp: \"%s\"\n", ts[i]);
86  fprintf(stderr, TEST_TAB "Result is: \"%lu\"\n",
87  (unsigned long int) rv);
88  fprintf(stderr, TEST_TAB "Should be: \"%lu\"\n",
89  (unsigned long int) t[i]);
90  res = POSIX_EXIT_FAILURE;
91  break;
92  }
93  }
94 
95  return(res);
96 }
97 
98 
99 /*! @} */
100 
101 /* EOF */
TEST_TAB
#define TEST_TAB
Tabulator to indent messages from test programs.
Definition: test.h:13
core_time_t
unsigned long int core_time_t
Time in seconds since the epoche (in terms of POSIX.1)
Definition: core.h:54
enc_timestamp_decode
core_time_t enc_timestamp_decode(const char *timestamp)
Decode canonical timestamp to POSIX time (seconds since epoche)
Definition: encoding.c:4154
test_timestamp
int test_timestamp(void)
Test enc_timestamp_decode() implementation.
Definition: test_timestamp.c:44
print_error
void print_error(const char *)
Print error message.
Definition: main.cxx:276

Generated at 2024-04-27 using  doxygen