1 /* 2 * Copyright (C) 2009 The Guava Authors 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.google.common.collect; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.collect.Table.Cell; 23 24 /** @author Gregory Kick */ 25 @GwtCompatible 26 public class RegularImmutableTableTest extends AbstractImmutableTableTest { 27 private static final ImmutableSet<Cell<Character, Integer, String>> CELLS = 28 ImmutableSet.of( 29 Tables.immutableCell('a', 1, "foo"), 30 Tables.immutableCell('b', 1, "bar"), 31 Tables.immutableCell('a', 2, "baz")); 32 33 private static final ImmutableSet<Character> ROW_SPACE = ImmutableSet.of('a', 'b'); 34 35 private static final ImmutableSet<Integer> COLUMN_SPACE = ImmutableSet.of(1, 2); 36 37 private static final SparseImmutableTable<Character, Integer, String> SPARSE = 38 new SparseImmutableTable<>(CELLS.asList(), ROW_SPACE, COLUMN_SPACE); 39 40 private static final DenseImmutableTable<Character, Integer, String> DENSE = 41 new DenseImmutableTable<>(CELLS.asList(), ROW_SPACE, COLUMN_SPACE); 42 43 @Override getTestInstances()44 Iterable<ImmutableTable<Character, Integer, String>> getTestInstances() { 45 return ImmutableList.<ImmutableTable<Character, Integer, String>>of(SPARSE, DENSE); 46 } 47 testCellSet()48 public void testCellSet() { 49 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 50 assertEquals(CELLS, testInstance.cellSet()); 51 } 52 } 53 testValues()54 public void testValues() { 55 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 56 assertThat(testInstance.values()).containsExactly("foo", "bar", "baz").inOrder(); 57 } 58 } 59 testSize()60 public void testSize() { 61 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 62 assertEquals(3, testInstance.size()); 63 } 64 } 65 testContainsValue()66 public void testContainsValue() { 67 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 68 assertTrue(testInstance.containsValue("foo")); 69 assertTrue(testInstance.containsValue("bar")); 70 assertTrue(testInstance.containsValue("baz")); 71 assertFalse(testInstance.containsValue("blah")); 72 } 73 } 74 testIsEmpty()75 public void testIsEmpty() { 76 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 77 assertFalse(testInstance.isEmpty()); 78 } 79 } 80 testForCells()81 public void testForCells() { 82 assertTrue(RegularImmutableTable.forCells(CELLS) instanceof DenseImmutableTable<?, ?, ?>); 83 assertTrue( 84 RegularImmutableTable.forCells( 85 ImmutableSet.of( 86 Tables.immutableCell('a', 1, "blah"), 87 Tables.immutableCell('b', 2, "blah"), 88 Tables.immutableCell('c', 3, "blah"))) 89 instanceof SparseImmutableTable<?, ?, ?>); 90 } 91 testGet()92 public void testGet() { 93 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 94 assertEquals("foo", testInstance.get('a', 1)); 95 assertEquals("bar", testInstance.get('b', 1)); 96 assertEquals("baz", testInstance.get('a', 2)); 97 assertNull(testInstance.get('b', 2)); 98 assertNull(testInstance.get('c', 3)); 99 } 100 } 101 testColumn()102 public void testColumn() { 103 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 104 assertEquals(ImmutableMap.of('a', "foo", 'b', "bar"), testInstance.column(1)); 105 assertEquals(ImmutableMap.of('a', "baz"), testInstance.column(2)); 106 assertEquals(ImmutableMap.of(), testInstance.column(3)); 107 } 108 } 109 testColumnKeySet()110 public void testColumnKeySet() { 111 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 112 assertEquals(ImmutableSet.of(1, 2), testInstance.columnKeySet()); 113 } 114 } 115 testColumnMap()116 public void testColumnMap() { 117 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 118 assertEquals( 119 ImmutableMap.of( 120 1, ImmutableMap.of('a', "foo", 'b', "bar"), 2, ImmutableMap.of('a', "baz")), 121 testInstance.columnMap()); 122 } 123 } 124 testContains()125 public void testContains() { 126 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 127 assertTrue(testInstance.contains('a', 1)); 128 assertTrue(testInstance.contains('b', 1)); 129 assertTrue(testInstance.contains('a', 2)); 130 assertFalse(testInstance.contains('b', 2)); 131 assertFalse(testInstance.contains('c', 3)); 132 } 133 } 134 testContainsColumn()135 public void testContainsColumn() { 136 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 137 assertTrue(testInstance.containsColumn(1)); 138 assertTrue(testInstance.containsColumn(2)); 139 assertFalse(testInstance.containsColumn(3)); 140 } 141 } 142 testContainsRow()143 public void testContainsRow() { 144 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 145 assertTrue(testInstance.containsRow('a')); 146 assertTrue(testInstance.containsRow('b')); 147 assertFalse(testInstance.containsRow('c')); 148 } 149 } 150 testRow()151 public void testRow() { 152 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 153 assertEquals(ImmutableMap.of(1, "foo", 2, "baz"), testInstance.row('a')); 154 assertEquals(ImmutableMap.of(1, "bar"), testInstance.row('b')); 155 assertEquals(ImmutableMap.of(), testInstance.row('c')); 156 } 157 } 158 testRowKeySet()159 public void testRowKeySet() { 160 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 161 assertEquals(ImmutableSet.of('a', 'b'), testInstance.rowKeySet()); 162 } 163 } 164 testRowMap()165 public void testRowMap() { 166 for (ImmutableTable<Character, Integer, String> testInstance : getTestInstances()) { 167 assertEquals( 168 ImmutableMap.of('a', ImmutableMap.of(1, "foo", 2, "baz"), 'b', ImmutableMap.of(1, "bar")), 169 testInstance.rowMap()); 170 } 171 } 172 } 173