tests/libjava-mauve/src/gnu/testlet/java/io/LineNumberReader/Test2.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // ------------------------------------------------------------------------
       
     2 // Test2.java -- Tests LineNumberReader
       
     3 //
       
     4 // Copyright (c) 2003 Free Software Foundation, Inc.
       
     5 // Written by Guilhem Lavaux <guilhem@kaffe.org>, Based on a test by
       
     6 // Dalibor Topic <robilad@kaffe.org>
       
     7 //
       
     8 // This program is free software; you can redistribute it and/or modify
       
     9 // it under the terms of the GNU General Public License as published 
       
    10 // by the Free Software Foundation, either version 2 of the License, or
       
    11 // (at your option) any later version.
       
    12 //
       
    13 // This program is distributed in the hope that it will be useful, but
       
    14 // WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16 // GNU General Public License for more details.
       
    17 //
       
    18 // You should have received a copy of the GNU General Public License
       
    19 // along with this program; if not, write to the Free Software Foundation
       
    20 // Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
       
    21 // ------------------------------------------------------------------------
       
    22 // Tags: JDK1.1
       
    23 
       
    24 package gnu.testlet.java.io.LineNumberReader;
       
    25 
       
    26 import gnu.testlet.Testlet;
       
    27 import gnu.testlet.TestHarness;
       
    28 import java.io.*;
       
    29 
       
    30 public class Test2 implements Testlet
       
    31 {
       
    32   static abstract class LineReaderTest
       
    33   {
       
    34     abstract void test(TestHarness harness) throws Exception;
       
    35   }
       
    36 
       
    37   static class LineTest1 extends LineReaderTest
       
    38   {
       
    39     void test(TestHarness harness) throws Exception
       
    40     {
       
    41       StringReader sr = new StringReader("X");
       
    42       LineNumberReader lnr = new LineNumberReader(sr);
       
    43 
       
    44       try
       
    45 	{
       
    46 	  lnr.mark(-5);
       
    47 	  harness.check(false);
       
    48 	}
       
    49       catch (IllegalArgumentException e)
       
    50 	{
       
    51 	  harness.check(true);
       
    52 	}
       
    53     }
       
    54   }
       
    55 
       
    56   static class LineTest2 extends LineReaderTest
       
    57   {
       
    58     void test(TestHarness harness) throws Exception
       
    59     {
       
    60       StringReader sr = new StringReader("X");
       
    61       LineNumberReader lnr = new LineNumberReader(sr);
       
    62 
       
    63       try
       
    64 	{
       
    65 	  lnr.read(null, 0, 0);
       
    66 	  harness.check(false);
       
    67 	}
       
    68       catch (NullPointerException e)
       
    69 	{
       
    70 	  harness.check(true);
       
    71 	}
       
    72 
       
    73       // Read too many bytes for the buffer.
       
    74       try
       
    75 	{
       
    76 	  lnr.read(new char[1], 0, 2);
       
    77 	  harness.check(false);
       
    78 	}
       
    79       catch (IndexOutOfBoundsException e)
       
    80 	{
       
    81 	  harness.check(true);
       
    82 	}
       
    83 
       
    84       // Read at a negative position.
       
    85       try
       
    86 	{
       
    87 	  lnr.read(new char[1], -5, 0);
       
    88 	  harness.check(false);
       
    89 	}
       
    90       catch (IndexOutOfBoundsException e)
       
    91 	{
       
    92 	  harness.check(true);
       
    93 	}
       
    94  
       
    95       // Read with a negative length.
       
    96       try
       
    97 	{
       
    98 	  lnr.read(new char[1], 0, -5);
       
    99 	  harness.check(false);
       
   100 	}
       
   101       catch (IndexOutOfBoundsException e)
       
   102 	{
       
   103 	  harness.check(true);
       
   104 	}
       
   105     }
       
   106   }
       
   107 
       
   108   static class LineTest3 extends LineReaderTest
       
   109   {
       
   110     void test(TestHarness harness) throws Exception
       
   111     {
       
   112       StringReader sr = new StringReader("X");
       
   113       LineNumberReader lnr = new LineNumberReader(sr);
       
   114 
       
   115       lnr.setLineNumber(-5);
       
   116       harness.check(lnr.getLineNumber(), -5);
       
   117     }
       
   118   }
       
   119   
       
   120   static class LineTest4 extends LineReaderTest
       
   121   {
       
   122     void test(TestHarness harness) throws Exception
       
   123     {
       
   124       StringReader sr = new StringReader("\r\n");
       
   125       LineNumberReader lnr = new LineNumberReader(sr);
       
   126 
       
   127       char[] ch = new char[2];
       
   128       int r = lnr.read(ch, 0, 2);
       
   129       harness.check(ch[0] == '\r' && ch[1] == '\n');
       
   130       harness.check(lnr.getLineNumber(), 1);
       
   131     }
       
   132   }
       
   133 
       
   134   static class LineTest5 extends LineReaderTest
       
   135   {
       
   136     void test(TestHarness harness) throws Exception
       
   137     {
       
   138       StringReader sr = new StringReader("\r\n\r");
       
   139       LineNumberReader lnr = new LineNumberReader(sr);
       
   140 
       
   141       harness.check(lnr.read(), '\n');
       
   142       harness.check(lnr.read(), '\n');
       
   143       harness.check(lnr.getLineNumber(), 2);
       
   144     }
       
   145   }
       
   146 
       
   147   static class LineTest6 extends LineReaderTest
       
   148   {
       
   149     void test(TestHarness harness) throws Exception
       
   150     {
       
   151       StringReader sr = new StringReader("\r\r\n");
       
   152       LineNumberReader lnr = new LineNumberReader(sr);
       
   153 
       
   154       harness.check(lnr.read(), '\n');
       
   155       harness.check(lnr.read(), '\n');
       
   156       harness.check(lnr.getLineNumber(), 2);
       
   157     }
       
   158   }
       
   159 
       
   160   static class LineTest7 extends LineReaderTest
       
   161   {
       
   162     void test(TestHarness harness) throws Exception
       
   163     {
       
   164       StringReader sr = new StringReader("\r\n\r");
       
   165       LineNumberReader lnr = new LineNumberReader(sr);
       
   166       char[] ch = new char[1];
       
   167 
       
   168       harness.check(lnr.read(), '\n');
       
   169       harness.check(lnr.read(ch, 0, 1), 1);
       
   170       harness.check(ch[0], '\n');
       
   171       harness.check(lnr.read(), '\n');
       
   172       harness.check(lnr.getLineNumber(), 2);
       
   173     }
       
   174   }
       
   175 
       
   176   static class LineTest8 extends LineReaderTest
       
   177   {
       
   178     void test(TestHarness harness) throws Exception
       
   179     {
       
   180       StringReader sr = new StringReader("\r\n\r");
       
   181       LineNumberReader lnr = new LineNumberReader(sr);
       
   182       char[] ch = new char[1];
       
   183 
       
   184       harness.check(lnr.read(ch, 0, 1), 1);
       
   185       harness.check(ch[0], '\r');
       
   186       harness.check(lnr.read(), '\n');
       
   187       harness.check(lnr.read(ch, 0, 1), -1);
       
   188       harness.check(ch[0], '\r');
       
   189       harness.check(lnr.getLineNumber(), 2);
       
   190     }
       
   191   }
       
   192 
       
   193 
       
   194   static class LineTest9 extends LineReaderTest
       
   195   {
       
   196     void test(TestHarness harness) throws Exception
       
   197     {
       
   198       StringReader sr = new StringReader("\r\n\r");
       
   199       LineNumberReader lnr = new LineNumberReader(sr);
       
   200       char[] ch = new char[1];
       
   201 
       
   202       lnr.read();
       
   203       lnr.mark(5);
       
   204       harness.check(lnr.read(ch, 0, 1), 1);
       
   205       harness.check(ch[0], '\n');
       
   206       harness.check(lnr.read(), '\n');
       
   207       lnr.reset();
       
   208       harness.check(lnr.read(ch, 0, 1), 1);
       
   209       harness.check(ch[0], '\n');
       
   210       harness.check(lnr.read(), '\n');
       
   211       
       
   212       harness.check(lnr.getLineNumber(), 2);
       
   213     }    
       
   214   }
       
   215 
       
   216   static class LineTest10 extends LineReaderTest
       
   217   {
       
   218     void test(TestHarness harness) throws Exception
       
   219     {
       
   220       StringReader sr = new StringReader("X");
       
   221       LineNumberReader lnr = new LineNumberReader(sr);
       
   222      
       
   223       try
       
   224 	{
       
   225 	  lnr.reset();
       
   226 	  harness.check(false);
       
   227 	}
       
   228       catch (IOException e)
       
   229 	{
       
   230 	  harness.check(true);
       
   231 	}
       
   232     }
       
   233   }
       
   234 
       
   235   static class LineTest11 extends LineReaderTest
       
   236   {
       
   237     void test(TestHarness harness) throws Exception
       
   238     {
       
   239       StringReader sr = new StringReader("X");
       
   240       LineNumberReader lnr = new LineNumberReader(sr);
       
   241       int old_linenumber = lnr.getLineNumber();
       
   242       
       
   243       lnr.mark(5);
       
   244       lnr.setLineNumber(10);
       
   245       lnr.reset();
       
   246       harness.check(lnr.getLineNumber(), old_linenumber);
       
   247     }
       
   248   }
       
   249 
       
   250   static LineReaderTest[] tests = {
       
   251     new LineTest1(),
       
   252     new LineTest2(),
       
   253     new LineTest3(),
       
   254     new LineTest4(),
       
   255     new LineTest5(),
       
   256     new LineTest6(),
       
   257     new LineTest7(),
       
   258     new LineTest8(),
       
   259     new LineTest9(),
       
   260     new LineTest10(),
       
   261     new LineTest11()
       
   262   };
       
   263 
       
   264   public void test(TestHarness harness)
       
   265   {
       
   266     for (int i = 0; i < tests.length; i++)
       
   267       {
       
   268 	String name = tests[i].getClass().getName();
       
   269 
       
   270 	name = name.substring(name.indexOf('$')+1);
       
   271 	harness.checkPoint("LineNumberReader stress test (" + name + ")");
       
   272 	try
       
   273 	  {
       
   274 	    tests[i].test(harness);
       
   275 	  }
       
   276 	catch (Exception e)
       
   277 	  {
       
   278 	    harness.check(false);
       
   279 	    harness.debug(e);
       
   280 	  }
       
   281       }
       
   282   }  
       
   283 }