1 /* 2 * Copyright (C) 2021 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.car; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.android.car.util.SetMultimap; 22 23 import com.google.common.collect.ImmutableMultimap; 24 import com.google.common.collect.Multimap; 25 26 import org.junit.Test; 27 28 public final class SetMultimapTest { 29 30 @Test testGet_empty_returnsEmptySet()31 public void testGet_empty_returnsEmptySet() { 32 SetMultimap<Integer, String> map = new SetMultimap<>(); 33 34 assertThat(map.get(1)).isEmpty(); 35 } 36 37 @Test testGet_keyDoesNotExist_returnsEmptySet()38 public void testGet_keyDoesNotExist_returnsEmptySet() { 39 SetMultimap<Integer, String> map = createFromMultimap(ImmutableMultimap.of(1, "value1")); 40 41 assertThat(map.get(2)).isEmpty(); 42 } 43 44 @Test testGet()45 public void testGet() { 46 SetMultimap<Integer, String> map = new SetMultimap<>(); 47 48 map.put(1, "value1"); 49 assertThat(map.get(1)).containsExactly("value1"); 50 map.put(1, "value2"); 51 assertThat(map.get(1)).containsExactly("value1", "value2"); 52 map.put(1, "value3"); 53 assertThat(map.get(1)).containsExactly("value1", "value2", "value3"); 54 } 55 56 @Test testPut_duplicateValue()57 public void testPut_duplicateValue() { 58 SetMultimap<Integer, String> map = new SetMultimap<>(); 59 map.put(1, "value1"); 60 map.put(1, "value1"); 61 62 assertThat(map.get(1)).containsExactly("value1"); 63 } 64 65 @Test testContainsEntry()66 public void testContainsEntry() { 67 SetMultimap<Integer, String> map = createFromMultimap(ImmutableMultimap.of(1, "value1")); 68 69 assertThat(map.containsEntry(1, "value1")).isTrue(); 70 } 71 72 @Test testContainsEntry_keyDoesNotExist()73 public void testContainsEntry_keyDoesNotExist() { 74 SetMultimap<Integer, String> map = createFromMultimap(ImmutableMultimap.of(1, "value1")); 75 76 assertThat(map.containsEntry(2, "value1")).isFalse(); 77 } 78 79 @Test testContainsEntry_valueDoesNotExist()80 public void testContainsEntry_valueDoesNotExist() { 81 SetMultimap<Integer, String> map = createFromMultimap(ImmutableMultimap.of(1, "value1")); 82 83 assertThat(map.containsEntry(1, "value2")).isFalse(); 84 } 85 86 @Test testRemove_success()87 public void testRemove_success() { 88 SetMultimap<Integer, String> map = createFromMultimap( 89 ImmutableMultimap.of(1, "value1", 1, "value2", 2, "value3")); 90 91 assertThat(map.remove(1, "value1")).isTrue(); 92 assertContainsExactlyEntries(map, ImmutableMultimap.of(1, "value2", 2, "value3")); 93 } 94 95 @Test testRemove_lastEntryOfKey()96 public void testRemove_lastEntryOfKey() { 97 SetMultimap<Integer, String> map = createFromMultimap( 98 ImmutableMultimap.of(1, "value1", 1, "value2", 2, "value3")); 99 100 assertThat(map.remove(2, "value3")).isTrue(); 101 assertContainsExactlyEntries(map, ImmutableMultimap.of(1, "value1", 1, "value2")); 102 } 103 104 @Test testRemove_keyDoesNotExist()105 public void testRemove_keyDoesNotExist() { 106 SetMultimap<Integer, String> map = createFromMultimap( 107 ImmutableMultimap.of(1, "value1", 1, "value2", 2, "value3")); 108 109 assertThat(map.remove(3, "value3")).isFalse(); 110 assertContainsExactlyEntries( 111 map, ImmutableMultimap.of(1, "value1", 1, "value2", 2, "value3")); 112 } 113 114 @Test testRemove_entryDoesNotExist()115 public void testRemove_entryDoesNotExist() { 116 SetMultimap<Integer, String> map = createFromMultimap( 117 ImmutableMultimap.of(1, "value1", 1, "value2", 2, "value3")); 118 119 assertThat(map.remove(1, "value3")).isFalse(); 120 assertContainsExactlyEntries( 121 map, ImmutableMultimap.of(1, "value1", 1, "value2", 2, "value3")); 122 } 123 124 @Test testClear()125 public void testClear() { 126 SetMultimap<Integer, String> map = createFromMultimap( 127 ImmutableMultimap.of(1, "value1", 1, "value2", 2, "value3")); 128 129 map.clear(); 130 131 assertContainsExactlyEntries(map, ImmutableMultimap.<Integer, String>of()); 132 } 133 134 @Test testKeySet()135 public void testKeySet() { 136 SetMultimap<Integer, String> map = createFromMultimap( 137 ImmutableMultimap.of(1, "value1", 1, "value2", 2, "value3")); 138 139 assertThat(map.keySet()).containsExactly(1, 2); 140 } 141 142 @Test testKeySet_empty()143 public void testKeySet_empty() { 144 SetMultimap<Integer, String> map = new SetMultimap<>(); 145 146 assertThat(map.keySet()).isEmpty(); 147 } 148 createFromMultimap(Multimap<K, V> multimap)149 private static <K, V> SetMultimap<K, V> createFromMultimap(Multimap<K, V> multimap) { 150 SetMultimap<K, V> map = new SetMultimap<>(); 151 multimap.entries().forEach(entry -> map.put(entry.getKey(), entry.getValue())); 152 153 return map; 154 } 155 assertContainsExactlyEntries( SetMultimap<K, V> actual, Multimap<K, V> expected)156 private static <K, V> void assertContainsExactlyEntries( 157 SetMultimap<K, V> actual, Multimap<K, V> expected) { 158 ImmutableMultimap.Builder<K, V> multimapBuilder = ImmutableMultimap.builder(); 159 actual.keySet().forEach(key -> multimapBuilder.putAll(key, actual.get(key))); 160 161 assertThat(multimapBuilder.build()).containsExactlyEntriesIn(expected); 162 } 163 } 164