]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/TestBlob.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / TestBlob.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.tests;
13
14 import static org.junit.Assert.assertEquals;\r
15 import static org.junit.Assert.fail;\r
16 \r
17 import org.junit.Before;\r
18 import org.junit.Test;\r
19 import org.simantics.databoard.util.binary.BinaryMemory;\r
20 import org.simantics.databoard.util.binary.Blob;\r
21 import org.simantics.databoard.util.binary.RandomAccessBinary;\r
22 import org.simantics.databoard.util.binary.RandomAccessBinary.ByteSide;\r
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 /*      \r
149         public @Test void testMove1Gdata() throws IOException {\r
150                 \r
151                 File file = File.createTempFile("TestBlob", ".tmp");\r
152                 file.deleteOnExit();\r
153                 BinaryFile b = new BinaryFile(file);\r
154                 try {\r
155                 \r
156                         System.out.println("Creating 1GB file.");\r
157                         for (int i=0; i<1*1024*1024*1024/4; i++) {\r
158                                 b.writeInt(i);\r
159                         }\r
160                         \r
161                         System.out.println("Adding 64 bytes at position 0");\r
162                         long time = System.nanoTime();\r
163                         b.position(0l);\r
164                         b.insertBytes(64L, ByteSide.Left);\r
165                         long elapsed = System.nanoTime() - time;\r
166                         System.out.println("Elapsed: "+elapsed+" ns");\r
167 \r
168                 } finally {\r
169                         b.close();\r
170                         file.delete();\r
171                 }\r
172         }
173 */
174
175 }
176