RandomAccessMemoryImpl.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 fi.helsinki.cs.ttk91.*;
9
10
import java.util.HashMap;
11
12
/**
13
 * This class represents the memory of a TTK-91 computer.
14
 */
15
public class RandomAccessMemoryImpl
16
        implements TTK91Memory, RandomAccessMemory {
17 2
    private SymbolTable symbols = new SymbolTable();
18
    private int size;
19
    private MemoryLine[] memory;
20 3
    private int codeAreaSize = 0;
21 3
    private int dataAreaSize = 0;
22
23
    //Added by Harri Tuomikoski, 12.10.2004, Koskelo-project
24 3
    private int memory_references = 0;
25
26
    /**
27
     * Creates a memory with a given size and initializes it with
28
     * rows containing 0.
29
     *
30
     * @param size Size of the memory.
31
     */
32
    public RandomAccessMemoryImpl(int size) {
33 2
        if (size < 0) {
34 3
            throw new IllegalArgumentException(new Message("Memory size cannot be negative.").toString());
35
        }
36 1
        this.size = size;
37 1
        memory = new MemoryLine[size];
38 5
        for (int i = 0; i < size; i++) {
39 3
            memory[i] = new MemoryLine(0, "");
40
        }
41
42 3
        this.memory_references = 0; //Added by HT, 12.10.2004, Koskelo-project
43
    }
44
45
    /**
46
     * Returns the size of the memory. Defined in TTK91Memory.
47
     *
48
     * @return Size of the memory.
49
     */
50
    public int getSize() {
51 1
        return size;
52
    }
53
54
    /**
55
     * Returns the value of an indexed memory slot.
56
     *
57
     * @return Value of an indexed memory slot.
58
     */
59
    public int getValue(int memorySlot) {
60 4
        ++this.memory_references; //Added by HT, 12.10.2004, Koskelo-project
61 2
        return memory[memorySlot].getBinary();
62
    }
63
64
    /**
65
     * Returns the symbol table of currently used symbols as
66
     * a hashmap, with String names of the symbols as keys
67
     * referencing the Integer values of the symbols.
68
     * Defined in TTK91Memory.
69
     *
70
     * @return The symboltable as a hashmap.
71
     */
72
    public HashMap<String, Integer> getSymbolTable() {
73 2
        return symbols.toHashMap();
74
    }
75
76
    /**
77
     * Returns a memory dump. Defined in TTK91Memory.
78
     *
79
     * @return Memory dump in integer form.
80
     */
81
    public int[] getMemory() {
82
        int[] mem = new int[size];
83 5
        for (int i = 0; i < size; i++) {
84 1
            mem[i] = memory[i].getBinary();
85
        }
86 1
        return mem;
87
    }
88
89
    /**
90
     * Returns a code area dump. Defined in TTK91Memory.
91
     *
92
     * @return Code area dump in integer form.
93
     */
94
    public int[] getCodeArea() {
95
        int[] codeArea = new int[codeAreaSize];
96 5
        for (int i = 0; i < codeAreaSize; i++) {
97 1
            codeArea[i] = memory[i].getBinary();
98
        }
99 1
        return codeArea;
100
    }
101
102
    /**
103
     * Returns a data area dump.
104
     *
105
     * @return Data area dump in integer form.
106
     */
107
    public int[] getDataArea() {
108
        int[] dataArea = new int[dataAreaSize];
109 5
        for (int i = 0; i < dataAreaSize; i++) {
110 2
            dataArea[i] = memory[i + codeAreaSize].getBinary();
111
        }
112 1
        return dataArea;
113
    }
114
115
    /**
116
     * Returns the size of the code area.
117
     *
118
     * @return Size of the code area.
119
     */
120
    public int getCodeAreaSize() {
121 1
        return codeAreaSize;
122
    }
123
124
    /**
125
     * Returns the size of the data area.
126
     *
127
     * @return Size of the data area.
128
     */
129
    public int getDataAreaSize() {
130 1
        return dataAreaSize;
131
    }
132
133
    /**
134
     * Returns memory line at given slot.
135
     *
136
     * @param index Index to memory.
137
     * @return Memory line at given slot.
138
     */
139
    public MemoryLine getMemoryLine(int index) {
140 1
        return memory[index];
141
    }
142
143
    /**
144
     * This method returns a copy of all the memory lines.
145
     *
146
     * @return An array containing all the memory lines.
147
     */
148
    @SuppressWarnings("unchecked")
149
    public MemoryLine[] getMemoryLines() {
150 2
        return (MemoryLine[]) memory.clone();
151
    }
152
153
    /**
154
     * Changes the symbol table stored in this class. It is not
155
     * used by RandomAccessMemory, but can be returned as a HashMap.
156
     *
157
     * @param symbols The new symboltable to store here.
158
     */
159
    public void setSymbolTable(SymbolTable symbols) {
160 1
        if (symbols == null) {
161 3
            throw new IllegalArgumentException(new Message("Tried to set " +
162
                    "symbol table to " +
163
                    "null.").toString());
164
        }
165 1
        this.symbols = symbols;
166
    }
167
168
    /**
169
     * Sets new memory line to given memory slot.
170
     *
171
     * @param index      Index to memory.
172
     * @param memoryLine New memory line which will replace the old.
173
     */
174
    public void setMemoryLine(int index, MemoryLine memoryLine)
175
            throws TTK91AddressOutOfBounds {
176
        String errorMessage;
177
        String[] errorParameters;
178 1
        if (memoryLine == null) {
179 2
            errorMessage = new Message("Trying to load a null memory " +
180
                    "line.").toString();
181 1
            throw new IllegalArgumentException(errorMessage);
182
        }
183 2
        if (index > memory.length) {
184 2
            errorParameters = new String[2];
185 6
            errorParameters[0] = "" + index;
186 6
            errorParameters[1] = "" + memory.length;
187 2
            errorMessage = new Message("Address {0} too large, memory size " +
188
                    "{1} (indexing starts at 0).",
189
                    errorParameters).toString();
190 1
            throw new TTK91AddressOutOfBounds(errorMessage);
191
        }
192 2
        if (index < 0) {
193 6
            errorMessage = new Message("Address {0} below zero.",
194
                    "" + index).toString();
195 1
            throw new TTK91AddressOutOfBounds(errorMessage);
196
197
        }
198
        memory[index] = memoryLine;
199 4
        memory_references++;
200
    }
201
202
    /**
203
     * Sets the size of the code area.
204
     *
205
     * @param size Size of the code area.
206
     */
207
    public void setCodeAreaLength(int size) {
208 2
        if (size < 0) {
209 1
            throw new IllegalArgumentException("Code area size cannot be negative.");
210
        }
211 2
        if (size > this.size) {
212 1
            throw new IllegalArgumentException("Code area size cannot be bigger than the size of the whole memory.");
213
        }
214 1
        codeAreaSize = size;
215
    }
216
217
    /**
218
     * Sets the size of the data area.
219
     *
220
     * @param size Size of the data area.
221
     */
222
    public void setDataAreaLength(int size) {
223 2
        if (size < 0) {
224 1
            throw new IllegalArgumentException("Data area size cannot be negative.");
225
        }
226 2
        if (size > this.size) {
227 1
            throw new IllegalArgumentException("Data area size cannot be bigger than size of the whole memory.");
228
        }
229 1
        dataAreaSize = size;
230
    }
231
232
    // Added by HT, 12.10.2004, Koskelo-project. Modiefied by Kohahdus
233
    // to substract code and data area size 2006-11-23.
234
235
    /**
236
     * Return the number of data references made.
237
     */
238
    public int getMemoryReferences() {
239 5
        return memory_references - getCodeAreaSize() - getDataAreaSize();
240
241
    }//getMemoryReferences
242
}

Mutations

17

removed call to fi/helsinki/cs/titokone/SymbolTable::<init> : SURVIVED

Removed assignment to member variable symbols : SURVIVED

20

Substituted 0 with 1 : SURVIVED

Removed assignment to member variable codeAreaSize : SURVIVED

Substituted 0 with 1 : SURVIVED

21

Substituted 0 with 1 : SURVIVED

Removed assignment to member variable dataAreaSize : SURVIVED

Substituted 0 with 1 : SURVIVED

24

Substituted 0 with 1 : SURVIVED

Substituted 0 with 1 : SURVIVED

Removed assignment to member variable memory_references : SURVIVED

33

changed conditional boundary : SURVIVED

negated conditional : KILLED -> fi.helsinki.cs.titokone.ControlTest.testGetApplicationDefinitions(fi.helsinki.cs.titokone.ControlTest)

34

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

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

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

36

Removed assignment to member variable size : KILLED -> fi.helsinki.cs.titokone.ControlTest.testChangeMemorySize(fi.helsinki.cs.titokone.ControlTest)

37

Removed assignment to member variable memory : KILLED -> fi.helsinki.cs.titokone.ControlTest.testGetApplicationDefinitions(fi.helsinki.cs.titokone.ControlTest)

38

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

Substituted 0 with 1 : SURVIVED

changed conditional boundary : KILLED -> fi.helsinki.cs.titokone.ControlTest.testGetApplicationDefinitions(fi.helsinki.cs.titokone.ControlTest)

Substituted 0 with 1 : SURVIVED

negated conditional : SURVIVED

39

Substituted 0 with 1 : SURVIVED

Substituted 0 with 1 : SURVIVED

removed call to fi/helsinki/cs/titokone/MemoryLine::<init> : SURVIVED

42

Removed assignment to member variable memory_references : SURVIVED

Substituted 0 with 1 : SURVIVED

Substituted 0 with 1 : SURVIVED

51

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

60

Replaced integer addition with subtraction : NO_COVERAGE

Substituted 1 with 0 : NO_COVERAGE

Substituted 1 with 0 : NO_COVERAGE

Removed assignment to member variable memory_references : NO_COVERAGE

61

removed call to fi/helsinki/cs/titokone/MemoryLine::getBinary : NO_COVERAGE

replaced return of integer sized value with (x == 0 ? 1 : 0) : NO_COVERAGE

73

removed call to fi/helsinki/cs/titokone/SymbolTable::toHashMap : NO_COVERAGE

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

83

changed conditional boundary : NO_COVERAGE

negated conditional : NO_COVERAGE

Changed increment from 1 to -1 : NO_COVERAGE

Substituted 0 with 1 : NO_COVERAGE

Substituted 0 with 1 : NO_COVERAGE

84

removed call to fi/helsinki/cs/titokone/MemoryLine::getBinary : NO_COVERAGE

86

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

96

Changed increment from 1 to -1 : NO_COVERAGE

Substituted 0 with 1 : NO_COVERAGE

changed conditional boundary : NO_COVERAGE

negated conditional : NO_COVERAGE

Substituted 0 with 1 : NO_COVERAGE

97

removed call to fi/helsinki/cs/titokone/MemoryLine::getBinary : NO_COVERAGE

99

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

109

negated conditional : NO_COVERAGE

changed conditional boundary : NO_COVERAGE

Changed increment from 1 to -1 : NO_COVERAGE

Substituted 0 with 1 : NO_COVERAGE

Substituted 0 with 1 : NO_COVERAGE

110

Replaced integer addition with subtraction : NO_COVERAGE

removed call to fi/helsinki/cs/titokone/MemoryLine::getBinary : NO_COVERAGE

112

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

121

replaced return of integer sized value with (x == 0 ? 1 : 0) : NO_COVERAGE

130

replaced return of integer sized value with (x == 0 ? 1 : 0) : NO_COVERAGE

140

mutated return of Object value for fi/helsinki/cs/titokone/RandomAccessMemoryImpl::getMemoryLine to ( if (x != null) null else throw new RuntimeException ) : KILLED -> fi.helsinki.cs.titokone.ControlTest.testLoad(fi.helsinki.cs.titokone.ControlTest)

150

mutated return of Object value for fi/helsinki/cs/titokone/RandomAccessMemoryImpl::getMemoryLines to ( if (x != null) null else throw new RuntimeException ) : KILLED -> fi.helsinki.cs.titokone.ControlTest.testLoad(fi.helsinki.cs.titokone.ControlTest)

removed call to [Lfi/helsinki/cs/titokone/MemoryLine;::clone : KILLED -> fi.helsinki.cs.titokone.ControlTest.testLoad(fi.helsinki.cs.titokone.ControlTest)

160

negated conditional : NO_COVERAGE

161

removed call to java/lang/IllegalArgumentException::<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

165

Removed assignment to member variable symbols : NO_COVERAGE

178

negated conditional : KILLED -> fi.helsinki.cs.titokone.ControlTest.testLoad(fi.helsinki.cs.titokone.ControlTest)

179

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

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

181

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

183

changed conditional boundary : SURVIVED

negated conditional : KILLED -> fi.helsinki.cs.titokone.ControlTest.testLoad(fi.helsinki.cs.titokone.ControlTest)

184

Substituted 2 with 3 : NO_COVERAGE

Substituted 2 with 3 : NO_COVERAGE

185

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

Substituted 0 with 1 : NO_COVERAGE

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

Substituted 0 with 1 : NO_COVERAGE

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

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

186

Substituted 1 with 0 : NO_COVERAGE

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

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

Substituted 1 with 0 : NO_COVERAGE

187

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

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

190

removed call to fi/helsinki/cs/ttk91/TTK91AddressOutOfBounds::<init> : NO_COVERAGE

192

negated conditional : KILLED -> fi.helsinki.cs.titokone.ControlTest.testLoad(fi.helsinki.cs.titokone.ControlTest)

changed conditional boundary : KILLED -> fi.helsinki.cs.titokone.ControlTest.testLoad(fi.helsinki.cs.titokone.ControlTest)

193

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

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

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/StringBuilder::<init> : NO_COVERAGE

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

195

removed call to fi/helsinki/cs/ttk91/TTK91AddressOutOfBounds::<init> : NO_COVERAGE

199

Removed assignment to member variable memory_references : SURVIVED

Substituted 1 with 0 : SURVIVED

Replaced integer addition with subtraction : SURVIVED

Substituted 1 with 0 : SURVIVED

208

negated conditional : KILLED -> fi.helsinki.cs.titokone.ControlTest.testLoad(fi.helsinki.cs.titokone.ControlTest)

changed conditional boundary : SURVIVED

209

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

211

changed conditional boundary : SURVIVED

negated conditional : KILLED -> fi.helsinki.cs.titokone.ControlTest.testLoad(fi.helsinki.cs.titokone.ControlTest)

212

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

214

Removed assignment to member variable codeAreaSize : SURVIVED

223

changed conditional boundary : SURVIVED

negated conditional : KILLED -> fi.helsinki.cs.titokone.ControlTest.testLoad(fi.helsinki.cs.titokone.ControlTest)

224

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

226

negated conditional : KILLED -> fi.helsinki.cs.titokone.ControlTest.testLoad(fi.helsinki.cs.titokone.ControlTest)

changed conditional boundary : SURVIVED

227

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

229

Removed assignment to member variable dataAreaSize : SURVIVED

239

replaced return of integer sized value with (x == 0 ? 1 : 0) : NO_COVERAGE

removed call to fi/helsinki/cs/titokone/RandomAccessMemoryImpl::getCodeAreaSize : NO_COVERAGE

removed call to fi/helsinki/cs/titokone/RandomAccessMemoryImpl::getDataAreaSize : NO_COVERAGE

Replaced integer subtraction with addition : NO_COVERAGE

Replaced integer subtraction with addition : NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 0.27