-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestVariable.java
More file actions
98 lines (84 loc) · 4.11 KB
/
TestVariable.java
File metadata and controls
98 lines (84 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.util.*;
public class TestVariable {
public static String runTest() {
String header = "TestVariable results:" + System.lineSeparator();
String errors = "";
// var1 starts at { 1, 2, 3}, then { 9, 8, 7 }, then reverts
TreeMap<String, VariableDelta> variables = new TreeMap<>();
variables.put("main,var1", new VariableDelta("int [3]", "main", "var1", "<multielement>", 500));
variables.put("main,var1:[0]", new VariableDelta("int", "main", "var1:[0]", "1", 500));
variables.put("main,var1:[1]", new VariableDelta("int", "main", "var1:[1]", "2", 504));
variables.put("main,var1:[2]", new VariableDelta("int", "main", "var1:[2]", "3", 508));
// first=old, second=new
ArrayList<Pair<VariableDelta, VariableDelta>> deltaSequence = new ArrayList<>();
deltaSequence.add(new Pair<>(
new VariableDelta(variables.get("main,var1:[0]")),
new VariableDelta("int", "main", "var1:[0]", "9", 500)));
deltaSequence.add(new Pair<>(
new VariableDelta(variables.get("main,var1:[1]")),
new VariableDelta("int", "main", "var1:[1]", "8", 504)));
deltaSequence.add(new Pair<>(
new VariableDelta(variables.get("main,var1:[2]")),
new VariableDelta("int", "main", "var1:[2]", "7", 508)));
// Construction done, now apply changes and check them each in turn
VariableDelta.applyDelta(variables.get(deltaSequence.get(0).getSecond().getKey()),
deltaSequence.get(0).getSecond());
errors += verifyVariableState(
variables.get(deltaSequence.get(0).getSecond().getKey()),
"int", "main", "var1:[0]", "9", 500);
VariableDelta.applyDelta(variables.get(deltaSequence.get(1).getSecond().getKey()),
deltaSequence.get(1).getSecond());
errors += verifyVariableState(
variables.get(deltaSequence.get(1).getSecond().getKey()),
"int", "main", "var1:[1]", "8", 504);
VariableDelta.applyDelta(variables.get(deltaSequence.get(2).getSecond().getKey()),
deltaSequence.get(2).getSecond());
errors += verifyVariableState(
variables.get(deltaSequence.get(2).getSecond().getKey()),
"int", "main", "var1:[2]", "7", 508);
// Now make sure we can reverse the changes
VariableDelta.applyDelta(variables.get(deltaSequence.get(2).getFirst().getKey()),
deltaSequence.get(2).getFirst());
errors += verifyVariableState(
variables.get(deltaSequence.get(2).getFirst().getKey()),
"int", "main", "var1:[2]", "3", 508);
VariableDelta.applyDelta(variables.get(deltaSequence.get(1).getFirst().getKey()),
deltaSequence.get(1).getFirst());
errors += verifyVariableState(
variables.get(deltaSequence.get(1).getFirst().getKey()),
"int", "main", "var1:[1]", "2", 504);
VariableDelta.applyDelta(variables.get(deltaSequence.get(0).getFirst().getKey()),
deltaSequence.get(0).getFirst());
errors += verifyVariableState(
variables.get(deltaSequence.get(0).getFirst().getKey()),
"int", "main", "var1:[0]", "1", 500);
if (errors.isEmpty()) errors = " All tests passed!" + System.lineSeparator();
return (header + errors);
} // runTest()
private static String verifyVariableState(
VariableDelta var,
String type,
String scope,
String name,
String value,
long address)
{
String errors = "";
if (!var.type.equals(type))
errors += " TYPE ERROR: [" + type + "] and [" + var.type +
"] are different." + System.lineSeparator();
if (!var.scope.equals(scope))
errors += " SCOPE ERROR: [" + scope + "] and [" + var.scope +
"] are different." + System.lineSeparator();
if (!var.name.equals(name))
errors += " NAME ERROR: [" + name + "] and [" + var.name +
"] are different." + System.lineSeparator();
if (!var.value.equals(value))
errors += " VALUE ERROR: [" + value + "] and [" + var.value +
"] are different." + System.lineSeparator();
if (var.address != address)
errors += " ADDRESS ERROR: [" + address + "] and [" + var.address +
"] are different." + System.lineSeparator();
return errors;
} // verifyVariableState()
} // TestVariable class