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 static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.annotations.GwtIncompatible; 23 import com.google.common.testing.NullPointerTester; 24 import com.google.common.testing.SerializableTester; 25 26 /** 27 * Test cases for {@link HashBasedTable}. 28 * 29 * @author Jared Levy 30 */ 31 @GwtCompatible(emulated = true) 32 public class HashBasedTableTest extends AbstractTableTest { 33 34 @Override create(Object... data)35 protected Table<String, Integer, Character> create(Object... data) { 36 Table<String, Integer, Character> table = HashBasedTable.create(); 37 table.put("foo", 4, 'a'); 38 table.put("cat", 1, 'b'); 39 table.clear(); 40 populate(table, data); 41 return table; 42 } 43 testIterationOrder()44 public void testIterationOrder() { 45 Table<String, String, String> table = HashBasedTable.create(); 46 for (int i = 0; i < 5; i++) { 47 table.put("r" + i, "c" + i, "v" + i); 48 } 49 assertThat(table.rowKeySet()).containsExactly("r0", "r1", "r2", "r3", "r4").inOrder(); 50 assertThat(table.columnKeySet()).containsExactly("c0", "c1", "c2", "c3", "c4").inOrder(); 51 assertThat(table.values()).containsExactly("v0", "v1", "v2", "v3", "v4").inOrder(); 52 } 53 testCreateWithValidSizes()54 public void testCreateWithValidSizes() { 55 Table<String, Integer, Character> table1 = HashBasedTable.create(100, 20); 56 table1.put("foo", 1, 'a'); 57 assertEquals((Character) 'a', table1.get("foo", 1)); 58 59 Table<String, Integer, Character> table2 = HashBasedTable.create(100, 0); 60 table2.put("foo", 1, 'a'); 61 assertEquals((Character) 'a', table2.get("foo", 1)); 62 63 Table<String, Integer, Character> table3 = HashBasedTable.create(0, 20); 64 table3.put("foo", 1, 'a'); 65 assertEquals((Character) 'a', table3.get("foo", 1)); 66 67 Table<String, Integer, Character> table4 = HashBasedTable.create(0, 0); 68 table4.put("foo", 1, 'a'); 69 assertEquals((Character) 'a', table4.get("foo", 1)); 70 } 71 testCreateWithInvalidSizes()72 public void testCreateWithInvalidSizes() { 73 try { 74 HashBasedTable.create(100, -5); 75 fail(); 76 } catch (IllegalArgumentException expected) { 77 } 78 79 try { 80 HashBasedTable.create(-5, 20); 81 fail(); 82 } catch (IllegalArgumentException expected) { 83 } 84 } 85 testCreateCopy()86 public void testCreateCopy() { 87 Table<String, Integer, Character> original = 88 create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); 89 Table<String, Integer, Character> copy = HashBasedTable.create(original); 90 assertEquals(original, copy); 91 assertEquals((Character) 'a', copy.get("foo", 1)); 92 } 93 94 @GwtIncompatible // SerializableTester testSerialization()95 public void testSerialization() { 96 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); 97 SerializableTester.reserializeAndAssert(table); 98 } 99 100 @GwtIncompatible // NullPointerTester testNullPointerStatic()101 public void testNullPointerStatic() { 102 new NullPointerTester().testAllPublicStaticMethods(HashBasedTable.class); 103 } 104 } 105