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