aboutsummaryrefslogtreecommitdiff
path: root/test/org/gnunet/construct/FixedSizeTest.java
blob: 573e120cb064876279a2ccb676c1e006ed3158fc (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
package org.gnunet.construct;

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

/**
 * ...
 *
 * @author Florian Dold
 */
public class FixedSizeTest {

    public static class Msg implements Message {
        @UInt8
        public int v;

        public Msg() {
            // default ctor required by Construct
        }

        public Msg(int v) {
            this.v = v;
        }
    }

    public static class FixedSizeTestMessage implements Message {
        @FixedSizeArray(length = 4)
        public Msg[] msgs;
    }


    @Test
    public void test_fixedNested() {
        FixedSizeTestMessage m = new FixedSizeTestMessage();
        m.msgs = new Msg[]{new Msg(1), new Msg(2), new Msg(3), new Msg(4)};
        byte[] bytes = Construct.toBinary(m);

        FixedSizeTestMessage m2 = Construct.parseAs(bytes, FixedSizeTestMessage.class);

        Assert.assertEquals(m.msgs[0].v, m2.msgs[0].v);
        Assert.assertEquals(m.msgs[1].v, m2.msgs[1].v);
        Assert.assertEquals(m.msgs[2].v, m2.msgs[2].v);
        Assert.assertEquals(m.msgs[3].v, m2.msgs[3].v);
    }

    @Test(expected = AssertionError.class)
    public void test_sizeMismatch() {
        FixedSizeTestMessage m = new FixedSizeTestMessage();
        m.msgs = new Msg[]{new Msg(1), new Msg(2)};
        byte[] bytes = Construct.toBinary(m);
    }
}