View Javadoc
1   /*
2    * Copyright (C) 2008 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 com.google.common.annotations.GwtCompatible;
20  
21  /**
22   * Test cases for {@link HashBasedTable}.
23   *
24   * @author Jared Levy
25   */
26  @GwtCompatible(emulated = true)
27  public class HashBasedTableTest extends AbstractTableTest {
28  
29    @Override protected Table<String, Integer, Character> create(
30        Object... data) {
31      Table<String, Integer, Character> table = HashBasedTable.create();
32      table.put("foo", 4, 'a');
33      table.put("cat", 1, 'b');
34      table.clear();
35      populate(table, data);
36      return table;
37    }
38  
39    public void testCreateWithValidSizes() {
40      Table<String, Integer, Character> table1 = HashBasedTable.create(100, 20);
41      table1.put("foo", 1, 'a');
42      assertEquals((Character) 'a', table1.get("foo", 1));
43  
44      Table<String, Integer, Character> table2 = HashBasedTable.create(100, 0);
45      table2.put("foo", 1, 'a');
46      assertEquals((Character) 'a', table2.get("foo", 1));
47  
48      Table<String, Integer, Character> table3 = HashBasedTable.create(0, 20);
49      table3.put("foo", 1, 'a');
50      assertEquals((Character) 'a', table3.get("foo", 1));
51  
52      Table<String, Integer, Character> table4 = HashBasedTable.create(0, 0);
53      table4.put("foo", 1, 'a');
54      assertEquals((Character) 'a', table4.get("foo", 1));
55    }
56  
57    public void testCreateWithInvalidSizes() {
58      try {
59        HashBasedTable.create(100, -5);
60        fail();
61      } catch (IllegalArgumentException expected) {}
62  
63      try {
64        HashBasedTable.create(-5, 20);
65        fail();
66      } catch (IllegalArgumentException expected) {}
67    }
68  
69    public void testCreateCopy() {
70      Table<String, Integer, Character> original
71          = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
72      Table<String, Integer, Character> copy = HashBasedTable.create(original);
73      assertEquals(original, copy);
74      assertEquals((Character) 'a', copy.get("foo", 1));
75    }
76  }
77