• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.providers.media.util;
18 
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 
23 import androidx.test.runner.AndroidJUnit4;
24 
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 
28 @RunWith(AndroidJUnit4.class)
29 public class LongArrayTest {
30 
31     @Test
testLongArray()32     public void testLongArray() {
33         LongArray a = new LongArray();
34         a.add(1);
35         a.add(2);
36         a.add(3);
37         verify(new long[]{1, 2, 3}, a);
38 
39         LongArray b = LongArray.fromArray(new long[]{4, 5, 6, 7, 8}, 3);
40         a.addAll(b);
41         verify(new long[]{1, 2, 3, 4, 5, 6}, a);
42 
43         a.resize(2);
44         verify(new long[]{1, 2}, a);
45 
46         a.resize(8);
47         verify(new long[]{1, 2, 0, 0, 0, 0, 0, 0}, a);
48 
49         a.set(5, 10);
50         verify(new long[]{1, 2, 0, 0, 0, 10, 0, 0}, a);
51 
52         a.add(5, 20);
53         assertEquals(20, a.get(5));
54         assertEquals(5, a.indexOf(20));
55         verify(new long[]{1, 2, 0, 0, 0, 20, 10, 0, 0}, a);
56 
57         assertEquals(-1, a.indexOf(99));
58 
59         a.resize(15);
60         a.set(14, 30);
61         verify(new long[]{1, 2, 0, 0, 0, 20, 10, 0, 0, 0, 0, 0, 0, 0, 30}, a);
62 
63         long[] backingArray = new long[]{1, 2, 3, 4};
64         a = LongArray.wrap(backingArray);
65         a.set(0, 10);
66         assertEquals(10, backingArray[0]);
67         backingArray[1] = 20;
68         backingArray[2] = 30;
69         verify(backingArray, a);
70         assertEquals(2, a.indexOf(30));
71 
72         a.resize(2);
73         assertEquals(0, backingArray[2]);
74         assertEquals(0, backingArray[3]);
75 
76         a.add(50);
77         verify(new long[]{10, 20, 50}, a);
78 
79         LongArray cloned = a.clone();
80         verify(new long[]{10, 20, 50}, cloned);
81         assertTrue(LongArray.elementsEqual(a, cloned));
82 
83         a.remove(0);
84         verify(new long[]{20, 50}, a);
85 
86         a.clear();
87         verify(new long[]{}, a);
88     }
89 
verify(long[] expected, LongArray longArrays)90     public void verify(long[] expected, LongArray longArrays) {
91         assertEquals(expected.length, longArrays.size());
92         assertArrayEquals(expected, longArrays.toArray());
93     }
94 }
95