aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/android/tools/r8/utils/ArrayUtilsTest.java
blob: c44d1cf9eac876487f47512652b03e2c65f07057 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.utils;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import it.unimi.dsi.fastutil.ints.Int2IntArrayMap;
import java.util.Map;
import org.junit.Test;

public class ArrayUtilsTest {

  @Test
  public void testCopyWithSparseChanges_identical() throws Exception {
    int size = 3;
    Integer[] input = new Integer[size];
    for (int i = 0; i < size; i++) {
      input[i] = i;
    }
    Integer[] output =
        ArrayUtils.copyWithSparseChanges(Integer[].class, input, new Int2IntArrayMap());
    assertNotEquals(input, output);
    for (int i = 0; i < size; i++) {
      assertTrue(i == output[i]);
    }
  }

  @Test
  public void testCopyWithSparseChanges_oneChangeAtBeginning() throws Exception {
    int size = 3;
    Integer[] input = new Integer[size];
    for (int i = 0; i < size; i++) {
      input[i] = i;
    }
    Map<Integer, Integer> changes = new Int2IntArrayMap();
    changes.put(0, size);
    Integer[] output = ArrayUtils.copyWithSparseChanges(Integer[].class, input, changes);
    assertNotEquals(input, output);
    assertTrue(size == output[0]);
    for (int i = 1; i < size; i++) {
      assertTrue(i == output[i]);
    }
  }

  @Test
  public void testCopyWithSparseChanges_oneChangeInMiddle() throws Exception {
    int size = 3;
    Integer[] input = new Integer[size];
    for (int i = 0; i < size; i++) {
      input[i] = i;
    }
    Map<Integer, Integer> changes = new Int2IntArrayMap();
    changes.put(1, size);
    Integer[] output = ArrayUtils.copyWithSparseChanges(Integer[].class, input, changes);
    assertNotEquals(input, output);
    assertTrue(size == output[1]);
    for (int i = 0; i < size; i++) {
      if (i == 1) {
        continue;
      }
      assertTrue(i == output[i]);
    }
  }

  @Test
  public void testCopyWithSparseChanges_oneChangeAtEnd() throws Exception {
    int size = 3;
    Integer[] input = new Integer[size];
    for (int i = 0; i < size; i++) {
      input[i] = i;
    }
    Map<Integer, Integer> changes = new Int2IntArrayMap();
    changes.put(2, size);
    Integer[] output = ArrayUtils.copyWithSparseChanges(Integer[].class, input, changes);
    assertNotEquals(input, output);
    assertTrue(size == output[2]);
    for (int i = 0; i < size - 1; i++) {
      assertTrue(i == output[i]);
    }
  }

  @Test
  public void testCopyWithSparseChanges_twoChangesAtEnds() throws Exception {
    int size = 3;
    Integer[] input = new Integer[size];
    for (int i = 0; i < size; i++) {
      input[i] = i;
    }
    Map<Integer, Integer> changes = new Int2IntArrayMap();
    changes.put(0, size);
    changes.put(2, size);
    Integer[] output = ArrayUtils.copyWithSparseChanges(Integer[].class, input, changes);
    assertNotEquals(input, output);
    assertTrue(size == output[0]);
    assertFalse(size == output[1]);
    assertTrue(size == output[2]);
  }

}