File: Test158.ML

package info (click to toggle)
polyml 5.6-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 31,892 kB
  • ctags: 34,453
  • sloc: cpp: 44,983; ansic: 24,520; asm: 14,850; sh: 11,730; makefile: 551; exp: 484; python: 253; awk: 91; sed: 9
file content (288 lines) | stat: -rw-r--r-- 8,467 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
(* Various tests for the OS.Path structure.  The Unix tests are taken largely
   from the documentation.  The Windows tests are inferred from the documentation
   which isn't always clear. *)

datatype sys = Windows | Posix;

open OS.Path;

fun check s true = () | check s false = raise Fail ("check " ^ s);

val check' = check "";

val sys = if isRoot "/" then Posix else Windows;

(* Windows uses either / or \ as a separator but the
   output routines generally produce \.  To simplify the
   checks map backslashes into forward slashes in some cases. *)
val mapSep =
    case sys of
        Posix => (fn c => c)
    |   Windows => String.map (fn #"\\" => #"/" | c => c);

(* fromString *)
val testFromTo =
[
    {
        s = "",
        p = {isAbs=false, vol="", arcs=[]},
        w = {isAbs=false, vol="", arcs=[]}
    },
    {
        s = "/",
        p = {isAbs=true, vol="", arcs=[""]},
        (* In Windows this means the "root" directory on the current drive. *)
        w = {isAbs = false, vol = "", arcs = ["", ""]}
    },
    {
        s = "//",
        p = {isAbs=true, vol="", arcs=["", ""]},
        w = {isAbs = false, vol = "", arcs = ["", "", ""]}
    },
    {
        s = "a",
        p = {isAbs=false, vol="", arcs=["a"]},
        w = {isAbs=false, vol="", arcs=["a"]}
    },
    {
        s = "/a",
        p = {isAbs=true, vol="", arcs=["a"]},
        w = {isAbs=false, vol="", arcs=["", "a"]}
    },
    {
        s = "//a",
        p = {isAbs=true, vol="", arcs=["","a"]},
        w = {isAbs=false, vol="", arcs=["","", "a"]}
    },
    {
        s = "a/",
        p = {isAbs=false, vol="", arcs=["a", ""]},
        w = {isAbs=false, vol="", arcs=["a", ""]}
    },
    {
        s = "a//",
        p = {isAbs=false, vol="", arcs=["a", "", ""]},
        w = {isAbs=false, vol="", arcs=["a", "", ""]}
    },
    {
        s = "a/b",
        p = {isAbs=false, vol="", arcs=["a", "b"]},
        w = {isAbs=false, vol="", arcs=["a", "b"]}
    },
    {
        s = "c:\\",
        p = {isAbs=false, vol="", arcs=["c:\\"]},
        w = {isAbs = true, vol = "c:", arcs = [""]}
    },
    {
        s = "c:",
        p = {isAbs=false, vol="", arcs=["c:"]},
        w = {isAbs=false, vol = "c:", arcs = [""]}
    },
    {
        s = "c:\\abc",
        p = {isAbs=false, vol="", arcs=["c:\\abc"]},
        w = {isAbs=true, vol = "c:", arcs = ["abc"]}
    }


];

fun test { s, p, w } =
let
    val res = fromString s
    val ts = toString res
in
    case sys of
        Posix => check s (res = p)
    |   Windows => check s (res = w);
    check s (ts = s orelse mapSep ts = s)
end;

val () = List.app test testFromTo;

(* validVolume *)
val v1 = validVolume{isAbs = true, vol = ""};
case sys of Posix => check' v1 | Windows => check' (not v1);

val v2 = validVolume{isAbs = false, vol = ""};
check' v2;

val v3 = validVolume{isAbs = true, vol = "C:"};
case sys of Posix => check'(not v3) | Windows => check' v3;

val v4 = validVolume{isAbs = false, vol = "C:"};
case sys of Posix => check'(not v4) | Windows => check' v4;

val v5 = validVolume{isAbs = false, vol = "\\\\server\\share" };
case sys of Posix => check'(not v5) | Windows => check' v5;


(* getParent *)
val testGetParent =
[
    { s = "/", p = "/", w = "/" },
    { s = "a", p = ".", w = "." },
    { s = "a/", p = "a/..", w = "a/.." },
    { s = "a///", p = "a///..", w = "a///.." },
    { s = "a/b", p = "a", w = "a" },
    { s = "a/b/", p = "a/b/..", w = "a/b/.." },
    { s = "..", p = "../..", w = "../.." },
    { s = ".", p = "..", w = ".." },
    { s = "C:\\", p = ".", w = "C:\\" },
    { s = "\\\\server\\share\\", p = ".", w = "\\\\server\\share\\" }
];

fun test { s, p, w } =
let
    val res = getParent s
in
    case sys of
        Posix => check s (res = p)
    |   Windows => check s (res = w orelse mapSep res = mapSep w)
end;

val () = List.app test testGetParent;


(* splitDirFile *)
val testSplitFile =
[
    {
        s = "",
        p = {dir = "", file = ""},
        w = {dir = "", file = ""}
    },
    {
        s = ".",
        p = {dir = "", file = "."},
        w = {dir = "", file = "."}
    },
    {
        s = "b",
        p = {dir = "", file = "b"},
        w = {dir = "", file = "b"}
    },
    {
        s = "b/",
        p = {dir = "b", file = ""},
        w = {dir = "b", file = ""}
    },
    {
        s = "a/b",
        p = {dir = "a", file = "b"},
        w = {dir = "a", file = "b"}
    },
    {
        s = "/a",
        p = {dir = "/", file = "a"},
        w = {dir = "/", file = "a"}
    },
    {
        s = "c:\\a",
        p = {dir = "", file = "c:\\a"},
        w = {dir = "c:\\", file = "a"}
    }

];

fun test { s, p, w } =
let
    val res as { dir, file } = splitDirFile s
in
    case sys of
        Posix => check s (res = p)
    |   Windows => check s (res = w orelse mapSep dir = #dir w andalso mapSep file = #file w)
end;

val () = List.app test testSplitFile;

val testAbsolute =
[
    { s = "/a", p = true, w = false },
    { s = "c:/a", p = false, w = true }
];

fun test { s, p, w } =
let
    val res = isAbsolute s
in
    case sys of
        Posix => check s (res = p)
    |   Windows => check s (res = w)
end;

val () = List.app test testAbsolute;

case sys of
    Posix =>
    let
        val testMkRelative =
        [
            (* These are all relative paths in Windows. *)
            { s = { path = "a/b",   relativeTo = "/c/d" },      p = "a/b"},
            { s = { path = "/",     relativeTo = "/a/b/c" },    p = "../../.." },
            { s = { path = "/a/b/", relativeTo = "/a/c" },      p = "../b/" },
            { s = { path = "/a/b",  relativeTo = "/a/c" },      p = "../b" },
            { s = { path = "/a/b/", relativeTo = "/a/c/"},      p = "../b/" },
            { s = { path = "/a/b",  relativeTo = "/a/c/"},      p = "../b" },
            { s = { path = "/",     relativeTo = "/"},          p = "." },
            { s = { path = "/",     relativeTo = "/."},         p = "." },
            { s = { path = "/",     relativeTo = "/.."},        p = "." },
            { s = { path = "/a/b/../c", relativeTo = "/a/d"},   p = "../b/../c" },
            { s = { path = "/a/b",      relativeTo = "/c/d"},   p = "../../a/b" },
            { s = { path = "/c/a/b",    relativeTo =  "/c/d"},  p = "../a/b" },
            { s = { path = "/c/d/a/b",  relativeTo =  "/c/d"},  p = "a/b" }
        ];

        fun test { s, p } =
        let
            val res = mkRelative s
        in
            check (#path s ^ " " ^ #relativeTo s) (res = p)
        end;
    in
        List.app test testMkRelative
    end
|   Windows =>
    let
        val testMkRelative =
        [
            { s = { path = "a/b",   relativeTo = "c:/c/d" },        w = "a/b" },
            { s = { path = "c:/",     relativeTo = "c:/a/b/c" },    w = "../../.." },
            { s = { path = "c:/a/b/", relativeTo = "c:/a/c" },      w = "../b/" },
            { s = { path = "c:/a/b",  relativeTo = "c:/a/c" },      w = "../b" },
            { s = { path = "c:/a/b/", relativeTo = "c:/a/c/"},      w = "../b/" },
            { s = { path = "c:/a/b",  relativeTo = "c:/a/c/"},      w = "../b" },
            { s = { path = "c:/",     relativeTo = "c:/"},          w = "." },
            { s = { path = "c:/",     relativeTo = "c:/."},         w = "." },
            { s = { path = "c:/",     relativeTo = "c:/.."},        w = "." },
            { s = { path = "c:/a/b/../c", relativeTo = "c:/a/d"},   w = "../b/../c" },
            { s = { path = "c:/a/b",      relativeTo = "c:/c/d"},   w = "../../a/b" },
            { s = { path = "c:/c/a/b",    relativeTo =  "c:/c/d"},  w = "../a/b" },
            { s = { path = "c:/c/d/a/b",  relativeTo =  "c:/c/d"},  w = "a/b" } 
        ];

        fun test { s, w } =
        let
            val res = mkRelative s
        in
            check (#path s ^ " " ^ #relativeTo s) (res = w orelse mapSep res = mapSep w)
        end;
    in
        List.app test testMkRelative
    end;

case sys of
    Windows =>
    (
        (* Special cases for the relative path \\abc.  This is relative to the current volume. *)
        check "" (mkAbsolute{path="\\abc", relativeTo="c:\\def\\xyz"} = "c:\\abc");
        check "" (concat("c:\\abc\\def", "\\ghi") = "c:\\ghi")
    )
|   Posix => ();

(* OS.Path.joinDirFile should raise InvalidArc if the file name does not correspond to an arc *)
(OS.Path.joinDirFile {dir="abc/def", file="ghi/jkl"}; raise Fail "incorrect") handle OS.Path.InvalidArc => ();