1
2 /*
3 * Copyright (C) NGINX, Inc.
4 * Copyright (C) Valentin V. Bartenev
5 */
6
7 #include <nxt_main.h>
8 #include "nxt_tests.h"
9
10
11 typedef struct {
12 const char *v1;
13 const char res;
14 const char *v2;
15 } nxt_strverscmp_test_t;
16
17
18 nxt_int_t
nxt_strverscmp_test(nxt_thread_t * thr)19 nxt_strverscmp_test(nxt_thread_t *thr)
20 {
21 nxt_int_t ret;
22 nxt_uint_t i;
23
24 static const nxt_strverscmp_test_t tests[] = {
25 { "word", '=', "word" },
26 { "42", '=', "42" },
27 { "000", '=', "000" },
28 { "2", '>', "1" },
29 { "2", '<', "10" },
30 { "rc2", '>', "rc" },
31 { "rc2", '<', "rc3" },
32 { "1.13.8", '>', "1.1.9" },
33 { "1.9", '<', "1.13.8" },
34 { "9.9", '<', "10.0" },
35 { "1", '>', "007" },
36 { "2b01", '<', "2b013" },
37 { "011", '>', "01" },
38 { "011", '>', "01.1" },
39 { "011", '>', "01+1" },
40 { "011", '<', "01:1" },
41 { "011", '<', "01b" },
42 { "020", '>', "01b" },
43 { "a0", '>', "a01" },
44 { "b00", '<', "b01" },
45 { "c000", '<', "c01" },
46 { "000", '<', "00" },
47 { "000", '<', "00a" },
48 { "00.", '>', "000" },
49 { "a.0", '<', "a0" },
50 { "b11", '>', "b0" },
51 };
52
53 nxt_thread_time_update(thr);
54
55 for (i = 0; i < nxt_nitems(tests); i++) {
56
57 ret = nxt_strverscmp((u_char *) tests[i].v1, (u_char *) tests[i].v2);
58
59 switch (tests[i].res) {
60
61 case '<':
62 if (ret < 0) {
63 continue;
64 }
65
66 break;
67
68 case '=':
69 if (ret == 0) {
70 continue;
71 }
72
73 break;
74
75 case '>':
76 if (ret > 0) {
77 continue;
78 }
79
80 break;
81 }
82
83 nxt_log_alert(thr->log,
84 "nxt_strverscmp() test \"%s\" %c \"%s\" failed: %i",
85 tests[i].v1, tests[i].res, tests[i].v2, ret);
86
87 return NXT_ERROR;
88 }
89
90 nxt_log_error(NXT_LOG_NOTICE, thr->log,
91 "nxt_strverscmp() test passed");
92
93 return NXT_OK;
94 }
95