1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.tests;
14 import static org.junit.Assert.assertEquals;
\r
15 import static org.junit.Assert.fail;
\r
17 import org.junit.Before;
\r
18 import org.junit.Test;
\r
19 import org.simantics.databoard.util.binary.BinaryMemory;
\r
20 import org.simantics.databoard.util.binary.Blob;
\r
21 import org.simantics.databoard.util.binary.RandomAccessBinary;
\r
22 import org.simantics.databoard.util.binary.RandomAccessBinary.ByteSide;
\r
24 public class TestBlob {
26 // Blob with 0..1600 bytes
28 // 10 sub blobs of 160 bytes
30 // 10*10 sub-blobs of 16 bytes
33 public @Before void initData() throws Exception {
34 RandomAccessBinary mem;
35 mem = new BinaryMemory(1600);
36 // mem = BinaryFile.tempFile(1600);
38 level1 = new Blob[10];
39 level2 = new Blob[10][];
40 for (int i=0; i<10; i++) {
41 level1[i] = blob.createSubBlob(i*100, 160);
42 level2[i] = new Blob[10];
43 for (int j=0; j<10; j++) {
44 level2[i][j] = level1[i].createSubBlob(i*10, 16);
49 public @Test void testSize() throws Exception {
51 assertEquals(b.length(), 1600);
53 for (int l=0; l<10; l++) {
55 assertEquals(b.length(), 160);
57 for (int l2=0; l2<10; l2++) {
59 assertEquals(b.length(), 16);
64 public @Test void testIsolation() throws Exception {
70 for (int i=0; i<10; i++) {
75 for (int j=0; j<10; j++) {
84 void testIsolation(Blob b) throws Exception {
85 int parentCount = (int) b.getSource().length()/4;
86 int c = (int) b.length()/4;
90 for (int i=0; i<c; i++) {
96 for (int i=0; i<c; i++) {
97 assertEquals(i^0x58, b.readInt());
100 // Fail test (cannot read more)
103 fail("Did not catch boundary exceed");
104 } catch (IndexOutOfBoundsException e) {}
106 // Write once, enlarges the blob
108 assertEquals(c+1, b.length()/4);
109 assertEquals(parentCount+1, b.getSource().length()/4);
112 assertEquals(c, b.length()/4);
113 assertEquals(parentCount, b.getSource().length()/4);
118 public void testInsertRemove(Blob b) throws Exception {
119 int c = (int) b.length()/4;
123 for (int i=0; i<c; i++) {
129 b.removeBytes(4, ByteSide.Left);
133 assertEquals(0x53, b.readInt());
134 for (int i=2; i<c; i++) {
135 assertEquals(i^0x53, b.readInt());
140 b.insertBytes(4, ByteSide.Left);
144 for (int i=0; i<c; i++) {
145 assertEquals(i^0x53, b.readInt());
149 public @Test void testMove1Gdata() throws IOException {
\r
151 File file = File.createTempFile("TestBlob", ".tmp");
\r
152 file.deleteOnExit();
\r
153 BinaryFile b = new BinaryFile(file);
\r
156 System.out.println("Creating 1GB file.");
\r
157 for (int i=0; i<1*1024*1024*1024/4; i++) {
\r
161 System.out.println("Adding 64 bytes at position 0");
\r
162 long time = System.nanoTime();
\r
164 b.insertBytes(64L, ByteSide.Left);
\r
165 long elapsed = System.nanoTime() - time;
\r
166 System.out.println("Elapsed: "+elapsed+" ns");
\r