Branch data Line data Source code
1 : : /** @file unittest.cc
2 : : * @brief Unit tests of non-Xapian-specific internal code.
3 : : */
4 : : /* Copyright (C) 2010 Olly Betts
5 : : *
6 : : * This program is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU General Public License as
8 : : * published by the Free Software Foundation; either version 2 of the
9 : : * License, or (at your option) any later version.
10 : : *
11 : : * This program is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : * GNU General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with this program; if not, write to the Free Software
18 : : * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 : : * USA
20 : : */
21 : :
22 : : #include <config.h>
23 : :
24 : : #include <iostream>
25 : :
26 : : using namespace std;
27 : :
28 : : #include "../common/fileutils.cc"
29 : :
30 : : // Currently the test harness drags in Xapian (for reporting Xapian::Error
31 : : // exceptions, backendmanager-related stuff, and maybe other things, so we
32 : : // can't use it here and have to make do with crude versions of what the test
33 : : // harness does much better.
34 : : // FIXME: Sort out the test harness so we can use it here.
35 : :
36 : : #define TEST_EQUAL(A, B) do {\
37 : : const string & test_equal_a = (A);\
38 : : const string & test_equal_b = (B);\
39 : : if (test_equal_a != test_equal_b) {\
40 : : cout << test_equal_a << " != " << test_equal_b << endl;\
41 : : return false;\
42 : : }\
43 : : } while (0)
44 : :
45 : : inline string
46 : 24 : r_r_p(string a, const string & b)
47 : : {
48 : 24 : resolve_relative_path(a, b);
49 : 24 : return a;
50 : : }
51 : :
52 : 1 : static bool test_resolverelativepath1()
53 : : {
54 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("/abs/o/lute", ""), "/abs/o/lute");
[ + - ]
55 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("/abs/o/lute", "/"), "/abs/o/lute");
[ + - ]
56 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("/abs/o/lute", "//"), "/abs/o/lute");
[ + - ]
57 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("/abs/o/lute", "foo"), "/abs/o/lute");
[ + - ]
58 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("/abs/o/lute", "foo/"), "/abs/o/lute");
[ + - ]
59 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("/abs/o/lute", "/foo"), "/abs/o/lute");
[ + - ]
60 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("/abs/o/lute", "/foo/"), "/abs/o/lute");
[ + - ]
61 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("/abs/o/lute", "foo/bar"), "/abs/o/lute");
[ + - ]
62 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("/abs/o/lute", "foo/bar/"), "/abs/o/lute");
[ + - ]
63 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("/abs/o/lute", "/foo/bar"), "/abs/o/lute");
[ + - ]
64 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("/abs/o/lute", "/foo/bar/"), "/abs/o/lute");
[ + - ]
65 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("rel/a/tive", ""), "rel/a/tive");
[ + - ]
66 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("rel/a/tive", "/"), "/rel/a/tive");
[ + - ]
67 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("rel/a/tive", "//"), "//rel/a/tive");
[ + - ]
68 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("rel/a/tive", "foo"), "rel/a/tive");
[ + - ]
69 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("rel/a/tive", "foo/"), "foo/rel/a/tive");
[ + - ]
70 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("rel/a/tive", "/foo"), "/rel/a/tive");
[ + - ]
71 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("rel/a/tive", "/foo/"), "/foo/rel/a/tive");
[ + - ]
72 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("rel/a/tive", "foo/bar"), "foo/rel/a/tive");
[ + - ]
73 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("rel/a/tive", "foo/bar/"), "foo/bar/rel/a/tive");
[ + - ]
74 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("rel/a/tive", "/foo/bar"), "/foo/rel/a/tive");
[ + - ]
75 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("rel/a/tive", "/foo/bar/"), "/foo/bar/rel/a/tive");
[ + - ]
76 : : #ifndef __WIN32__
77 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("/abs/o/lute", "/foo\\bar"), "/abs/o/lute");
[ + - ]
78 [ - + ][ - + ]: 1 : TEST_EQUAL(r_r_p("rel/a/tive", "/foo\\bar"), "/rel/a/tive");
[ + - ]
79 : : #else
80 : : TEST_EQUAL(r_r_p("\\dos\\path", ""), "\\dos\\path");
81 : : TEST_EQUAL(r_r_p("\\dos\\path", "/"), "\\dos\\path");
82 : : TEST_EQUAL(r_r_p("\\dos\\path", "\\"), "\\dos\\path");
83 : : TEST_EQUAL(r_r_p("\\dos\\path", "c:"), "c:\\dos\\path");
84 : : TEST_EQUAL(r_r_p("\\dos\\path", "c:\\"), "c:\\dos\\path");
85 : : TEST_EQUAL(r_r_p("\\dos\\path", "c:\\temp"), "c:\\dos\\path");
86 : : TEST_EQUAL(r_r_p("\\dos\\path", "c:\\temp\\"), "c:\\dos\\path");
87 : : TEST_EQUAL(r_r_p("rel/a/tive", "\\"), "\\rel/a/tive");
88 : : TEST_EQUAL(r_r_p("rel/a/tive", "foo\\"), "foo\\rel/a/tive");
89 : : TEST_EQUAL(r_r_p("rel\\a\\tive", "/foo/"), "/foo/rel\\a\\tive");
90 : : TEST_EQUAL(r_r_p("rel/a/tive", "c:/foo/bar"), "c:/foo/rel/a/tive");
91 : : TEST_EQUAL(r_r_p("rel/a/tive", "c:foo/bar/"), "c:foo/bar/rel/a/tive");
92 : : TEST_EQUAL(r_r_p("rel/a/tive", "c:"), "c:rel/a/tive");
93 : : TEST_EQUAL(r_r_p("rel/a/tive", "c:\\"), "c:\\rel/a/tive");
94 : : TEST_EQUAL(r_r_p("C:rel/a/tive", "c:\\foo\\bar"), "C:\\foo\\rel/a/tive");
95 : : TEST_EQUAL(r_r_p("C:rel/a/tive", "c:"), "C:rel/a/tive");
96 : : // This one is impossible to reliably resolve without knowing the current
97 : : // drive - if it is C:, then the answer is: "C:/abs/o/rel/a/tive"
98 : : TEST_EQUAL(r_r_p("C:rel/a/tive", "/abs/o/lute"), "C:rel/a/tive");
99 : : // UNC paths tests:
100 : : TEST_EQUAL(r_r_p("\\\\SRV\\VOL\\FILE", "/a/b"), "\\\\SRV\\VOL\\FILE");
101 : : TEST_EQUAL(r_r_p("rel/a/tive", "\\\\SRV\\VOL\\DIR\\FILE"), "\\\\SRV\\VOL\\DIR\\rel/a/tive");
102 : : TEST_EQUAL(r_r_p("/abs/o/lute", "\\\\SRV\\VOL\\FILE"), "\\\\SRV\\VOL/abs/o/lute");
103 : : TEST_EQUAL(r_r_p("/abs/o/lute", "\\\\S\\V\\FILE"), "\\\\S\\V/abs/o/lute");
104 : : TEST_EQUAL(r_r_p("/abs/o/lute", "\\\\S\\V\\"), "\\\\S\\V/abs/o/lute");
105 : : TEST_EQUAL(r_r_p("/abs/o/lute", "\\\\S\\V"), "\\\\S\\V/abs/o/lute");
106 : : TEST_EQUAL(r_r_p("//SRV/VOL/FILE", "/a/b"), "//SRV/VOL/FILE");
107 : : TEST_EQUAL(r_r_p("rel/a/tive", "//SRV/VOL/DIR/FILE"), "//SRV/VOL/DIR/rel/a/tive");
108 : : TEST_EQUAL(r_r_p("/abs/o/lute", "//SRV/VOL/FILE"), "//SRV/VOL/abs/o/lute");
109 : : TEST_EQUAL(r_r_p("/abs/o/lute", "//S/V/FILE"), "//S/V/abs/o/lute");
110 : : TEST_EQUAL(r_r_p("/abs/o/lute", "//S/V/"), "//S/V/abs/o/lute");
111 : : TEST_EQUAL(r_r_p("/abs/o/lute", "//S/V"), "//S/V/abs/o/lute");
112 : : TEST_EQUAL(r_r_p("/abs/o/lute", "\\\\?\\C:\\wibble"), "\\\\?\\C:\\abs\\o\\lute");
113 : : TEST_EQUAL(r_r_p("/abs/o/lute", "\\\\?\\UNC\\S\\V"), "\\\\?\\UNC\\S\\V\\abs\\o\\lute");
114 : : TEST_EQUAL(r_r_p("/abs/o/lute", "\\\\?\\UNC\\S\\V\\"), "\\\\?\\UNC\\S\\V\\abs\\o\\lute");
115 : : TEST_EQUAL(r_r_p("/abs/o/lute", "\\\\?\\UNC\\S\\V\\TMP\\README.TXT"), "\\\\?\\UNC\\S\\V\\abs\\o\\lute");
116 : : TEST_EQUAL(r_r_p("r/elativ/e", "\\\\?\\C:\\wibble"), "\\\\?\\C:\\r\\elativ\\e");
117 : : TEST_EQUAL(r_r_p("r/elativ/e", "\\\\?\\C:\\wibble\\wobble"), "\\\\?\\C:\\wibble\\r\\elativ\\e");
118 : : #if 0 // Is this a valid testcase? It fails, but isn't relevant to Xapian.
119 : : TEST_EQUAL(r_r_p("r/elativ/e", "\\\\?\\UNC\\S\\V"), "\\\\?\\UNC\\S\\V\\r\\elativ\\e");
120 : : #endif
121 : : TEST_EQUAL(r_r_p("r/elativ/e", "\\\\?\\UNC\\S\\V\\"), "\\\\?\\UNC\\S\\V\\r\\elativ\\e");
122 : : TEST_EQUAL(r_r_p("r/elativ/e", "\\\\?\\UNC\\S\\V\\TMP\\README.TXT"), "\\\\?\\UNC\\S\\V\\TMP\\r\\elativ\\e");
123 : : #endif
124 : 1 : return true;
125 : : }
126 : :
127 : 1 : int main()
128 : : try {
129 : 1 : int result = 0;
130 : :
131 : 1 : cout << "resolverelativepath1 ... ";
132 [ + - ]: 1 : if (test_resolverelativepath1()) {
133 : 1 : cout << "ok" << endl;
134 : : } else {
135 : 0 : cout << "FAIL" << endl;
136 : 0 : result = 1;
137 : : }
138 : :
139 : 1 : return result;
140 : 1 : } catch (const char * e) {
141 : 0 : cout << e << endl;
142 : 0 : return 1;
143 [ + - ][ + - ]: 3 : }
|