aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/UInt24ArrayTest.java
blob: a96c217e4fdd162a5fc45b0e3795f4b5d3cf2da3 (plain)
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
/*
 * Copyright (C) 2023, Google LLC
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.internal.storage.file;

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

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

public class UInt24ArrayTest {

	private static final byte[] DATA = { 0x00, 0x00, 0x00, // 0
			0x00, 0x00, 0x05, // 5
			0x00, 0x00, 0x0a, // 10
			0x00, 0x00, 0x0f, // 15
			0x00, 0x00, 0x14, // 20
			0x00, 0x00, 0x19, // 25
			(byte) 0xff, 0x00, 0x00, // Uint with MSB=1
			(byte) 0xff, (byte) 0xff, (byte) 0xff, // MAX
	};

	private static final UInt24Array asArray = new UInt24Array(DATA);

	@Test
	public void uInt24Array_size() {
		assertEquals(8, asArray.size());
	}

	@Test
	public void uInt24Array_get() {
		assertEquals(0, asArray.get(0));
		assertEquals(5, asArray.get(1));
		assertEquals(10, asArray.get(2));
		assertEquals(15, asArray.get(3));
		assertEquals(20, asArray.get(4));
		assertEquals(25, asArray.get(5));
		assertEquals(0xff0000, asArray.get(6));
		assertEquals(0xffffff, asArray.get(7));
		assertThrows(IndexOutOfBoundsException.class, () -> asArray.get(9));
	}

	@Test
	public void uInt24Array_getLastValue() {
		assertEquals(0xffffff, asArray.getLastValue());
	}

	@Test
	public void uInt24Array_find() {
		assertEquals(0, asArray.binarySearch(0));
		assertEquals(1, asArray.binarySearch(5));
		assertEquals(2, asArray.binarySearch(10));
		assertEquals(3, asArray.binarySearch(15));
		assertEquals(4, asArray.binarySearch(20));
		assertEquals(5, asArray.binarySearch(25));
		assertEquals(6, asArray.binarySearch(0xff0000));
		assertEquals(7, asArray.binarySearch(0xffffff));
		assertThrows(IllegalArgumentException.class,
				() -> asArray.binarySearch(Integer.MAX_VALUE));
	}

	@Test
	public void uInt24Array_empty() {
		Assert.assertTrue(UInt24Array.EMPTY.isEmpty());
		assertEquals(0, UInt24Array.EMPTY.size());
		assertEquals(-1, UInt24Array.EMPTY.binarySearch(1));
		assertThrows(IndexOutOfBoundsException.class,
				() -> UInt24Array.EMPTY.getLastValue());
		assertThrows(IndexOutOfBoundsException.class,
				() -> UInt24Array.EMPTY.get(0));
	}
}