From 291f469dee3f412dc48c9783bb4c82c4801a41e0 Mon Sep 17 00:00:00 2001 From: andrea Date: Fri, 29 Apr 2016 17:02:00 +0200 Subject: added checkFLOAT() and checkString() Signed-off-by: andrea --- src/regex/RegexTest.java | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/regex/RegexTest.java b/src/regex/RegexTest.java index f149d62..7dd7e70 100644 --- a/src/regex/RegexTest.java +++ b/src/regex/RegexTest.java @@ -8,33 +8,33 @@ import dk.brics.automaton.RegExp; import dk.brics.automaton.RunAutomaton; public class RegexTest { - + private final RunAutomaton r; public RegexTest(String regex) { - System.out.println("regular expression = " + regex); - r = new RunAutomaton(new RegExp(regex).toAutomaton()); + System.out.println("regular expression = " + regex); + r = new RunAutomaton(new RegExp(regex).toAutomaton()); } - + public long dfaMatch(String input, int index) { long start = System.nanoTime(); int length = r.run(input, index); long end = System.nanoTime(); - + if (length == -1) { System.out.println("No match found!"); } else { String s = input.substring(index, index + length); System.out.println("Found: " + s); } - + return end - start; } - + void runTest(String input, int index) { - System.out.println("input string = " + input); - System.out.println("index = " + index); - + System.out.println("input string = " + input); + System.out.println("index = " + index); + long dfaMatchTime = dfaMatch(input, index); System.out.println("dfaMatchTime " + dfaMatchTime); } @@ -65,7 +65,7 @@ public class RegexTest { reId.assertFail("aD"); System.out.println(); } - + static void checkNAT() { RegexTest reNAT = new RegexTest("[0]|[1-9][0-9]*"); reNAT.assertOk("2"); @@ -76,6 +76,30 @@ public class RegexTest { System.out.println(); } + static void checkFLOAT() { + String UnsignedInt = "([0]|([1-9][0-9]*))"; + String SignedInt = "[\\+\\-]?" + UnsignedInt; + String UnsignedReal = "(" + UnsignedInt + "\\." + "[0-9]+" + "([eE]" + SignedInt + ")?" + ")"; + + UnsignedReal += "|(" + UnsignedInt + "[eE]" + SignedInt + ")"; + + String Number = UnsignedInt + "|" + UnsignedReal; + + RegexTest reFLOAT = new RegexTest(Number); + reFLOAT.assertOk("3.14e-7"); + reFLOAT.assertFail("3.14e-07"); + reFLOAT.assertOk("1"); + reFLOAT.assertOk("0.1"); + reFLOAT.assertOk("3e4"); + reFLOAT.assertFail("00"); + reFLOAT.assertFail("01s"); + reFLOAT.assertFail("Id"); + reFLOAT.assertFail("-145711"); + reFLOAT.assertFail("04.1"); + System.out.println(); + + } + static void checkString() { // String ::= (UnescapedChar | "\" EscapedChar)* // UnescapedChar ::= Char - ["] = "\" @@ -97,6 +121,7 @@ public class RegexTest { public static void main(String[] args) { checkId(); checkNAT(); + checkFLOAT(); checkString(); System.out.println("Passed."); } -- cgit v1.2.1