next release will be 1.3.3
[ppastats.git] / tests / test_strrep.c
1 /*
2  * Copyright (C) 2010-2011 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <pstr.h>
25
26 static int test_strrep(const char *str,
27                        const char *old,
28                        const char *new,
29                        const char *ref)
30 {
31         char *result, *s;
32
33         if (str)
34                 s = strdup(str);
35         else
36                 s = NULL;
37
38         result = strrep(s, old, new);
39
40         if (result == ref ||
41             (result != NULL && ref != NULL && !strcmp(result, ref)))
42                 return 0;
43
44         fprintf(stderr, "strrep(%s, %s, %s) = %s\n", str, old, new, result);
45
46         return 1;
47 }
48
49 static int tests_strrep()
50 {
51         int failures;
52
53         failures = 0;
54
55         failures += test_strrep(NULL, NULL, NULL, NULL);
56         failures += test_strrep("astring", NULL, NULL, "astring");
57         failures += test_strrep("astring", "astring", NULL, "astring");
58         failures += test_strrep("astring", NULL, "astring", "astring");
59         failures += test_strrep("astring", "astring", "astring", "astring");
60         failures += test_strrep(NULL, "astring", "astring", NULL);
61         failures += test_strrep(NULL, NULL, "astring", NULL);
62         failures += test_strrep(NULL, "astring", NULL, NULL);
63
64         failures += test_strrep("astring", "astring", "", "");
65         failures += test_strrep("DastringE", "astring", "", "DE");
66         failures += test_strrep("Dastring", "astring", "", "D");
67         failures += test_strrep("astringE", "astring", "", "E");
68
69         failures += test_strrep("astring", "old", "new", "astring");
70         failures += test_strrep("aoldstring", "old", "new", "anewstring");
71         failures += test_strrep("astring", "astring", "thenewstring",
72                                 "thenewstring");
73         failures += test_strrep("Dastring", "astring", "thenewstring",
74                                 "Dthenewstring");
75         failures += test_strrep("astringE", "astring", "thenewstring",
76                                 "thenewstringE");
77
78         return failures;
79 }
80
81 int main(int argc, char **argv)
82 {
83         int failures;
84
85         failures = 0;
86
87         failures += tests_strrep();
88
89         if (failures)
90                 exit(EXIT_FAILURE);
91         else
92                 exit(EXIT_SUCCESS);
93 }