]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/TestBinaryMemory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / TestBinaryMemory.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.FileNotFoundException;
15 import java.io.IOException;
16
17 import junit.framework.TestCase;
18
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;
24
25 public class TestBinaryMemory extends TestCase {
26
27         public static final int ITERATIONS = 50;
28         RandomAccessBinary file;
29         
30         public void setUp() throws Exception {
31                 file = new BinaryMemory(4096);
32         }
33         
34         public void tearDown() throws Exception {
35                 file.close();
36         }
37         
38         public void testInsertRemove()
39         throws IOException
40         {
41                 // Write 4 MB file
42                 for (int i=0; i<1024*1024; i++)
43                 {
44                         file.writeInt(i ^0x53);
45                 }
46                 file.flush();
47
48                 int iterCount = 16;
49                 long totInsertTime = 0;
50                 long totRemoveTime = 0;
51                 
52                 for (int iter=0; iter<iterCount; iter++) {
53                 // Verify content
54                 // 1. half
55                 file.position(0L);
56                 for (int i=0; i<1024*1024; i++)
57                         assertEquals(i^0x53, file.readInt());
58                 
59                 // Add 256 kb at 2MB
60                 System.gc();
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);
67                 
68                 // Verify file size
69                 assertEquals(4*1024*1024 + 256*1024, file.length()); 
70                 
71                 // Verify content
72                 // 1. half
73                 file.position(0);
74                 for (int i=0; i<512*1024; i++)
75                         assertEquals(i^0x53, file.readInt());
76                 // 2. half
77                 file.position(512*1024*4+256*1024);
78                 for (int i=512*1024; i<1024*1024; i++)
79                         assertEquals(i^0x53, file.readInt());
80
81                 // Remove content
82                 System.gc();
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);
89                 
90                 // Verify file size
91                 assertEquals(4*1024*1024, file.length());
92                 
93                 // Verify content
94                 // 1. half
95                 file.position(0);
96                 for (int i=0; i<1024*1024; i++)
97                         assertEquals(i^0x53, file.readInt());
98                 }
99
100                 System.out.println("Average insert time: " + totInsertTime / iterCount);
101                 System.out.println("Average remove time: " + totRemoveTime / iterCount);
102         }
103         
104         public void testWriteRead() 
105         throws FileNotFoundException, IOException 
106         {               
107                 long time4 = measureWrite(file);
108                 System.out.println();
109                 System.out.println("Write Time : "+time4);
110
111                 file.flush();
112                 file.position(0);
113                 
114                 time4 = measureRead(file);                              
115                 System.out.println("Read Time : "+time4);
116         }
117         
118         static long measureWrite(BinaryWriteable write) 
119         throws IOException
120         {               
121                 System.gc();
122                 byte[] data = new byte[1024];
123                 for (int i=0; i<data.length; i++)
124                         data[i] = (byte) i;
125                 
126                 long startTime = System.currentTimeMillis();            
127                 for (int iter=0; iter<ITERATIONS; iter++)
128                 {
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++)
135                                 write.writeFloat(i);
136                         for (int i=0; i<256; i++)
137                                 write.writeInt(i);
138                         for (int i=0; i<256; i++)
139                                 write.writeShort((short)i);
140                         write.write(data);                      
141                 }               
142                 write.flush();
143                 long elapsedTime = System.currentTimeMillis() - startTime;              
144                 
145                 return elapsedTime;
146         }
147         
148         static long measureRead(BinaryReadable read) 
149         throws IOException
150         {               
151                 System.gc();
152                 byte[] data = new byte[1024];
153                 
154                 long startTime = System.currentTimeMillis();            
155                 for (int iter=0; iter<ITERATIONS; iter++)
156                 {
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);
171                 }               
172                 long elapsedTime = System.currentTimeMillis() - startTime;
173                 
174                 return elapsedTime;
175         }
176         
177 }
178