aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/test/java/com/yahoo/searchlib/expression/IntegerResultNodeTestCase.java
blob: 744df472fb2974e5b07f74e43288a69887c535da (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
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.expression;

import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;

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

/**
 * @author Ulf Lilleengen
 * @since 5.1
 */
public class IntegerResultNodeTestCase extends ResultNodeTest {

    List<NumericResultNode> getResultNodes(long startvalue) {
        return Arrays.asList(new Int8ResultNode((byte)startvalue),
                             new Int16ResultNode((short)startvalue),
                             new Int32ResultNode((int)startvalue),
                             new IntegerResultNode(startvalue));
    }

    @Test
    public void testClassId() {
        assertEquals(Int8ResultNode.classId, new Int8ResultNode().getClassId());
        assertEquals(Int16ResultNode.classId, new Int16ResultNode().getClassId());
        assertEquals(Int32ResultNode.classId, new Int32ResultNode().getClassId());
        assertEquals(IntegerResultNode.classId, new IntegerResultNode().getClassId());
        assertEquals(BoolResultNode.classId, new BoolResultNode().getClassId());
    }

    @Test
    public void testTypeConversion() {
        for (NumericResultNode node : getResultNodes(3)) {
            assertEquals(3, node.getInteger());
            assertEquals(3.0, node.getFloat(), 0.01);
            assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, (byte) 3}, node.getRaw());
            assertEquals("3", node.getString());
            assertEquals("3", node.getNumber().toString());
        }
    }

    @Test
    public void testMath() {
        for (NumericResultNode node : getResultNodes(5)) {
            assertEquals(5, node.getInteger());
            node.negate();
            assertEquals(-5, node.getInteger());
            node.multiply(new Int32ResultNode(3));
            assertEquals(-15, node.getInteger());
            node.add(new Int32ResultNode(1));
            assertEquals(-14, node.getInteger());
            node.divide(new Int32ResultNode(2));
            assertEquals(-7, node.getInteger());
            node.modulo(new Int32ResultNode(3));
            assertEquals(-1, node.getInteger());
            node.min(new Int32ResultNode(2));
            assertEquals(-1, node.getInteger());
            node.min(new Int32ResultNode(-2));
            assertEquals(-2, node.getInteger());
            node.max(new Int32ResultNode(-4));
            assertEquals(-2, node.getInteger());
            node.max(new Int32ResultNode(4));
            assertEquals(4, node.getInteger());
            assertEquals(1, node.onCmp(new Int32ResultNode(3)));
            assertEquals(0, node.onCmp(new Int32ResultNode(4)));
            assertEquals(-1, node.onCmp(new Int32ResultNode(5)));
            node.set(new Int32ResultNode(8));
            assertEquals(8, node.getInteger());
            assertEquals(8 + node.getClassId(), node.hashCode());
            assertTrue(dumpNode(node).contains("value: 8"));
        }
    }

    @Test
    public void testBool() {
        BoolResultNode node = new BoolResultNode();
        assertEquals(0, node.getInteger());
        assertEquals(0.0, node.getFloat(), 0.000000000001);
        assertEquals("false", node.getString());
        node.setValue(true);
        assertEquals(1, node.getInteger());
        assertEquals(1.0, node.getFloat(), 0.000000000001);
        assertEquals("true", node.getString());
    }

    @Test
    public void testInt8() {
        Int8ResultNode node = new Int8ResultNode();
        node.setValue((byte) 5);
        assertEquals(5, node.getInteger());
    }

    @Test
    public void testInt16() {
        Int16ResultNode node = new Int16ResultNode();
        node.setValue((short)5);
        assertEquals(5, node.getInteger());
    }

    @Test
    public void testInt32() {
        Int32ResultNode node = new Int32ResultNode();
        node.setValue(5);
        assertEquals(5, node.getInteger());
    }

    @Test
    public void testLong() {
        IntegerResultNode node = new IntegerResultNode();
        node.setValue(5);
        assertEquals(5, node.getInteger());
    }

    @Test
    public void testSerialization() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        for (NumericResultNode node : getResultNodes(8)) {
            assertEquals(8, node.getInteger());
            NumericResultNode out = node.getClass().getConstructor().newInstance();
            assertCorrectSerialization(node, out);
            assertEquals(out.getInteger(), node.getInteger());
        }
    }
}