-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ml
More file actions
154 lines (140 loc) · 6.73 KB
/
test.ml
File metadata and controls
154 lines (140 loc) · 6.73 KB
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
open Printf;;
open Patch;;
module Tf = Textformat;;
let println = print_endline;;
println (str_of_patchSegment (KeepChars 34));;
println (str_of_patchSegment (DelChars 34));;
printf "4 %d; 0 %d\n" (readDim (DelChars 4)) (writeDim (DelChars 4));;
printf "4 %d; 4 %d\n" (readDim (KeepChars 4)) (writeDim (KeepChars 4));;
printf "0 %d; 4 %d\n" (readDim (InsChars "abcd")) (writeDim (InsChars "abcd"));;
let testdata = [
([], [], []);
([], [del 4], [del 4]);
([ins "feuyd"], [], [ins "feuyd"]);
([ins "foo"], [del 4], [del 4; ins "foo"]);
(* lhs del *)
([del 5], [keep 5], [del 5]);
([del 5], [keep 3; ins "af"], [del 3]);
([del 5;keep 2], [keep 7], [del 5;keep 2]);
([del 5], [ins "abcde"], []);
([del 5; keep 2], [ins "abcdefg"], [ins "fg"]);
([del 5], [ins "abc"; keep 2], [del 2]);
(* lhs keep *)
([keep 5], [keep 5], [keep 5]);
([keep 5], [keep 3; ins "ab"], [keep 3; ins "ab"]);
([keep 5; del 2], [keep 7], [keep 5; del 2]);
([keep 5], [ins "wxyza"], [ins "wxyza"]);
([keep 5], [ins "efg"; keep 2], [ins "efg"; keep 2]);
([keep 5; del 2], [ins "abcdefg"], [ins "abcde"]);
(* no repeated segments! *)
([ins "abc"; keep 3; ins "def"], [ins "xyz"], [ins "abcxyzdef"]);
([del 4], [del 3; keep 4; del 7], [del 14]);
(* make sure ins/del sequences are normalized *)
([ins "abc"; del 3; ins "def"], [keep 3], [del 3; ins "abcdef"]);
([ins "abc"; del 3; ins "def"; del 3; ins "ghi"], [keep 6], [del 6; ins "abcdefghi"]);
(* misc *)
([keep 4], [ins "ex"; del 2; keep 2], [del 2; ins "ex"; keep 2])
];;
let test_tuple t =
let (a, b, c) = t in try
let result = (apply a b) in
if not (result = c) then
(printf "Test %s * %s failed. %s != %s.\n"
(str_of_patch a)
(str_of_patch b)
(str_of_patch result)
(str_of_patch c))
with Failure s -> (printf "Test %s * %s blew up: %s.\n" (str_of_patch a) (str_of_patch b) s)
;;
println "testing application";;
List.iter test_tuple testdata;;
printf "%d cases tested.\n" (List.length testdata);;
let texttest = [
(* pretend / is backslash in these comments, because OCaml is annoying *)
("\\", [ins "\\"]); (* "/" -> ins "/" *)
("\\z", [ins "\\z"]); (* "/z" -> ins "/z" *)
("\\\\", [ins "\\\\"]); (* "//" -> ins "//" *)
("\\%", [ins "\\%"]); (* "/%" -> ins "/%" *)
("\\%[", [ins "%["]); (* "/%[" -> ins "%[" *)
("\\%\\\\", [ins "\\%\\\\"]); (* "/%//" -> "/%//" *)
("%[D23]", [del 23]);
("%[K34]", [keep 34]);
("freep %[K5]%[D3]zuu%[D4]", [ins "freep "; keep 5; del 3; ins "zuu"; del 4]);
("abcd%[K3]\\%[xx%[D2]f", [ins "abcd"; keep 3; ins "%[xx"; del 2; ins "f"])
];;
println "testing reading text format";;
List.iter (fun t -> let (data, expected) = t in try
let result = (Textformat.readString data) in begin
printf "Testing \"%s\"\n" data;
if not (result = expected) then
(printf " Test \"%s\" failed. %s != %s.\n"
data (str_of_patch result) (str_of_patch expected));
let reserialized = (Tf.writeString result) in
if not (reserialized = data) then
(printf " Re-serialized doesn't match. \"%s\" != \"%s\".\n"
reserialized data);
end
with Failure s -> (printf "Test \"%s\" blew up: %s.\n" data s))
texttest;;
printf "%d cases tested.\n" (List.length texttest);;
let commtest = [
(* (patch, base), (base', patch') *)
(* simple, non-overlapping cases *)
( ([], [del 4]), ([del 4], [keep 4]) );
( ([ins "cat"], []), ([keep 3], [ins "cat"]) );
( ([keep 3], [keep 3]), ([keep 3], [keep 3]) );
( ([keep 3], [ins "gob"]), ([ins "gob"], []) );
( ([del 3], [ins "abc"]), ([], []) );
( ([del 3], [keep 3]), ([], [del 3]) );
(* original cases *)
( ([keep 7; ins "mno"; keep 3], [keep 2; ins "xyz"; keep 5]), ([keep 2; ins "xyz"; keep 8], [keep 4; ins "mno"; keep 3]) );
( ([keep 2; ins "AB"; keep 2], [keep 1; ins "xy"; keep 1]), ([keep 1; ins "x"; keep 2; ins "y"; keep 1], [keep 1; ins "AB"; keep 1]) );
(* OT/patch-theory-ish test cases *)
( ([keep 2; del 1; ins "b"], [keep 2; del 2; ins "t"]), ([keep 2; del 2; keep 1], [keep 4; ins "b"]) );
(* fuzz cases *)
( ([keep 2; keep 3; del 3], [keep 4; ins "z"; ins "y"; ins "x"; keep 1; del 4]), ([keep 4; del 4; ins "z"], [keep 4; del 1; keep 4]) )
];;
println "testing commutation";;
List.iter (fun ((patch, base), (expected_comm_base, expected_comm_patch)) -> try
let (comm_base, comm_patch) = commute patch base in
let fwd = (apply patch base) in
let rev = (apply comm_base comm_patch) in begin
(if not ((comm_base = expected_comm_base) && (comm_patch = expected_comm_patch)) then
printf "Test %s * %s failed:\n comm_base: %s, expected %s\n comm_patch: %s, expected %s\n"
(str_of_patch patch)
(str_of_patch base)
(str_of_patch comm_base)
(str_of_patch expected_comm_base)
(str_of_patch comm_patch)
(str_of_patch expected_comm_patch));
if not (fwd = rev) then
printf "Test %s * %s failed: fwd %s != rev %s"
(str_of_patch patch)
(str_of_patch base)
(str_of_patch fwd)
(str_of_patch rev)
end
with Failure s -> (printf "Test %s * %s blew up: %s.\n" (str_of_patch patch) (str_of_patch base) s))
commtest;;
printf "%d cases tested.\n" (List.length commtest);;
printf "Testing inversion.\n";;
let invtest = [
(* patch, base, inverse *)
([ins "foo"], [], [del 3]);
([del 3], [ins "hey"], [ins "hey"]);
([keep 4], [ins "fred"], [keep 4]);
([keep 6; del 4; ins "tom"; del 9; ins "how r u"], [ins "hello fred whats up"], [keep 6; del 10; ins "fred whats up"]);
([],[],[])
];;
List.iter (fun (patch, base, expected) -> try
let inv = invert patch base in
if not (inv = expected) then
printf "Test %s * %s failed: %s != expected %s\n"
(str_of_patch patch)
(str_of_patch base)
(str_of_patch inv)
(str_of_patch expected)
with Failure s -> (printf "Test %s * %s blew up: %s.\n" (str_of_patch patch) (str_of_patch base) s))
invtest
;;
printf "%d cases tested.\n" (List.length invtest);;