You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RawParseUtils_LineMapTest.java 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.util;
  11. import static java.nio.charset.StandardCharsets.ISO_8859_1;
  12. import static org.junit.Assert.assertArrayEquals;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertThrows;
  15. import org.eclipse.jgit.errors.BinaryBlobException;
  16. import org.junit.Test;
  17. public class RawParseUtils_LineMapTest {
  18. @Test
  19. public void testEmpty() throws Exception {
  20. final IntList map = RawParseUtils.lineMap(new byte[] {}, 0, 0);
  21. assertNotNull(map);
  22. assertArrayEquals(new int[]{Integer.MIN_VALUE, 0}, asInts(map));
  23. }
  24. @Test
  25. public void testOneBlankLine() throws Exception {
  26. final IntList map = RawParseUtils.lineMap(new byte[] { '\n' }, 0, 1);
  27. assertArrayEquals(new int[]{Integer.MIN_VALUE, 0, 1}, asInts(map));
  28. }
  29. @Test
  30. public void testTwoLineFooBar() {
  31. final byte[] buf = "foo\nbar\n".getBytes(ISO_8859_1);
  32. final IntList map = RawParseUtils.lineMap(buf, 0, buf.length);
  33. assertArrayEquals(new int[]{Integer.MIN_VALUE, 0, 4, buf.length}, asInts(map));
  34. }
  35. @Test
  36. public void testTwoLineNoLF() {
  37. final byte[] buf = "foo\nbar".getBytes(ISO_8859_1);
  38. final IntList map = RawParseUtils.lineMap(buf, 0, buf.length);
  39. assertArrayEquals(new int[]{Integer.MIN_VALUE, 0, 4, buf.length}, asInts(map));
  40. }
  41. @Test
  42. public void testNulByte() {
  43. final byte[] buf = "xxxfoo\nb\0ar".getBytes(ISO_8859_1);
  44. final IntList map = RawParseUtils.lineMap(buf, 3, buf.length);
  45. assertArrayEquals(new int[] { Integer.MIN_VALUE, 3, 7, buf.length },
  46. asInts(map));
  47. }
  48. @Test
  49. public void testLineMapOrBinary() throws Exception {
  50. final byte[] buf = "xxxfoo\nb\0ar".getBytes(ISO_8859_1);
  51. assertThrows(BinaryBlobException.class,
  52. () -> RawParseUtils.lineMapOrBinary(buf, 3, buf.length));
  53. }
  54. @Test
  55. public void testFourLineBlanks() {
  56. final byte[] buf = "foo\n\n\nbar\n".getBytes(ISO_8859_1);
  57. final IntList map = RawParseUtils.lineMap(buf, 0, buf.length);
  58. assertArrayEquals(new int[]{
  59. Integer.MIN_VALUE, 0, 4, 5, 6, buf.length
  60. }, asInts(map));
  61. }
  62. private int[] asInts(IntList l) {
  63. int[] result = new int[l.size()];
  64. for (int i = 0; i < l.size(); i++) {
  65. result[i] = l.get(i);
  66. }
  67. return result;
  68. }
  69. }