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 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 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()); } }
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()); } }
@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()); } }
protected void testNormalizedKeysGreatSmall(boolean greater, TypeComparator<T> comparator, boolean halfLength) { try { T[] data = getSortedData();
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()); } }
protected void testGreatSmallAscDesc(boolean ascending, boolean greater) { try { T[] data = getSortedData();
@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); } } }