]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/testFileUtil.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / testFileUtil.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
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
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.tests;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.RandomAccessFile;
17
18 import junit.framework.TestCase;
19
20 import org.simantics.databoard.util.binary.BinaryFile;
21
22 public class testFileUtil extends TestCase {
23
24         
25         public void testInsertRemove()
26         throws IOException
27         {
28                 File tmp1 = File.createTempFile("tmp", "tmp");
29                 tmp1.deleteOnExit();
30                 
31                 RandomAccessFile file = new RandomAccessFile(tmp1, "rw");
32                 
33                 // Write 4 MB file
34                 BinaryFile write = new BinaryFile(file);
35                 for (int i=0; i<1024*1024; i++)
36                 {
37                         write.writeInt(i ^0x53);
38                 }
39                 write.flush();
40                 write.close();
41
42                 int iterCount = 16;
43                 long totInsertTime = 0;
44                 long totRemoveTime = 0;
45                 
46                 for (int iter=0; iter<iterCount; iter++) {
47                 // Verify content
48                 // 1. half
49                 file = new RandomAccessFile(tmp1, "rw");
50                 BinaryFile read = new BinaryFile(file);         
51                 read.position(0);
52                 for (int i=0; i<1024*1024; i++)
53                         assertEquals(i^0x53, read.readInt());
54                 read.close();
55                 file.close();
56                 
57                 // Add 256 kb at 2MB
58                 System.gc();
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);
65                 
66                 // Verify file size
67                 assertEquals(4*1024*1024 + 256*1024, file.length());
68                 
69                 // Verify content
70                 // 1. half
71                 read = new BinaryFile(file);            
72                 read.position(0);
73                 for (int i=0; i<512*1024; i++)
74                         assertEquals(i^0x53, read.readInt());
75                 // 2. half
76                 read.position(512*1024*4+256*1024);
77                 for (int i=512*1024; i<1024*1024; i++)
78                         assertEquals(i^0x53, read.readInt());
79                 read.close();
80                 file.close();
81
82                 // Remove content
83                 file = new RandomAccessFile(tmp1, "rw");
84                 System.gc();
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);
90                 
91                 // Verify file size
92                 assertEquals(4*1024*1024, file.length());
93                 
94                 // Verify content
95                 // 1. half
96                 read = new BinaryFile(file);            
97                 read.position(0);
98                 for (int i=0; i<1024*1024; i++)
99                         assertEquals(i^0x53, read.readInt());
100                 read.close();           
101                 }
102
103                 System.out.println("Average insert time: " + totInsertTime / iterCount);
104                 System.out.println("Average remove time: " + totRemoveTime / iterCount);
105                 
106                 tmp1.delete();
107         }
108         
109         
110 }
111