xref: /unit/src/test/nxt_term_parse_test.c (revision 384:8f86d3ff3e29)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 #include "nxt_tests.h"
9 
10 
11 typedef struct {
12     nxt_str_t   string;
13     nxt_bool_t  is_sec;
14     nxt_int_t   value;
15 } nxt_term_parse_test_t;
16 
17 
18 static const nxt_term_parse_test_t  terms[] = {
19     { nxt_string("1y"),                    1,  365 * 24 * 60 * 60 },
20     { nxt_string("1w"),                    1,  7 * 24 * 60 * 60 },
21     { nxt_string("1w"),                    0,  7 * 24 * 60 * 60 * 1000 },
22     { nxt_string("1w  1d"),                0,  8 * 24 * 60 * 60 * 1000 },
23     { nxt_string("1w  d"),                 0,  -1 },
24     { nxt_string("w"),                     0,  -1 },
25     { nxt_string("1d 1w"),                 0,  -1 },
26     { nxt_string("25d"),                   0,  -2 },
27     { nxt_string("300"),                   1,  300 },
28     { nxt_string("300"),                   0,  300000 },
29     { nxt_string("300s"),                  1,  300 },
30     { nxt_string("300ms"),                 0,  300 },
31     { nxt_string("1y 1M 1w1d1h1m1s"),      1,
32                        (((((365 + 30 + 7 + 1) * 24 + 1) * 60) + 1) * 60) + 1 },
33 };
34 
35 
36 nxt_int_t
nxt_term_parse_test(nxt_thread_t * thr)37 nxt_term_parse_test(nxt_thread_t *thr)
38 {
39     nxt_int_t        val;
40     nxt_uint_t       i;
41     const nxt_str_t  *s;
42 
43     nxt_thread_time_update(thr);
44 
45     for (i = 0; i < nxt_nitems(terms); i++) {
46 
47         s = &terms[i].string;
48         val = nxt_term_parse(s->start, s->length, terms[i].is_sec);
49 
50         if (val != terms[i].value) {
51             nxt_log_alert(thr->log,
52                           "term parse test failed: \"%V\": %i %i",
53                           s, terms[i].value, val);
54             return NXT_ERROR;
55         }
56     }
57 
58     nxt_log_error(NXT_LOG_NOTICE, thr->log, "term parse test passed");
59     return NXT_OK;
60 }
61