Skip to content

Commit 6258820

Browse files
committed
minor: add equality methods for locals & stack
1 parent 2b4fc69 commit 6258820

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group 'dev.xdark'
8-
version '1.4.1'
8+
version '1.4.2'
99

1010
repositories {
1111
mavenCentral()

src/main/java/dev/xdark/ssvm/execution/Locals.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,35 @@ public void close() {
9797
deallocate();
9898
}
9999

100+
@Override
101+
public boolean equals(Object o) {
102+
if (o == this) return true;
103+
if (!(o instanceof Locals)) return false;
104+
val other = (Locals) o;
105+
val table = this.table;
106+
int length = table.length();
107+
val otherTable = other.table;
108+
if (table.length() != otherTable.length()) return false;
109+
for (int i = 0; i < length; i++) {
110+
if (!Objects.equals(table.get(i), otherTable.get(i))) {
111+
return false;
112+
}
113+
}
114+
return true;
115+
}
116+
117+
@Override
118+
public int hashCode() {
119+
int result = 1;
120+
val table = this.table;
121+
int cursor = table.length();
122+
for (int i = 0; i < cursor; i++) {
123+
result *= 31;
124+
result += Objects.hashCode(table.get(i).hashCode());
125+
}
126+
return result;
127+
}
128+
100129
@Override
101130
public String toString() {
102131
return "Locals{" +

src/main/java/dev/xdark/ssvm/execution/Stack.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,33 @@ public void close() {
337337
deallocate();
338338
}
339339

340+
@Override
341+
public boolean equals(Object o) {
342+
if (o == this) return true;
343+
if (!(o instanceof Stack)) return false;
344+
val other = (Stack) o;
345+
int cursor = this.cursor;
346+
if (cursor != other.cursor) return false;
347+
for (int i = 0; i < cursor; i++) {
348+
if (!Objects.equals(getAt(i), other.getAt(i))) {
349+
return false;
350+
}
351+
}
352+
return true;
353+
}
354+
355+
@Override
356+
public int hashCode() {
357+
int result = 1;
358+
int cursor = this.cursor;
359+
val stack = this.stack;
360+
for (int i = 0; i < cursor; i++) {
361+
result *= 31;
362+
result += Objects.hashCode(stack.get(i).hashCode());
363+
}
364+
return result;
365+
}
366+
340367
@Override
341368
public String toString() {
342369
return "Stack{" +
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package dev.xdark.ssvm;
2+
3+
import dev.xdark.ssvm.execution.Locals;
4+
import dev.xdark.ssvm.execution.Stack;
5+
import dev.xdark.ssvm.value.DoubleValue;
6+
import dev.xdark.ssvm.value.IntValue;
7+
import dev.xdark.ssvm.value.LongValue;
8+
import dev.xdark.ssvm.value.NullValue;
9+
import lombok.val;
10+
import org.junit.jupiter.api.Test;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
public class LocalsTest {
15+
16+
@Test
17+
public void testEquality() {
18+
try (val locals1 = new Locals(5); val locals2 = new Locals(5)) {
19+
fillLocals(locals1);
20+
fillLocals(locals2);
21+
assertEquals(locals1, locals2);
22+
}
23+
}
24+
25+
private static void fillLocals(Locals locals) {
26+
locals.set(0, IntValue.ONE);
27+
locals.setWide(1, LongValue.ONE);
28+
locals.set(3, IntValue.of(5));
29+
locals.setWide(4, new DoubleValue(6.7D));
30+
}
31+
}

src/test/java/dev/xdark/ssvm/StackTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package dev.xdark.ssvm;
22

33
import dev.xdark.ssvm.execution.Stack;
4+
import dev.xdark.ssvm.value.DoubleValue;
45
import dev.xdark.ssvm.value.IntValue;
56
import dev.xdark.ssvm.value.LongValue;
7+
import dev.xdark.ssvm.value.NullValue;
68
import lombok.val;
79
import org.junit.jupiter.api.Test;
810

@@ -190,4 +192,21 @@ public void testSwap() {
190192
assertTrue(stack.isEmpty());
191193
}
192194
}
195+
196+
@Test
197+
public void testEquality() {
198+
try (val stack1 = new Stack(8); val stack2 = new Stack(8)) {
199+
filLStack(stack1);
200+
filLStack(stack2);
201+
assertEquals(stack1, stack2);
202+
}
203+
}
204+
205+
private static void filLStack(Stack stack) {
206+
stack.push(IntValue.ONE);
207+
stack.pushWide(LongValue.ZERO);
208+
stack.pushWide(new DoubleValue(1.3D));
209+
stack.push(NullValue.INSTANCE);
210+
stack.pushWide(LongValue.ONE);
211+
}
193212
}

0 commit comments

Comments
 (0)