aboutsummaryrefslogtreecommitdiffstats
path: root/src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java
blob: c06dbe8acfdfab7e7a5150b2254dabbccf5484f7 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
 * Copyright 2014 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */
package com.jogamp.common.nio;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

import org.junit.Assert;
import org.junit.Test;

import com.jogamp.junit.util.JunitTracer;

import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;

/**
 * Testing {@link MappedByteBufferInputStream} and {@link MappedByteBufferOutputStream} editing functionality.
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestByteBufferOutputStream extends JunitTracer {

    static void testImpl(final String fname,
                         final byte[] payLoad, final long payLoadOffset, final long postPayLoadFiller,
                         final byte[] endBytes,
                         final int sliceShift)
            throws IOException
    {
        final File file = new File(fname);
        file.delete();
        file.createNewFile();
        file.deleteOnExit();
        final RandomAccessFile out = new RandomAccessFile(file, "rw");
        final MappedByteBufferInputStream.FileResizeOp szOp = new MappedByteBufferInputStream.FileResizeOp() {
            @Override
            public void setLength(final long newSize) throws IOException {
                out.setLength(newSize);
            }
        };
        final MappedByteBufferInputStream mis = new MappedByteBufferInputStream(out.getChannel(),
                                                                                FileChannel.MapMode.READ_WRITE,
                                                                                MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT,
                                                                                sliceShift);
        final MappedByteBufferOutputStream mos = mis.getOutputStream(szOp);

        // resize to payLoad start and position to it
        mos.setLength(payLoadOffset);
        Assert.assertEquals(payLoadOffset, out.length());
        Assert.assertEquals(payLoadOffset, mos.length());
        Assert.assertEquals(0, mos.position()); // no change
        mos.position(payLoadOffset);
        Assert.assertEquals(payLoadOffset, mos.position());

        // mark, write-expand payLoad
        mis.mark(1);
        mos.write(payLoad);
        Assert.assertEquals(payLoadOffset+payLoad.length, out.length());
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.length());
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());

        // expand + 1
        mos.setLength(payLoadOffset+payLoad.length+1);
        Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length());
        Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length());
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); // no change

        // expand up-to very end, ahead of write - position to endBytes start
        mos.setLength(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length);
        Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, out.length());
        Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.length());
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); // no change
        mos.skip(postPayLoadFiller);
        Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller, mos.position());

        // write endBytes (no resize)
        mos.write(endBytes);
        Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.position());

        // Reset to payLoad, read it and verify
        mis.reset();
        Assert.assertEquals(payLoadOffset, mos.position());
        Assert.assertEquals(payLoadOffset, mis.position());
        final byte[] tmp = new byte[payLoad.length];
        Assert.assertEquals(payLoad.length, mis.read(tmp));
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
        Assert.assertEquals(payLoadOffset+payLoad.length, mis.position());
        Assert.assertArrayEquals(payLoad, tmp);

        // Shrink to end of payLoad, mark, read >= 0, reset .. redo
        Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, out.length());
        Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.length());
        mos.setLength(payLoadOffset+payLoad.length+1);
        Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length());
        Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length());
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
        mis.mark(1);
        Assert.assertTrue(mis.read()>=0);
        Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position());
        mis.reset();
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
        Assert.assertTrue(mis.read()>=0);
        Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position());

        // Shrink -1, read EOS
        mos.setLength(payLoadOffset+payLoad.length);
        Assert.assertEquals(payLoadOffset+payLoad.length, out.length());
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.length());
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
        Assert.assertEquals(-1, mis.read());

        // Expand + 1, mark, read >= 0, reset .. redo
        mos.setLength(payLoadOffset+payLoad.length+1);
        Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length());
        Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length());
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
        mis.mark(1);
        Assert.assertTrue(mis.read()>=0);
        Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position());
        mis.reset();
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
        Assert.assertTrue(mis.read()>=0);
        Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position());

        // Shrink -1, read EOS, write-expand, reset and verify
        mos.setLength(payLoadOffset+payLoad.length);
        Assert.assertEquals(payLoadOffset+payLoad.length, out.length());
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.length());
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
        Assert.assertEquals(-1, mis.read());
        mos.write('Z'); // expand while writing ..
        Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length());
        Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length());
        Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position());
        mis.reset();
        Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
        Assert.assertEquals(payLoadOffset+payLoad.length, mis.position());
        Assert.assertEquals('Z', mis.read());
        Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position());
        Assert.assertEquals(payLoadOffset+payLoad.length+1, mis.position());

        // Shrink -2, shall clear mark, test reset failure
        mos.setLength(payLoadOffset+payLoad.length-1);
        Assert.assertEquals(payLoadOffset+payLoad.length-1, out.length());
        Assert.assertEquals(payLoadOffset+payLoad.length-1, mos.length());
        Assert.assertEquals(payLoadOffset+payLoad.length-1, mos.position());
        try {
            mis.reset();
            Assert.assertTrue(false); // shall not reach
        } catch( final IOException ioe ) {
            Assert.assertNotNull(ioe);
        }
        mis.mark(1);

        // ZERO file, test reset failure, read EOS, write-expand
        mos.setLength(0);
        Assert.assertEquals(0, out.length());
        Assert.assertEquals(0, mos.length());
        Assert.assertEquals(0, mos.position());
        try {
            mis.reset();
            Assert.assertTrue(false); // shall not reach
        } catch( final IOException ioe ) {
            Assert.assertNotNull(ioe);
        }
        Assert.assertEquals(-1, mis.read());
        mos.write('Z'); // expand while writing ..
        Assert.assertEquals(1, out.length());
        Assert.assertEquals(1, mos.length());
        Assert.assertEquals(1, mos.position());
        mis.position(0);
        Assert.assertEquals(0, mos.position());
        Assert.assertEquals(0, mis.position());
        Assert.assertEquals('Z', mis.read());

        mos.close();
        mis.close();
        out.close();
        file.delete();
    }

    @Test
    public void test00() throws IOException {
        final int sliceShift = 13; // 8192 bytes per slice
        testImpl("./test01.bin", "123456789AB".getBytes(), 0L, 0L, "EOF".getBytes(), sliceShift);
    }

    @Test
    public void test01() throws IOException {
        final int sliceShift = 13; // 8192 bytes per slice
        testImpl("./test01.bin", "123456789AB".getBytes(), 9000L, 100L, "EOF".getBytes(), sliceShift);
    }

    @Test
    public void test02() throws IOException {
        final int sliceShift = 13; // 8192 bytes per slice
        testImpl("./test01.bin", "123456789AB".getBytes(), 8189L, 9001L, "EOF".getBytes(), sliceShift);
    }

    @Test
    public void test03() throws IOException {
        final int sliceShift = 13; // 8192 bytes per slice
        testImpl("./test01.bin", "123456789AB".getBytes(), 58189L, 109001L, "EOF".getBytes(), sliceShift);
    }

    @Test
    public void test10() throws IOException {
        final int sliceShift = 10; // 1024 bytes per slice
        final byte[] payLoad = new byte[4096];
        for(int i=0; i<payLoad.length; i++) {
            payLoad[i] = (byte)('A' + i%26);
        }
        testImpl("./test01.bin", payLoad, 0L, 0L, "EOF".getBytes(), sliceShift);
    }

    @Test
    public void test11() throws IOException {
        final int sliceShift = 10; // 1024 bytes per slice
        final byte[] payLoad = new byte[4096];
        for(int i=0; i<payLoad.length; i++) {
            payLoad[i] = (byte)('A' + i%26);
        }
        testImpl("./test01.bin", payLoad, 1030L, 99L, "EOF".getBytes(), sliceShift);
    }

    @Test
    public void test12() throws IOException {
        final int sliceShift = 10; // 1024 bytes per slice
        final byte[] payLoad = new byte[4096];
        for(int i=0; i<payLoad.length; i++) {
            payLoad[i] = (byte)('A' + i%26);
        }
        testImpl("./test01.bin", payLoad, 1021L, 1301L, "EOF".getBytes(), sliceShift);
    }

    @Test
    public void test13() throws IOException {
        final int sliceShift = 10; // 1024 bytes per slice
        final byte[] payLoad = new byte[4096];
        for(int i=0; i<payLoad.length; i++) {
            payLoad[i] = (byte)('A' + i%26);
        }
        testImpl("./test01.bin", payLoad, 3021L, 6301L, "EOF".getBytes(), sliceShift);
    }

    public static void main(final String args[]) throws IOException {
        final String tstname = TestByteBufferOutputStream.class.getName();
        org.junit.runner.JUnitCore.main(tstname);
    }
}