From: Kim Nguyễn Date: Sun, 10 Mar 2013 08:37:19 +0000 (+0100) Subject: Adds a set of testing script: X-Git-Tag: v0.1~151^2~1 X-Git-Url: http://git.nguyen.vg/gitweb/?p=tatoo.git;a=commitdiff_plain;h=37e8a9fe5d5f1f430ced3aa65daf572d330d3398 Adds a set of testing script: - xml_diff: program to compare two XML files - XPathEval.java: reference implementation of XPath 1.0 in Java JAXP - gen_test.sh: create reference query results --- diff --git a/.gitignore b/.gitignore index 60db691..dae0385 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ _build *.native *.byte +tests/*.results/* + diff --git a/scripts/gen_mlpack.sh b/scripts/gen_mlpack.sh deleted file mode 100755 index 95f78ec..0000000 --- a/scripts/gen_mlpack.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh - -cd src -echo `pwd` -for dir in * -do - if [ -d "$dir" ] - then - echo "$dir" - rm -f "$dir".mlpack - cd "$dir" - for i in *.ml *.mly *.mll - do - if [ ! -f "$i" ] - then - continue - fi - echo "$i" - f=`echo "$i" | cut -b1` - l=`basename "$i" .mll` - l=`basename "$l" .mly` - l=`basename "$l" .ml | cut -b2-` - o=`echo "$f" | tr a-z A-Z` - echo "$dir"/"$o""$l" >> ../"$dir".mlpack - done - cd .. - fi -done -cd .. \ No newline at end of file diff --git a/tests/alphabet.xml b/tests/alphabet.xml new file mode 100644 index 0000000..f0412a9 --- /dev/null +++ b/tests/alphabet.xml @@ -0,0 +1 @@ +clergywomandecadentgentilityhappy-go-lucky manjigsawkerchiefThe letter L is followed by the letter:which is followed by the letter:ovenware

plentiful

quarrelsome
sagetatteredvoluptuarywriggle
yawnzuzzurellone
diff --git a/tests/alphabet.xml.queries b/tests/alphabet.xml.queries new file mode 100644 index 0000000..740d084 --- /dev/null +++ b/tests/alphabet.xml.queries @@ -0,0 +1,66 @@ +A1 //L/* +A2 //L/parent::* +A3 //L/descendant::* +A4 //L/descendant-or-self::* +A5 //L/ancestor::* +A6 //L/ancestor-or-self::* +A7 //L/following-sibling::* +A8 //L/preceding-sibling::* +A9 //L/following::* +A10 //L/preceding::* +A11 //L/self::* +A12 //L/@id/parent::* +P1 //*[L] +P2 //*[parent::L] +P3 //*[descendant::L] +P4 //*[descendant-or-self::L] +P5 //*[ancestor::L] +P6 //*[ancestor-or-self::L] +P7 //*[following-sibling::L] +P8 //*[preceding-sibling::L] +P9 //*[following::L] +P10 //*[preceding::L] +P11 //*[self::L] +P12 //*[@id] +T1 //L/text() +T2 //L/comment() +# T3 //L/processing-instruction() +# T4 //L/processing-instruction("myPI") +T5 //L/node() +T6 //L/N +T7 //L/* +O1 //*[child::* and preceding::Q] +O2 //*[not(child::*) and preceding::Q] +O3 //*[preceding::L or following::L] +O4 //L/ancestor::* | //L/descendant::* +# O5 //*[.="happy-go-lucky man"] +# O6 //*[@pre > 12 and @post < 15] +# O7 //*[@pre != @post] +# O8 //*[((@post * @post + @pre * @pre) div (@post + @pre)) > ((@post - @pre) * (@post - @pre))] +# O9 //*[@pre mod 2 = 0] +# F1 //*[contains(.,"plentiful")] +# F2 //*[starts-with(.,"plentiful")] +# F3 //*[substring(.,1,9) = "plentiful"] +# F4 //*[substring-after(.,"oven") = "ware"] +# F5 //*[substring-before(.,"ful") = "plenti"] +# F6 //*[string-length(translate(normalize-space(.)," ","")) > 100] +# F7 //*[concat(.,..) = ..] +# F8 //*[ceiling(@pre div @post) = 1] +# F9 //*[floor(@pre div @post) = 0] +# F10 //*[round(@pre div @post) = 0] +# F11 //*[name(.) = "X"] +# F12 //*[lang("it")] +# F13 //L/child::*[last()] +# F14 //L/descendant::*[4] +# F15 //L/ancestor::*[2] +# F16 //L/following-sibling::*[1] +# F17 //L/preceding-sibling::*[1] +# F18 //L/following::*[7] +# F19 //L/preceding::*[7] +# F20 //*[count(ancestor::*) > 3] +# F21 //*[sum(ancestor::*/@pre) < sum(descendant::*/@pre)] +# F22 id("n1 n26") +# F23 id(id(//*[.="happy-go-lucky man"]/@idrefs)/@idrefs) +# F24 //*[number(@pre) < number(@post)] +# F25 //*[string(@pre - 1) = "0"] +# F26 //*[boolean(@id) = true() and boolean(@idrefs) = false()] diff --git a/tools/Makefile b/tools/Makefile new file mode 100644 index 0000000..89d8986 --- /dev/null +++ b/tools/Makefile @@ -0,0 +1,12 @@ +all: XPathEval.class xml_diff + + +xml_diff: xml_diff.ml + ocamlfind ocamlopt -o xml_diff -package expat -linkpkg xml_diff.ml + +XPathEval.class: XPathEval.java + javac XPathEval.java + + +clean: + rm -f XPathEval.class xml_diff *.o *.cm* diff --git a/tools/XPathEval.java b/tools/XPathEval.java new file mode 100644 index 0000000..8c854ac --- /dev/null +++ b/tools/XPathEval.java @@ -0,0 +1,36 @@ +import javax.xml.xpath.*; +import org.xml.sax.*; +import org.w3c.dom.*; +import javax.xml.transform.*; +import javax.xml.transform.dom.*; +import javax.xml.transform.stream.*; + +public class XPathEval { + + + public static void main(String args[]) { + try { + + + XPath xpath = XPathFactory.newInstance().newXPath(); + String expression = args[1]; + InputSource inputSource = new InputSource(args[0]); + NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); + Transformer serializer = TransformerFactory.newInstance().newTransformer(); + serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + StreamResult o = new StreamResult(System.out); + System.out.println(""); + for(int i = 0; i < nodes.getLength(); i++){ + serializer.transform(new DOMSource(nodes.item(i)), o); + System.out.println(""); + }; + System.out.println(""); + } catch (XPathException e) { + System.out.println (e.getCause()); + } catch (Exception e) { + System.out.println(e); + }; + + + } +} diff --git a/tools/gen_mlpack.sh b/tools/gen_mlpack.sh new file mode 100755 index 0000000..95f78ec --- /dev/null +++ b/tools/gen_mlpack.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +cd src +echo `pwd` +for dir in * +do + if [ -d "$dir" ] + then + echo "$dir" + rm -f "$dir".mlpack + cd "$dir" + for i in *.ml *.mly *.mll + do + if [ ! -f "$i" ] + then + continue + fi + echo "$i" + f=`echo "$i" | cut -b1` + l=`basename "$i" .mll` + l=`basename "$l" .mly` + l=`basename "$l" .ml | cut -b2-` + o=`echo "$f" | tr a-z A-Z` + echo "$dir"/"$o""$l" >> ../"$dir".mlpack + done + cd .. + fi +done +cd .. \ No newline at end of file diff --git a/tools/gen_test.sh b/tools/gen_test.sh new file mode 100755 index 0000000..111052f --- /dev/null +++ b/tools/gen_test.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +usage() { + echo "error: missing input, query file or XPathEval class" + echo "usage: $0 file.xml" +} + +FILE="$1" +RESULTS="$FILE".results +QUERIES="$FILE".queries + +if test ! -f "$FILE" -o ! -f "$QUERIES" -o ! -f XPathEval.class +then + usage; + exit 1 +fi + + +mkdir -p "$RESULTS" + +cat "$QUERIES" | grep -v '^#' | while read qname q +do + echo "Computing $q" + java XPathEval "$FILE" "$q" > "$RESULTS"/"$qname".xml +done diff --git a/tools/xml_diff.ml b/tools/xml_diff.ml new file mode 100644 index 0000000..b6b9695 --- /dev/null +++ b/tools/xml_diff.ml @@ -0,0 +1,100 @@ +type event = Open of string * (string * string) list | Close of string | Text of string + +let eq_event e1 e2 = + match e1, e2 with + | Open(t1, l1), Open(t2, l2) -> t1 = t2 && + let sl1 = List.sort compare l1 + and sl2 = List.sort compare l2 in sl1 = sl2 + | Close t1, Close t2 -> t1 = t2 + | Text s1 , Text s2 -> s1 = s2 + | _ -> false + + +type position = { + byte_index : int; + column_num : int; + line_num : int; + byte_count : int; +} + +type ctx = { + text_buffer : Buffer.t; + events : (event*position) Queue.t; +} +let get_position p = + { byte_index = Expat.get_current_byte_index p; + column_num = Expat.get_current_column_number p; + line_num = Expat.get_current_line_number p; + byte_count = Expat.get_current_byte_count p; + } + + +let rec start_element_handler parser_ ctx tag attr_list = + do_text parser_ ctx; + Queue.add (Open(tag, attr_list), get_position parser_) ctx.events + +and end_element_handler parser_ ctx tag = + do_text parser_ ctx; + Queue.add (Close(tag), get_position parser_) ctx.events + +and do_text parser_ ctx = + if Buffer.length ctx.text_buffer != 0 then + let s = Buffer.contents ctx.text_buffer in + Buffer.clear ctx.text_buffer; + Queue.add (Text s, get_position parser_) ctx.events + +let character_data_handler parser_ ctx text = + Buffer.add_string ctx.text_buffer text + +let create_parser () = + let ctx1 = { text_buffer = Buffer.create 512; events = Queue.create (); } in + let parser1 = Expat.parser_create ~encoding:None in + Expat.set_start_element_handler parser1 (start_element_handler parser1 ctx1); + Expat.set_end_element_handler parser1 (end_element_handler parser1 ctx1); + Expat.set_character_data_handler + parser1 (character_data_handler parser1 ctx1); + parser1, ctx1 + +exception Diff of position + +let diffs fd1 fd2 = + let buffer1 = String.create 4096 in + let buffer2 = String.create 4096 in + let parser1,ctx1 = create_parser () in + let parser2,ctx2 = create_parser () in + let rec loop () = + let read1 = input fd1 buffer1 0 4096 in + let read2 = input fd2 buffer2 0 4096 in + if read1 == 0 && read2 == 0 then () else + let () = Expat.parse_sub parser1 buffer1 0 read1 in + let () = Expat.parse_sub parser2 buffer2 0 read2 in + for i = 1 to min (Queue.length ctx1.events) (Queue.length ctx2.events) do + let e1,p1 = Queue.pop ctx1.events in + let e2,_ = Queue.pop ctx2.events in + if not (eq_event e1 e2) then + raise (Diff p1) + done; + loop () + in + loop () + +let main () = + if Array.length Sys.argv != 3 then + Printf.eprintf "usage: %s file1.xml file2.xml\n%!" Sys.argv.(0) + else + let fn1 = Sys.argv.(1) in + let fn2 = Sys.argv.(2) in + try + let fd1 = open_in fn1 in + let fd2 = open_in fn2 in + let () = + try + diffs fd1 fd2 + with + Diff p -> Printf.printf "File %s and %s differ at line %i, column %i\n%!" + fn1 fn2 p.line_num p.column_num + in close_in fd1; close_in fd2 + with + _ -> Printf.eprintf "Error\n%!" +let () = main () +