SymbolTable.java

1
// Copyright © 2004-2006 University of Helsinki, Department of Computer Science
2
// Copyright © 2012 various contributors
3
// This software is released under GNU Lesser General Public License 2.1.
4
// The license text is at http://www.gnu.org/licenses/lgpl-2.1.html
5
6
package fi.helsinki.cs.titokone;
7
8
import java.util.*;
9
10
11
/**
12
 * This class represents the symbol table of a TTK-91 program. It contains
13
 * symbols with defined values. It also contains DEF-definitions, which are
14
 * like symbols except the value is String. Symbols and DEFs are in separated
15
 * maps.
16
 */
17
public class SymbolTable {
18
    /**
19
     * This field contains the SymbolTable's symbols.
20
     */
21 2
    private HashMap<String, Integer> symbols = new HashMap<String, Integer>();
22
    /**
23
     * This field contains the SymbolTable's definitions.
24
     */
25 2
    private HashMap<String, String> definitions = new HashMap<String, String>();
26
27
    /**
28
     * This function returns the integer value that corresponds to a
29
     * given symbol.
30
     *
31
     * @param symbolName Name of the symbol.
32
     * @return Integer value that corresponds to the symbol.
33
     * @throws InvalidSymbolException If there is no such
34
     *                                symbol in the SymbolTable.
35
     */
36
    public int getSymbol(String symbolName)
37
            throws InvalidSymbolException {
38 2
        if (!symbols.containsKey(symbolName)) {
39 6
            throw new InvalidSymbolException("Symbol " + symbolName + " not found.");
40
        }
41 3
        return ((Integer) symbols.get(symbolName)).intValue();
42
    }
43
44
    /**
45
     * This method returns a string array that contains all the
46
     * currently defined symbols. (Not pre-defined value symbols
47
     * which are not used in the application this symbol table
48
     * is related to.)
49
     *
50
     * @return An Array that contains all the defined symbols.
51
     */
52
    public String[] getAllSymbols() {
53 1
        Set keySet = symbols.keySet();
54 1
        String[] keys = new String[keySet.size()];
55 1
        Iterator keyIt = keySet.iterator();
56 2
        int i = 0;
57 2
        while (keyIt.hasNext()) {
58 2
            keys[i++] = (String) keyIt.next();
59
        }
60 1
        return keys;
61
    }
62
63
    /**
64
     * This method adds a new symbol to the symbol table. If the
65
     * symbol was already defined this changes its value.
66
     *
67
     * @param symbolName  Name of the symbol.
68
     * @param symbolValue Integer value of the symbol.
69
     */
70
    public void addSymbol(String symbolName, int symbolValue) {
71 1
        if (symbolName == null) {
72 3
            throw new IllegalArgumentException(new Message("SymbolName was " +
73
                    "null.").toString());
74
        }
75 2
        symbols.put(symbolName, new Integer(symbolValue));
76
    }
77
78
    /**
79
     * This method adds a new definition. A definition is much like a
80
     * symbol, only the value is a string instead of an integer, and
81
     * the usage differs.
82
     *
83
     * @param key   The name of the definition.
84
     * @param value The value to be attached to the name.
85
     */
86
    public void addDefinition(String key, String value) {
87 1
        if (key == null) {
88 3
            throw new IllegalArgumentException(new Message("Definition key " +
89
                    "was null.").toString());
90
        }
91 1
        definitions.put(key, value);
92
    }
93
94
    /**
95
     * This function returns the string value that corresponds to a
96
     * given definition.
97
     *
98
     * @param key Name of the definition.
99
     * @return String that corresponds to the definition.
100
     * @throws InvalidDefinitionException If there is no such
101
     *                                    definition in the SymbolTable.
102
     */
103
    public String getDefinition(String key)
104
            throws InvalidDefinitionException {
105 2
        if (!definitions.containsKey(key)) {
106 3
            throw new InvalidDefinitionException(new Message("Definition " +
107
                    "{0} not found.", key).toString());
108
        }
109 2
        return (String) definitions.get(key);
110
    }
111
112
    /**
113
     * This method returns a string array that contains all the
114
     * currently made definitions.
115
     *
116
     * @return An Array that contains all the definition keys with values
117
     *         to them.
118
     */
119
    public String[] getAllDefinitions() {
120 1
        Set definitionSet = definitions.keySet();
121 1
        String[] defs = new String[definitionSet.size()];
122 1
        Iterator defIt = definitionSet.iterator();
123 2
        int i = 0;
124 2
        while (defIt.hasNext()) {
125 2
            defs[i++] = (String) defIt.next();
126
        }
127 1
        return defs;
128
    }
129
130
    /**
131
     * This method returns the symbol table as a hashmap. The
132
     * hashmap returned is a copy, not a reference.
133
     *
134
     * @return A HashMap containing the symbol table, with the symbol
135
     *         names as key Strings and the integer values as Integer
136
     *         objects.
137
     */
138
    @SuppressWarnings("unchecked")
139
    public HashMap<String, Integer> toHashMap() {
140 2
        return (HashMap<String, Integer>) symbols.clone();
141
    }
142
}

Mutations

21

Removed assignment to member variable symbols : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

removed call to java/util/HashMap::<init> : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

25

removed call to java/util/HashMap::<init> : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

Removed assignment to member variable definitions : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

38

negated conditional : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

removed call to java/util/HashMap::containsKey : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

39

removed call to java/lang/StringBuilder::append : NO_COVERAGE

removed call to java/lang/StringBuilder::<init> : NO_COVERAGE

removed call to java/lang/StringBuilder::append : NO_COVERAGE

removed call to java/lang/StringBuilder::append : NO_COVERAGE

removed call to fi/helsinki/cs/titokone/InvalidSymbolException::<init> : NO_COVERAGE

removed call to java/lang/StringBuilder::toString : NO_COVERAGE

41

removed call to java/util/HashMap::get : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

removed call to java/lang/Integer::intValue : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

replaced return of integer sized value with (x == 0 ? 1 : 0) : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

53

removed call to java/util/HashMap::keySet : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

54

removed call to java/util/Set::size : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

55

removed call to java/util/Set::iterator : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

56

Substituted 0 with 1 : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

Substituted 0 with 1 : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

57

removed call to java/util/Iterator::hasNext : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

negated conditional : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

58

removed call to java/util/Iterator::next : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

Changed increment from 1 to -1 : SURVIVED

60

mutated return of Object value for fi/helsinki/cs/titokone/SymbolTable::getAllSymbols to ( if (x != null) null else throw new RuntimeException ) : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

71

negated conditional : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

72

removed call to fi/helsinki/cs/titokone/Message::<init> : NO_COVERAGE

removed call to fi/helsinki/cs/titokone/Message::toString : NO_COVERAGE

removed call to java/lang/IllegalArgumentException::<init> : NO_COVERAGE

75

removed call to java/util/HashMap::put : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

removed call to java/lang/Integer::<init> : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

87

negated conditional : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testBinaryToApplication(fi.helsinki.cs.titokone.BinaryTest)

88

removed call to fi/helsinki/cs/titokone/Message::<init> : NO_COVERAGE

removed call to fi/helsinki/cs/titokone/Message::toString : NO_COVERAGE

removed call to java/lang/IllegalArgumentException::<init> : NO_COVERAGE

91

removed call to java/util/HashMap::put : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

105

removed call to java/util/HashMap::containsKey : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

negated conditional : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

106

removed call to fi/helsinki/cs/titokone/InvalidDefinitionException::<init> : NO_COVERAGE

removed call to fi/helsinki/cs/titokone/Message::<init> : NO_COVERAGE

removed call to fi/helsinki/cs/titokone/Message::toString : NO_COVERAGE

109

removed call to java/util/HashMap::get : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

mutated return of Object value for fi/helsinki/cs/titokone/SymbolTable::getDefinition to ( if (x != null) null else throw new RuntimeException ) : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

120

removed call to java/util/HashMap::keySet : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

121

removed call to java/util/Set::size : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

122

removed call to java/util/Set::iterator : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

123

Substituted 0 with 1 : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

Substituted 0 with 1 : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

124

removed call to java/util/Iterator::hasNext : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

negated conditional : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

125

removed call to java/util/Iterator::next : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

Changed increment from 1 to -1 : KILLED -> fi.helsinki.cs.titokone.ControlTest.testLoad(fi.helsinki.cs.titokone.ControlTest)

127

mutated return of Object value for fi/helsinki/cs/titokone/SymbolTable::getAllDefinitions to ( if (x != null) null else throw new RuntimeException ) : KILLED -> fi.helsinki.cs.titokone.BinaryTest.testToString(fi.helsinki.cs.titokone.BinaryTest)

140

mutated return of Object value for fi/helsinki/cs/titokone/SymbolTable::toHashMap to ( if (x != null) null else throw new RuntimeException ) : NO_COVERAGE

removed call to java/util/HashMap::clone : NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 0.27