@Test public void serialization() { int size = COLLISIONS.size(); for (int i = 1; i < size; i++) { MutableSet<Integer> set = UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, SIZE) .withAll(COLLISIONS.subList(0, i)); Verify.assertPostSerializedEqualsAndHashCode(set); set.add(null); Verify.assertPostSerializedEqualsAndHashCode(set); } UnifiedSetWithHashingStrategy<Integer> nullBucketZero = UnifiedSetWithHashingStrategy.newSetWith( INTEGER_HASHING_STRATEGY, null, COLLISION_1, COLLISION_2); Verify.assertPostSerializedEqualsAndHashCode(nullBucketZero); UnifiedSetWithHashingStrategy<Integer> simpleSetWithNull = UnifiedSetWithHashingStrategy.newSetWith(INTEGER_HASHING_STRATEGY, null, 1, 2); Verify.assertPostSerializedEqualsAndHashCode(simpleSetWithNull); UnifiedSetWithHashingStrategy<Person> people = UnifiedSetWithHashingStrategy.newSet(LAST_NAME_HASHING_STRATEGY, PEOPLE); Verify.assertPostSerializedEqualsAndHashCode(people); // Testing the hashingStrategy is serialized correctly by making sure it is still hashing by // last name Verify.assertSetsEqual(LAST_NAME_HASHED_SET.castToSet(), people.withAll(PEOPLE.castToList())); }
@Test public void get_with_hashingStrategy() { UnifiedSetWithHashingStrategy<Person> people = UnifiedSetWithHashingStrategy.newSet( HashingStrategies.nullSafeHashingStrategy(LAST_NAME_HASHING_STRATEGY), 2) .withAll(PEOPLE.castToList()); // Putting null then testing geting a null Verify.assertSize(3, people.with((Person) null)); Assert.assertNull(people.get(null)); // Testing it is getting the same reference Assert.assertSame(JOHNSMITH, people.get(JANESMITH)); Assert.assertSame(JOHNSMITH, people.get(JOHNSMITH)); Assert.assertSame(JOHNDOE, people.get(JANEDOE)); Assert.assertSame(JOHNDOE, people.get(JOHNDOE)); Assert.assertSame(JOHNSMITH, people.get(new Person("Anything", "Smith"))); Assert.assertNull(people.get(new Person("John", "NotHere"))); // Testing get throws NullPointerException if the hashingStrategy is not null safe Verify.assertThrows( NullPointerException.class, () -> { UnifiedSetWithHashingStrategy.newSet(LAST_NAME_HASHING_STRATEGY).get(null); }); }
@Test public void put_with_hashingStrategy() { UnifiedSetWithHashingStrategy<Person> people = UnifiedSetWithHashingStrategy.newSet( HashingStrategies.nullSafeHashingStrategy(LAST_NAME_HASHING_STRATEGY), 2) .withAll(PEOPLE.castToList()); // Testing if element already exists, returns the instance in the set Assert.assertSame(JOHNSMITH, people.put(new Person("Anything", "Smith"))); Verify.assertSize(2, people); // Testing if the element doesn't exist, returns the element itself Person notInSet = new Person("Not", "inSet"); Assert.assertSame(notInSet, people.put(notInSet)); Verify.assertSize(3, people); // Testing putting a null to force a rehash Assert.assertNull(people.put(null)); Verify.assertSize(4, people); // Testing put throws NullPointerException if the hashingStrategy is not null safe Verify.assertThrows( NullPointerException.class, () -> { UnifiedSetWithHashingStrategy.newSet(LAST_NAME_HASHING_STRATEGY).put(null); }); }
@Override @Test public void reject() { super.reject(); UnifiedSetWithHashingStrategy<Person> people = UnifiedSetWithHashingStrategy.newSet(LAST_NAME_HASHING_STRATEGY) .withAll(PEOPLE.castToList()); Verify.assertSetsEqual( UnifiedSet.newSetWith(JOHNSMITH), people.reject(each -> "Doe".equals(each.getLastName())).with(JANESMITH)); }