1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.car.obd2.test;
18
19import static org.junit.Assert.*;
20
21import com.android.car.obd2.IntegerArrayStream;
22import org.junit.Test;
23
24/** Tests for IntegerArrayStream */
25public class IntegerArrayStreamTest {
26    private static int[] DATA_SET = new int[] {1, 2, 3, 4, 5, 6};
27
28    @Test
29    public void testPeekConsume() {
30        IntegerArrayStream stream = new IntegerArrayStream(DATA_SET);
31        assertEquals(1, stream.peek());
32        assertEquals(1, stream.consume());
33        assertEquals(2, stream.peek());
34        assertEquals(2, stream.consume());
35    }
36
37    @Test
38    public void testResidualLength() {
39        IntegerArrayStream stream = new IntegerArrayStream(DATA_SET);
40        assertEquals(DATA_SET.length, stream.residualLength());
41        stream.consume();
42        assertEquals(DATA_SET.length - 1, stream.residualLength());
43    }
44
45    @Test
46    public void testHasAtLeast() {
47        IntegerArrayStream stream = new IntegerArrayStream(DATA_SET);
48        assertTrue(stream.hasAtLeast(1));
49        assertTrue(stream.hasAtLeast(DATA_SET.length));
50        assertFalse(stream.hasAtLeast(100));
51        assertTrue(stream.hasAtLeast(1, theStream -> true));
52        assertFalse(stream.hasAtLeast(100, theStream -> true, theStream -> false));
53    }
54
55    @Test
56    public void testExpect() {
57        IntegerArrayStream stream = new IntegerArrayStream(DATA_SET);
58        assertTrue(stream.expect(1, 2, 3));
59        assertFalse(stream.expect(4, 6));
60        assertEquals(5, stream.peek());
61    }
62
63    @Test
64    public void testIsEmpty() {
65        IntegerArrayStream stream = new IntegerArrayStream(DATA_SET);
66        assertFalse(stream.isEmpty());
67        stream.expect(1, 2, 3, 4, 5);
68        assertFalse(stream.isEmpty());
69        stream.consume();
70        assertTrue(stream.isEmpty());
71    }
72}
73