-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathValueGeneratorTest.java
More file actions
341 lines (294 loc) · 13.3 KB
/
ValueGeneratorTest.java
File metadata and controls
341 lines (294 loc) · 13.3 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright 2013-2025 chronicle.software; SPDX-License-Identifier: Apache-2.0
*/
package net.openhft.chronicle.values;
import net.openhft.chronicle.bytes.Byteable;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.core.values.LongValue;
import org.junit.jupiter.api.Test;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.util.Date;
import static net.openhft.chronicle.values.Generators.generateHeapClass;
import static net.openhft.chronicle.values.Generators.generateNativeClass;
import static net.openhft.chronicle.values.Values.newHeapInstance;
import static net.openhft.chronicle.values.Values.newNativeReference;
import static net.openhft.compiler.CompilerUtils.CACHED_COMPILER;
import static org.junit.jupiter.api.Assertions.*;
/**
* Tests code generation and serialisation routines.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class ValueGeneratorTest extends ValuesTestCommon {
@Test
public void testGenerateJavaCode() {
// JavaBeanInterface jbi = Values.newHeapInstance(JavaBeanInterface.class);
// jbi.setByte((byte) 1);
// jbi.setChar('2');
// jbi.setShort((short) 3);
// jbi.setInt(4);
// jbi.setFloat(5);
// jbi.setLong(6);
// jbi.setDouble(7);
// jbi.setFlag(true);
//
// assertEquals(1, jbi.getByte());
// assertEquals('2', jbi.getChar());
// assertEquals(3, jbi.getShort());
// assertEquals(4, jbi.getInt());
// assertEquals(5.0, jbi.getFloat(), 0);
// assertEquals(6, jbi.getLong());
// assertEquals(7.0, jbi.getDouble(), 0.0);
// assertTrue(jbi.getFlag());
}
@Test
public void testGenerateJavaCode2() {
MinimalInterface mi = newHeapInstance(MinimalInterface.class);
mi.byte$((byte) 1);
mi.char$('2');
mi.short$((short) 3);
mi.int$(4);
mi.float$(5);
mi.long$(6);
mi.double$(7);
mi.flag(true);
assertEquals(1, mi.byte$());
assertEquals('2', mi.char$());
assertEquals(3, mi.short$());
assertEquals(4, mi.int$());
assertEquals(5.0, mi.float$(), 0);
assertEquals(6, mi.long$());
assertEquals(7.0, mi.double$(), 0.0);
assertTrue(mi.flag());
Bytes<ByteBuffer> bbb = Bytes.wrapForWrite(ByteBuffer.allocate(64));
mi.writeMarshallable(bbb);
System.out.println("size: " + bbb.writePosition());
MinimalInterface mi2 = newHeapInstance(MinimalInterface.class);
bbb.readPosition(0);
mi2.readMarshallable(bbb);
assertEquals(1, mi2.byte$());
assertEquals('2', mi2.char$());
assertEquals(3, mi2.short$());
assertEquals(4, mi2.int$());
assertEquals(5.0, mi2.float$(), 0);
assertEquals(6, mi2.long$());
assertEquals(7.0, mi2.double$(), 0.0);
assertTrue(mi2.flag());
}
@SuppressWarnings("rawtypes")
@Test
public void testGenerateNativeWithGetUsing() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
String actual = generateNativeClass(ValueModel.acquire(JavaBeanInterfaceGetUsing.class),
ValueModel.simpleName(JavaBeanInterfaceGetUsing.class) + "$$Native");
System.out.println(actual);
Class<?> aClass = CACHED_COMPILER.loadFromJava(
BytecodeGen.getClassLoader(JavaBeanInterfaceGetUsing.class),
JavaBeanInterfaceGetUsing.class.getName() + "$$Native", actual);
JavaBeanInterfaceGetUsing jbi = (JavaBeanInterfaceGetUsing) aClass.asSubclass(JavaBeanInterfaceGetUsing.class).getDeclaredConstructor().newInstance();
BytesStore<?, ByteBuffer> bytes = BytesStore.wrap(ByteBuffer.allocate(64));
((Byteable) jbi).bytesStore(bytes, 0L, ((Byteable) jbi).maxSize());
jbi.setString("G'day");
assertEquals("G'day", jbi.getUsingString(new StringBuilder()).toString());
}
@Test
public void testGenerateNativeWithGetUsingAt() throws IllegalAccessException, InstantiationException {
JavaBeanInterfaceGetUsingAt jbi = loadNativeTypeAndCreateValue(JavaBeanInterfaceGetUsingAt.class);
JavaBeanInterfaceGetUsingAt jbi2 = loadNativeTypeAndCreateValue(JavaBeanInterfaceGetUsingAt.class);
LongValue val = Values.newHeapInstance(LongValue.class);
val.setValue(2L);
jbi.setItemAt(0, val);
LongValue ret = jbi.getUsingItemAt(0, val);
assertNotNull(ret);
assertEquals(2L, val.getValue());
ret = jbi.getUsingItemAt(1, val);
assertEquals(0L, ret.getValue());
assertNotEquals(jbi, jbi2);
val.setValue(2L);
jbi2.setItemAt(0, val);
assertEquals(jbi, jbi2);
}
@Test
public void testGenerateHeapWithGetUsingAt() throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
JavaBeanInterfaceGetUsingAt jbi = loadHeapTypeAndCreateValue(JavaBeanInterfaceGetUsingAt.class);
JavaBeanInterfaceGetUsingAt jbi2 = loadHeapTypeAndCreateValue(JavaBeanInterfaceGetUsingAt.class);
LongValue val = Values.newHeapInstance(LongValue.class);
val.setValue(2L);
jbi.setItemAt(0, val);
LongValue ret = jbi.getUsingItemAt(0, val);
assertNotNull(ret);
assertEquals(2L, val.getValue());
ret = jbi.getUsingItemAt(1, val);
assertEquals(0L, ret.getValue());
assertNotEquals(jbi, jbi2);
val.setValue(2L);
jbi2.setItemAt(0, val);
assertEquals(jbi, jbi2);
}
@Test
public void testGenerateNativeWithGetAt() throws IllegalAccessException, InstantiationException {
JavaBeanInterfaceGetAt jbi = loadNativeTypeAndCreateValue(JavaBeanInterfaceGetAt.class);
JavaBeanInterfaceGetAt jbi2 = loadNativeTypeAndCreateValue(JavaBeanInterfaceGetAt.class);
LongValue val = Values.newHeapInstance(LongValue.class);
val.setValue(2L);
jbi.setItemAt(0, val);
LongValue ret = jbi.getItemAt(0);
assertEquals(2L, ret.getValue());
ret = jbi.getItemAt(1);
assertEquals(0L, ret.getValue());
assertNotEquals(jbi, jbi2);
jbi2.setItemAt(0, val);
assertEquals(jbi, jbi2);
}
@Test
public void testGenerateHeapWithGetAt() throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
JavaBeanInterfaceGetAt jbi = loadHeapTypeAndCreateValue(JavaBeanInterfaceGetAt.class);
JavaBeanInterfaceGetAt jbi2 = loadHeapTypeAndCreateValue(JavaBeanInterfaceGetAt.class);
LongValue val = Values.newHeapInstance(LongValue.class);
val.setValue(2L);
jbi.setItemAt(0, val);
LongValue ret = jbi.getItemAt(0);
assertEquals(2L, ret.getValue());
ret = jbi.getItemAt(1);
assertEquals(0L, ret.getValue());
assertNotEquals(jbi, jbi2);
jbi2.setItemAt(0, val);
assertEquals(jbi, jbi2);
}
private <T> T loadNativeTypeAndCreateValue(Class<T> type) throws InstantiationException, IllegalAccessException {
String actual = generateNativeClass(ValueModel.acquire(type),
ValueModel.simpleName(type) + "$$Native");
System.out.println(actual);
Class<?> aClass = Values.nativeClassFor(type);
T jbi;
try {
jbi = (T) aClass.asSubclass(type).getConstructor().newInstance();
} catch (NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
BytesStore<?, ByteBuffer> bytes = BytesStore.wrap(ByteBuffer.allocate(64));
((Byteable) jbi).bytesStore(bytes, 0L, ((Byteable) jbi).maxSize());
return jbi;
}
private <T> T loadHeapTypeAndCreateValue(Class<T> type) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
String actual = generateHeapClass(ValueModel.acquire(type),
ValueModel.simpleName(type) + "$$Heap");
System.out.println(actual);
Class<T> aClass = Values.heapClassFor(type);
T jbi = (T) aClass.asSubclass(type).getDeclaredConstructor().newInstance();
return jbi;
}
@Test
public void testGenerateNativeWithHasArrays() {
HasArraysInterface hai = Values.newNativeReference(HasArraysInterface.class);
long length = ((Byteable) hai).maxSize();
BytesStore<?, ByteBuffer> bytes = BytesStore.wrap(ByteBuffer.allocate((int) length));
((Byteable) hai).bytesStore(bytes, 0L, length);
hai.setStringAt(0, "G'day");
assertEquals("G'day", hai.getStringAt(0));
}
@Test
public void testGenerateNativeWithGetUsingHeapInstance() {
JavaBeanInterfaceGetUsingHeap si = newHeapInstance(JavaBeanInterfaceGetUsingHeap.class);
si.setString("G'day");
assertEquals("G'day", si.getUsingString(new StringBuilder()).toString());
}
@Test
public void testStringFields() {
StringInterface si = newHeapInstance(StringInterface.class);
si.setString("Hello world");
assertEquals("Hello world", si.getString());
StringInterface si2 = newNativeReference(StringInterface.class);
BytesStore<?, ByteBuffer> bytes = BytesStore.wrap(ByteBuffer.allocate(192));
((Byteable) si2).bytesStore(bytes, 0L, ((Byteable) si2).maxSize());
si2.setString("Hello world £€");
si2.setText("Hello world £€");
assertEquals("Hello world £€", si2.getString());
assertEquals("Hello world £€", si2.getText());
}
@Test
public void testGetUsingStringFieldsWithStringBuilderHeapInstance() {
GetUsingStringInterface si = newHeapInstance(GetUsingStringInterface.class);
si.setSomeStringField("Hello world");
si.setAnotherStringField("Hello world 2");
assertEquals("Hello world", si.getSomeStringField());
{
StringBuilder builder = new StringBuilder();
si.getUsingSomeStringField(builder);
assertEquals("Hello world", builder.toString());
}
{
StringBuilder builder = new StringBuilder();
si.getUsingAnotherStringField(builder);
assertEquals("Hello world 2", builder.toString());
}
}
@Test
public void testNested() {
NestedB nestedB1 = newHeapInstance(NestedB.class);
nestedB1.ask(100);
nestedB1.bid(100);
NestedB nestedB2 = newHeapInstance(NestedB.class);
nestedB2.ask(91);
nestedB2.bid(92);
NestedA nestedA = newNativeReference(NestedA.class);
BytesStore<?, ByteBuffer> bytes = BytesStore.wrap(ByteBuffer.allocate(192));
((Byteable) nestedA).bytesStore(bytes, 0L, ((Byteable) nestedA).maxSize());
nestedA.key("key");
nestedA.one(nestedB1);
nestedA.two(nestedB2);
assertEquals("key", nestedA.key());
assertEquals(nestedB1.ask(), nestedA.one().ask(), 0.0);
assertEquals(nestedB1.bid(), nestedA.one().bid(), 0.0);
assertEquals(nestedB2.ask(), nestedA.two().ask(), 0.0);
assertEquals(nestedB2.bid(), nestedA.two().bid(), 0.0);
assertEquals(nestedB1, nestedA.one());
assertEquals(nestedB2, nestedA.two());
assertEquals(nestedB1.hashCode(), nestedA.one().hashCode());
assertEquals(nestedB2.hashCode(), nestedA.two().hashCode());
}
@Test
public void testGenerateInterfaceWithEnumOnHeap() {
JavaBeanInterfaceGetMyEnum jbie = newHeapInstance(JavaBeanInterfaceGetMyEnum.class);
jbie.setMyEnum(MyEnum.B);
}
@Test
public void testGenerateInterfaceWithEnumNativeInstance() {
JavaBeanInterfaceGetMyEnum jbie = newNativeReference(JavaBeanInterfaceGetMyEnum.class);
BytesStore<?, ByteBuffer> bytes = BytesStore.wrap(ByteBuffer.allocate(64));
((Byteable) jbie).bytesStore(bytes, 0L, ((Byteable) jbie).maxSize());
jbie.setMyEnum(MyEnum.C);
}
@Test
public void testGenerateInterfaceWithDateOnHeap() {
//dvg.setDumpCode(true);
JavaBeanInterfaceGetDate jbid = newHeapInstance(JavaBeanInterfaceGetDate.class);
jbid.setDate(new Date());
}
@Test
public void testGenerateInterfaceWithDateNativeInstace() {
//dvg.setDumpCode(true);
JavaBeanInterfaceGetDate jbid = newNativeReference(JavaBeanInterfaceGetDate.class);
BytesStore<?, ByteBuffer> bytes = BytesStore.wrap(ByteBuffer.allocate(64));
((Byteable) jbid).bytesStore(bytes, 0L, ((Byteable) jbid).maxSize());
Date date = new Date();
jbid.setDate(date);
assertEquals(date, jbid.getDate());
}
@Test
public void testGenerateInterfaceWithMoreThanOneEnums() {
//dvg.setDumpCode(true);
JavaBeanInterfaceMoreThanOneEnums jbid = newNativeReference(JavaBeanInterfaceMoreThanOneEnums.class);
BytesStore<?, ByteBuffer> bytes = BytesStore.wrap(ByteBuffer.allocate(64));
((Byteable) jbid).bytesStore(bytes, 0L, ((Byteable) jbid).maxSize());
MyEnum myEnum1 = MyEnum.B;
jbid.setMyEnum1(myEnum1);
MyEnum myEnum2 = MyEnum.A;
jbid.setMyEnum2(myEnum2);
BuySell buySell = BuySell.BUY;
jbid.setBuySell(buySell);
assertEquals(myEnum1, jbid.getMyEnum1());
assertEquals(myEnum2, jbid.getMyEnum2());
assertEquals(buySell, jbid.getBuySell());
}
}