diff options
author | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2019-11-11 13:21:30 +0100 |
---|---|---|
committer | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2019-11-11 13:21:30 +0100 |
commit | fba2e1f41625a998624c6918eaf034cf3e9bd71a (patch) | |
tree | 1df8815b9a65d8f92ff72de6f5a918a796c9b897 /src | |
parent | 900fb0aee0b68cbb88f578f20019738b4416f436 (diff) |
Add interpreter tests
Diffstat (limited to 'src')
40 files changed, 214 insertions, 7 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java index a9857ed..76ea1e6 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java @@ -25,6 +25,8 @@ package pl.edu.mimuw.cloudatlas.interpreter; import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.PrintStream; import java.net.InetAddress; import java.net.UnknownHostException; import java.text.ParseException; @@ -53,30 +55,35 @@ public class Main { private static ZMI root; public static void main(String[] args) throws Exception { + runTest(System.in, System.out); + } + + public static void runTest(InputStream in, PrintStream out) throws Exception { root = createTestHierarchy(); - Scanner scanner = new Scanner(System.in); + Scanner scanner = new Scanner(in); scanner.useDelimiter("\\n"); - while(scanner.hasNext()) - executeQueries(root, scanner.next()); + while(scanner.hasNext()) { + executeQueries(root, scanner.next(), out); + } scanner.close(); - } + } private static PathName getPathName(ZMI zmi) { String name = ((ValueString)zmi.getAttributes().get("name")).getValue(); return zmi.getFather() == null? PathName.ROOT : getPathName(zmi.getFather()).levelDown(name); } - private static void executeQueries(ZMI zmi, String query) throws Exception { + private static void executeQueries(ZMI zmi, String query, PrintStream out) throws Exception { if(!zmi.getSons().isEmpty()) { for(ZMI son : zmi.getSons()) - executeQueries(son, query); + executeQueries(son, query, out); Interpreter interpreter = new Interpreter(zmi); Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes())); try { List<QueryResult> result = interpreter.interpretProgram((new parser(lex)).pProgram()); PathName zone = getPathName(zmi); for(QueryResult r : result) { - System.out.println(zone + ": " + r); + out.println(zone + ": " + r); zmi.getAttributes().addOrChange(r.getName(), r.getValue()); } } catch(InterpreterException exception) {} diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/interpreter/InterpreterTests.java b/src/test/java/pl/edu/mimuw/cloudatlas/interpreter/InterpreterTests.java new file mode 100644 index 0000000..54312b8 --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/interpreter/InterpreterTests.java @@ -0,0 +1,126 @@ +package pl.edu.mimuw.cloudatlas.interpreter; + +import java.io.PrintStream; +import java.io.FileInputStream; +import java.io.File; +import java.io.ByteArrayOutputStream; +import java.net.URL; + +import static org.junit.Assert.*; +import org.junit.Test; + +public class InterpreterTests { + @Test + public void fileTest1() throws Exception { + runFileTest(1); + } + + @Test + public void fileTest2() throws Exception { + runFileTest(2); + } + + @Test + public void fileTest3() throws Exception { + runFileTest(3); + } + + @Test + public void fileTest4() throws Exception { + runFileTest(4); + } + + @Test + public void fileTest5() throws Exception { + runFileTest(5); + } + + @Test + public void fileTest6() throws Exception { + runFileTest(6); + } + + @Test + public void fileTest7() throws Exception { + runFileTest(7); + } + + @Test + public void fileTest8() throws Exception { + runFileTest(8); + } + + @Test + public void fileTest9() throws Exception { + runFileTest(9); + } + + @Test + public void fileTest10() throws Exception { + runFileTest(10); + } + + @Test + public void fileTest11() throws Exception { + runFileTest(11); + } + + @Test + public void fileTest12() throws Exception { + runFileTest(12); + } + + @Test + public void fileTest13() throws Exception { + runFileTest(13); + } + + @Test + public void fileTest14() throws Exception { + runFileTest(14); + } + + @Test + public void fileTest15() throws Exception { + runFileTest(15); + } + + @Test + public void fileTest16() throws Exception { + runFileTest(16); + } + + @Test + public void fileTest17() throws Exception { + runFileTest(17); + } + + @Test + public void fileTest18() throws Exception { + runFileTest(18); + } + + @Test + public void fileTest19() throws Exception { + runFileTest(19); + } + + private void runFileTest(int i) throws Exception { + URL test = InterpreterTests.class.getResource(i + ".in"); + URL testOut = InterpreterTests.class.getResource(i + ".out"); + + FileInputStream in = new FileInputStream(test.getFile()); + ByteArrayOutputStream outByteArray = new ByteArrayOutputStream(); + PrintStream outPrint = new PrintStream(outByteArray); + Main.runTest(in, outPrint); + String actual = outByteArray.toString(); + + File expectedFile = new File(testOut.getFile()); + FileInputStream expectedIn = new FileInputStream(expectedFile); + byte[] buffer = new byte[(int)expectedFile.length()]; + expectedIn.read(buffer); + String expected = new String(buffer, "UTF-8"); + + assertEquals(expected, actual); + } +} diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/1.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/1.in new file mode 100644 index 0000000..4c01bba --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/1.in @@ -0,0 +1 @@ +SELECT 2 + 2 AS two_plus_two diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/1.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/1.out new file mode 100644 index 0000000..b834f59 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/1.out @@ -0,0 +1,3 @@ +/uw: two_plus_two: 4 +/pjwstk: two_plus_two: 4 +/: two_plus_two: 4 diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/10.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/10.in new file mode 100644 index 0000000..eb1b0da --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/10.in @@ -0,0 +1 @@ +SELECT min(name) AS min_name, to_string(first(1, name)) AS max_name ORDER BY name DESC diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/10.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/10.out new file mode 100644 index 0000000..56c050c --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/10.out @@ -0,0 +1,6 @@ +/uw: min_name: khaki13 +/uw: max_name: [violet07] +/pjwstk: min_name: whatever01 +/pjwstk: max_name: [whatever02] +/: min_name: pjwstk +/: max_name: [uw] diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/11.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/11.in new file mode 100644 index 0000000..3081ead --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/11.in @@ -0,0 +1 @@ +SELECT epoch() AS epoch, land(timestamp > epoch()) AS afterY2K diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/11.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/11.out new file mode 100644 index 0000000..72ac484 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/11.out @@ -0,0 +1,6 @@ +/uw: epoch: 2000/01/01 00:00:00.000 +/uw: afterY2K: true +/pjwstk: epoch: 2000/01/01 00:00:00.000 +/pjwstk: afterY2K: true +/: epoch: 2000/01/01 00:00:00.000 +/: afterY2K: true diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/12.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/12.in new file mode 100644 index 0000000..5340bed --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/12.in @@ -0,0 +1 @@ +SELECT min(timestamp) + (max(timestamp) - epoch()) / 2 AS t2 diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/12.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/12.out new file mode 100644 index 0000000..f662146 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/12.out @@ -0,0 +1,3 @@ +/uw: t2: 2019/04/16 05:31:30.000 +/pjwstk: t2: 2019/04/16 08:48:30.000 +/: t2: 2019/04/16 07:12:19.684 diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/13.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/13.in new file mode 100644 index 0000000..af367ff --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/13.in @@ -0,0 +1 @@ +SELECT lor(unfold(some_names) + "xx" REGEXP "([a-z]*)atkax([a-z]*)") AS beatka diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/13.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/13.out new file mode 100644 index 0000000..cc70937 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/13.out @@ -0,0 +1 @@ +/uw: beatka: true diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/14.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/14.in new file mode 100644 index 0000000..4a662ba --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/14.in @@ -0,0 +1 @@ +SELECT (SELECT avg(cpu_usage) WHERE false) AS smth diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/14.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/14.out new file mode 100644 index 0000000..1f9a626 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/14.out @@ -0,0 +1,2 @@ +/uw: smth: NULL +/pjwstk: smth: NULL diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/15.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/15.in new file mode 100644 index 0000000..35e3b89 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/15.in @@ -0,0 +1 @@ +SELECT avg(cpu_usage) AS cpu_usage WHERE (SELECT sum(cardinality)) > (SELECT to_integer((1 + 2 + 3 + 4) / 5)) diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/15.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/15.out new file mode 100644 index 0000000..4d9c0d7 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/15.out @@ -0,0 +1,3 @@ +/uw: cpu_usage: 0.5 +/pjwstk: cpu_usage: NULL +/: cpu_usage: NULL diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/16.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/16.in new file mode 100644 index 0000000..0657efc --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/16.in @@ -0,0 +1 @@ +SELECT ceil(to_double(min(num_cores)) / 1.41) AS sth diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/16.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/16.out new file mode 100644 index 0000000..2e77b3d --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/16.out @@ -0,0 +1,2 @@ +/uw: sth: 3.0 +/pjwstk: sth: 5.0 diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/17.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/17.in new file mode 100644 index 0000000..8eb556d --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/17.in @@ -0,0 +1 @@ +SELECT floor(5.0 / 1.9) AS fl diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/17.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/17.out new file mode 100644 index 0000000..062a8cc --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/17.out @@ -0,0 +1,3 @@ +/uw: fl: 2.0 +/pjwstk: fl: 2.0 +/: fl: 2.0 diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/18.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/18.in new file mode 100644 index 0000000..6c9859a --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/18.in @@ -0,0 +1 @@ +SELECT to_time("2013/07/05 12:54:32.098") + to_duration(6811) AS tim diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/18.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/18.out new file mode 100644 index 0000000..db9ade3 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/18.out @@ -0,0 +1,3 @@ +/uw: tim: 2013/07/05 12:54:38.909 +/pjwstk: tim: 2013/07/05 12:54:38.909 +/: tim: 2013/07/05 12:54:38.909 diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/19.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/19.in new file mode 100644 index 0000000..6ab2d74 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/19.in @@ -0,0 +1 @@ +SELECT avg(cpu_usage * to_double(num_cores)) AS cpu_load, sum(num_cores) AS num_cores diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/19.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/19.out new file mode 100644 index 0000000..4ff843d --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/19.out @@ -0,0 +1,4 @@ +/uw: cpu_load: 2.7 +/uw: num_cores: 6 +/pjwstk: cpu_load: 2.95 +/pjwstk: num_cores: 20 diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/2.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/2.in new file mode 100644 index 0000000..d707ae3 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/2.in @@ -0,0 +1 @@ +SELECT to_integer((to_double(3) - 5.6) / 11.0 + to_double(47 * (31 - 15))) AS math WHERE true diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/2.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/2.out new file mode 100644 index 0000000..8e12aa1 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/2.out @@ -0,0 +1,3 @@ +/uw: math: 751 +/pjwstk: math: 751 +/: math: 751 diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/3.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/3.in new file mode 100644 index 0000000..25203ce --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/3.in @@ -0,0 +1 @@ +SELECT count(members) AS members_count diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/3.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/3.out new file mode 100644 index 0000000..66d6424 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/3.out @@ -0,0 +1,2 @@ +/uw: members_count: 3 +/pjwstk: members_count: 2 diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/4.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/4.in new file mode 100644 index 0000000..4a8347f --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/4.in @@ -0,0 +1 @@ +SELECT first(99, name) AS new_contacts ORDER BY cpu_usage DESC NULLS LAST, num_cores ASC NULLS FIRST diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/4.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/4.out new file mode 100644 index 0000000..b8e0300 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/4.out @@ -0,0 +1,3 @@ +/uw: new_contacts: [khaki13, violet07, khaki31] +/pjwstk: new_contacts: [whatever01, whatever02] +/: new_contacts: [uw, pjwstk] diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/5.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/5.in new file mode 100644 index 0000000..168bc5e --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/5.in @@ -0,0 +1 @@ +SELECT count(num_cores - size(some_names)) AS sth diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/5.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/5.out new file mode 100644 index 0000000..4145a74 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/5.out @@ -0,0 +1 @@ +/uw: sth: 2 diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/6.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/6.in new file mode 100644 index 0000000..67dd68f --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/6.in @@ -0,0 +1 @@ +SELECT min(sum(distinct(2 * level)) + 38 * size(contacts)) AS sth WHERE num_cores < 8 diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/6.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/6.out new file mode 100644 index 0000000..419e97d --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/6.out @@ -0,0 +1,3 @@ +/uw: sth: 80 +/pjwstk: sth: 80 +/: sth: NULL diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/7.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/7.in new file mode 100644 index 0000000..a338281 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/7.in @@ -0,0 +1 @@ +SELECT first(1, name) + last(1,name) AS concat_name WHERE num_cores >= (SELECT min(num_cores) ORDER BY timestamp) ORDER BY creation ASC NULLS LAST diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/7.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/7.out new file mode 100644 index 0000000..8da8318 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/7.out @@ -0,0 +1,2 @@ +/uw: concat_name: [violet07, khaki31] +/pjwstk: concat_name: [whatever01, whatever02] diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/8.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/8.in new file mode 100644 index 0000000..d484389 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/8.in @@ -0,0 +1 @@ +SELECT sum(cardinality) AS cardinality diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/8.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/8.out new file mode 100644 index 0000000..fb762ad --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/8.out @@ -0,0 +1,3 @@ +/uw: cardinality: 3 +/pjwstk: cardinality: 2 +/: cardinality: 5 diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/9.in b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/9.in new file mode 100644 index 0000000..7b87d6e --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/9.in @@ -0,0 +1 @@ +SELECT land(cpu_usage < 0.5) AS cpu_ok diff --git a/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/9.out b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/9.out new file mode 100644 index 0000000..81a4ad9 --- /dev/null +++ b/src/test/resources/pl/edu/mimuw/cloudatlas/interpreter/9.out @@ -0,0 +1,2 @@ +/uw: cpu_ok: false +/pjwstk: cpu_ok: true |