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.
116 lines
3.1 KiB
116 lines
3.1 KiB
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import android.util.SparseArray;
|
|
import com.google.common.collect.ImmutableSet;
|
|
import java.util.Collection;
|
|
import java.util.HashMap;
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
|
|
public class CollectionTest {
|
|
|
|
interface MyCollection<E> extends Collection<E> {}
|
|
|
|
void iterate_over_mycollection(MyCollection<Integer> list) {
|
|
for (int i = 0, size = list.size(); i < size; ++i) {}
|
|
}
|
|
|
|
void iterate_over_some_java_collection(
|
|
ConcurrentLinkedQueue<MyCollection<Integer>> mSubscribers) {
|
|
for (MyCollection<Integer> list : mSubscribers) {}
|
|
}
|
|
|
|
void iterate_over_mycollection_quad(ConcurrentLinkedQueue<MyCollection<Integer>> mSubscribers) {
|
|
for (MyCollection<Integer> list : mSubscribers) {
|
|
iterate_over_mycollection(list);
|
|
}
|
|
}
|
|
|
|
// expected: same as iterate_over_mycollection(list)
|
|
void ensure_call(MyCollection<Integer> list) {
|
|
iterate_over_mycollection(list);
|
|
}
|
|
|
|
// expected: O (|size| . |list|)
|
|
void loop_over_call(int size, MyCollection<Integer> list) {
|
|
for (int i = 0; i < size; i++) {
|
|
iterate_over_mycollection(list);
|
|
}
|
|
}
|
|
|
|
// expected: O (|list|^2)
|
|
void iterate_over_call_quad(int size, MyCollection<Integer> list) {
|
|
for (Integer i : list) {
|
|
iterate_over_mycollection(list);
|
|
}
|
|
}
|
|
|
|
// expected O (|list|^3)
|
|
void nested_iterator_qubic(int size, MyCollection<Integer> list1, MyCollection<Integer> list2) {
|
|
for (Integer i : list1) {
|
|
for (Integer j : list2) {
|
|
iterate_over_mycollection(list1);
|
|
iterate_over_mycollection(list1);
|
|
}
|
|
}
|
|
}
|
|
|
|
void sparse_array_linear(SparseArray<Integer> arr) {
|
|
for (int i = 0; i < arr.size(); i++) {}
|
|
}
|
|
|
|
void sparse_array_new_constant() {
|
|
SparseArray<Integer> new_arr = new SparseArray<Integer>();
|
|
new_arr.put(1, 1);
|
|
for (int i = 0; i < new_arr.size(); i++) {}
|
|
}
|
|
|
|
static class Dummy {}
|
|
|
|
public enum MyEnumType {
|
|
/* The elements of enum is initialized in `<clinit>`. */
|
|
A(1);
|
|
|
|
public final int mValue;
|
|
|
|
private MyEnumType(int i) {
|
|
mValue = i;
|
|
}
|
|
|
|
/* This field is also initialized in `<clinit>`, in which `<init>` is called. */
|
|
private static Dummy s =
|
|
new Dummy() {
|
|
{
|
|
/* This loop is in `<init>` function. It needs the results of `<clinit>` in order to
|
|
get `MyEnumType.values()`. */
|
|
for (MyEnumType type : MyEnumType.values()) {}
|
|
}
|
|
};
|
|
}
|
|
|
|
void immutable_set_of_constant() {
|
|
|
|
ImmutableSet<Integer> set = ImmutableSet.of();
|
|
for (int i = 0; i < set.size(); i++) {}
|
|
}
|
|
|
|
void immutable_set_of_multiple_constant() {
|
|
|
|
ImmutableSet<Integer> set = ImmutableSet.of(1, 2, 3, 4, 5);
|
|
for (int i = 0; i < set.size(); i++) {}
|
|
}
|
|
|
|
// O(|keyMap| x |coll|)
|
|
void containsAll_quadratic(HashMap<Integer, String> keyMap, Collection<String> coll) {
|
|
keyMap.values().containsAll(coll);
|
|
}
|
|
|
|
void containsNull_linear(HashMap<Integer, String> keyMap) {
|
|
keyMap.values().contains(null);
|
|
}
|
|
}
|