1 | package de.uka.ipd.sdq.sensorframework.storage.tests; |
2 | |
3 | import java.io.File; |
4 | import java.io.IOException; |
5 | import java.io.RandomAccessFile; |
6 | |
7 | import junit.framework.Assert; |
8 | import junit.framework.TestCase; |
9 | import de.uka.ipd.sdq.sensorframework.storage.lists.DoubleSerialiser; |
10 | |
11 | public class DoubleSerialiserTest extends TestCase { |
12 | |
13 | public void testDoubleSerialiser() throws IOException { |
14 | File tempFile = File.createTempFile("TestDB", "lst"); |
15 | RandomAccessFile raf = new RandomAccessFile(tempFile.getAbsolutePath(),"rw"); |
16 | int count = 1000000; |
17 | double[] d = new double[count]; |
18 | for (int i = 0; i < count; i++) |
19 | d[i] = Math.random()*1000.0-500.0; |
20 | DoubleSerialiser ds = new DoubleSerialiser(); |
21 | byte[] bytes = ds.serialise(toDoubleArray(d),d.length); |
22 | raf.write(bytes); |
23 | raf.seek(0); |
24 | raf.read(bytes); |
25 | raf.close(); |
26 | Assert.assertTrue(bytes.length == ds.getElementLength() * count); |
27 | Double[] doubles = (Double[]) ds.deserialise(bytes); |
28 | for (int i=0; i < count; i++) |
29 | Assert.assertEquals(d[i],doubles[i]); |
30 | } |
31 | |
32 | private Double[] toDoubleArray(double[] d){ |
33 | Double[] result = new Double[d.length]; |
34 | for (int i = 0; i < d.length; i++){ |
35 | result[i] = d[i]; |
36 | } |
37 | return result; |
38 | } |
39 | |
40 | } |