|
| 1 | +package org.codefx.libfx.collection.transform; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.function.BiPredicate; |
| 9 | +import java.util.function.ToIntFunction; |
| 10 | + |
| 11 | +import junit.framework.JUnit4TestAdapter; |
| 12 | +import junit.framework.Test; |
| 13 | +import junit.framework.TestSuite; |
| 14 | + |
| 15 | +import org.junit.Before; |
| 16 | + |
| 17 | +import com.google.common.collect.testing.SampleElements; |
| 18 | +import com.google.common.collect.testing.SetTestSuiteBuilder; |
| 19 | +import com.google.common.collect.testing.TestSetGenerator; |
| 20 | +import com.google.common.collect.testing.features.CollectionFeature; |
| 21 | +import com.google.common.collect.testing.features.CollectionSize; |
| 22 | +import com.google.common.collect.testing.features.Feature; |
| 23 | + |
| 24 | +/** |
| 25 | + * Tests {@link EqualityTransformingSet}. |
| 26 | + */ |
| 27 | +public class EqualityTransformingSetTest { |
| 28 | + |
| 29 | + /** |
| 30 | + * JUnit-3-style method to create the tests run for this class. |
| 31 | + * |
| 32 | + * @return the tests to run |
| 33 | + */ |
| 34 | + public static Test suite() { |
| 35 | + TestSuite suite = new TestSuite("org.codefx.libfx.collection.transform.TransformingSet"); |
| 36 | + suite.addTest(originalEquality()); |
| 37 | + suite.addTest(lengthBasedEquality()); |
| 38 | + return suite; |
| 39 | + } |
| 40 | + |
| 41 | + private static Feature<?>[] features() { |
| 42 | + return new Feature<?>[] { |
| 43 | + // since 'EqualityTransformingSet' passes all calls along, |
| 44 | + // the features are determined by the backing data structure (which is a 'HashSet') |
| 45 | + CollectionSize.ANY, |
| 46 | + CollectionFeature.ALLOWS_NULL_VALUES, |
| 47 | + CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION, |
| 48 | + CollectionFeature.SUPPORTS_ADD, |
| 49 | + CollectionFeature.SUPPORTS_ITERATOR_REMOVE, |
| 50 | + CollectionFeature.SUPPORTS_REMOVE, |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates a test which uses hashCode and equals of the original keys. |
| 56 | + * |
| 57 | + * @return the test case |
| 58 | + */ |
| 59 | + private static Test originalEquality() { |
| 60 | + return SetTestSuiteBuilder |
| 61 | + .using(new TransformingSetGenerator(String::equals, String::hashCode)) |
| 62 | + .named("original equality and hashCode") |
| 63 | + .withFeatures(features()) |
| 64 | + .createTestSuite(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a test which uses hashCode and equals based on the string's lengths. |
| 69 | + * |
| 70 | + * @return the test case |
| 71 | + */ |
| 72 | + private static Test lengthBasedEquality() { |
| 73 | + BiPredicate<String, String> equals = (s1, s2) -> s1.length() == s2.length(); |
| 74 | + ToIntFunction<String> hash = s -> s.length(); |
| 75 | + |
| 76 | + Test generalTests = SetTestSuiteBuilder |
| 77 | + .using(new TransformingSetGenerator(equals, hash)) |
| 78 | + .named("length-based equality and hashCode - general tests") |
| 79 | + .withFeatures(features()) |
| 80 | + .createTestSuite(); |
| 81 | + TestSuite specificTests = new TestSuite("length-based equality and hashCode - specific tests"); |
| 82 | + specificTests.addTest(new JUnit4TestAdapter(LengthBasedEqualityAndHashCodeTests.class)); |
| 83 | + |
| 84 | + TestSuite tests = new TestSuite("length-based equality and hashCode"); |
| 85 | + tests.addTest(generalTests); |
| 86 | + tests.addTest(specificTests); |
| 87 | + return tests; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Tests {@link EqualityTransformingSet} with a specific set of tests geared towards its special functionality, i.e. |
| 92 | + * transforming equals and hashCode. |
| 93 | + */ |
| 94 | + public static class LengthBasedEqualityAndHashCodeTests { |
| 95 | + |
| 96 | + private Set<String> testedSet; |
| 97 | + |
| 98 | + private final BiPredicate<String, String> equals = (s1, s2) -> s1.length() == s2.length(); |
| 99 | + |
| 100 | + private final ToIntFunction<String> hash = s -> s.length(); |
| 101 | + |
| 102 | + @Before |
| 103 | + @SuppressWarnings("javadoc") |
| 104 | + public void createSet() { |
| 105 | + testedSet = EqualityTransformingBuilder |
| 106 | + .forKeyType(String.class) |
| 107 | + .withEquals(equals) |
| 108 | + .withHash(hash) |
| 109 | + .buildSet(); |
| 110 | + } |
| 111 | + |
| 112 | + @org.junit.Test |
| 113 | + @SuppressWarnings("javadoc") |
| 114 | + public void add_containsWithSameLengthElement_true() { |
| 115 | + testedSet.add("aaa"); |
| 116 | + |
| 117 | + assertTrue(testedSet.contains("bbb")); |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + private static class TransformingSetGenerator implements TestSetGenerator<String> { |
| 123 | + |
| 124 | + private final BiPredicate<String, String> equals; |
| 125 | + private final ToIntFunction<String> hash; |
| 126 | + |
| 127 | + public TransformingSetGenerator(BiPredicate<String, String> equals, ToIntFunction<String> hash) { |
| 128 | + this.equals = equals; |
| 129 | + this.hash = hash; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public Set<String> create(Object... elements) { |
| 134 | + Set<String> transformingSet = EqualityTransformingBuilder |
| 135 | + .forKeyType(String.class) |
| 136 | + .withEquals(equals) |
| 137 | + .withHash(hash) |
| 138 | + .buildSet(); |
| 139 | + Arrays.stream(elements) |
| 140 | + .map(String.class::cast) |
| 141 | + .forEach(transformingSet::add); |
| 142 | + return transformingSet; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public SampleElements<String> samples() { |
| 147 | + return new SampleElements<String>("A", "AA", "AAA", "AAAA", "AAAAA"); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public String[] createArray(int length) { |
| 152 | + return new String[length]; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public Iterable<String> order(List<String> insertionOrder) { |
| 157 | + return insertionOrder; |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments