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

import static org.junit.Assert.*;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

/**
 * Completeness check for GenericWriter.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class GenericWriterTestCase {
    private static class MockWriter extends GenericWriter {
        private StringBuilder written = new StringBuilder();

        @Override
        public void write(char[] cbuf, int off, int len) throws IOException {
            written.append(String.copyValueOf(cbuf, off, len));
        }

        @Override
        public void flush() throws IOException {
        }

        @Override
        public void close() throws IOException {
        }
    }

    MockWriter mock;

    @Before
    public void setUp() throws Exception {
        mock = new MockWriter();
    }

    @Test
    public final void testWriteInt() throws Exception {
        mock.write(0xa);
        assertEquals("\n", mock.written.toString());
    }

    @Test
    public final void testWriteChar() throws IOException {
        mock.write('\u0020');
        assertEquals(" ", mock.written.toString());
    }

    @Test
    public final void testWriteCharSequence() throws IOException {
        mock.write((CharSequence) "abc");
        assertEquals("abc", mock.written.toString());
    }

    @Test
    public final void testWriteString() throws IOException {
        mock.write("abc");
        assertEquals("abc", mock.written.toString());
    }

    @Test
    public final void testWriteLong() throws IOException {
        mock.write(42L);
        assertEquals("42", mock.written.toString());
    }

    @Test
    public final void testWriteShort() throws IOException {
        mock.write((short) 42);
        assertEquals("42", mock.written.toString());
    }

    @Test
    public final void testWriteByte() throws IOException {
        mock.write((byte) 42);
        assertEquals("42", mock.written.toString());
    }

    @Test
    public final void testWriteDouble() throws IOException {
        mock.write(0.0d);
        assertEquals("0.0", mock.written.toString());
    }

    @Test
    public final void testWriteFloat() throws IOException {
        mock.write(0.0f);
        assertEquals("0.0", mock.written.toString());
    }

    @Test
    public final void testWriteBoolean() throws IOException {
        mock.write(true);
        assertEquals("true", mock.written.toString());
    }

    @Test
    public final void testWriteAbstractUtf8Array() throws IOException {
        mock.write(new Utf8Array(Utf8.toBytes("abc")));
        assertEquals("abc", mock.written.toString());
    }

}