OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2014, the Dart project authors. |
| 3 * |
| 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except |
| 5 * in compliance with the License. You may obtain a copy of the License at |
| 6 * |
| 7 * http://www.eclipse.org/legal/epl-v10.html |
| 8 * |
| 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License |
| 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express |
| 11 * or implied. See the License for the specific language governing permissions a
nd limitations under |
| 12 * the License. |
| 13 */ |
| 14 package com.google.dart.engine.internal.index.file; |
| 15 |
| 16 import junit.framework.TestCase; |
| 17 |
| 18 import static org.fest.assertions.Assertions.assertThat; |
| 19 |
| 20 public class IntToIntSetMapTest extends TestCase { |
| 21 private static int HASH_32_KEY_1 = 0x60000; |
| 22 private static int HASH_32_KEY_2 = 0x90000; |
| 23 private static int HASH_32_KEY_3 = 0x100000; |
| 24 private IntToIntSetMap map = new IntToIntSetMap(16, 0.75f); |
| 25 |
| 26 public void test_add_duplicate() throws Exception { |
| 27 map.add(HASH_32_KEY_1, 1); |
| 28 map.add(HASH_32_KEY_1, 1); |
| 29 map.add(HASH_32_KEY_1, 10); |
| 30 assertThat(map.get(HASH_32_KEY_1)).containsOnly(1, 10); |
| 31 } |
| 32 |
| 33 public void test_add_hasCollision() throws Exception { |
| 34 map = new IntToIntSetMap(33, 0.75f); |
| 35 map.add(HASH_32_KEY_1, 10); |
| 36 map.add(HASH_32_KEY_1, 11); |
| 37 map.add(HASH_32_KEY_2, 20); |
| 38 map.add(HASH_32_KEY_2, 21); |
| 39 map.add(HASH_32_KEY_2, 22); |
| 40 map.add(HASH_32_KEY_3, 30); |
| 41 assertThat(map.get(HASH_32_KEY_1)).containsOnly(10, 11); |
| 42 assertThat(map.get(HASH_32_KEY_2)).containsOnly(20, 21, 22); |
| 43 assertThat(map.get(HASH_32_KEY_3)).containsOnly(30); |
| 44 } |
| 45 |
| 46 public void test_add_negativeKey() throws Exception { |
| 47 try { |
| 48 map.add(-1, 10); |
| 49 fail(); |
| 50 } catch (IllegalArgumentException e) { |
| 51 } |
| 52 } |
| 53 |
| 54 public void test_add_noCollision() throws Exception { |
| 55 map.add(HASH_32_KEY_1, 1); |
| 56 map.add(HASH_32_KEY_1, 2); |
| 57 map.add(HASH_32_KEY_1, 3); |
| 58 assertThat(map.get(HASH_32_KEY_1)).containsOnly(1, 2, 3); |
| 59 } |
| 60 |
| 61 public void test_clear() throws Exception { |
| 62 map.add(HASH_32_KEY_1, 1); |
| 63 assertEquals(1, map.size()); |
| 64 // clear |
| 65 map.clear(); |
| 66 assertEquals(0, map.size()); |
| 67 } |
| 68 |
| 69 public void test_get_no() throws Exception { |
| 70 assertThat(map.get(HASH_32_KEY_1)).isEmpty(); |
| 71 } |
| 72 |
| 73 public void test_size() throws Exception { |
| 74 // empty |
| 75 assertEquals(0, map.size()); |
| 76 // 1 key, 1 value |
| 77 map.add(HASH_32_KEY_1, 1); |
| 78 assertEquals(1, map.size()); |
| 79 // 1 key, 2 values |
| 80 map.add(HASH_32_KEY_1, 2); |
| 81 assertEquals(1, map.size()); |
| 82 // 2 keys |
| 83 map.add(HASH_32_KEY_2, 3); |
| 84 assertEquals(2, map.size()); |
| 85 } |
| 86 |
| 87 public void test_stress() throws Exception { |
| 88 int count = 1000; |
| 89 // fill map |
| 90 for (int i = 0; i < count; i++) { |
| 91 int key = i << 16; |
| 92 map.add(key, i * 10); |
| 93 map.add(key, i * 100); |
| 94 map.add(key, i * 1000); |
| 95 } |
| 96 // check map |
| 97 assertEquals(count, map.size()); |
| 98 for (int i = 0; i < count; i++) { |
| 99 int key = i << 16; |
| 100 assertThat(map.get(key)).containsOnly(i * 10, i * 100, i * 1000); |
| 101 } |
| 102 } |
| 103 } |
OLD | NEW |