SingletonImmutableTableTest.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
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.base.Objects;
22import com.google.common.testing.EqualsTester;
23
24/**
25 * Tests {@link SingletonImmutableTable}.
26 *
27 * @author gak@google.com (Gregory Kick)
28 */
29public class SingletonImmutableTableTest extends AbstractImmutableTableTest {
30  private final ImmutableTable<Character, Integer, String> testTable =
31      new SingletonImmutableTable<Character, Integer, String>('a', 1, "blah");
32
33  public void testHashCode() {
34    assertEquals(Objects.hashCode('a', 1, "blah"), testTable.hashCode());
35  }
36
37  public void testCellSet() {
38    assertEquals(ImmutableSet.of(Tables.immutableCell('a', 1, "blah")),
39        testTable.cellSet());
40  }
41
42  public void testColumn() {
43    assertEquals(ImmutableMap.of(), testTable.column(0));
44    assertEquals(ImmutableMap.of('a', "blah"), testTable.column(1));
45  }
46
47  public void testColumnKeySet() {
48    assertEquals(ImmutableSet.of(1), testTable.columnKeySet());
49  }
50
51  public void testColumnMap() {
52    assertEquals(ImmutableMap.of(1, ImmutableMap.of('a', "blah")),
53        testTable.columnMap());
54  }
55
56  public void testRow() {
57    assertEquals(ImmutableMap.of(), testTable.row('A'));
58    assertEquals(ImmutableMap.of(1, "blah"), testTable.row('a'));
59  }
60
61  public void testRowKeySet() {
62    assertEquals(ImmutableSet.of('a'), testTable.rowKeySet());
63  }
64
65  public void testRowMap() {
66    assertEquals(ImmutableMap.of('a', ImmutableMap.of(1, "blah")),
67        testTable.rowMap());
68  }
69
70  public void testEqualsObject() {
71    new EqualsTester()
72        .addEqualityGroup(testTable, ArrayTable.create(testTable),
73            HashBasedTable.create(testTable))
74        .addEqualityGroup(EmptyImmutableTable.INSTANCE,
75            HashBasedTable.create())
76        .addEqualityGroup(HashBasedTable.create(ImmutableTable.of('A', 2, "")))
77        .testEquals();
78  }
79
80  public void testToString() {
81    assertEquals("{a={1=blah}}", testTable.toString());
82  }
83
84  public void testContains() {
85    assertTrue(testTable.contains('a', 1));
86    assertFalse(testTable.contains('a', 2));
87    assertFalse(testTable.contains('A', 1));
88    assertFalse(testTable.contains('A', 2));
89  }
90
91  public void testContainsColumn() {
92    assertTrue(testTable.containsColumn(1));
93    assertFalse(testTable.containsColumn(2));
94  }
95
96  public void testContainsRow() {
97    assertTrue(testTable.containsRow('a'));
98    assertFalse(testTable.containsRow('A'));
99  }
100
101  public void testContainsValue() {
102    assertTrue(testTable.containsValue("blah"));
103    assertFalse(testTable.containsValue(""));
104  }
105
106  public void testGet() {
107    assertEquals("blah", testTable.get('a', 1));
108    assertNull(testTable.get('a', 2));
109    assertNull(testTable.get('A', 1));
110    assertNull(testTable.get('A', 2));
111  }
112
113  public void testIsEmpty() {
114    assertFalse(testTable.isEmpty());
115  }
116
117  public void testSize() {
118    assertEquals(1, testTable.size());
119  }
120
121  public void testValues() {
122    ASSERT.that(testTable.values()).hasContentsInOrder("blah");
123  }
124
125  @Override Iterable<ImmutableTable<Character, Integer, String>>
126      getTestInstances() {
127    return ImmutableSet.of(testTable);
128  }
129}
130