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: /** JUnit test for MapAsListFromIntegerToString implementation */
014:
015: public class TestMapAsListFromIntegerToString extends
016: junit.framework.TestCase {
017:
018: private String[] entryStr;
019: private Integer[] entryInt;
020:
021: /** puts i and str in the corresponding arrays at place nr */
022: private void put(int nr, int i, String str) {
023: entryInt[nr] = new Integer(i);
024: entryStr[nr] = str;
025: }
026:
027: public TestMapAsListFromIntegerToString(String name) {
028: super (name);
029: }
030:
031: public void setUp() {
032: entryStr = new String[4];
033: entryInt = new Integer[4];
034: put(0, 0, "Null");
035: put(1, 1, "Eins");
036: put(2, 2, "Zwei");
037: put(3, 3, "Drei");
038: }
039:
040: private MapFromIntegerToString createMap() {
041: MapFromIntegerToString map = MapAsListFromIntegerToString.EMPTY_MAP;
042: // create map with entrys like (1,"Eins")
043: for (int i = 0; i < entryStr.length; i++) {
044: map = map.put(entryInt[i], entryStr[i]);
045: }
046: return map;
047: }
048:
049: public void testMapEntriesAreTheSameThatHaveBeenPutInside() {
050: MapFromIntegerToString map = createMap();
051: // assert that all entries are in list
052: for (int i = 0; i < entryStr.length; i++) {
053: assertEquals("Map does not contain entry(" + entryInt[i]
054: + ", " + entryStr[i] + ")", map.get(entryInt[i]),
055: entryStr[i]);
056: }
057: }
058:
059: public void testReplaceIfSameKeyWithNewValueIsPutInMap() {
060: MapFromIntegerToString map = createMap();
061: map = map.put(new Integer(0), "Zero");
062: // zero is in list
063: assertTrue("Zero is not in list.", map.containsValue("Zero"));
064: // but not so old element Null with same key (0)
065: assertTrue(
066: "Null is in list but should have been replaced by Zero",
067: !map.containsValue("Null"));
068: }
069:
070: public void testImmutability() {
071: MapFromIntegerToString map = createMap();
072: MapFromIntegerToString old = map;
073: map = map.put(new Integer(5), "Fuenf");
074: // 5 is in map but not in old
075: assertTrue("Fuenf is not in map", map.containsValue("Fuenf"));
076: assertTrue(
077: "Fuenf is in old map, but it should not be there. Map is not immutable.",
078: !old.containsValue("Fuenf"));
079: }
080:
081: public void testMapCanContainSameValueWithDifferentKeys() {
082: MapFromIntegerToString map = createMap();
083: // add a mapping with a value that has been mapped to
084: // another key before
085: Integer hundred = new Integer(100);
086: map = map.put(hundred, entryStr[1]);
087: assertSame(entryStr[1] + " is not mapped to the newer key 100",
088: map.get(hundred), entryStr[1]);
089: assertSame(entryStr[1] + " is not mapped to the older key "
090: + entryInt[1], map.get(entryInt[1]), entryStr[1]);
091: }
092:
093: public void testRemoveOneMappingWithSpecifiedKey() {
094: MapFromIntegerToString map = createMap();
095: // delete map (1,"Eins")
096: map = map.remove(entryInt[1]);
097: assertTrue("Deleted Mapping found in map", !map
098: .containsKey(entryInt[1]));
099: }
100:
101: public void testRemoveAllMappingToSpecifiedValue() {
102: MapFromIntegerToString map = createMap();
103: // add a mapping with a value that has been mapped to
104: // another key before
105: Integer hundred = new Integer(100);
106: map = map.put(hundred, entryStr[1]);
107: // delete map (*,"Eins")
108: map = map.removeAll(entryStr[1]);
109: assertTrue("Value :" + entryStr[1]
110: + " found in map. But I deleted all"
111: + " of these values :-(", !map
112: .containsValue(entryStr[1]));
113: }
114:
115: public void testSpecialCases() {
116: MapFromIntegerToString map = MapAsListFromIntegerToString.EMPTY_MAP;
117: map = map.put(new Integer(0), "A");
118: assertTrue(
119: "Map should be empty and therefore equal to the EMPTY_MAP",
120: map.remove(new Integer(0)) == MapAsListFromIntegerToString.EMPTY_MAP);
121:
122: assertTrue(
123: "Repeated key removal should not change anything",
124: map.remove(new Integer(0)).remove(new Integer(0)) == MapAsListFromIntegerToString.EMPTY_MAP);
125:
126: map = map.put(new Integer(0), "B");
127: assertTrue(
128: "Map should have only one element with key 0 and value \"B\" ",
129: map.size() == 1 && "B".equals(map.get(new Integer(0))));
130:
131: map = map.removeAll("B");
132: assertTrue(
133: "Map should be empty and therefore equal to the EMPTY_MAP",
134: map == MapAsListFromIntegerToString.EMPTY_MAP);
135:
136: map = map.put(new Integer(0), "B");
137: map = map.put(new Integer(1), "C");
138: map = map.put(new Integer(2), "B");
139:
140: map = map.removeAll("B");
141: assertTrue("Map should not contain value \"B\" any longer ",
142: map.size() == 1 && !map.containsValue("B"));
143:
144: map = map.removeAll("B");
145: assertTrue(
146: "Removing non-existant values should not change anything",
147: map.size() == 1 && !map.containsValue("B"));
148:
149: }
150:
151: }
|