1 #include <stdlib.h>
2 #include <iostream>
3 #include <stdio.h>
4 #include <string>
5 #include <string.h>
6 #include <map>
7 
8 // before including helper.h, pretend to be Windows to enable the drive (c:\) handling routines ...
9 #define _WIN32
10 // ... but at the same time force forward slash as our path separator for our tests
11 #define NATIVE_PATH_SEPARATOR '/'
12 
13 #include "../helper.h"
14 
main()15 int main() {
16     int iErrors = 0;
17 
18     // test cases for function strip2ndFromEndOf1st()
19     {
20         // (a) input string, (b) expected result string
21         std::pair<std::string, std::string> examples[] = {
22             { "", "" },
23             { "/", "" },
24             { "//", "" },
25             { "///", "" },
26             { "/foo/bla", "/foo/bla" },
27             { "/foo/bla/", "/foo/bla" },
28             { "/foo/bla//", "/foo/bla" },
29             { "/foo/bla///", "/foo/bla" },
30             { "/foo", "/foo" },
31             { "/foo/", "/foo" },
32             { "/foo//", "/foo" },
33             { "/foo///", "/foo" },
34             { "/foo/asdf.gig", "/foo/asdf.gig" },
35             { "foo", "foo" },
36             { "asdf.gig", "asdf.gig" },
37             { "C:/", "C:" },
38             { "C://", "C:" },
39             { "C:", "C:" },
40             { "C:/asdf", "C:/asdf" },
41             { "C:/asdf/", "C:/asdf"},
42             { "C:/asdf//", "C:/asdf"},
43             { "C:/asdf///", "C:/asdf"},
44         };
45         for (auto ex : examples) {
46             const std::string in  = ex.first;
47             const std::string out = strip2ndFromEndOf1st(in, '/');
48             const std::string expected = ex.second;
49             if (out != expected) iErrors++;
50             printf("strip2ndFromEndOf1st('%s', '/')  =>  '%s' [%s]\n",
51                    in.c_str(), out.c_str(),
52                    ((out == expected) ? "ok" : "WRONG")
53             );
54         }
55     }
56 
57     // test cases for function parentPath()
58     {
59         // (a) input string, (b) expected result string
60         std::pair<std::string, std::string> examples[] = {
61             { "", "" },
62             { "/", "/" },
63             { "//", "/" },
64             { "///", "/" },
65             { "/foo/bla", "/foo" },
66             { "/foo/bla/", "/foo" },
67             { "/foo/bla//", "/foo" },
68             { "/foo/bla///", "/foo" },
69             { "/foo", "/" },
70             { "/foo/", "/" },
71             { "/foo//", "/" },
72             { "/foo///", "/" },
73             { "/foo/asdf.gig", "/foo" },
74             { "foo", "" },
75             { "asdf.gig", "" },
76             { "C:", "C:" },
77             { "C:/", "C:" },
78             { "C://", "C:" },
79             { "C:///", "C:" },
80             { "C:/asdf", "C:" },
81             { "C:/asdf/", "C:"},
82             { "C:/asdf//", "C:"},
83             { "C:/asdf///", "C:"},
84             { "C:/foo.gig", "C:"},
85         };
86         for (auto ex : examples) {
87             const std::string in  = ex.first;
88             const std::string out = parentPath(in);
89             const std::string expected = ex.second;
90             if (out != expected) iErrors++;
91             printf("parentPath('%s') => '%s' [%s]\n",
92                    in.c_str(), out.c_str(),
93                    ((out == expected) ? "ok" : "WRONG")
94             );
95         }
96     }
97 
98     // test cases for function lastPathComponent()
99     {
100         // (a) input string, (b) expected result string
101         std::pair<std::string, std::string> examples[] = {
102             { "", "" },
103             { "/", "" },
104             { "//", "" },
105             { "///", "" },
106             { "/foo/bla", "bla" },
107             { "/foo/bla/", "" },
108             { "/foo/bla//", "" },
109             { "/foo/bla///", "" },
110             { "/foo", "foo" },
111             { "/foo/", "" },
112             { "/foo//", "" },
113             { "/foo///", "" },
114             { "/foo/asdf.gig", "asdf.gig" },
115             { "foo", "foo" },
116             { "asdf.gig", "asdf.gig" },
117             { "C:", "" },
118             { "C:/", "" },
119             { "C://", "" },
120             { "C:///", "" },
121             { "C:/asdf", "asdf" },
122             { "C:/asdf/", ""},
123             { "C:/asdf//", ""},
124             { "C:/asdf///", ""},
125             { "C:/foo.gig", "foo.gig"},
126         };
127         for (auto ex : examples) {
128             const std::string in  = ex.first;
129             const std::string out = lastPathComponent(in);
130             const std::string expected = ex.second;
131             if (out != expected) iErrors++;
132             printf("lastPathComponent('%s') => '%s' [%s]\n",
133                    in.c_str(), out.c_str(),
134                    ((out == expected) ? "ok" : "WRONG")
135             );
136         }
137     }
138 
139     // test cases for function pathWithoutExtension()
140     {
141         // (a) input string, (b) expected result string
142         std::pair<std::string, std::string> examples[] = {
143             { "", "" },
144             { "/foo/bla", "/foo/bla" },
145             { "/foo", "/foo" },
146             { "/foo/asdf.gig", "/foo/asdf" },
147             { "/foo/asdf.gigx", "/foo/asdf" },
148             { "foo", "foo" },
149             { "asdf.gig", "asdf" },
150             { "asdf.blabla", "asdf" },
151             { "C:/foo.gig", "C:/foo"},
152             { "C:/foo.blabla", "C:/foo"},
153         };
154         for (auto ex : examples) {
155             const std::string in  = ex.first;
156             const std::string out = pathWithoutExtension(in);
157             const std::string expected = ex.second;
158             if (out != expected) iErrors++;
159             printf("pathWithoutExtension('%s') => '%s' [%s]\n",
160                    in.c_str(), out.c_str(),
161                    ((out == expected) ? "ok" : "WRONG")
162             );
163         }
164     }
165 
166     // test cases for function extensionOfPath()
167     {
168         // (a) input string, (b) expected result string
169         std::pair<std::string, std::string> examples[] = {
170             { "", "" },
171             { "/foo/bla", "" },
172             { "/foo", "" },
173             { "/foo/asdf.gig", "gig" },
174             { "/foo/asdf.gigx", "gigx" },
175             { "foo", "" },
176             { "asdf.gig", "gig" },
177             { "asdf.blabla", "blabla" },
178             { "C:/foo.gig", "gig"},
179             { "C:/foo.blabla", "blabla"},
180         };
181         for (auto ex : examples) {
182             const std::string in  = ex.first;
183             const std::string out = extensionOfPath(in);
184             const std::string expected = ex.second;
185             if (out != expected) iErrors++;
186             printf("extensionOfPath('%s') => '%s' [%s]\n",
187                    in.c_str(), out.c_str(),
188                    ((out == expected) ? "ok" : "WRONG")
189             );
190         }
191     }
192 
193     printf("\n[There were %d errors]\n", iErrors);
194 }
195