Merge pull request #1710 from rpv-tomsk/perl-plugin-fixes
[collectd.git] / src / daemon / utils_subst_test.c
1 /**
2  * collectd - src/daemon/utils_subst_test.c
3  * Copyright (C) 2015       Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  */
26
27 #include "common.h" /* for STATIC_ARRAY_SIZE */
28 #include "collectd.h"
29 #include "testing.h"
30 #include "utils_subst.h"
31
32 #if HAVE_LIBKSTAT
33 kstat_ctl_t *kc;
34 #endif /* HAVE_LIBKSTAT */
35
36 DEF_TEST(subst)
37 {
38   struct {
39     const char *str;
40     int off1;
41     int off2;
42     const char *rplmt;
43     const char *want;
44   } cases[] = {
45     {"foo_____bar", 3, 8, " - ", "foo - bar"}, /* documentation example */
46     {"foo bar", 0, 2, "m",     "mo bar"},    /* beginning, shorten */
47     {"foo bar", 0, 1, "m",     "moo bar"},   /* beginning, same length */
48     {"foo bar", 0, 3, "milk",  "milk bar"},  /* beginning, extend */
49     {"foo bar", 3, 6, "de",    "fooder"},    /* center, shorten */
50     {"foo bar", 2, 6, "rste",  "forster"},   /* center, same length */
51     {"foo bar", 1, 3, "ish",   "fish bar"},  /* center, extend */
52     {"foo bar", 2, 7, "ul",    "foul"},      /* end, shorten */
53     {"foo bar", 3, 7, "lish",  "foolish"},   /* end, same length */
54     {"foo bar", 3, 7, "dwear", "foodwear"},  /* end, extend */
55     /* truncation (buffer is 16 chars) */
56     {"01234567890123",        8, 8,    "", "01234567890123"},
57     {"01234567890123",        8, 8,   "*", "01234567*890123"},
58     {"01234567890123",        8, 8,  "**", "01234567**89012"},
59     /* input > buffer */
60     {"012345678901234----",   0,  0,   "", "012345678901234"},
61     {"012345678901234----",  17, 18,   "", "012345678901234"},
62     {"012345678901234----",   0,  3,   "", "345678901234---"},
63     {"012345678901234----",   0,  4,   "", "45678901234----"},
64     {"012345678901234----",   0,  5,   "", "5678901234----"},
65     {"012345678901234----",   8,  8,  "#", "01234567#890123"},
66     {"012345678901234----",  12, 12, "##", "012345678901##2"},
67     {"012345678901234----",  13, 13, "##", "0123456789012##"},
68     {"012345678901234----",  14, 14, "##", "01234567890123#"},
69     {"012345678901234----",  15, 15, "##", "012345678901234"},
70     {"012345678901234----",  16, 16, "##", "012345678901234"},
71     /* error cases */
72     {NULL,       3,  4, "_",  NULL}, /* no input */
73     {"foo bar",  3, 10, "_",  NULL}, /* offset exceeds input */
74     {"foo bar", 10, 13, "_",  NULL}, /* offset exceeds input */
75     {"foo bar",  4,  3, "_",  NULL}, /* off1 > off2 */
76     {"foo bar",  3,  4, NULL, NULL}, /* no replacement */
77   };
78   size_t i;
79
80   for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++) {
81     char buffer[16] = "!!!!!!!!!!!!!!!";
82
83     if (cases[i].want == NULL) {
84       OK(subst (buffer, sizeof (buffer), cases[i].str, cases[i].off1, cases[i].off2, cases[i].rplmt) == NULL);
85       continue;
86     }
87
88     OK(subst (buffer, sizeof (buffer), cases[i].str, cases[i].off1, cases[i].off2, cases[i].rplmt) == &buffer[0]);
89     EXPECT_EQ_STR(cases[i].want, buffer);
90   }
91
92   return 0;
93 }
94
95 DEF_TEST(subst_string)
96 {
97   struct {
98     const char *str;
99     const char *srch;
100     const char *rplmt;
101     const char *want;
102   } cases[] = {
103     {"Hello %{name}",    "%{name}", "world", "Hello world"},
104     {"abcccccc",         "abc",     "cab",   "ccccccab"},
105     {"(((()(())))())",   "()",      "",      ""},
106     {"food booth",       "oo",      "ee",    "feed beeth"},
107     {"foo bar",          "baz",     "qux",   "foo bar"},
108     {"foo bar",          "oo",      "oo",    "foo bar"},
109     {"sixteen chars",    "chars",   "characters", "sixteen charact"},
110   };
111   size_t i;
112
113   for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++) {
114     char buffer[16];
115
116     if (cases[i].want == NULL) {
117       OK(subst_string (buffer, sizeof (buffer), cases[i].str, cases[i].srch, cases[i].rplmt) == NULL);
118       continue;
119     }
120
121     OK(subst_string (buffer, sizeof (buffer), cases[i].str, cases[i].srch, cases[i].rplmt) == buffer);
122     EXPECT_EQ_STR(cases[i].want, buffer);
123   }
124
125   return 0;
126 }
127
128 int main (void)
129 {
130   RUN_TEST(subst);
131   RUN_TEST(subst_string);
132
133   END_TEST;
134 }
135
136 /* vim: set sw=2 sts=2 et : */