]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/TestBlob.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / TestBlob.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 static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.fail;
16
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.simantics.databoard.util.binary.BinaryMemory;
20 import org.simantics.databoard.util.binary.Blob;
21 import org.simantics.databoard.util.binary.RandomAccessBinary;
22 import org.simantics.databoard.util.binary.RandomAccessBinary.ByteSide;
23
24 public class TestBlob {
25
26         // Blob with 0..1600 bytes
27         Blob blob;
28         // 10 sub blobs of 160 bytes
29         Blob[] level1;
30         // 10*10 sub-blobs of 16 bytes
31         Blob[][] level2;
32
33         public @Before void initData() throws Exception {
34                 RandomAccessBinary mem;
35                 mem = new BinaryMemory(1600);
36 //              mem = BinaryFile.tempFile(1600);
37                 blob = new Blob(mem);
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);
45                         }
46                 }
47         }
48         
49         public @Test void testSize() throws Exception {
50                 Blob b = blob;
51                 assertEquals(b.length(), 1600);
52                 
53                 for (int l=0; l<10; l++) {                      
54                         b = level1[l];
55                         assertEquals(b.length(), 160);
56                         
57                         for (int l2=0; l2<10; l2++) {
58                                 b = level2[l][l2];
59                                 assertEquals(b.length(), 16);
60                         }
61                 }
62         }
63         
64         public @Test void testIsolation() throws Exception {
65                 
66                 Blob b = blob;
67                 testIsolation(b);
68                 testInsertRemove(b);                            
69                 
70                 for (int i=0; i<10; i++) {                      
71                         b = level1[i];
72                         testIsolation(b);
73                         testInsertRemove(b);                            
74                         
75                         for (int j=0; j<10; j++) {
76                                 b = level2[i][j];
77                                 testIsolation(b);       
78                                 testInsertRemove(b);                            
79                         }
80                 }
81                 
82         }
83         
84         void testIsolation(Blob b) throws Exception {
85                 int parentCount = (int) b.getSource().length()/4;
86                 int c = (int) b.length()/4;
87                 
88                 // Write test           
89                 b.position(0);
90                 for (int i=0; i<c; i++) {
91                         b.writeInt(i^0x58);
92                 }
93
94                 // Read test
95                 b.position(0);
96                 for (int i=0; i<c; i++) {
97                         assertEquals(i^0x58, b.readInt());                      
98                 }
99                 
100                 // Fail test (cannot read more)
101                 try {
102                         b.readInt();
103                         fail("Did not catch boundary exceed");
104                 } catch (IndexOutOfBoundsException e) {}
105                 
106                 // Write once, enlarges the blob
107                 b.writeInt(50);
108                 assertEquals(c+1, b.length()/4);
109                 assertEquals(parentCount+1, b.getSource().length()/4);
110                 
111                 b.setLength(c*4);
112                 assertEquals(c, b.length()/4);
113                 assertEquals(parentCount, b.getSource().length()/4);
114                 
115         }
116         
117         
118         public void testInsertRemove(Blob b) throws Exception {         
119                 int c = (int) b.length()/4;
120                 
121                 // Write test           
122                 b.position(0);
123                 for (int i=0; i<c; i++) {
124                         b.writeInt(i^0x53);
125                 }
126                 
127                 // Remove 2. index
128                 b.position(4);
129                 b.removeBytes(4, ByteSide.Left);
130
131                 // Read test
132                 b.position(0);
133                 assertEquals(0x53, b.readInt());                        
134                 for (int i=2; i<c; i++) {
135                         assertEquals(i^0x53, b.readInt());                      
136                 }
137                 
138                 // Insert 2. index
139                 b.position(4);
140                 b.insertBytes(4, ByteSide.Left);
141                 b.writeInt(1^0x53);
142                 // Read test
143                 b.position(0);
144                 for (int i=0; i<c; i++) {
145                         assertEquals(i^0x53, b.readInt());                      
146                 }
147         }
148 /*      
149         public @Test void testMove1Gdata() throws IOException {
150                 
151                 File file = File.createTempFile("TestBlob", ".tmp");
152                 file.deleteOnExit();
153                 BinaryFile b = new BinaryFile(file);
154                 try {
155                 
156                         System.out.println("Creating 1GB file.");
157                         for (int i=0; i<1*1024*1024*1024/4; i++) {
158                                 b.writeInt(i);
159                         }
160                         
161                         System.out.println("Adding 64 bytes at position 0");
162                         long time = System.nanoTime();
163                         b.position(0l);
164                         b.insertBytes(64L, ByteSide.Left);
165                         long elapsed = System.nanoTime() - time;
166                         System.out.println("Elapsed: "+elapsed+" ns");
167
168                 } finally {
169                         b.close();
170                         file.delete();
171                 }
172         }
173 */
174
175 }
176