File: BufTest.java

package info (click to toggle)
jcdf 1.2.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 572 kB
  • sloc: java: 5,315; makefile: 198; sh: 98
file content (159 lines) | stat: -rw-r--r-- 5,873 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package uk.ac.bristol.star.cdf.test;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import uk.ac.bristol.star.cdf.record.BankBuf;
import uk.ac.bristol.star.cdf.record.Buf;
import uk.ac.bristol.star.cdf.record.Pointer;
import uk.ac.bristol.star.cdf.record.SimpleNioBuf;

public class BufTest {

    private static boolean assertionsOn_;
    private final int blk_ = 54;
    private final int nn_ = 64;

    // Puts the various Buf implementations through their paces.
    public void testBufs() throws IOException {
        byte[] data = new byte[ 8 * 100 ];
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream( bout );
        for ( int i = 0; i < nn_; i++ ) {
            dout.writeByte( -i );
            dout.writeByte( i );
            dout.writeShort( -i );
            dout.writeShort( i );
            dout.writeInt( -i );
            dout.writeInt( i );
            dout.writeLong( -i );
            dout.writeLong( i );
            dout.writeFloat( -i );
            dout.writeFloat( i );
            dout.writeDouble( -i );
            dout.writeDouble( i );
        }
        dout.flush();
        dout.close();
        byte[] bytes = bout.toByteArray();
        int nbyte = bytes.length;
        assert nbyte == blk_ * nn_;

        boolean isBit64 = false;
        boolean isBigEndian = true;
        ByteBuffer buf1 = ByteBuffer.wrap( bytes );
        checkBuf( new SimpleNioBuf( buf1, isBit64, isBigEndian ) );
        checkBuf( BankBuf.createSingleBankBuf( buf1, isBit64, isBigEndian ) );
        checkBuf( BankBuf.createMultiBankBuf( new ByteBuffer[] { buf1 },
                                              isBit64, isBigEndian ) );

        int[] banksizes =
            { 23, blk_ - 1, blk_ + 1, 49, blk_ * 4, blk_ * 2 + 2 };
        List<ByteBuffer> bblist = new ArrayList<ByteBuffer>();
        int ioff = 0;
        int ibuf = 0;
        int nleft = nbyte;
        while ( nleft > 0 ) {
            int leng = Math.min( banksizes[ ibuf % banksizes.length ], nleft );
            byte[] bb = new byte[ leng ];
            System.arraycopy( bytes, ioff, bb, 0, leng );
            bblist.add( ByteBuffer.wrap( bb ) );
            ibuf++;
            ioff += leng;
            nleft -= leng;
        }
        ByteBuffer[] bbufs = bblist.toArray( new ByteBuffer[ 0 ] );
        assert bbufs.length > 6;
        checkBuf( BankBuf
                 .createMultiBankBuf( bbufs, isBit64, isBigEndian ) );

        File tmpFile = File.createTempFile( "data", ".bin" );
        tmpFile.deleteOnExit();
        FileOutputStream fout = new FileOutputStream( tmpFile );
        fout.write( bytes );
        fout.close();
        FileChannel inchan = new FileInputStream( tmpFile ).getChannel();
        int[] banksizes2 = new int[ banksizes.length + 2 ];
        System.arraycopy( banksizes, 0, banksizes2, 0, banksizes.length );
        banksizes2[ banksizes.length + 0 ] = nbyte;
        banksizes2[ banksizes.length + 1 ] = nbyte * 2;
        for ( int banksize : banksizes2 ) {
            checkBuf( BankBuf.createMultiBankBuf( inchan, nbyte, banksize,
                                                  isBit64, isBigEndian ) );
        }
        inchan.close();

        FileChannel copychan = new FileInputStream( tmpFile ).getChannel();
        assert copychan.size() == nbyte;
        ByteBuffer copybuf = ByteBuffer.allocate( nbyte );
        copychan.read( copybuf );
        copychan.close();
        assert copybuf.position() == nbyte;
        checkBuf( new SimpleNioBuf( copybuf, isBit64, isBigEndian ) );

        tmpFile.delete();
    }

    private void checkBuf( Buf buf ) throws IOException {
        assert buf.getLength() == nn_ * blk_;
        byte[] abytes = new byte[ 2 ];
        short[] ashorts = new short[ 2 ];
        int[] aints = new int[ 4 ];
        long[] alongs = new long[ 2 ];
        float[] afloats = new float[ 21 ];
        double[] adoubles = new double[ 2 ];
        for ( int i = 0; i < nn_; i++ ) {
            int ioff = i * blk_;
            buf.readDataBytes( ioff + 0, 2, abytes );
            buf.readDataShorts( ioff + 2, 2, ashorts );
            buf.readDataInts( ioff + 6, 2, aints );
            buf.readDataLongs( ioff + 14, 2, alongs );
            buf.readDataFloats( ioff + 30, 2, afloats );
            buf.readDataDoubles( ioff + 38, 2, adoubles );
            assert abytes[ 0 ] == -i;
            assert abytes[ 1 ] == i;
            assert ashorts[ 0 ] == -i;
            assert ashorts[ 1 ] == i;
            assert aints[ 0 ] == -i;
            assert aints[ 1 ] == i;
            assert alongs[ 0 ] == -i;
            assert alongs[ 1 ] == i;
            assert afloats[ 0 ] == -i;
            assert afloats[ 1 ] == i;
            assert adoubles[ 0 ] == -i;
            assert adoubles[ 1 ] == i;
        }
        Pointer p = new Pointer( 0 );
        assert buf.readUnsignedByte( p ) == 0;
        assert buf.readUnsignedByte( p ) == 0;
        p.set( blk_ );
        assert buf.readUnsignedByte( p ) == 255;
        assert buf.readUnsignedByte( p ) == 1;
    }

    private static boolean checkAssertions() {
        assertionsOn_ = true;
        return true;
    }

    private static void runTests() throws IOException {
        assert checkAssertions();
        if ( ! assertionsOn_ ) {
            throw new RuntimeException( "Assertions disabled - bit pointless" );
        }
        BufTest test = new BufTest();
        test.testBufs();
    }

    public static void main( String[] args ) throws IOException {
        runTests();
    }
}