tests/libjava-mauve/src/gnu/testlet/java/util/regex/Pattern/pcrematches.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Tags: JDK1.4
       
     2 
       
     3 // Copyright (C) 2005 Ziga Mahkovec (ziga.mahkovec@klika.si)
       
     4 
       
     5 // This file is part of Mauve.
       
     6 
       
     7 // Mauve is free software; you can redistribute it and/or modify
       
     8 // it under the terms of the GNU General Public License as published by
       
     9 // the Free Software Foundation; either version 2, or (at your option)
       
    10 // any later version.
       
    11 
       
    12 // Mauve is distributed in the hope that it will be useful,
       
    13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 // GNU General Public License for more details.
       
    16 
       
    17 // You should have received a copy of the GNU General Public License
       
    18 // along with Mauve; see the file COPYING.  If not, write to
       
    19 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    20 // Boston, MA 02111-1307, USA.
       
    21 
       
    22 package gnu.testlet.java.util.regex.Pattern;
       
    23 
       
    24 import gnu.testlet.*;
       
    25 import java.io.*;
       
    26 import java.util.*;
       
    27 import java.util.regex.*;
       
    28 
       
    29 /**
       
    30  * Tests the java.util.regex regular expression engine.  The test cases are
       
    31  * adapted from PCRE (www.pcre.org).  Each test cases if formatted as:
       
    32  * <pre>
       
    33  * /regular expression 1/
       
    34  *     test string 1
       
    35  *     0: matching group 0
       
    36  *     1: matching group 1
       
    37  *     test string 2
       
    38  * No match
       
    39        test string 3
       
    40  *     ...
       
    41  *
       
    42  * /regular expression 2/
       
    43  * ...
       
    44  * </pre>
       
    45  */
       
    46 public class pcrematches implements Testlet
       
    47 {
       
    48   private TestHarness harness;
       
    49 
       
    50   /** Regex test suites from adapted from PCRE (http://www.pcre.org/license.txt). */
       
    51   private static final String[] TEST_SUITES = {"testdata1", /*"testdata2",*/ "testdata3"};
       
    52 
       
    53   /**
       
    54    * Regex test case (containing a single regular expression and a list of tests).
       
    55    */
       
    56   private static class RETestcase {
       
    57     String regex;
       
    58     List tests; // list of RETestcaseTest instances
       
    59   }
       
    60 
       
    61   /**
       
    62    * Regex test (containing a single text string and a list of resulting groups).
       
    63    */
       
    64   private static class RETestcaseTest {
       
    65     String text;
       
    66     List groups;
       
    67   }
       
    68 
       
    69   public void test (TestHarness harness)
       
    70   {
       
    71     this.harness = harness;
       
    72     try { 
       
    73       for (int i=0; i<TEST_SUITES.length; i++) {
       
    74         String suite = "gnu#testlet#java#util#regex#Pattern#" + TEST_SUITES[i];
       
    75         BufferedReader reader = new BufferedReader(harness.getResourceReader(suite));
       
    76         RETestcase tc = null;
       
    77         while ((tc = readTestcase(reader)) != null) {
       
    78           try
       
    79           {
       
    80             test(tc);
       
    81           }
       
    82           catch (PatternSyntaxException e)
       
    83           {
       
    84             harness.check(false, tc.regex);
       
    85           }
       
    86         }
       
    87       }
       
    88     }
       
    89     catch (gnu.testlet.ResourceNotFoundException _)
       
    90     {
       
    91         harness.check(false, "All tests failed (ResourceNotFoundException)");
       
    92     }
       
    93     catch (IOException _)
       
    94     {
       
    95         harness.check(false, "All tests failed (IOException)");
       
    96     }
       
    97   }
       
    98   
       
    99   private void test(RETestcase tc) throws PatternSyntaxException {
       
   100     String regex = tc.regex.substring(tc.regex.indexOf('/') + 1, 
       
   101                                       tc.regex.lastIndexOf('/'));
       
   102     String qual = tc.regex.substring(tc.regex.lastIndexOf('/') + 1);
       
   103     int flags = 0;
       
   104     if (qual.indexOf("i") != -1) {
       
   105       flags |= Pattern.CASE_INSENSITIVE;
       
   106     }
       
   107     if (qual.indexOf("m") != -1) {
       
   108       flags |= Pattern.MULTILINE;
       
   109     }
       
   110     Pattern pat = Pattern.compile(regex, flags);
       
   111     
       
   112     for (Iterator i=tc.tests.iterator(); i.hasNext(); ) {
       
   113       RETestcaseTest t = (RETestcaseTest)i.next();
       
   114       Matcher mat = pat.matcher(decode(t.text));
       
   115       if (mat.find()) {
       
   116         int groups = mat.groupCount();
       
   117         if (groups != t.groups.size() - 1) {
       
   118 	  harness.debug("groups=" + groups + " expected=" + (t.groups.size() - 1));
       
   119           harness.check(false, regex);
       
   120           return;
       
   121         }
       
   122         boolean failed = false;
       
   123         for (int j=0; j<=groups; j++) {
       
   124           String g = decode((String)t.groups.get(j));
       
   125           String g2 = mat.group(j);
       
   126           if (!g.trim().equals(g2.trim())) {
       
   127 	    harness.debug("j=" + j + " expected=" + g + " found=" + g2);
       
   128             harness.check(false, regex);
       
   129             return;
       
   130           }
       
   131         }
       
   132       } else if (!t.groups.isEmpty()) {
       
   133 	harness.debug("match not found: regex=/" + regex + "/ text=\"" + t.text +"\"");
       
   134         harness.check(false, regex);
       
   135         return;
       
   136       }
       
   137     }
       
   138     harness.check(true, regex);
       
   139   }
       
   140   
       
   141   private static RETestcase readTestcase(BufferedReader reader) throws IOException {
       
   142     String line = reader.readLine();
       
   143     if (line == null)
       
   144       return null;
       
   145     line = line.trim();
       
   146     while (!line.startsWith("/") || line.lastIndexOf("/") == 0) {
       
   147       line = reader.readLine();
       
   148       if (line == null)
       
   149         return null;
       
   150     }
       
   151     RETestcase tc = new RETestcase();
       
   152     tc.regex = line;
       
   153     tc.tests = new ArrayList();
       
   154     while ((line = reader.readLine()) != null) {
       
   155       line = line.trim();
       
   156       if (line.length() == 0)
       
   157         break;
       
   158       RETestcaseTest test = new RETestcaseTest();
       
   159       test.text = line;
       
   160       test.groups = new ArrayList();
       
   161       reader.mark(8096);
       
   162       while ((line = reader.readLine()) != null) {
       
   163         if (line.length() == 0)
       
   164           break;
       
   165         else if (line.startsWith("    ")) {
       
   166           reader.reset();
       
   167           break;
       
   168         }
       
   169         if (line.equals("No match"))
       
   170           break;
       
   171         line = line.substring(line.indexOf(':') + 1);
       
   172         if (line.length() > 0)
       
   173           line = line.substring(1);
       
   174         test.groups.add(line);
       
   175         reader.mark(8096);
       
   176       }
       
   177       tc.tests.add(test);
       
   178       if (line == null || line.length() == 0)
       
   179         break;
       
   180     }
       
   181     return tc;
       
   182   }
       
   183 
       
   184   private static String decode(String s) {
       
   185     StringBuffer sb = new StringBuffer();
       
   186     int p = 0;
       
   187     int q = 0;
       
   188     while (true) {
       
   189       p = s.indexOf("\\u", q);
       
   190       if (p == -1) {
       
   191 	sb.append(s.substring(q));
       
   192 	break;
       
   193       }
       
   194       sb.append(s.substring(q, p));
       
   195       if (p + 6 <= s.length()) {
       
   196 	String hex = s.substring(p+2, p+6);
       
   197 	try {
       
   198 	  int c = Integer.parseInt(hex, 16);
       
   199 	  sb.append((char)c);
       
   200 	}
       
   201 	catch (NumberFormatException _) {
       
   202 	  sb.append(s.substring(p, p+6));
       
   203 	}
       
   204 	q = p + 6;
       
   205       }
       
   206       else {
       
   207 	sb.append(s.substring(p, p+2));
       
   208 	q = p + 2;
       
   209       }
       
   210     }
       
   211     return sb.toString();
       
   212   }
       
   213 }