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