mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Add a DoubleArray example.
This commit is contained in:
@@ -9,15 +9,16 @@ import net.openhft.lang.model.Copyable;
|
||||
* Created by peter.lawrey on 23/04/2015.
|
||||
*/
|
||||
public class DoubleArray implements Byteable, Copyable<DoubleArray> {
|
||||
private static int LENGTH = 0; // assume a 32-bit size.
|
||||
private static int CAPACITY = LENGTH + 4; // assume a 32-bit size.
|
||||
private static int BASE = CAPACITY + 4;
|
||||
private static int CAPACITY = 0; // assume a 32-bit size.
|
||||
private static int LENGTH = CAPACITY + 4; // assume a 32-bit size.
|
||||
private static int BASE = LENGTH + 4;
|
||||
|
||||
private Bytes bytes;
|
||||
private long offset;
|
||||
|
||||
public DoubleArray(int capacity) {
|
||||
bytes = DirectStore.allocate(BASE + capacity * 8L).bytes();
|
||||
bytes.writeInt(CAPACITY, capacity);
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package net.openhft.lang.values;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class DoubleArrayTest {
|
||||
|
||||
@Test
|
||||
public void testSetData() {
|
||||
DoubleArray da = new DoubleArray(64);
|
||||
da.setData(new double[]{1, 2, 3, 4, 5, 6, 7, 8});
|
||||
assertEquals(8, da.length());
|
||||
for (int i = 0; i < da.length(); i++)
|
||||
assertEquals(i + 1.0, da.getDataAt(i), 0.0);
|
||||
da.addData(9);
|
||||
da.addData(10);
|
||||
double[] ds = new double[64];
|
||||
int len = da.getDataUsing(ds);
|
||||
assertEquals(10, len);
|
||||
for (int i = 0; i < len; i++)
|
||||
assertEquals(i + 1.0, ds[i], 0.0);
|
||||
|
||||
for (int i = 0; i < 64; i++)
|
||||
ds[i] = i * 1.01;
|
||||
da.setData(ds);
|
||||
assertEquals(64, da.length());
|
||||
for (int i = 0; i < da.length(); i++)
|
||||
assertEquals(i * 1.01, da.getDataAt(i), 0.0);
|
||||
|
||||
double[] ds2 = new double[65];
|
||||
assertEquals(64, da.getDataUsing(ds2));
|
||||
for (int i = 0; i < 64; i++) {
|
||||
assertEquals(i * 1.01, da.getDataAt(i), 0.0);
|
||||
assertEquals(i * 1.01, ds2[i], 0.0);
|
||||
}
|
||||
|
||||
try {
|
||||
da.setData(ds2);
|
||||
fail();
|
||||
} catch (IllegalArgumentException expected) {
|
||||
// expected
|
||||
}
|
||||
// free the memory.
|
||||
da.bytes().release();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user