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;
15 import java.io.IOException;
16 import java.io.RandomAccessFile;
18 import junit.framework.TestCase;
20 import org.simantics.databoard.util.binary.BinaryFile;
22 public class testFileUtil extends TestCase {
25 public void testInsertRemove()
28 File tmp1 = File.createTempFile("tmp", "tmp");
31 RandomAccessFile file = new RandomAccessFile(tmp1, "rw");
34 BinaryFile write = new BinaryFile(file);
35 for (int i=0; i<1024*1024; i++)
37 write.writeInt(i ^0x53);
43 long totInsertTime = 0;
44 long totRemoveTime = 0;
46 for (int iter=0; iter<iterCount; iter++) {
49 file = new RandomAccessFile(tmp1, "rw");
50 BinaryFile read = new BinaryFile(file);
52 for (int i=0; i<1024*1024; i++)
53 assertEquals(i^0x53, read.readInt());
59 long startTime = System.currentTimeMillis();
60 file = new RandomAccessFile(tmp1, "rw");
61 BinaryFile.insertBytes(file, 512*1024*4, 256*1024);
62 long elapsedTime = System.currentTimeMillis() - startTime;
63 totInsertTime += elapsedTime;
64 System.out.println("Insert 256kb in front of 2MB, time: "+elapsedTime);
67 assertEquals(4*1024*1024 + 256*1024, file.length());
71 read = new BinaryFile(file);
73 for (int i=0; i<512*1024; i++)
74 assertEquals(i^0x53, read.readInt());
76 read.position(512*1024*4+256*1024);
77 for (int i=512*1024; i<1024*1024; i++)
78 assertEquals(i^0x53, read.readInt());
83 file = new RandomAccessFile(tmp1, "rw");
85 startTime = System.currentTimeMillis();
86 BinaryFile.removeBytes(file, 512*1024*4, 256*1024);
87 elapsedTime = System.currentTimeMillis() - startTime;
88 totRemoveTime += elapsedTime;
89 System.out.println("remove 256kb in front of 2,25MB, time: "+elapsedTime);
92 assertEquals(4*1024*1024, file.length());
96 read = new BinaryFile(file);
98 for (int i=0; i<1024*1024; i++)
99 assertEquals(i^0x53, read.readInt());
103 System.out.println("Average insert time: " + totInsertTime / iterCount);
104 System.out.println("Average remove time: " + totRemoveTime / iterCount);