1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.tests;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
17 import junit.framework.TestCase;
19 import org.simantics.databoard.util.binary.BinaryMemory;
20 import org.simantics.databoard.util.binary.BinaryReadable;
21 import org.simantics.databoard.util.binary.BinaryWriteable;
22 import org.simantics.databoard.util.binary.RandomAccessBinary;
23 import org.simantics.databoard.util.binary.RandomAccessBinary.ByteSide;
25 public class TestBinaryMemory extends TestCase {
27 public static final int ITERATIONS = 50;
28 RandomAccessBinary file;
30 public void setUp() throws Exception {
31 file = new BinaryMemory(4096);
34 public void tearDown() throws Exception {
38 public void testInsertRemove()
42 for (int i=0; i<1024*1024; i++)
44 file.writeInt(i ^0x53);
49 long totInsertTime = 0;
50 long totRemoveTime = 0;
52 for (int iter=0; iter<iterCount; iter++) {
56 for (int i=0; i<1024*1024; i++)
57 assertEquals(i^0x53, file.readInt());
61 long startTime = System.currentTimeMillis();
62 file.position(512*1024*4L);
63 file.insertBytes(256*1024, ByteSide.Left);
64 long elapsedTime = System.currentTimeMillis() - startTime;
65 totInsertTime += elapsedTime;
66 System.out.println("Insert 256kb in front of 2MB, time: "+elapsedTime);
69 assertEquals(4*1024*1024 + 256*1024, file.length());
74 for (int i=0; i<512*1024; i++)
75 assertEquals(i^0x53, file.readInt());
77 file.position(512*1024*4+256*1024);
78 for (int i=512*1024; i<1024*1024; i++)
79 assertEquals(i^0x53, file.readInt());
83 startTime = System.currentTimeMillis();
84 file.position(512*1024*4);
85 file.removeBytes(256*1024, ByteSide.Left);
86 elapsedTime = System.currentTimeMillis() - startTime;
87 totRemoveTime += elapsedTime;
88 System.out.println("remove 256kb in front of 2,25MB, time: "+elapsedTime);
91 assertEquals(4*1024*1024, file.length());
96 for (int i=0; i<1024*1024; i++)
97 assertEquals(i^0x53, file.readInt());
100 System.out.println("Average insert time: " + totInsertTime / iterCount);
101 System.out.println("Average remove time: " + totRemoveTime / iterCount);
104 public void testWriteRead()
105 throws FileNotFoundException, IOException
107 long time4 = measureWrite(file);
108 System.out.println();
109 System.out.println("Write Time : "+time4);
114 time4 = measureRead(file);
115 System.out.println("Read Time : "+time4);
118 static long measureWrite(BinaryWriteable write)
122 byte[] data = new byte[1024];
123 for (int i=0; i<data.length; i++)
126 long startTime = System.currentTimeMillis();
127 for (int iter=0; iter<ITERATIONS; iter++)
129 if (iter % 10==0) System.out.print(".");
130 for (int i=0; i<256; i++)
131 write.write((byte)i);
132 for (int i=0; i<256; i++)
133 write.writeDouble(i);
134 for (int i=0; i<256; i++)
136 for (int i=0; i<256; i++)
138 for (int i=0; i<256; i++)
139 write.writeShort((short)i);
143 long elapsedTime = System.currentTimeMillis() - startTime;
148 static long measureRead(BinaryReadable read)
152 byte[] data = new byte[1024];
154 long startTime = System.currentTimeMillis();
155 for (int iter=0; iter<ITERATIONS; iter++)
157 if (iter % 10==0) System.out.print(".");
158 for (int i=0; i<256; i++)
159 assertEquals((byte) i, read.readByte());
160 for (int i=0; i<256; i++)
161 assertEquals((double) i, read.readDouble());
162 for (int i=0; i<256; i++)
163 assertEquals((float) i, read.readFloat());
164 for (int i=0; i<256; i++)
165 assertEquals((int) i, read.readInt());
166 for (int i=0; i<256; i++)
167 assertEquals((short) i, read.readShort());
168 read.readFully(data);
169 for (int i=0; i<data.length; i++)
170 assertEquals(data[i], (byte) i);
172 long elapsedTime = System.currentTimeMillis() - startTime;