aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/serialization/SerializationHelperTestCase.java
blob: 6b603077b1efef248ee426202e731e91aeca99a8 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.serialization;

import com.yahoo.document.datatypes.Raw;
import com.yahoo.io.GrowableByteBuffer;
import com.yahoo.text.Utf8;
import com.yahoo.text.Utf8Array;
import com.yahoo.vespa.objects.BufferSerializer;
import org.junit.Test;

import java.nio.ByteBuffer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * @author Einar M R Rosenvinge
 */
public class SerializationHelperTestCase {

    @Test
    public void testGetNullTerminatedString() {
        //This is a test.0ab
        byte[] test = {0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x0,
                0x61, 0x62};

        BufferSerializer data = BufferSerializer.wrap(test);

        assertTrue(data.position() == 0);

        Utf8Array thisIsATest = VespaDocumentDeserializer6.parseNullTerminatedString(data.getBuf().getByteBuffer());

        assertTrue(thisIsATest.equals(new Utf8Array(Utf8.toBytes("This is a test."))));
        assertTrue(data.position() == 16);
        assertTrue(test[16] == 0x61); //a

        data.position(0);

        assertTrue(data.position() == 0);

        Utf8Array thisIsATestAgain = VespaDocumentDeserializer6.parseNullTerminatedString(data.getBuf().getByteBuffer(), 15);

        assertTrue(thisIsATestAgain.equals(new Utf8Array(Utf8.toBytes("This is a test."))));
        assertTrue(data.position() == 16);
        assertTrue(test[16] == 0x61); //a
    }

    @Test
    public void testSerializeRawField() {
        GrowableByteBuffer gbuf = new GrowableByteBuffer();
        ByteBuffer rawValue = ByteBuffer.wrap(Utf8.toBytes("0123456789"));
        rawValue.position(7);
        Raw value = new Raw(rawValue);
        value.serialize(gbuf);

        assertEquals(7, gbuf.position());
        assertEquals(7, rawValue.position());

        value = new Raw(rawValue);
        value.serialize(gbuf);

        assertEquals(14, gbuf.position());
        assertEquals(7, rawValue.position());
    }

}