summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/io/BlobTestCase.java
blob: 5b03c63de5440ca058839795f3511302950a3d23 (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;

import java.nio.ByteBuffer;

public class BlobTestCase extends junit.framework.TestCase {

    public void testEmpty() {
        Blob empty = new Blob();
        assertTrue(empty.get() != null);
        assertEquals(0, empty.get().length);
    }

    public void testCopyArray() {
        byte[] d = { 1, 2, 3 };
        Blob b = new Blob(d);
        d[0] = 7;
        d[1] = 8;
        d[2] = 9;
        assertEquals(3, b.get().length);
        assertEquals(1, b.get()[0]);
        assertEquals(2, b.get()[1]);
        assertEquals(3, b.get()[2]);
    }

    public void testCopyArraySubset() {
        byte[] d = { 1, 2, 3 };
        Blob b = new Blob(d, 1, 1);
        d[0] = 7;
        d[1] = 8;
        d[2] = 9;
        assertEquals(1, b.get().length);
        assertEquals(2, b.get()[0]);
    }

    public void testCopyBlob() {
        byte[] d = { 1, 2, 3 };
        Blob b = new Blob(d);
        Blob x = new Blob(b);
        b.get()[1] = 4;
        assertEquals(3, x.get().length);
        assertEquals(1, x.get()[0]);
        assertEquals(4, b.get()[1]);
        assertEquals(2, x.get()[1]);
        assertEquals(3, x.get()[2]);
    }

    public void testReadBuffer() {
        ByteBuffer buf = ByteBuffer.allocate(100);
        buf.put((byte)1);
        buf.put((byte)2);
        buf.put((byte)3);
        buf.flip();
        assertEquals(3, buf.remaining());
        Blob b = new Blob(buf);
        assertEquals(0, buf.remaining());
        assertEquals(3, b.get().length);
        assertEquals(1, b.get()[0]);
        assertEquals(2, b.get()[1]);
        assertEquals(3, b.get()[2]);
    }

    public void testReadPartialBuffer() {
        ByteBuffer buf = ByteBuffer.allocate(100);
        buf.put((byte)1);
        buf.put((byte)2);
        buf.put((byte)3);
        buf.put((byte)4);
        buf.put((byte)5);
        buf.flip();
        assertEquals(5, buf.remaining());
        Blob b = new Blob(buf, 3);
        assertEquals(2, buf.remaining());
        assertEquals(3, b.get().length);
        assertEquals(1, b.get()[0]);
        assertEquals(2, b.get()[1]);
        assertEquals(3, b.get()[2]);
        assertEquals(4, buf.get());
        assertEquals(5, buf.get());
        assertEquals(0, buf.remaining());
    }

    public void testWriteBuffer() {
        byte[] d = { 1, 2, 3 };
        Blob b = new Blob(d);
        ByteBuffer buf = ByteBuffer.allocate(100);
        b.write(buf);
        buf.flip();
        assertEquals(3, buf.remaining());
        assertEquals(1, buf.get());
        assertEquals(2, buf.get());
        assertEquals(3, buf.get());
        assertEquals(0, buf.remaining());
    }
}