1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
|
/*
* Copyright (C) 2009 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.collect;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.testing.SerializableTester;
/**
* Tests common methods in {@link ImmutableTable}
*
* @author Gregory Kick
*/
@GwtCompatible(emulated = true)
public class ImmutableTableTest extends AbstractTableReadTest {
@Override
protected Table<String, Integer, Character> create(Object... data) {
ImmutableTable.Builder<String, Integer, Character> builder = ImmutableTable.builder();
for (int i = 0; i < data.length; i = i + 3) {
builder.put((String) data[i], (Integer) data[i + 1], (Character) data[i + 2]);
}
return builder.build();
}
// TODO(b/172823566): Use mainline testToImmutableMap once CollectorTester is usable to java7.
public void testToImmutableTable_java7_combine() {
ImmutableTable.Builder<String, String, Integer> zis =
ImmutableTable.<String, String, Integer>builder().put("one", "uno", 1).put("two", "dos", 2);
ImmutableTable.Builder<String, String, Integer> zat =
ImmutableTable.<String, String, Integer>builder()
.put("one", "eins", 1)
.put("two", "twei", 2);
ImmutableTable<String, String, Integer> table = zis.combine(zat).build();
ImmutableTable<String, String, Integer> expected =
ImmutableTable.<String, String, Integer>builder()
.put("one", "uno", 1)
.put("two", "dos", 2)
.put("one", "eins", 1)
.put("two", "twei", 2)
.build();
assertThat(table).isEqualTo(expected);
}
public void testBuilder() {
ImmutableTable.Builder<Character, Integer, String> builder = new ImmutableTable.Builder<>();
assertEquals(ImmutableTable.of(), builder.build());
assertEquals(ImmutableTable.of('a', 1, "foo"), builder.put('a', 1, "foo").build());
Table<Character, Integer, String> expectedTable = HashBasedTable.create();
expectedTable.put('a', 1, "foo");
expectedTable.put('b', 1, "bar");
expectedTable.put('a', 2, "baz");
Table<Character, Integer, String> otherTable = HashBasedTable.create();
otherTable.put('b', 1, "bar");
otherTable.put('a', 2, "baz");
assertEquals(expectedTable, builder.putAll(otherTable).build());
}
public void testBuilder_withImmutableCell() {
ImmutableTable.Builder<Character, Integer, String> builder = new ImmutableTable.Builder<>();
assertEquals(
ImmutableTable.of('a', 1, "foo"), builder.put(Tables.immutableCell('a', 1, "foo")).build());
}
public void testBuilder_withImmutableCellAndNullContents() {
ImmutableTable.Builder<Character, Integer, String> builder = new ImmutableTable.Builder<>();
try {
builder.put(Tables.immutableCell((Character) null, 1, "foo"));
fail();
} catch (NullPointerException e) {
// success
}
try {
builder.put(Tables.immutableCell('a', (Integer) null, "foo"));
fail();
} catch (NullPointerException e) {
// success
}
try {
builder.put(Tables.immutableCell('a', 1, (String) null));
fail();
} catch (NullPointerException e) {
// success
}
}
private static class StringHolder {
String string;
}
public void testBuilder_withMutableCell() {
ImmutableTable.Builder<Character, Integer, String> builder = new ImmutableTable.Builder<>();
final StringHolder holder = new StringHolder();
holder.string = "foo";
Table.Cell<Character, Integer, String> mutableCell =
new Tables.AbstractCell<Character, Integer, String>() {
@Override
public Character getRowKey() {
return 'K';
}
@Override
public Integer getColumnKey() {
return 42;
}
@Override
public String getValue() {
return holder.string;
}
};
// Add the mutable cell to the builder
builder.put(mutableCell);
// Mutate the value
holder.string = "bar";
// Make sure it uses the original value.
assertEquals(ImmutableTable.of('K', 42, "foo"), builder.build());
}
public void testBuilder_noDuplicates() {
ImmutableTable.Builder<Character, Integer, String> builder =
new ImmutableTable.Builder<Character, Integer, String>()
.put('a', 1, "foo")
.put('a', 1, "bar");
try {
builder.build();
fail();
} catch (IllegalArgumentException e) {
// success
}
}
public void testBuilder_noNulls() {
ImmutableTable.Builder<Character, Integer, String> builder = new ImmutableTable.Builder<>();
try {
builder.put(null, 1, "foo");
fail();
} catch (NullPointerException e) {
// success
}
try {
builder.put('a', null, "foo");
fail();
} catch (NullPointerException e) {
// success
}
try {
builder.put('a', 1, null);
fail();
} catch (NullPointerException e) {
// success
}
}
private static <R, C, V> void validateTableCopies(Table<R, C, V> original) {
Table<R, C, V> copy = ImmutableTable.copyOf(original);
assertEquals(original, copy);
validateViewOrdering(original, copy);
Table<R, C, V> built = ImmutableTable.<R, C, V>builder().putAll(original).build();
assertEquals(original, built);
validateViewOrdering(original, built);
}
private static <R, C, V> void validateViewOrdering(Table<R, C, V> original, Table<R, C, V> copy) {
assertThat(copy.cellSet()).containsExactlyElementsIn(original.cellSet()).inOrder();
assertThat(copy.rowKeySet()).containsExactlyElementsIn(original.rowKeySet()).inOrder();
assertThat(copy.values()).containsExactlyElementsIn(original.values()).inOrder();
}
public void testCopyOf() {
Table<Character, Integer, String> table = TreeBasedTable.create();
validateTableCopies(table);
table.put('b', 2, "foo");
validateTableCopies(table);
table.put('b', 1, "bar");
table.put('a', 2, "baz");
validateTableCopies(table);
// Even though rowKeySet, columnKeySet, and cellSet have the same
// iteration ordering, row has an inconsistent ordering.
assertThat(table.row('b').keySet()).containsExactly(1, 2).inOrder();
assertThat(ImmutableTable.copyOf(table).row('b').keySet()).containsExactly(2, 1).inOrder();
}
public void testCopyOfSparse() {
Table<Character, Integer, String> table = TreeBasedTable.create();
table.put('x', 2, "foo");
table.put('r', 1, "bar");
table.put('c', 3, "baz");
table.put('b', 7, "cat");
table.put('e', 5, "dog");
table.put('c', 0, "axe");
table.put('e', 3, "tub");
table.put('r', 4, "foo");
table.put('x', 5, "bar");
validateTableCopies(table);
}
public void testCopyOfDense() {
Table<Character, Integer, String> table = TreeBasedTable.create();
table.put('c', 3, "foo");
table.put('c', 2, "bar");
table.put('c', 1, "baz");
table.put('b', 3, "cat");
table.put('b', 1, "dog");
table.put('a', 3, "foo");
table.put('a', 2, "bar");
table.put('a', 1, "baz");
validateTableCopies(table);
}
public void testBuilder_orderRowsAndColumnsBy_putAll() {
Table<Character, Integer, String> table = HashBasedTable.create();
table.put('b', 2, "foo");
table.put('b', 1, "bar");
table.put('a', 2, "baz");
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
Table<Character, Integer, String> copy =
builder
.orderRowsBy(Ordering.natural())
.orderColumnsBy(Ordering.natural())
.putAll(table)
.build();
assertThat(copy.rowKeySet()).containsExactly('a', 'b').inOrder();
assertThat(copy.columnKeySet()).containsExactly(1, 2).inOrder();
assertThat(copy.values()).containsExactly("baz", "bar", "foo").inOrder();
assertThat(copy.row('b').keySet()).containsExactly(1, 2).inOrder();
}
public void testBuilder_orderRowsAndColumnsBy_sparse() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.orderRowsBy(Ordering.natural());
builder.orderColumnsBy(Ordering.natural());
builder.put('x', 2, "foo");
builder.put('r', 1, "bar");
builder.put('c', 3, "baz");
builder.put('b', 7, "cat");
builder.put('e', 5, "dog");
builder.put('c', 0, "axe");
builder.put('e', 3, "tub");
builder.put('r', 4, "foo");
builder.put('x', 5, "bar");
Table<Character, Integer, String> table = builder.build();
assertThat(table.rowKeySet()).containsExactly('b', 'c', 'e', 'r', 'x').inOrder();
assertThat(table.columnKeySet()).containsExactly(0, 1, 2, 3, 4, 5, 7).inOrder();
assertThat(table.values())
.containsExactly("cat", "axe", "baz", "tub", "dog", "bar", "foo", "foo", "bar")
.inOrder();
assertThat(table.row('c').keySet()).containsExactly(0, 3).inOrder();
assertThat(table.column(5).keySet()).containsExactly('e', 'x').inOrder();
}
public void testBuilder_orderRowsAndColumnsBy_dense() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.orderRowsBy(Ordering.natural());
builder.orderColumnsBy(Ordering.natural());
builder.put('c', 3, "foo");
builder.put('c', 2, "bar");
builder.put('c', 1, "baz");
builder.put('b', 3, "cat");
builder.put('b', 1, "dog");
builder.put('a', 3, "foo");
builder.put('a', 2, "bar");
builder.put('a', 1, "baz");
Table<Character, Integer, String> table = builder.build();
assertThat(table.rowKeySet()).containsExactly('a', 'b', 'c').inOrder();
assertThat(table.columnKeySet()).containsExactly(1, 2, 3).inOrder();
assertThat(table.values())
.containsExactly("baz", "bar", "foo", "dog", "cat", "baz", "bar", "foo")
.inOrder();
assertThat(table.row('c').keySet()).containsExactly(1, 2, 3).inOrder();
assertThat(table.column(1).keySet()).containsExactly('a', 'b', 'c').inOrder();
}
public void testBuilder_orderRowsBy_sparse() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.orderRowsBy(Ordering.natural());
builder.put('x', 2, "foo");
builder.put('r', 1, "bar");
builder.put('c', 3, "baz");
builder.put('b', 7, "cat");
builder.put('e', 5, "dog");
builder.put('c', 0, "axe");
builder.put('e', 3, "tub");
builder.put('r', 4, "foo");
builder.put('x', 5, "bar");
Table<Character, Integer, String> table = builder.build();
assertThat(table.rowKeySet()).containsExactly('b', 'c', 'e', 'r', 'x').inOrder();
assertThat(table.column(5).keySet()).containsExactly('e', 'x').inOrder();
}
public void testBuilder_orderRowsBy_dense() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.orderRowsBy(Ordering.natural());
builder.put('c', 3, "foo");
builder.put('c', 2, "bar");
builder.put('c', 1, "baz");
builder.put('b', 3, "cat");
builder.put('b', 1, "dog");
builder.put('a', 3, "foo");
builder.put('a', 2, "bar");
builder.put('a', 1, "baz");
Table<Character, Integer, String> table = builder.build();
assertThat(table.rowKeySet()).containsExactly('a', 'b', 'c').inOrder();
assertThat(table.column(1).keySet()).containsExactly('a', 'b', 'c').inOrder();
}
public void testBuilder_orderColumnsBy_sparse() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.orderColumnsBy(Ordering.natural());
builder.put('x', 2, "foo");
builder.put('r', 1, "bar");
builder.put('c', 3, "baz");
builder.put('b', 7, "cat");
builder.put('e', 5, "dog");
builder.put('c', 0, "axe");
builder.put('e', 3, "tub");
builder.put('r', 4, "foo");
builder.put('x', 5, "bar");
Table<Character, Integer, String> table = builder.build();
assertThat(table.columnKeySet()).containsExactly(0, 1, 2, 3, 4, 5, 7).inOrder();
assertThat(table.row('c').keySet()).containsExactly(0, 3).inOrder();
}
public void testBuilder_orderColumnsBy_dense() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.orderColumnsBy(Ordering.natural());
builder.put('c', 3, "foo");
builder.put('c', 2, "bar");
builder.put('c', 1, "baz");
builder.put('b', 3, "cat");
builder.put('b', 1, "dog");
builder.put('a', 3, "foo");
builder.put('a', 2, "bar");
builder.put('a', 1, "baz");
Table<Character, Integer, String> table = builder.build();
assertThat(table.columnKeySet()).containsExactly(1, 2, 3).inOrder();
assertThat(table.row('c').keySet()).containsExactly(1, 2, 3).inOrder();
}
public void testSerialization_empty() {
validateReserialization(ImmutableTable.of());
}
public void testSerialization_singleElement() {
validateReserialization(ImmutableTable.of('a', 2, "foo"));
}
public void testDenseSerialization_manualOrder() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.put('b', 2, "foo");
builder.put('b', 1, "bar");
builder.put('a', 2, "baz");
Table<Character, Integer, String> table = builder.build();
assertThat(table).isInstanceOf(DenseImmutableTable.class);
validateReserialization(table);
}
public void testDenseSerialization_rowOrder() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.orderRowsBy(Ordering.<Character>natural());
builder.put('b', 2, "foo");
builder.put('b', 1, "bar");
builder.put('a', 2, "baz");
Table<Character, Integer, String> table = builder.build();
assertThat(table).isInstanceOf(DenseImmutableTable.class);
validateReserialization(table);
}
public void testDenseSerialization_columnOrder() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.orderColumnsBy(Ordering.<Integer>natural());
builder.put('b', 2, "foo");
builder.put('b', 1, "bar");
builder.put('a', 2, "baz");
Table<Character, Integer, String> table = builder.build();
assertThat(table).isInstanceOf(DenseImmutableTable.class);
validateReserialization(table);
}
public void testDenseSerialization_bothOrders() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.orderRowsBy(Ordering.<Character>natural());
builder.orderColumnsBy(Ordering.<Integer>natural());
builder.put('b', 2, "foo");
builder.put('b', 1, "bar");
builder.put('a', 2, "baz");
Table<Character, Integer, String> table = builder.build();
assertThat(table).isInstanceOf(DenseImmutableTable.class);
validateReserialization(table);
}
public void testSparseSerialization_manualOrder() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.put('b', 2, "foo");
builder.put('b', 1, "bar");
builder.put('a', 2, "baz");
builder.put('c', 3, "cat");
builder.put('d', 4, "dog");
Table<Character, Integer, String> table = builder.build();
assertThat(table).isInstanceOf(SparseImmutableTable.class);
validateReserialization(table);
}
public void testSparseSerialization_rowOrder() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.orderRowsBy(Ordering.<Character>natural());
builder.put('b', 2, "foo");
builder.put('b', 1, "bar");
builder.put('a', 2, "baz");
builder.put('c', 3, "cat");
builder.put('d', 4, "dog");
Table<Character, Integer, String> table = builder.build();
assertThat(table).isInstanceOf(SparseImmutableTable.class);
validateReserialization(table);
}
public void testSparseSerialization_columnOrder() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.orderColumnsBy(Ordering.<Integer>natural());
builder.put('b', 2, "foo");
builder.put('b', 1, "bar");
builder.put('a', 2, "baz");
builder.put('c', 3, "cat");
builder.put('d', 4, "dog");
Table<Character, Integer, String> table = builder.build();
assertThat(table).isInstanceOf(SparseImmutableTable.class);
validateReserialization(table);
}
public void testSparseSerialization_bothOrders() {
ImmutableTable.Builder<Character, Integer, String> builder = ImmutableTable.builder();
builder.orderRowsBy(Ordering.<Character>natural());
builder.orderColumnsBy(Ordering.<Integer>natural());
builder.put('b', 2, "foo");
builder.put('b', 1, "bar");
builder.put('a', 2, "baz");
builder.put('c', 3, "cat");
builder.put('d', 4, "dog");
Table<Character, Integer, String> table = builder.build();
assertThat(table).isInstanceOf(SparseImmutableTable.class);
validateReserialization(table);
}
private static <R, C, V> void validateReserialization(Table<R, C, V> original) {
Table<R, C, V> copy = SerializableTester.reserializeAndAssert(original);
assertThat(copy.cellSet()).containsExactlyElementsIn(original.cellSet()).inOrder();
assertThat(copy.rowKeySet()).containsExactlyElementsIn(original.rowKeySet()).inOrder();
assertThat(copy.columnKeySet()).containsExactlyElementsIn(original.columnKeySet()).inOrder();
}
@GwtIncompatible // Mind-bogglingly slow in GWT
@AndroidIncompatible // slow
public void testOverflowCondition() {
// See https://code.google.com/p/guava-libraries/issues/detail?id=1322 for details.
ImmutableTable.Builder<Integer, Integer, String> builder = ImmutableTable.builder();
for (int i = 1; i < 0x10000; i++) {
builder.put(i, 0, "foo");
builder.put(0, i, "bar");
}
assertTrue(builder.build() instanceof SparseImmutableTable);
}
}
|