1 #include "../src/utils.c"
2 #include "greatest.h"
3 
test_string_replace_char(void)4 TEST test_string_replace_char(void)
5 {
6         char *text = g_malloc(128 * sizeof(char));
7 
8         strcpy(text, "a aa aaa");
9         ASSERT_STR_EQ("b bb bbb", string_replace_char('a', 'b', text));
10 
11         strcpy(text, "Nothing to replace");
12         ASSERT_STR_EQ("Nothing to replace", string_replace_char('s', 'a', text));
13 
14         strcpy(text, "");
15         ASSERT_STR_EQ("", string_replace_char('a', 'b', text));
16 
17         g_free(text);
18         PASS();
19 }
20 
21 /*
22  * We trust that string_replace_all and string_replace properly reallocate
23  * memory if the result is longer than the given string, no real way to test for
24  * that far as I know.
25  */
26 
test_string_replace_all(void)27 TEST test_string_replace_all(void)
28 {
29         char *text = g_malloc(128 * sizeof(char));
30 
31         strcpy(text, "aaaaa");
32         ASSERT_STR_EQ("bbbbb", (text = string_replace_all("a", "b", text)));
33 
34         strcpy(text, "");
35         ASSERT_STR_EQ("", (text = string_replace_all("a", "b", text)));
36 
37         strcpy(text, "Nothing to replace");
38         ASSERT_STR_EQ((text = string_replace_all("z", "a", text)), "Nothing to replace");
39 
40         strcpy(text, "Reverse this");
41         ASSERT_STR_EQ("Reverse sith", (text = string_replace_all("this", "sith", text)));
42 
43         strcpy(text, "abcdabc");
44         ASSERT_STR_EQ("xyzabcdxyzabc", (text = string_replace_all("a", "xyza", text)));
45 
46         g_free(text);
47         PASS();
48 }
49 
test_string_append(void)50 TEST test_string_append(void)
51 {
52         char *exp;
53 
54         ASSERT_STR_EQ("text_sep_bit", (exp = string_append(g_strdup("text"), "bit", "_sep_")));
55         g_free(exp);
56         ASSERT_STR_EQ("textbit", (exp = string_append(g_strdup("text"), "bit", NULL)));
57         g_free(exp);
58         ASSERT_STR_EQ("textbit", (exp = string_append(g_strdup("text"), "bit", "")));
59         g_free(exp);
60 
61         ASSERT_STR_EQ("text", (exp = string_append(g_strdup("text"), "", NULL)));
62         g_free(exp);
63         ASSERT_STR_EQ("text", (exp = string_append(g_strdup("text"), "", "_sep_")));
64         g_free(exp);
65 
66         ASSERT_STR_EQ("b", (exp = string_append(g_strdup(""), "b", NULL)));
67         g_free(exp);
68         ASSERT_STR_EQ("b", (exp = string_append(NULL, "b", "_sep_")));
69         g_free(exp);
70 
71         ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), "", NULL)));
72         g_free(exp);
73         ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), NULL, "_sep_")));
74         g_free(exp);
75 
76         ASSERT_STR_EQ("", (exp = string_append(g_strdup(""), "", "_sep_")));
77         g_free(exp);
78         ASSERT_EQ(NULL, (exp = string_append(NULL, NULL, "_sep_")));
79         g_free(exp);
80 
81         PASS();
82 }
83 
test_string_strip_quotes(void)84 TEST test_string_strip_quotes(void)
85 {
86         char *exp = string_strip_quotes(NULL);
87         ASSERT_FALSE(exp);
88 
89         ASSERT_STR_EQ("NewString", (exp = string_strip_quotes("NewString")));
90         g_free(exp);
91 
92         ASSERT_STR_EQ("becomes unquoted", (exp = string_strip_quotes("\"becomes unquoted\"")));
93         g_free(exp);
94 
95         ASSERT_STR_EQ("\"stays quoted", (exp = string_strip_quotes("\"stays quoted")));
96         g_free(exp);
97 
98         ASSERT_STR_EQ("stays quoted\"", (exp = string_strip_quotes("stays quoted\"")));
99         g_free(exp);
100 
101         ASSERT_STR_EQ("stays \"quoted\"", (exp = string_strip_quotes("stays \"quoted\"")));
102         g_free(exp);
103 
104         ASSERT_STR_EQ(" \"stays quoted\"", (exp = string_strip_quotes(" \"stays quoted\"")));
105         g_free(exp);
106 
107         PASS();
108 }
109 
test_string_strip_delimited(void)110 TEST test_string_strip_delimited(void)
111 {
112         char *text = g_malloc(128 * sizeof(char));
113 
114         strcpy(text, "A <simple> string_strip_delimited test");
115         string_strip_delimited(text, '<', '>');
116         ASSERT_STR_EQ("A  string_strip_delimited test", text);
117 
118         strcpy(text, "Remove <blink>html <b><i>tags</i></b></blink>");
119         string_strip_delimited(text, '<', '>');
120         ASSERT_STR_EQ("Remove html tags", text);
121 
122         strcpy(text, "Calls|with|identical|delimiters|are|handled|properly");
123         string_strip_delimited(text, '|', '|');
124         ASSERT_STR_EQ("Calls", text);
125 
126         strcpy(text, "<Return empty string if there is nothing left>");
127         string_strip_delimited(text, '<', '>');
128         ASSERT_STR_EQ("", text);
129 
130         strcpy(text, "Nothing is done if there are no delimiters in the string");
131         string_strip_delimited(text, '<', '>');
132         ASSERT_STR_EQ("Nothing is done if there are no delimiters in the string", text);
133 
134         g_free(text);
135         PASS();
136 }
137 
test_string_to_path(void)138 TEST test_string_to_path(void)
139 {
140         char *ptr, *exp;
141         char *home = getenv("HOME");
142 
143         exp = "/usr/local/bin/script";
144         ASSERT_STR_EQ(exp, (ptr = string_to_path(g_strdup(exp))));
145         free(ptr);
146 
147         exp = "~path/with/wrong/tilde";
148         ASSERT_STR_EQ(exp, (ptr = string_to_path(g_strdup(exp))));
149         free(ptr);
150 
151         ASSERT_STR_EQ((exp = g_strconcat(home, "/.path/with/tilde", NULL)),
152                       (ptr = string_to_path(g_strdup("~/.path/with/tilde"))));
153         free(exp);
154         free(ptr);
155 
156         ASSERT_STR_EQ((exp = g_strconcat(home, "/.path/with/tilde and some space", NULL)),
157                       (ptr = string_to_path(g_strdup("~/.path/with/tilde and some space"))));
158         free(exp);
159         free(ptr);
160 
161         PASS();
162 }
163 
test_string_to_time(void)164 TEST test_string_to_time(void)
165 {
166         char *input[] = { "5000 ms", "5000ms",  "100", "10s",   "2m",    "11h",      "9d", "   5 ms   ", NULL };
167         gint64  exp[] = {      5000,     5000, 100000, 10000, 120000, 39600000, 777600000,     5,         0};
168 
169         int i = 0;
170         while (input[i]){
171                 ASSERT_EQ_FMT(string_to_time(input[i]), exp[i]*1000, "%ld");
172                 i++;
173         }
174 
175         PASS();
176 }
177 
SUITE(suite_utils)178 SUITE(suite_utils)
179 {
180         RUN_TEST(test_string_replace_char);
181         RUN_TEST(test_string_replace_all);
182         RUN_TEST(test_string_append);
183         RUN_TEST(test_string_strip_quotes);
184         RUN_TEST(test_string_strip_delimited);
185         RUN_TEST(test_string_to_path);
186         RUN_TEST(test_string_to_time);
187 }
188 /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */
189