private int iteratorCompareTo( Comparator<AnyValue> comparator, ListValue otherList ) { Iterator<AnyValue> thisIterator = iterator(); Iterator<AnyValue> thatIterator = otherList.iterator(); while ( thisIterator.hasNext() ) { if ( !thatIterator.hasNext() ) { return 1; } int compare = comparator.compare( thisIterator.next(), thatIterator.next() ); if ( compare != 0 ) { return compare; } } if ( thatIterator.hasNext() ) { return -1; } else { return 0; } } }
private void assertListValuesEquals( ListValue appended, ListValue expected ) { assertEquals( expected, appended ); assertEquals( expected.hashCode(), appended.hashCode() ); assertArrayEquals( expected.asArray(), appended.asArray() ); assertTrue( iteratorsEqual(expected.iterator(), appended.iterator()) ); } }
@Test void shouldReturnEmptyListIfEmptyRange() { // Given ListValue inner = list( longValue( 5L ), longValue( 6L ), longValue( 7L ), longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) ); // When ListValue slice = inner.slice( 4, 2 ); // Then assertEquals( slice, EMPTY_LIST ); assertTrue( iteratorsEqual(slice.iterator(), EMPTY_LIST.iterator()) ); }
@Test void shouldHandleNegativeStart() { // Given ListValue inner = list( longValue( 5L ), longValue( 6L ), longValue( 7L ), longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) ); // When ListValue slice = inner.slice( -2, 400000 ); // Then assertEquals( inner, slice ); assertEquals( inner.hashCode(), slice.hashCode() ); assertArrayEquals( inner.asArray(), slice.asArray() ); assertTrue( iteratorsEqual(inner.iterator(), slice.iterator()) ); }
@Test void shouldBeAbleToDropFromList() { // Given ListValue inner = list( longValue( 5L ), longValue( 6L ), longValue( 7L ), longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) ); // When ListValue drop = inner.drop( 4 ); // Then ListValue expected = list( longValue( 9L ), longValue( 10L ), longValue( 11L ) ); assertEquals( expected, drop ); assertEquals( expected.hashCode(), drop.hashCode() ); assertArrayEquals( expected.asArray(), drop.asArray() ); assertTrue( iteratorsEqual(expected.iterator(), drop.iterator()) ); }
@Test void shouldSliceList() { // Given ListValue inner = list( longValue( 5L ), longValue( 6L ), longValue( 7L ), longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) ); // When ListValue slice = inner.slice(2, 4 ); // Then ListValue expected = list( longValue( 7L ), longValue( 8L ) ); assertEquals( expected, slice ); assertEquals( expected.hashCode(), slice.hashCode() ); assertArrayEquals( expected.asArray(), slice.asArray() ); assertTrue( iteratorsEqual(expected.iterator(), slice.iterator()) ); }
@Test void shouldBeAbleToTakeFromList() { // Given ListValue inner = list( longValue( 5L ), longValue( 6L ), longValue( 7L ), longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) ); // When ListValue take = inner.take( 3 ); // Then ListValue expected = list( longValue( 5L ), longValue( 6L ), longValue( 7L ) ); assertEquals( expected, take ); assertEquals( expected.hashCode(), take.hashCode() ); assertArrayEquals( expected.asArray(), take.asArray() ); assertTrue( iteratorsEqual(expected.iterator(), take.iterator()) ); } }
@Test void shouldHandleExceedingRange() { // Given ListValue inner = list( longValue( 5L ), longValue( 6L ), longValue( 7L ), longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) ); // When ListValue slice = inner.slice( 2, 400000 ); // Then ListValue expected = list( longValue( 7L ), longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) ); assertEquals( expected, slice ); assertEquals( expected.hashCode(), slice.hashCode() ); assertArrayEquals( expected.asArray(), slice.asArray() ); assertTrue( iteratorsEqual(expected.iterator(), slice.iterator()) ); }
private int iteratorCompareTo( Comparator<AnyValue> comparator, ListValue otherList ) { Iterator<AnyValue> thisIterator = iterator(); Iterator<AnyValue> thatIterator = otherList.iterator(); while ( thisIterator.hasNext() ) { if ( !thatIterator.hasNext() ) { return 1; } int compare = comparator.compare( thisIterator.next(), thatIterator.next() ); if ( compare != 0 ) { return compare; } } if ( thatIterator.hasNext() ) { return -1; } else { return 0; } } }