001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: package de.uka.ilkd.key.collection;
012:
013: /** tests non-destructive Set implementation with String */
014:
015: public class TestSetAsListOfString extends junit.framework.TestCase {
016:
017: String str[] = new String[] { "Dies", "ist", "ein", "Test" };
018:
019: public TestSetAsListOfString(String name) {
020: super (name);
021: }
022:
023: // test if String is SAME as one in the array arr
024: private boolean isInArray(String str, String[] arr) {
025: for (int i = 0; i < arr.length; i++) {
026: if (arr[i] == str) {
027: return true;
028: }
029: }
030: return false;
031: }
032:
033: // tests add and implicitly iterator, size
034: public void testAdd() {
035: SetOfString[] newSet = new SetOfString[str.length + 1];
036: newSet[0] = SetAsListOfString.EMPTY_SET;
037:
038: for (int i = 1; i < str.length + 1; i++) {
039: newSet[i] = newSet[i - 1].add(str[i - 1]);
040: }
041: // Test elements in set
042: for (int i = 0; i < str.length + 1; i++) {
043: IteratorOfString it = newSet[i].iterator();
044: int size = newSet[i].size();
045: if (i > 0) { // set should have elements
046: assertTrue(
047: "Set has no elements, but should have some.",
048: it.hasNext());
049: assertTrue("Wrong cardinality", size == i);
050: } else { // set is empty
051: assertTrue("Elements but set should be empty.", !it
052: .hasNext());
053: assertTrue("Wrong cardinality.", size == 0);
054: }
055: int nr = 0;
056: while (it.hasNext()) {
057: assertTrue("Set has wrong elements", isInArray(it
058: .next(), str));
059: nr++;
060: }
061: // has right number of elements
062: assertTrue("Set has iterated to less/often", nr == size);
063: }
064:
065: // add existing element, has to be SAME set
066: assertSame(
067: "Element found 2 times in set or set is not the same.",
068: newSet[str.length], newSet[str.length].add(str[0]));
069: }
070:
071: // tests unify
072: public void testUnify() {
073: SetOfString[] newSet = new SetOfString[str.length + 1];
074: newSet[0] = (SetAsListOfString.EMPTY_SET.add(str[0]))
075: .add(str[1]);
076: newSet[1] = SetAsListOfString.EMPTY_SET.add(str[1]).add(str[2]);
077: // make the union of two sets and check if in the unions
078: // appearance of str[1] == 1
079: SetOfString union = newSet[1].union(newSet[0]);
080: assertTrue(union.size() == 3);
081: //test if set has all elements
082: for (int i = 0; i < 3; i++) {
083: assertTrue(union.contains(str[0]));
084: }
085: // just to check that contains can say no too
086: assertTrue(!union.contains(str[3]));
087: }
088:
089: public void testSubset() {
090: SetOfString subSet = SetAsListOfString.EMPTY_SET;
091: SetOfString super Set = SetAsListOfString.EMPTY_SET;
092: // subSet={Dies,ist}
093: // superSet={Dies,ist,ein}
094: subSet = subSet.add(str[0]).add(str[1]);
095: super Set = subSet.add(str[2]);
096: assertTrue("Failure: in subset relation (!sub<super)", subSet
097: .subset(super Set));
098: assertTrue("Failure: in subset relation (super<sub)", !super Set
099: .subset(subSet));
100: assertTrue("EmptySet is not part of another Set",
101: (SetAsListOfString.EMPTY_SET).subset(super Set));
102: assertTrue("A non empty set is subset of the empty set",
103: !subSet.subset(SetAsListOfString.EMPTY_SET));
104: }
105:
106: public void testRemove() {
107: SetOfString set = SetAsListOfString.EMPTY_SET;
108: // set={Dies,ist}
109: set = set.add(str[0]).add(str[1]);
110: assertTrue("Did not remove " + str[0] + " from list", !(set
111: .remove(str[0]).contains(str[0])));
112: }
113:
114: public void testToString() {
115: SetOfString newSet = SetAsListOfString.EMPTY_SET;
116: for (int i = 0; i < str.length; i++) {
117: newSet = newSet.add(str[str.length - 1 - i]);
118: }
119: assertEquals("{Dies,ist,ein,Test}", newSet.toString());
120: }
121:
122: }
|