diff --git a/build.xml b/build.xml index f9a5d0e..fb82d50 100644 --- a/build.xml +++ b/build.xml @@ -3,6 +3,7 @@ + @@ -10,7 +11,10 @@ - + + + + @@ -32,7 +36,7 @@ - + @@ -50,4 +54,11 @@ + + + + + + + \ No newline at end of file diff --git a/lib/junit/hamcrest-core-1.3.jar b/lib/junit/hamcrest-core-1.3.jar new file mode 100644 index 0000000..9d5fe16 Binary files /dev/null and b/lib/junit/hamcrest-core-1.3.jar differ diff --git a/src/lsystem/engine/Parser.java b/src/lsystem/engine/Parser.java index 9ab4883..366bd3d 100644 --- a/src/lsystem/engine/Parser.java +++ b/src/lsystem/engine/Parser.java @@ -69,17 +69,17 @@ public class Parser { return false; } if(old == '.'){ - for(int y = (type == Type.RULE ? 0 : 1); y < 12; y++){ + for(int y = (type == Type.RULE ? 0 : 1); y < 10; y++){ if(temp == Constants.VALID_CHARS[y]) return false; } } old = temp; for(char validChar : Constants.VALID_CHARS){ - if(temp == validChar) - break; if(validChar == ' ') return false; + if(temp == validChar) + break; } } return bracket == 0; @@ -156,6 +156,7 @@ public class Parser { } } } + assert root == null; return root; } diff --git a/src/lsystem/screen/main/Listener.java b/src/lsystem/screen/main/Listener.java index cdb9379..68f691b 100644 --- a/src/lsystem/screen/main/Listener.java +++ b/src/lsystem/screen/main/Listener.java @@ -65,8 +65,8 @@ public class Listener extends AbstractListener implements KeyListener, MouseWhee break; case "Clear": - tab.getTextArea((byte) 0).setText("Axiome : \n"); - tab.getTextArea((byte) 1).setText("Règles : \n"); + tab.getTextArea((byte) 0).setText("Axiome :"); + tab.getTextArea((byte) 1).setText("Règles :"); tab.getTextField((byte) 0).setText(""); tab.getTextField((byte) 1).setText(""); Listener kl = (Listener) tab.getTextField((byte) 0).getKeyListeners()[0]; diff --git a/src/lsystem/screen/main/Tab.java b/src/lsystem/screen/main/Tab.java index 73552a1..ab8fba2 100644 --- a/src/lsystem/screen/main/Tab.java +++ b/src/lsystem/screen/main/Tab.java @@ -3,6 +3,7 @@ package lsystem.screen.main; import javax.swing.*; import java.awt.*; import java.util.Arrays; +import java.util.stream.Collectors; public class Tab extends JPanel{ @@ -133,7 +134,7 @@ public class Tab extends JPanel{ */ public String getAxiom() { String str = axiomList.getText(); - str = str.substring(10).replaceAll(";", ""); + str = str.substring(9).replaceAll(";", ""); return str; } @@ -142,9 +143,9 @@ public class Tab extends JPanel{ */ public java.util.List getRules(){ String str = rulesList.getText(); - str = str.substring(10).replaceAll(";", ""); + str = str.substring(9).replaceAll(";", ""); String[] strsplit = str.split("\n"); - return Arrays.asList(strsplit); + return Arrays.stream(strsplit).filter(p -> !p.equals("")).collect(Collectors.toList()); // To eliminate empty string from the list } /** diff --git a/test/lsystem/TestLSystem.java b/test/lsystem/TestLSystem.java new file mode 100644 index 0000000..6e09d2a --- /dev/null +++ b/test/lsystem/TestLSystem.java @@ -0,0 +1,50 @@ +package lsystem; + +import lsystem.engine.Parser; +import lsystem.engine.Rewrite; +import lsystem.screen.gl3d.GLCanvas; +import lsystem.utils.Pair; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class TestLSystem { + + @Test + public void parserTest() { + GLCanvas joglFrame = new GLCanvas(); + Parser parser = new Parser("X", Arrays.asList("X=XY", "Y=X"), 5); + List> lSystemRules = parser.parseRules(); + joglFrame.setLSystem(parser.getAxiom(), lSystemRules, parser.getNbIterations()); + System.out.println(joglFrame.getLSystem()); + assertNotNull("L-System should not be null", joglFrame.getLSystem()); + } + + @Test + public void rewriteTest(){ + Parser parser = new Parser("X", Arrays.asList("X=XY", "Y=X"), 7); + String result = Rewrite.rewrite(parser.getAxiom(), parser.parseRules(), parser.getNbIterations()); + assertEquals("Not equals", "XYXXYXYXXYXXYXYXXYXYXXYXXYXYXXYXXY", result); + } + + @Test + public void incorrectTest() { + Parser parser = new Parser("X", Arrays.asList("X=XY", "y=X"), 3); + assertTrue(parser.isCorrect()); + parser = new Parser("X", Arrays.asList("X+XY" , "Y=X"), 3); + assertFalse(parser.isCorrect()); + parser = new Parser("X", Arrays.asList("X=X++Y", "Y=X"), 3); + assertTrue(parser.isCorrect()); + try { + assertNotNull(Parser.parse(Rewrite.rewrite("X", new Parser("X", Arrays.asList("X=XY", "Y=X"), 3).parseRules(), 3))); + } catch (NumberFormatException err) { + System.out.println("fail"); + err.printStackTrace(); + fail(); + } + } + +}