protected TypeComparator<T> getComparator(boolean ascending) { TypeComparator<T> comparator = createComparator(ascending); if (comparator == null) { throw new RuntimeException("Test case corrupt. Returns null as comparator."); } return comparator; }
protected TypeSerializer<T> getSerializer() { TypeSerializer<T> serializer = createSerializer(); if (serializer == null) { throw new RuntimeException("Test case corrupt. Returns null as serializer."); } return serializer; }
public void testNormalizedKeysEquals(boolean halfLength) { try { TypeComparator<T> comparator = getComparator(true); T[] data = getSortedData(); int normKeyLen = getNormKeyLen(halfLength, data, comparator); MemorySegment memSeg1 = setupNormalizedKeysMemSegment(data, normKeyLen, comparator); MemorySegment memSeg2 = setupNormalizedKeysMemSegment(data, normKeyLen, comparator); for (int i = 0; i < data.length; i++) { assertTrue(memSeg1.compare(memSeg2, i * normKeyLen, i * normKeyLen, normKeyLen) == 0); } } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); fail("Exception in test: " + e.getMessage()); } }
@Test public void testNormalizedKeysGreatSmallAscDescHalfLength() { // ascending/descending in comparator doesn't matter for normalized keys TypeComparator<T> comparator = getComparator(true); if (!comparator.supportsNormalizedKey()) { return; } testNormalizedKeysGreatSmall(true, comparator, true); testNormalizedKeysGreatSmall(false, comparator, true); }
@Test public void testNormalizedKeysEqualsFullLength() { // Ascending or descending does not matter in this case TypeComparator<T> comparator = getComparator(true); if (!comparator.supportsNormalizedKey()) { return; } testNormalizedKeysEquals(false); }
protected void testEquals(boolean ascending) { try { // Just setup two identical output/inputViews and go over their data to see if compare works TestOutputView out1; TestOutputView out2; TestInputView in1; TestInputView in2; // Now use comparator and compare TypeComparator<T> comparator = getComparator(ascending); T[] data = getSortedData(); for (T d : data) { out2 = new TestOutputView(); writeSortedData(d, out2); in2 = out2.getInputView(); out1 = new TestOutputView(); writeSortedData(d, out1); in1 = out1.getInputView(); assertTrue(comparator.compareSerialized(in1, in2) == 0); } } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); fail("Exception in test: " + e.getMessage()); } }
protected void testNormalizedKeysGreatSmall(boolean greater, TypeComparator<T> comparator, boolean halfLength) { try { T[] data = getSortedData(); int normKeyLen = getNormKeyLen(halfLength, data, comparator); MemorySegment memSegLow = setupNormalizedKeysMemSegment(data, normKeyLen, comparator); MemorySegment memSegHigh = setupNormalizedKeysMemSegment(data, normKeyLen, comparator);
@Test public void testEqualityWithReference() { try { TypeSerializer<T> serializer = createSerializer(); TypeComparator<T> comparator = getComparator(true); TypeComparator<T> comparator2 = getComparator(true); T[] data = getSortedData(); for (T d : data) { comparator.setReference(d); // Make a copy to compare T copy = serializer.copy(d, serializer.createInstance()); // And then test equalTo and compareToReference method of comparator assertTrue(comparator.equalToReference(d)); comparator2.setReference(copy); assertTrue(comparator.compareToReference(comparator2) == 0); } } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); fail("Exception in test: " + e.getMessage()); } }
@Test public void testDuplicate() { try { TypeComparator<T> comparator = getComparator(true); TypeComparator<T> clone = comparator.duplicate(); T[] data = getSortedData(); comparator.setReference(data[0]); clone.setReference(data[1]); assertTrue("Comparator duplication does not work: Altering the reference in a duplicated comparator alters the original comparator's reference.", comparator.equalToReference(data[0]) && clone.equalToReference(data[1])); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Test @SuppressWarnings("unchecked") public void testKeyExtraction() { TypeComparator<T> comparator = getComparator(true); T[] data = getSortedData(); for (T value : data) { TypeComparator[] comparators = comparator.getFlatComparators(); Object[] extractedKeys = new Object[comparators.length]; int insertedKeys = comparator.extractKeys(value, extractedKeys, 0); assertTrue(insertedKeys == comparators.length); for (int i = 0; i < insertedKeys; i++) { // check if some keys are null, although this is not supported if (!supportsNullKeys()) { assertNotNull(extractedKeys[i]); } // compare the extracted key with itself as a basic check // if the extracted key corresponds to the comparator assertTrue(comparators[i].compare(extractedKeys[i], extractedKeys[i]) == 0); } } }
protected void writeSortedData(T value, TestOutputView out) throws IOException { TypeSerializer<T> serializer = getSerializer(); // Write data into a outputView serializer.serialize(value, out); // This are the same tests like in the serializer // Just look if the data is really there after serialization, before testing comparator on it TestInputView in = out.getInputView(); assertTrue("No data available during deserialization.", in.available() > 0); T deserialized = serializer.deserialize(serializer.createInstance(), in); deepEquals("Deserialized value is wrong.", value, deserialized); }
protected T[] getSortedData() { T[] data = getSortedTestData(); if (data == null) { throw new RuntimeException("Test case corrupt. Returns null as test data."); } if (data.length < 2) { throw new RuntimeException("Test case does not provide enough sorted test data."); } return data; }
protected void testGreatSmallAscDesc(boolean ascending, boolean greater) { try { T[] data = getSortedData(); TypeComparator<T> comparator = getComparator(ascending); TestOutputView out1; TestOutputView out2; for (int y = x + 1; y < data.length; y++) { out1 = new TestOutputView(); writeSortedData(data[x], out1); in1 = out1.getInputView(); writeSortedData(data[y], out2); in2 = out2.getInputView();
@Test public void testNormalizedKeyReadWriter() { try { T[] data = getSortedData(); T reuse = getSortedData()[0]; TypeComparator<T> comp1 = getComparator(true); if(!comp1.supportsSerializationWithKeyNormalization()){ return; } TypeComparator<T> comp2 = comp1.duplicate(); comp2.setReference(reuse); TestOutputView out = new TestOutputView(); TestInputView in; for (T value : data) { comp1.setReference(value); comp1.writeWithKeyNormalization(value, out); in = out.getInputView(); comp1.readWithKeyDenormalization(reuse, in); assertTrue(comp1.compareToReference(comp2) == 0); } } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); fail("Exception in test: " + e.getMessage()); } }
@Test public void testNormalizedKeysGreatSmallFullLength() { // ascending/descending in comparator doesn't matter for normalized keys TypeComparator<T> comparator = getComparator(true); if (!comparator.supportsNormalizedKey()) { return; } testNormalizedKeysGreatSmall(true, comparator, false); testNormalizedKeysGreatSmall(false, comparator, false); }
@Test public void testNormalizedKeysEqualsHalfLength() { TypeComparator<T> comparator = getComparator(true); if (!comparator.supportsNormalizedKey()) { return; } testNormalizedKeysEquals(true); }
protected void testGreatSmallAscDescWithReference(boolean ascending, boolean greater) { try { T[] data = getSortedData(); TypeComparator<T> comparatorLow = getComparator(ascending); TypeComparator<T> comparatorHigh = getComparator(ascending); //compares every element in high with every element in low for (int x = 0; x < data.length - 1; x++) { for (int y = x + 1; y < data.length; y++) { comparatorLow.setReference(data[x]); comparatorHigh.setReference(data[y]); if (greater && ascending) { assertTrue(comparatorLow.compareToReference(comparatorHigh) > 0); } if (greater && !ascending) { assertTrue(comparatorLow.compareToReference(comparatorHigh) < 0); } if (!greater && ascending) { assertTrue(comparatorHigh.compareToReference(comparatorLow) < 0); } if (!greater && !ascending) { assertTrue(comparatorHigh.compareToReference(comparatorLow) > 0); } } } } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); fail("Exception in test: " + e.getMessage()); } }