LCOV - code coverage report
Current view: top level - test - test-date.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 71 73 97.3 %
Date: 2019-08-22 15:41:25 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <string.h>
       4             : 
       5             : #include "alloc-util.h"
       6             : #include "string-util.h"
       7             : #include "tests.h"
       8             : #include "time-util.h"
       9             : 
      10          37 : static void test_should_pass(const char *p) {
      11             :         usec_t t, q;
      12             :         char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX];
      13             : 
      14          37 :         log_info("Test: %s", p);
      15          37 :         assert_se(parse_timestamp(p, &t) >= 0);
      16          37 :         assert_se(format_timestamp_us(buf, sizeof(buf), t));
      17          37 :         log_info("\"%s\" → \"%s\"", p, buf);
      18             : 
      19          37 :         assert_se(parse_timestamp(buf, &q) >= 0);
      20          37 :         if (q != t) {
      21             :                 char tmp[FORMAT_TIMESTAMP_MAX];
      22             : 
      23           0 :                 log_error("round-trip failed: \"%s\" → \"%s\"",
      24             :                           buf, format_timestamp_us(tmp, sizeof(tmp), q));
      25             :         }
      26          37 :         assert_se(q == t);
      27             : 
      28          37 :         assert_se(format_timestamp_relative(buf_relative, sizeof(buf_relative), t));
      29          37 :         log_info("%s", strna(buf_relative));
      30          37 : }
      31             : 
      32           1 : static void test_should_parse(const char *p) {
      33             :         usec_t t;
      34             : 
      35           1 :         log_info("Test: %s", p);
      36           1 :         assert_se(parse_timestamp(p, &t) >= 0);
      37           1 :         log_info("\"%s\" → \"@%" PRI_USEC "\"", p, t);
      38           1 : }
      39             : 
      40          17 : static void test_should_fail(const char *p) {
      41             :         usec_t t;
      42             :         int r;
      43             : 
      44          17 :         log_info("Test: %s", p);
      45          17 :         r = parse_timestamp(p, &t);
      46          17 :         if (r >= 0)
      47           0 :                 log_info("\"%s\" → \"@%" PRI_USEC "\" (unexpected)", p, t);
      48             :         else
      49          17 :                 log_info("parse_timestamp() returns %d (expected)", r);
      50          17 :         assert_se(r < 0);
      51          17 : }
      52             : 
      53          13 : static void test_one(const char *p) {
      54          13 :         _cleanup_free_ char *with_utc;
      55             : 
      56          13 :         with_utc = strjoin(p, " UTC");
      57          13 :         test_should_pass(p);
      58          13 :         test_should_pass(with_utc);
      59          13 : }
      60             : 
      61           9 : static void test_one_noutc(const char *p) {
      62           9 :         _cleanup_free_ char *with_utc;
      63             : 
      64           9 :         with_utc = strjoin(p, " UTC");
      65           9 :         test_should_pass(p);
      66           9 :         test_should_fail(with_utc);
      67           9 : }
      68             : 
      69           1 : int main(int argc, char *argv[]) {
      70           1 :         test_setup_logging(LOG_DEBUG);
      71             : 
      72           1 :         test_one("17:41");
      73           1 :         test_one("18:42:44");
      74           1 :         test_one("18:42:44.0");
      75           1 :         test_one("18:42:44.999999999999");
      76           1 :         test_one("12-10-02 12:13:14");
      77           1 :         test_one("12-10-2 12:13:14");
      78           1 :         test_one("12-10-03 12:13");
      79           1 :         test_one("2012-12-30 18:42");
      80           1 :         test_one("2012-10-02");
      81           1 :         test_one("Tue 2012-10-02");
      82           1 :         test_one("yesterday");
      83           1 :         test_one("today");
      84           1 :         test_one("tomorrow");
      85           1 :         test_one_noutc("16:20 UTC");
      86           1 :         test_one_noutc("16:20 Asia/Seoul");
      87           1 :         test_one_noutc("tomorrow Asia/Seoul");
      88           1 :         test_one_noutc("2012-12-30 18:42 Asia/Seoul");
      89           1 :         test_one_noutc("now");
      90           1 :         test_one_noutc("+2d");
      91           1 :         test_one_noutc("+2y 4d");
      92           1 :         test_one_noutc("5months ago");
      93           1 :         test_one_noutc("@1395716396");
      94           1 :         test_should_parse("1970-1-1 UTC");
      95           1 :         test_should_pass("1970-1-1 00:00:01 UTC");
      96           1 :         test_should_fail("1969-12-31 UTC");
      97           1 :         test_should_fail("-100y");
      98           1 :         test_should_fail("today UTC UTC");
      99           1 :         test_should_fail("now Asia/Seoul");
     100           1 :         test_should_fail("+2d Asia/Seoul");
     101           1 :         test_should_fail("@1395716396 Asia/Seoul");
     102             : #if SIZEOF_TIME_T == 8
     103           1 :         test_should_pass("9999-12-30 23:59:59 UTC");
     104           1 :         test_should_fail("9999-12-31 00:00:00 UTC");
     105           1 :         test_should_fail("10000-01-01 00:00:00 UTC");
     106             : #elif SIZEOF_TIME_T == 4
     107             :         test_should_pass("2038-01-19 03:14:07 UTC");
     108             :         test_should_fail("2038-01-19 03:14:08 UTC");
     109             : #endif
     110             : 
     111           1 :         return 0;
     112             : }

Generated by: LCOV version 1.14