1 /* 2 * Copyright (c) Facebook, Inc. and its affiliates. 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.facebook.jni; 18 19 import static org.assertj.core.api.Assertions.assertThat; 20 21 import java.util.ArrayList; 22 import java.util.HashMap; 23 import java.util.List; 24 import java.util.Map; 25 import org.junit.Test; 26 27 public class IteratorTests extends BaseFBJniTests { 28 @Test testListIterator()29 public void testListIterator() { 30 List<String> list = new ArrayList<String>(); 31 list.add("red"); 32 list.add("green"); 33 list.add("blue"); 34 35 assertThat(nativeTestListIterator(list)).isTrue(); 36 } 37 nativeTestListIterator(List list)38 private static native boolean nativeTestListIterator(List list); 39 40 @Test testMapIterator()41 public void testMapIterator() { 42 Map<String, Integer> map = new HashMap<String, Integer>(); 43 map.put("one", 1); 44 map.put("two", 2); 45 map.put("four", 4); 46 47 assertThat(nativeTestMapIterator(map)).isTrue(); 48 } 49 nativeTestMapIterator(Map map)50 private static native boolean nativeTestMapIterator(Map map); 51 52 @Test(expected = ClassCastException.class) testMapIterateWrongType()53 public void testMapIterateWrongType() { 54 Map<String, Number> map = new HashMap<String, Number>(); 55 map.put("one", 1); 56 map.put("two", 2); 57 map.put("pi", 3.14); 58 59 assertThat(nativeTestIterateWrongType(map)).isTrue(); 60 } 61 nativeTestIterateWrongType(Map map)62 private static native boolean nativeTestIterateWrongType(Map map); 63 64 @Test testMapIterateNullKey()65 public void testMapIterateNullKey() { 66 Map<String, Integer> map = new HashMap<String, Integer>(); 67 map.put("one", 1); 68 map.put(null, -99); 69 map.put("four", 4); 70 71 assertThat(nativeTestIterateNullKey(map)).isTrue(); 72 } 73 nativeTestIterateNullKey(Map map)74 private static native boolean nativeTestIterateNullKey(Map map); 75 76 @Test testLargeMapIteration()77 public void testLargeMapIteration() { 78 Map<String, String> map = new HashMap<String, String>(); 79 for (int i = 0; i < 3000; i++) { 80 map.put("" + i, "value"); 81 } 82 assertThat(nativeTestLargeMapIteration(map)).isTrue(); 83 } 84 nativeTestLargeMapIteration(Map map)85 private static native boolean nativeTestLargeMapIteration(Map map); 86 } 87