-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathPointerTest.java
More file actions
75 lines (63 loc) · 3.06 KB
/
Copy pathPointerTest.java
File metadata and controls
75 lines (63 loc) · 3.06 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
/*
* Copyright 2013-2025 chronicle.software; SPDX-License-Identifier: Apache-2.0
*/
package net.openhft.chronicle.values.pointer;
import net.openhft.chronicle.bytes.Byteable;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.values.Values;
import net.openhft.chronicle.values.ValuesTestCommon;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import static java.util.Objects.requireNonNull;
import static org.junit.jupiter.api.Assertions.*;
public class PointerTest extends ValuesTestCommon {
private static long getAddress(Byteable byteable) {
return requireNonNull(byteable.bytesStore()).addressForRead(byteable.offset());
}
@SuppressWarnings("unchecked")
@NotNull
private static PointedInterface getPointed() {
PointedInterface pointed1 = Values.newNativeReference(PointedInterface.class);
long pointedSize = pointed1.maxSize();
//noinspection unchecked
pointed1.bytesStore(Bytes.allocateDirect(pointedSize), 0, pointedSize);
return pointed1;
}
@Test
public void testPointer() {
// System.setProperty("chronicle.values.dumpCode", "true");
PointedInterface pointedFoo = getPointed();
assertTrue(pointedFoo.offset() < 10_000);
pointedFoo.setString("foo");
long fooAddress = pointedFoo.address();
PointedInterface pointedBar = getPointed();
assertTrue(pointedBar.offset() < 10_000);
pointedBar.setString("bar");
long barAddress = pointedBar.address();
PointingInterface heapPointing = Values.newHeapInstance(PointingInterface.class);
assertNull(heapPointing.getPoint());
assertNull(heapPointing.getVolatilePoint());
heapPointing.setPoint(pointedFoo);
assertTrue(heapPointing.getPoint().offset() < 10_000);
// checks that heap object doesn't simply store reference to an object, only address
assertNotSame(heapPointing.getPoint(), pointedFoo);
assertNotSame(heapPointing.getVolatilePoint(), pointedFoo);
assertEquals(fooAddress, heapPointing.getPoint().address());
assertEquals(fooAddress, heapPointing.getVolatilePoint().address());
// check setVolatile
heapPointing.setVolatilePoint(pointedBar);
assertNotSame(heapPointing.getPoint(), pointedBar);
assertEquals(barAddress, heapPointing.getPoint().address());
// check setOrdered
heapPointing.setOrderedPoint(pointedFoo);
assertNotSame(heapPointing.getVolatilePoint(), pointedFoo);
assertEquals(fooAddress, heapPointing.getVolatilePoint().address());
assertFalse(heapPointing.compareAndSwapPoint(pointedBar, pointedFoo));
assertTrue(heapPointing.compareAndSwapPoint(pointedFoo, pointedBar));
assertNotSame(heapPointing.getPoint(), pointedBar);
assertEquals(barAddress, heapPointing.getPoint().address());
Values.nativeClassFor(PointedInterface.class);
requireNonNull(pointedFoo.bytesStore()).releaseLast();
requireNonNull(pointedBar.bytesStore()).releaseLast();
}
}