tests/libjava-mauve/src/gnu/testlet/java/util/TreeSet/basic.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Tags: JDK1.2
       
     2 
       
     3 // Copyright (C) 2004, 2005 Audrius Meskauskas <audriusa@bluewin.ch>
       
     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 
       
    23 package gnu.testlet.java.util.TreeSet;
       
    24 
       
    25 import gnu.testlet.TestHarness;
       
    26 import gnu.testlet.Testlet;
       
    27 
       
    28 import java.util.Iterator;
       
    29 import java.util.Set;
       
    30 import java.util.TreeSet;
       
    31 
       
    32 /**
       
    33  * Basic TreeSet test.
       
    34  * @author Audrius Meskauskas (AudriusA@Bluewin.ch)
       
    35  */
       
    36 public class basic implements Testlet
       
    37 {
       
    38   TreeSet set = new TreeSet();
       
    39 
       
    40   void checkContent(Set forSet, String content, TestHarness h, String note)
       
    41   {
       
    42     StringBuffer b = new StringBuffer();
       
    43     Iterator iter = forSet.iterator();
       
    44     while (iter.hasNext())
       
    45       {
       
    46         b.append(iter.next());
       
    47       }
       
    48 
       
    49     h.check(b.toString(), content, note);
       
    50   }
       
    51 
       
    52   void checkContent(String content, TestHarness h, String note)
       
    53   {
       
    54     checkContent(set, content, h, note);
       
    55   }
       
    56 
       
    57   TreeSet getSet(String content)
       
    58   {
       
    59     TreeSet t = new TreeSet();
       
    60 
       
    61     for (int i = 0; i < content.length(); i++)
       
    62       {
       
    63         t.add("" + content.charAt(i));
       
    64       }
       
    65 
       
    66     return t;
       
    67   }
       
    68 
       
    69   /* Test clone(). */
       
    70   public void test_clone(TestHarness harness)
       
    71   {
       
    72     TreeSet t = getSet("abcdef");
       
    73     set = (TreeSet) t.clone();
       
    74     checkContent("abcdef", harness, "clone");
       
    75   }
       
    76 
       
    77   /* Test add(Object). */
       
    78   public void test_add(TestHarness harness)
       
    79   {
       
    80     set = getSet("bcdabcddabbccaabbccadbcdababbcdabcxabcxccda");
       
    81     checkContent("abcdx", harness, "add");
       
    82     harness.check(set.size(), 5, "size");
       
    83     harness.check(set.first(), "a", "first");
       
    84     harness.check(set.last(), "x", "last");
       
    85     harness.check(set.comparator() == null, "null comparator expected");
       
    86   }
       
    87 
       
    88   /* Test addAll(Collection). */
       
    89   public void test_addAll(TestHarness harness)
       
    90   {
       
    91     set = getSet("dac");
       
    92 
       
    93     TreeSet t = getSet("xay");
       
    94 
       
    95     set.addAll(t);
       
    96 
       
    97     checkContent("acdxy", harness, "addAll");
       
    98   }
       
    99 
       
   100   /* Test contains(Object). */
       
   101   public void test_contains(TestHarness harness)
       
   102   {
       
   103     String t = "abcdefghij";
       
   104     set = getSet(t);
       
   105 
       
   106     for (int i = 0; i < t.length(); i++)
       
   107       {
       
   108         String s = t.substring(i, i + 1);
       
   109         harness.check(set.contains(s), "must contain '" + s + "'");
       
   110       }
       
   111 
       
   112     harness.check(!set.contains("aa"), "must not contain 'aa'");
       
   113   }
       
   114 
       
   115   /* Test remove(Object). */
       
   116   public void test_remove(TestHarness harness)
       
   117   {
       
   118     String t = "abcdefghij";
       
   119     set = getSet(t);
       
   120 
       
   121     for (int i = 0; i < t.length(); i++)
       
   122       {
       
   123         String s = t.substring(i, i + 1);
       
   124         set.remove(s);
       
   125 
       
   126         if (set.contains(s))
       
   127           harness.fail("Contains '" + s + "' after removing. ");
       
   128       }
       
   129 
       
   130     harness.check(set.size(), 0, "non zero size after removing all elements");
       
   131 
       
   132     harness.check(set.isEmpty(), "non empty when it should be");
       
   133   }
       
   134 
       
   135   /* Test clear(). */
       
   136   public void test_clear(TestHarness harness)
       
   137   {
       
   138     set = getSet("a");
       
   139     set.clear();
       
   140     harness.check(set.size(), 0, "clear");
       
   141   }
       
   142 
       
   143   /* Test headSet(Object). */
       
   144   public void test_subsets(TestHarness harness)
       
   145   {
       
   146     String content = "abcdefghijklmn";
       
   147 
       
   148     set = getSet(content);
       
   149 
       
   150     for (int i = 0; i < content.length() - 1; i++)
       
   151       {
       
   152         String s = content.substring(i, i + 1);
       
   153         checkContent(set.headSet(s), content.substring(0, i), harness, "headSet");
       
   154 
       
   155         checkContent(set.tailSet(s), content.substring(i), harness, "tailSet");
       
   156 
       
   157         checkContent(set.subSet(s, "n"),
       
   158                      content.substring(i, content.length() - 1), harness,
       
   159                      "subset"
       
   160                     );
       
   161       }
       
   162   }
       
   163 
       
   164   public void test(TestHarness harness)
       
   165   {
       
   166     test_clone(harness);
       
   167     test_add(harness);
       
   168     test_addAll(harness);
       
   169     test_contains(harness);
       
   170     test_remove(harness);
       
   171     test_clear(harness);
       
   172     test_subsets(harness);
       
   173   }
       
   174 }