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