tests/libjava-mauve/src/gnu/testlet/java/util/regex/PatternSplit.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) 2004, 2005 Mark Wielaard
       
     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;
       
    23 
       
    24 import gnu.testlet.*;
       
    25 import java.util.Arrays;
       
    26 import java.util.regex.*;
       
    27 
       
    28 public class PatternSplit implements Testlet
       
    29 {
       
    30   private TestHarness harness;
       
    31 
       
    32   public void test (TestHarness harness)
       
    33   {
       
    34     this.harness = harness;
       
    35     test("@", "test@example.com", new String[] { "test", "example.com" });
       
    36     test("\\.", "192.168.0.1", new String[] { "192", "168", "0", "1" });
       
    37 
       
    38     test(",", "a,b,c,d,e", new String[] { "a", "b", "c", "d", "e" });
       
    39 
       
    40     test("-", "a-", new String[] { "a", "" });
       
    41     test(";", ";b", new String[] { "", "b" });
       
    42 
       
    43     test(":", ":b:", new String[] { "", "b", "" });
       
    44 
       
    45     test(" ", " ", new String[] { "", "" });
       
    46     test("0", "00", new String[] { "", "", "" });
       
    47 
       
    48     test(",", "a,b,c,d,e", new String[] { "a", "b", "c", "d", "e" });
       
    49 
       
    50     test("\\w", "a,b,c,d,e", new String[] { "", ",", ",", ",", ",", "" });
       
    51     test("\\d+", "123,456,789", new String[] { "", ",", ",", "" });
       
    52 
       
    53     test("[^a-z]", "abc1defZghi", new String[] { "abc", "def", "ghi" });
       
    54 
       
    55     test("^[a-c]", "abc", new String[] { "", "bc" });
       
    56     test("[a-c]$", "abc", new String[] { "ab", "" });
       
    57 
       
    58     test("(?=[a-z])", "123abc", new String[] { "123", "a", "b", "c" });
       
    59 
       
    60     test(",", "a,,,b", new String[] { "a", "", "", "b" });
       
    61 
       
    62     // No match
       
    63     test("waku", "", new String[] { "" });
       
    64     test("waku", "wapu", new String[] { "wapu" });
       
    65     test("\\d+", "abc,def", new String[] { "abc,def" });
       
    66 
       
    67   }
       
    68 
       
    69   // Tests a pattern on a string with the given result
       
    70   // (result should include all trailing empty strings)
       
    71   void test(String pat, String str, String[] expected)
       
    72   {
       
    73     harness.checkPoint("test: " + pat);
       
    74     try
       
    75       {
       
    76 	Pattern pattern = Pattern.compile(pat);
       
    77 	String[] result = pattern.split(str, -1);
       
    78 	harness.check(Arrays.equals(expected, result));
       
    79 
       
    80 	result = pattern.split(str, Integer.MIN_VALUE);
       
    81 	harness.check(Arrays.equals(expected, result));
       
    82 
       
    83 	result = pattern.split(str);
       
    84 	String[] result0 = pattern.split(str, 0);
       
    85 	harness.check(Arrays.equals(result, result0));
       
    86 
       
    87 	// Strip trailing space or just use str as result when we don't match.
       
    88 	int total_len = expected.length;
       
    89 	String[] expected0;
       
    90 	if (pattern.matcher(str).find())
       
    91 	  {
       
    92 	    int trailing_empties = 0;
       
    93 	    for (int i = 0; i < total_len; i++)
       
    94 	      {
       
    95 		if ("".equals(expected[i]))
       
    96 		  trailing_empties++;
       
    97 		else
       
    98 		  trailing_empties = 0;
       
    99 	      }
       
   100 	    expected0 = new String[total_len - trailing_empties];
       
   101 	    for (int i = 0; i < expected0.length; i++)
       
   102 	      expected0[i] = expected[i];
       
   103 	  }
       
   104 	else
       
   105 	  expected0 = new String[] { str };
       
   106 	
       
   107 	harness.check(Arrays.equals(expected0, result0));
       
   108 
       
   109 	// A limit of one is lame. Either it doesn't match and the
       
   110 	// result is the given string, or it matches zero (1 - 1) times
       
   111 	// and the result is the whole given string (trailing part).
       
   112 	String[] result1 = pattern.split(str, 1);
       
   113 	harness.check(result1.length == 1 && str.equals(result1[0]));
       
   114 	
       
   115 	for (int i = 2; i <= total_len; i++)
       
   116 	  {
       
   117 	    result = pattern.split(str, i);
       
   118 	    boolean equal = (result.length == i);
       
   119 	    for (int j = 0; equal && j < i - 1; j++)
       
   120 	      equal = (expected[j].equals(result[j]));
       
   121 	    harness.check(equal);
       
   122 
       
   123 	    // The tail should start with the first remaining element
       
   124 	    harness.check(result.length > i - 1 &&
       
   125 			  result[i - 1].startsWith(expected[i - 1]));
       
   126 	    harness.check(result.length > i -1 &&
       
   127 			  result[i - 1].endsWith(expected[total_len - 1]));
       
   128 	  }
       
   129 
       
   130 	result = pattern.split(str, total_len + 1);
       
   131 	harness.check(Arrays.equals(expected, result));
       
   132 
       
   133 	result = pattern.split(str, Integer.MAX_VALUE);
       
   134 	harness.check(Arrays.equals(expected, result));
       
   135       }
       
   136     catch(PatternSyntaxException pse)
       
   137       {
       
   138 	harness.debug(pse);
       
   139 	harness.check(false);
       
   140       }
       
   141   }
       
   142 }