]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/pxp/pxp/rtests/reader/test_reader.ml
Initial revision
[helm.git] / helm / DEVEL / pxp / pxp / rtests / reader / test_reader.ml
1 open Pxp_reader;;
2 open Pxp_types;;
3 open Minilex;;
4
5 let make_channel s =
6   (* Returns a channel reading the bytes from the string s *)
7   let rd, wr = Unix.pipe() in
8   let ch_rd = Unix.in_channel_of_descr rd in
9   let ch_wr = Unix.out_channel_of_descr wr in
10   ignore
11     (Thread.create
12        (fun () ->
13           output_string ch_wr s;
14           close_out ch_wr;
15        )
16        ()
17     );
18   ch_rd
19 ;;
20
21 (**********************************************************************)
22
23 let t001 () =
24   (* Reads from a string (without recoding it), checks the lexbuf size *)
25   let s = "0123456789abc" in
26   let r = new resolve_read_this_string s in
27   r # init_rep_encoding `Enc_iso88591;
28   r # init_warner (new drop_warnings);
29   let lb = r # open_in Anonymous in
30   let c = nextchar lb in
31   assert (c = Some '0');
32   assert (lb.Lexing.lex_curr_pos = lb.Lexing.lex_buffer_len);
33   (* Note: the end of lb.lex_buffer is filled up, so lb.lex_curr_pos must
34    * now be at the end of the buffer indicating that the buffer is now
35    * empty.
36    *)
37   ignore(nextchar lb);
38   ignore(nextchar lb);
39   ignore(nextchar lb);
40   ignore(nextchar lb);
41   ignore(nextchar lb);
42   ignore(nextchar lb);
43   ignore(nextchar lb);
44   ignore(nextchar lb);
45   let c = nextchar lb in
46   assert (c = Some '9');
47   assert (lb.Lexing.lex_curr_pos = lb.Lexing.lex_buffer_len);
48   r # change_encoding "";
49   let c = nextchar lb in
50   assert (c = Some 'a');
51   assert (lb.Lexing.lex_curr_pos < lb.Lexing.lex_buffer_len);
52   ignore(nextchar lb);
53   let c = nextchar lb in
54   assert (c = Some 'c');
55   let c = nextchar lb in
56   assert (c = None);
57   r # close_in;
58   true
59 ;;
60
61
62 let t002 () =
63   (* Like t001, but reads from a channel *)
64   let ch = make_channel "0123456789abc" in
65   let r = new resolve_read_this_channel ch in
66   r # init_rep_encoding `Enc_iso88591;
67   r # init_warner (new drop_warnings);
68   let lb = r # open_in Anonymous in
69   let c = nextchar lb in
70   assert (c = Some '0');
71   assert (lb.Lexing.lex_curr_pos = lb.Lexing.lex_buffer_len);
72   (* Note: the end of lb.lex_buffer is filled up, so lb.lex_curr_pos must
73    * now be at the end of the buffer indicating that the buffer is now
74    * empty.
75    *)
76   ignore(nextchar lb);
77   ignore(nextchar lb);
78   ignore(nextchar lb);
79   ignore(nextchar lb);
80   ignore(nextchar lb);
81   ignore(nextchar lb);
82   ignore(nextchar lb);
83   ignore(nextchar lb);
84   let c = nextchar lb in
85   assert (c = Some '9');
86   assert (lb.Lexing.lex_curr_pos = lb.Lexing.lex_buffer_len);
87   r # change_encoding "";
88   let c = nextchar lb in
89   assert (c = Some 'a');
90   assert (lb.Lexing.lex_curr_pos < lb.Lexing.lex_buffer_len);
91   ignore(nextchar lb);
92   let c = nextchar lb in
93   assert (c = Some 'c');
94   let c = nextchar lb in
95   assert (c = None);
96   r # close_in;
97   true
98 ;;
99
100
101 let t003 () =
102   (* Tests non-automatic encoding conversion from ISO-8859-1 to UTF-8 *)
103   let s = "0«»°áàâãäÁÀÂÃÄéèêëíìîïÍÌÎÏóòôõøöÓÒÔÕØÖúùûüýÿÝßç¡¿ñÑ" in
104   let r = new resolve_read_this_string ~fixenc:`Enc_iso88591 s in
105   r # init_rep_encoding `Enc_utf8;
106   r # init_warner (new drop_warnings);
107   let lb = r # open_in Anonymous in
108   let c = ref (nextchar lb) in
109   assert (!c = Some '0');
110   assert (lb.Lexing.lex_curr_pos < lb.Lexing.lex_buffer_len);
111   (* Note: because we initialize the resolver with ~fixenc, the resolver can
112    * fill the buffer with more than one byte from the beginning.
113    *)
114   let u = ref "" in
115   while !c <> None do
116     ( match !c with
117           Some x -> u := !u ^ String.make 1 x
118         | None -> ()
119     );
120     c := nextchar lb
121   done;
122   r # close_in;
123   !u = "0\194\171\194\187\194\176\195\161\195\160\195\162\195\163\195\164\195\129\195\128\195\130\195\131\195\132\195\169\195\168\195\170\195\171\195\173\195\172\195\174\195\175\195\141\195\140\195\142\195\143\195\179\195\178\195\180\195\181\195\184\195\182\195\147\195\146\195\148\195\149\195\152\195\150\195\186\195\185\195\187\195\188\195\189\195\191\195\157\195\159\195\167\194\161\194\191\195\177\195\145"
124 ;;
125
126
127 let t004 () =
128   (* Tests non-automatic encoding conversion from UTF-8 to ISO-8859-1 *)
129   let s = "0\194\171\194\187\194\176\195\161\195\160\195\162\195\163\195\164\195\129\195\128\195\130\195\131\195\132\195\169\195\168\195\170\195\171\195\173\195\172\195\174\195\175\195\141\195\140\195\142\195\143\195\179\195\178\195\180\195\181\195\184\195\182\195\147\195\146\195\148\195\149\195\152\195\150\195\186\195\185\195\187\195\188\195\189\195\191\195\157\195\159\195\167\194\161\194\191\195\177\195\145" in
130   let r = new resolve_read_this_string ~fixenc:`Enc_utf8 s in
131   r # init_rep_encoding `Enc_iso88591;
132   r # init_warner (new drop_warnings);
133   let lb = r # open_in Anonymous in
134   let c = ref (nextchar lb) in
135   assert (!c = Some '0');
136   assert (lb.Lexing.lex_curr_pos < lb.Lexing.lex_buffer_len);
137   (* Note: because we initialize the resolver with ~fixenc, the resolver can
138    * fill the buffer with more than one byte from the beginning.
139    *)
140   let u = ref "" in
141   while !c <> None do
142     ( match !c with
143           Some x -> u := !u ^ String.make 1 x
144         | None -> ()
145     );
146     c := nextchar lb
147   done;
148   r # close_in;
149   !u = "0«»°áàâãäÁÀÂÃÄéèêëíìîïÍÌÎÏóòôõøöÓÒÔÕØÖúùûüýÿÝßç¡¿ñÑ"
150 ;;
151
152
153 let t005 () =
154   (* Tests automatic encoding conversion from UTF-8 to ISO-8859-1 *)
155   let s = "0\194\171\194\187\194\176\195\161\195\160\195\162\195\163\195\164\195\129\195\128\195\130\195\131\195\132\195\169\195\168\195\170\195\171\195\173\195\172\195\174\195\175\195\141\195\140\195\142\195\143\195\179\195\178\195\180\195\181\195\184\195\182\195\147\195\146\195\148\195\149\195\152\195\150\195\186\195\185\195\187\195\188\195\189\195\191\195\157\195\159\195\167\194\161\194\191\195\177\195\145" in
156   let r = new resolve_read_this_string s in
157   r # init_rep_encoding `Enc_iso88591;
158   r # init_warner (new drop_warnings);
159   let lb = r # open_in Anonymous in
160   let c = ref (nextchar lb) in
161   assert (!c = Some '0');
162   assert (lb.Lexing.lex_curr_pos = lb.Lexing.lex_buffer_len);
163   let u = ref "" in
164   while !c <> None do
165     ( match !c with
166           Some x -> u := !u ^ String.make 1 x
167         | None -> ()
168     );
169     c := nextchar lb
170   done;
171   r # close_in;
172   !u = "0«»°áàâãäÁÀÂÃÄéèêëíìîïÍÌÎÏóòôõøöÓÒÔÕØÖúùûüýÿÝßç¡¿ñÑ"
173 ;;
174
175
176 let t006 () =
177   (* Tests automatic encoding conversion from UTF-16-BE to UTF-8 
178    * This variant invokes change_encoding early.
179    *)
180   let s = "\254\255\0000\000«\000»\000°\000á\000à\000â\000ã\000ä\000Á\000À\000Â\000Ã\000Ä\000é\000è\000ê\000ë\000í\000ì\000î\000ï\000Í\000Ì\000Î\000Ï\000ó\000ò\000ô\000õ\000ø\000ö\000Ó\000Ò\000Ô\000Õ\000Ø\000Ö\000ú\000ù\000û\000ü\000ý\000ÿ\000Ý\000ß\000ç\000¡\000¿\000ñ\000Ñ" in
181   let r = new resolve_read_this_string s in
182   r # init_rep_encoding `Enc_utf8;
183   r # init_warner (new drop_warnings);
184   let lb = r # open_in Anonymous in
185   let c = ref (nextchar lb) in
186   assert (!c = Some '0');
187   assert (lb.Lexing.lex_curr_pos = lb.Lexing.lex_buffer_len);
188   r # change_encoding "";
189   let u = ref "" in
190   while !c <> None do
191     ( match !c with
192           Some x -> u := !u ^ String.make 1 x
193         | None -> ()
194     );
195     c := nextchar lb
196   done;
197   r # close_in;
198   !u = "0\194\171\194\187\194\176\195\161\195\160\195\162\195\163\195\164\195\129\195\128\195\130\195\131\195\132\195\169\195\168\195\170\195\171\195\173\195\172\195\174\195\175\195\141\195\140\195\142\195\143\195\179\195\178\195\180\195\181\195\184\195\182\195\147\195\146\195\148\195\149\195\152\195\150\195\186\195\185\195\187\195\188\195\189\195\191\195\157\195\159\195\167\194\161\194\191\195\177\195\145"
199 ;;
200
201
202 let t007 () =
203   (* Tests automatic encoding conversion from UTF-16-BE to UTF-8 
204    * This variant does not invoke change_encoding
205    *)
206   let s = "\254\255\0000\000«\000»\000°\000á\000à\000â\000ã\000ä\000Á\000À\000Â\000Ã\000Ä\000é\000è\000ê\000ë\000í\000ì\000î\000ï\000Í\000Ì\000Î\000Ï\000ó\000ò\000ô\000õ\000ø\000ö\000Ó\000Ò\000Ô\000Õ\000Ø\000Ö\000ú\000ù\000û\000ü\000ý\000ÿ\000Ý\000ß\000ç\000¡\000¿\000ñ\000Ñ" in
207   let r = new resolve_read_this_string s in
208   r # init_rep_encoding `Enc_utf8;
209   r # init_warner (new drop_warnings);
210   let lb = r # open_in Anonymous in
211   let c = ref (nextchar lb) in
212   assert (!c = Some '0');
213   assert (lb.Lexing.lex_curr_pos = lb.Lexing.lex_buffer_len);
214   let u = ref "" in
215   while !c <> None do
216     ( match !c with
217           Some x -> u := !u ^ String.make 1 x
218         | None -> ()
219     );
220     c := nextchar lb
221   done;
222   r # close_in;
223   !u = "0\194\171\194\187\194\176\195\161\195\160\195\162\195\163\195\164\195\129\195\128\195\130\195\131\195\132\195\169\195\168\195\170\195\171\195\173\195\172\195\174\195\175\195\141\195\140\195\142\195\143\195\179\195\178\195\180\195\181\195\184\195\182\195\147\195\146\195\148\195\149\195\152\195\150\195\186\195\185\195\187\195\188\195\189\195\191\195\157\195\159\195\167\194\161\194\191\195\177\195\145"
224 ;;
225
226 (**********************************************************************)
227
228 let t100 () =
229   (* Reads from a file without recoding it *)
230   let r = new resolve_as_file () in
231   r # init_rep_encoding `Enc_utf8;
232   r # init_warner (new drop_warnings);
233   let cwd = Sys.getcwd() in
234   let lb = r # open_in (System ("file://localhost" ^ cwd ^ "/t100.dat")) in
235   let c = nextchar lb in
236   assert (c = Some '0');
237   assert (lb.Lexing.lex_curr_pos = lb.Lexing.lex_buffer_len);
238   (* Note: the end of lb.lex_buffer is filled up, so lb.lex_curr_pos must
239    * now be at the end of the buffer indicating that the buffer is now
240    * empty.
241    *)
242   for i = 1 to 8 do
243     ignore(nextchar lb);
244   done;
245   let c = nextchar lb in
246   assert (c = Some '9');
247   r # close_in;
248   true
249 ;;
250
251 let t101 () =
252   (* Reads from a file without recoding it *)
253   let r = new resolve_as_file () in
254   r # init_rep_encoding `Enc_utf8;
255   r # init_warner (new drop_warnings);
256   let cwd = Sys.getcwd() in
257   let lb = r # open_in (System ("//localhost" ^ cwd ^ "/t100.dat")) in
258   let c = nextchar lb in
259   assert (c = Some '0');
260   assert (lb.Lexing.lex_curr_pos = lb.Lexing.lex_buffer_len);
261   (* Note: the end of lb.lex_buffer is filled up, so lb.lex_curr_pos must
262    * now be at the end of the buffer indicating that the buffer is now
263    * empty.
264    *)
265   for i = 1 to 8 do
266     ignore(nextchar lb);
267   done;
268   let c = nextchar lb in
269   assert (c = Some '9');
270   r # close_in;
271   true
272 ;;
273
274 let t102 () =
275   (* Reads from a file without recoding it *)
276   let r = new resolve_as_file () in
277   r # init_rep_encoding `Enc_utf8;
278   r # init_warner (new drop_warnings);
279   let cwd = Sys.getcwd() in
280   let lb = r # open_in (System (cwd ^ "/t100.dat")) in
281   let c = nextchar lb in
282   assert (c = Some '0');
283   assert (lb.Lexing.lex_curr_pos = lb.Lexing.lex_buffer_len);
284   (* Note: the end of lb.lex_buffer is filled up, so lb.lex_curr_pos must
285    * now be at the end of the buffer indicating that the buffer is now
286    * empty.
287    *)
288   for i = 1 to 8 do
289     ignore(nextchar lb);
290   done;
291   let c = nextchar lb in
292   assert (c = Some '9');
293   r # close_in;
294   true
295 ;;
296
297 let t103 () =
298   (* Reads from a file without recoding it *)
299   let r = new resolve_as_file () in
300   r # init_rep_encoding `Enc_utf8;
301   r # init_warner (new drop_warnings);
302   let lb = r # open_in (System "t100.dat") in
303   let c = nextchar lb in
304   assert (c = Some '0');
305   assert (lb.Lexing.lex_curr_pos = lb.Lexing.lex_buffer_len);
306   (* Note: the end of lb.lex_buffer is filled up, so lb.lex_curr_pos must
307    * now be at the end of the buffer indicating that the buffer is now
308    * empty.
309    *)
310   for i = 1 to 8 do
311     ignore(nextchar lb);
312   done;
313   let c = nextchar lb in
314   assert (c = Some '9');
315   r # close_in;
316   true
317 ;;
318
319 (**********************************************************************)
320
321 let t110 () =
322   (* Checks whether relative URLs are properly handled *)
323   let r = new resolve_as_file () in
324   r # init_rep_encoding `Enc_utf8;
325   r # init_warner (new drop_warnings);
326   let lb = r # open_in (System "t100.dat") in
327   let c = nextchar lb in
328   assert (c = Some '0');
329   assert (lb.Lexing.lex_curr_pos = lb.Lexing.lex_buffer_len);
330   (* Note: the end of lb.lex_buffer is filled up, so lb.lex_curr_pos must
331    * now be at the end of the buffer indicating that the buffer is now
332    * empty.
333    *)
334   for i = 1 to 8 do
335     ignore(nextchar lb);
336   done;
337   let r' = r # clone in
338   let lb' = r' # open_in (System "t100.dat") in
339   let c = nextchar lb' in
340   assert (c = Some '0');
341   for i = 1 to 8 do
342     ignore(nextchar lb');
343   done;
344   let c = nextchar lb' in
345   assert (c = Some '9');
346   r' # close_in;
347   let c = nextchar lb in
348   assert (c = Some '9');
349   r # close_in;
350   true
351 ;;
352
353 (**********************************************************************)
354 (* Tests whether the encoding handling of System IDs is okay *)
355
356 let t200 () =
357   (* Check the technique for the following tests:
358    * [Checks also 'combine' to some extent.)
359    *)
360   let r1 = new resolve_read_this_string
361              ~id:(System "b.xml")
362              ~fixenc:`Enc_iso88591
363              "ae" in
364   let r2 = new resolve_read_this_string
365              ~id:(System "a.xml")
366              ~fixenc:`Enc_iso88591
367              "<!DOCTYPE a [ <!ELEMENT a ANY> <!ENTITY ae SYSTEM 'b.xml'> ]> <a>&ae;</a>" in
368   let r = new combine [ r1; r2 ] in
369   (* It should now be possible to resolve &ae; *)
370   let _ =
371     Pxp_yacc.parse_document_entity 
372       { Pxp_yacc.default_config with Pxp_yacc.encoding = `Enc_iso88591 }
373       (Pxp_yacc.ExtID(System "a.xml", r))
374       Pxp_yacc.default_spec
375   in
376   true
377 ;;
378
379
380 let t201 () =
381   (* Check that System IDs are converted to UTF-8. rep_encoding = ISO-8859-1 *)
382   let r1 = new resolve_read_this_string
383              ~id:(System "\195\164.xml")      (* This is an UTF-8 "ä"! *)
384              ~fixenc:`Enc_iso88591
385              "ae" in
386   let r2 = new resolve_read_this_string
387              ~id:(System "a.xml")
388              ~fixenc:`Enc_iso88591
389              "<!DOCTYPE a [ <!ELEMENT a ANY> <!ENTITY ae SYSTEM 'ä.xml'> ]> <a>&ae;</a>" in
390   let r = new combine [ r1; r2 ] in
391   (* It should now be possible to resolve &ae; *)
392   let _ =
393     Pxp_yacc.parse_document_entity 
394       { Pxp_yacc.default_config with Pxp_yacc.encoding = `Enc_iso88591 }
395       (Pxp_yacc.ExtID(System "a.xml", r))
396       Pxp_yacc.default_spec
397   in
398   true
399 ;;
400
401
402 let t202 () =
403   (* Check that System IDs are converted to UTF-8. rep_encoding = UTF-8 *)
404   let r1 = new resolve_read_this_string
405              ~id:(System "\195\164.xml")
406              ~fixenc:`Enc_iso88591
407              "ae" in
408   let r2 = new resolve_read_this_string
409              ~id:(System "a.xml")
410              ~fixenc:`Enc_iso88591
411              "<!DOCTYPE a [ <!ELEMENT a ANY> <!ENTITY ae SYSTEM 'ä.xml'> ]> <a>&ae;</a>" in
412   let r = new combine [ r1; r2 ] in
413   (* It should now be possible to resolve &ae; *)
414   let _ =
415     Pxp_yacc.parse_document_entity 
416       { Pxp_yacc.default_config with Pxp_yacc.encoding = `Enc_utf8 }
417       (Pxp_yacc.ExtID(System "a.xml", r))
418       Pxp_yacc.default_spec
419   in
420   true
421 ;;
422
423 (**********************************************************************)
424
425 let test f n =
426   try
427     print_string ("Reader test " ^ n);
428     flush stdout;
429     if f() then
430       print_endline " ok"
431     else
432       print_endline " FAILED!!!!";
433   with
434       error ->
435         print_endline (" FAILED: " ^ string_of_exn error)
436 ;;
437
438 test t001 "001";;
439 test t002 "002";;
440 test t003 "003";;
441 test t004 "004";;
442 test t005 "005";;
443 test t006 "006";;
444 test t007 "007";;
445
446 test t100 "100";;
447 test t101 "101";;
448 test t102 "102";;
449 test t103 "103";;
450
451 test t110 "110";;
452
453 test t200 "200";;
454 test t201 "201";;
455 test t202 "202";;