aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_LineMapTest.java
blob: 1c2d8d7f65a211d2308ad7626061921375f06e8b (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
80
81
82
/*
 * Copyright (C) 2008-2009, Google Inc. and others
 *
 * 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.util;

import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

import org.eclipse.jgit.errors.BinaryBlobException;
import org.junit.Test;

public class RawParseUtils_LineMapTest {

	@Test
	public void testEmpty() throws Exception {
		final IntList map = RawParseUtils.lineMap(new byte[] {}, 0, 0);
		assertNotNull(map);
		assertArrayEquals(new int[]{Integer.MIN_VALUE, 0}, asInts(map));
	}

	@Test
	public void testOneBlankLine() throws Exception  {
		final IntList map = RawParseUtils.lineMap(new byte[] { '\n' }, 0, 1);
		assertArrayEquals(new int[]{Integer.MIN_VALUE, 0, 1}, asInts(map));
	}

	@Test
	public void testTwoLineFooBar() {
		final byte[] buf = "foo\nbar\n".getBytes(ISO_8859_1);
		final IntList map = RawParseUtils.lineMap(buf, 0, buf.length);
		assertArrayEquals(new int[]{Integer.MIN_VALUE, 0, 4, buf.length}, asInts(map));
	}

	@Test
	public void testTwoLineNoLF() {
		final byte[] buf = "foo\nbar".getBytes(ISO_8859_1);
		final IntList map = RawParseUtils.lineMap(buf, 0, buf.length);
		assertArrayEquals(new int[]{Integer.MIN_VALUE, 0, 4, buf.length}, asInts(map));
	}

	@Test
	public void testNulByte() {
		final byte[] buf = "xxxfoo\nb\0ar".getBytes(ISO_8859_1);
		final IntList map = RawParseUtils.lineMap(buf, 3, buf.length);
		assertArrayEquals(new int[] { Integer.MIN_VALUE, 3, 7, buf.length },
				asInts(map));
	}

	@Test
	public void testLineMapOrBinary() throws Exception {
		final byte[] buf = "xxxfoo\nb\0ar".getBytes(ISO_8859_1);
		assertThrows(BinaryBlobException.class,
				() -> RawParseUtils.lineMapOrBinary(buf, 3, buf.length));
	}

	@Test
	public void testFourLineBlanks() {
		final byte[] buf = "foo\n\n\nbar\n".getBytes(ISO_8859_1);
		final IntList map = RawParseUtils.lineMap(buf, 0, buf.length);

		assertArrayEquals(new int[]{
				Integer.MIN_VALUE, 0, 4, 5, 6, buf.length
		}, asInts(map));
	}

	private int[] asInts(IntList l) {
		int[] result = new int[l.size()];
		for (int i = 0; i < l.size(); i++) {
			result[i] = l.get(i);
		}
		return result;
	}
}