Added more contracts

This commit is contained in:
Claudio Maggioni 2022-11-30 13:43:23 +01:00
parent c15c687013
commit a6cf5541af
14 changed files with 437 additions and 534 deletions

12
pom.xml
View File

@ -52,6 +52,18 @@
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -32,7 +32,7 @@ import java.util.Objects;
*/
// TODO: This is no longer public and will be removed later as CharSet is moved
// to depend on Range.
final class CharRange implements Iterable<Character>, Serializable {
final class CharRange implements Iterable<Character>, Serializable, CharRangeContracts {
/**
* Required for serialization support. Lang version 2.0.
@ -156,6 +156,8 @@ final class CharRange implements Iterable<Character>, Serializable {
*
* @return the start char (inclusive)
*/
@Pure
@Override
public char getStart() {
return this.start;
}
@ -165,6 +167,8 @@ final class CharRange implements Iterable<Character>, Serializable {
*
* @return the end char (inclusive)
*/
@Pure
@Override
public char getEnd() {
return this.end;
}
@ -177,6 +181,8 @@ final class CharRange implements Iterable<Character>, Serializable {
*
* @return {@code true} if negated
*/
@Pure
@Override
public boolean isNegated() {
return negated;
}
@ -188,6 +194,9 @@ final class CharRange implements Iterable<Character>, Serializable {
* @param ch the character to check
* @return {@code true} if this range contains the input character
*/
@Pure
@Override
@Ensures("contains_uses_start_end_range")
public boolean contains(final char ch) {
return (ch >= start && ch <= end) != negated;
}
@ -200,6 +209,9 @@ final class CharRange implements Iterable<Character>, Serializable {
* @return {@code true} if this range entirely contains the input range
* @throws NullPointerException if {@code null} input
*/
@Pure
@Requires("char_range_is_not_null")
@Ensures("characters_in_param_are_in_this")
public boolean contains(final CharRange range) {
Objects.requireNonNull(range, "range");
if (negated) {

View File

@ -0,0 +1,55 @@
/* __ __ __ __ __ ___
* \ \ / / \ \ / / __/
* \ \/ / /\ \ \/ / /
* \____/__/ \__\____/__/
*
* Copyright 2014-2017 Vavr, http://vavr.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.usi.inf.sdm.sdm04;
import ch.usi.si.codelounge.jsicko.Contract;
import java.util.stream.StreamSupport;
@SuppressWarnings("unused") // methods used by jSicko
public interface CharRangeContracts extends Contract {
@Pure
char getStart();
@Pure
char getEnd();
@Pure
boolean isNegated();
@Pure
boolean contains(final char ch);
@Pure
default boolean char_range_is_not_null(final CharRange range) {
return range != null;
}
@Pure
default boolean contains_uses_start_end_range(final boolean returns, final char ch) {
return returns == (this.isNegated() != (this.getStart() <= ch && this.getEnd() >= ch));
}
@Pure
default boolean characters_in_param_are_in_this(final boolean returns, final CharRange range) {
return returns == StreamSupport.stream(range.spliterator(), false).allMatch(this::contains);
}
}

View File

@ -1,255 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.usi.inf.sdm.sdm04;
import org.apache.commons.lang3.Validate;
import java.util.Objects;
/**
* Provides IEEE-754r variants of NumberUtils methods.
*
* <p>See: <a href="https://en.wikipedia.org/wiki/IEEE_754r">https://en.wikipedia.org/wiki/IEEE_754r</a></p>
*
* @since 2.4
*/
public class IEEE754rUtils implements IEEE754rUtilsContracts {
/**
* Returns the minimum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws NullPointerException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty
* @since 3.4 Changed signature from min(double[]) to min(double...)
*/
@Requires("non_null_non_empty_arg")
@Ensures("is_minimum_element")
public double min(final double... array) {
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
// Finds and returns min
double min = array[0];
for (int i = 1; i < array.length; i++) {
min = min(array[i], min);
}
return min;
}
/**
* Returns the minimum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws NullPointerException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty
* @since 3.4 Changed signature from min(float[]) to min(float...)
*/
public float min(final float... array) {
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
// Finds and returns min
float min = array[0];
for (int i = 1; i < array.length; i++) {
min = min(array[i], min);
}
return min;
}
/**
* Gets the minimum of three {@code double} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the smallest of the values
*/
public double min(final double a, final double b, final double c) {
return min(min(a, b), c);
}
/**
* Gets the minimum of two {@code double} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @return the smallest of the values
*/
public double min(final double a, final double b) {
if (Double.isNaN(a)) {
return b;
}
if (Double.isNaN(b)) {
return a;
}
return Math.min(a, b);
}
/**
* Gets the minimum of three {@code float} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the smallest of the values
*/
public float min(final float a, final float b, final float c) {
return min(min(a, b), c);
}
/**
* Gets the minimum of two {@code float} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @return the smallest of the values
*/
public float min(final float a, final float b) {
if (Float.isNaN(a)) {
return b;
}
if (Float.isNaN(b)) {
return a;
}
return Math.min(a, b);
}
/**
* Returns the maximum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws NullPointerException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty
* @since 3.4 Changed signature from max(double[]) to max(double...)
*/
public double max(final double... array) {
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
// Finds and returns max
double max = array[0];
for (int j = 1; j < array.length; j++) {
max = max(array[j], max);
}
return max;
}
/**
* Returns the maximum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws NullPointerException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty
* @since 3.4 Changed signature from max(float[]) to max(float...)
*/
public float max(final float... array) {
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
// Finds and returns max
float max = array[0];
for (int j = 1; j < array.length; j++) {
max = max(array[j], max);
}
return max;
}
/**
* Gets the maximum of three {@code double} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the largest of the values
*/
public double max(final double a, final double b, final double c) {
return max(max(a, b), c);
}
/**
* Gets the maximum of two {@code double} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @return the largest of the values
*/
public double max(final double a, final double b) {
if (Double.isNaN(a)) {
return b;
}
if (Double.isNaN(b)) {
return a;
}
return Math.max(a, b);
}
/**
* Gets the maximum of three {@code float} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the largest of the values
*/
public float max(final float a, final float b, final float c) {
return max(max(a, b), c);
}
/**
* Gets the maximum of two {@code float} values.
*
* <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
*
* @param a value 1
* @param b value 2
* @return the largest of the values
*/
public float max(final float a, final float b) {
if (Float.isNaN(a)) {
return b;
}
if (Float.isNaN(b)) {
return a;
}
return Math.max(a, b);
}
}

View File

@ -1,65 +0,0 @@
package ch.usi.inf.sdm.sdm04;
import ch.usi.si.codelounge.jsicko.Contract;
import java.util.Optional;
import java.util.function.Supplier;
public class Option<T> extends Optional<T> Contract {
private final io.vavr.control.Option<T> backing;
public Option(final io.vavr.control.Option<T> backing) {
this.backing = backing;
}
public static <T> Option<T> none() {
return new Option<>(io.vavr.control.Option.none());
}
public static <T> Option<T> some(final T value) {
return new Option<>(io.vavr.control.Option.some(value));
}
static <T> Option<T> of(T value) {
return value == null ? none() : some(value);
}
static <T> Option<T> narrow(Option<? extends T> option) {
return new Option<>(io.vavr.control.Option.narrow(option.backing));
}
static <T> Option<T> when(boolean condition, Supplier<? extends T> supplier) {
return new Option<>(io.vavr.control.Option.when(condition, supplier));
}
static <T> Option<T> when(boolean condition, T value) {
return new Option<>(io.vavr.control.Option.when(condition, value));
}
static <T> Option<T> ofOptional(Optional<? extends T> optional) {
return new Option<>(io.vavr.control.Option.ofOptional(optional));
}
@Override
@Pure
public boolean isEmpty() {
return backing.isEmpty();
}
@Override
public String stringPrefix() {
return backing.stringPrefix();
}
@Override
@Requires("isNotEmpty")
public T get() {
return backing.get();
}
@Pure
@SuppressWarnings("unused")
public boolean isNotEmpty() {
return !isEmpty();
}
}

View File

@ -27,7 +27,7 @@ import java.util.Objects;
*
* @since 2.4
*/
public class IEEE754rUtils {
public class IEEE754rUtils implements IEEE754rUtilsContracts {
/**
* Returns the minimum value in an array.
@ -38,7 +38,9 @@ public class IEEE754rUtils {
* @throws IllegalArgumentException if {@code array} is empty
* @since 3.4 Changed signature from min(double[]) to min(double...)
*/
public static double min(final double... array) {
@Requires("non_null_non_empty_arg")
@Ensures("is_minimum_element")
public double min(final double... array) {
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
@ -60,7 +62,9 @@ public class IEEE754rUtils {
* @throws IllegalArgumentException if {@code array} is empty
* @since 3.4 Changed signature from min(float[]) to min(float...)
*/
public static float min(final float... array) {
@Requires("non_null_non_empty_arg_float")
@Ensures("is_minimum_element_float")
public float min(final float... array) {
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
@ -83,7 +87,8 @@ public class IEEE754rUtils {
* @param c value 3
* @return the smallest of the values
*/
public static double min(final double a, final double b, final double c) {
@Ensures("is_minimum_element_3")
public double min(final double a, final double b, final double c) {
return min(min(a, b), c);
}
@ -96,7 +101,8 @@ public class IEEE754rUtils {
* @param b value 2
* @return the smallest of the values
*/
public static double min(final double a, final double b) {
@Ensures("is_minimum_element_2")
public double min(final double a, final double b) {
if (Double.isNaN(a)) {
return b;
}
@ -116,7 +122,8 @@ public class IEEE754rUtils {
* @param c value 3
* @return the smallest of the values
*/
public static float min(final float a, final float b, final float c) {
@Ensures("is_minimum_element_3_float")
public float min(final float a, final float b, final float c) {
return min(min(a, b), c);
}
@ -129,7 +136,8 @@ public class IEEE754rUtils {
* @param b value 2
* @return the smallest of the values
*/
public static float min(final float a, final float b) {
@Ensures("is_minimum_element_2_float")
public float min(final float a, final float b) {
if (Float.isNaN(a)) {
return b;
}
@ -148,7 +156,9 @@ public class IEEE754rUtils {
* @throws IllegalArgumentException if {@code array} is empty
* @since 3.4 Changed signature from max(double[]) to max(double...)
*/
public static double max(final double... array) {
@Requires("non_null_non_empty_arg")
@Ensures("is_maximum_element")
public double max(final double... array) {
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
@ -170,7 +180,9 @@ public class IEEE754rUtils {
* @throws IllegalArgumentException if {@code array} is empty
* @since 3.4 Changed signature from max(float[]) to max(float...)
*/
public static float max(final float... array) {
@Requires("non_null_non_empty_arg_float")
@Ensures("is_maximum_element_float")
public float max(final float... array) {
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
@ -193,7 +205,8 @@ public class IEEE754rUtils {
* @param c value 3
* @return the largest of the values
*/
public static double max(final double a, final double b, final double c) {
@Ensures("is_maximum_element_3")
public double max(final double a, final double b, final double c) {
return max(max(a, b), c);
}
@ -206,7 +219,8 @@ public class IEEE754rUtils {
* @param b value 2
* @return the largest of the values
*/
public static double max(final double a, final double b) {
@Ensures("is_maximum_element_2")
public double max(final double a, final double b) {
if (Double.isNaN(a)) {
return b;
}
@ -226,7 +240,8 @@ public class IEEE754rUtils {
* @param c value 3
* @return the largest of the values
*/
public static float max(final float a, final float b, final float c) {
@Ensures("is_maximum_element_3_float")
public float max(final float a, final float b, final float c) {
return max(max(a, b), c);
}
@ -239,7 +254,8 @@ public class IEEE754rUtils {
* @param b value 2
* @return the largest of the values
*/
public static float max(final float a, final float b) {
@Ensures("is_maximum_element_2_float")
public float max(final float a, final float b) {
if (Float.isNaN(a)) {
return b;
}

View File

@ -0,0 +1,98 @@
/* __ __ __ __ __ ___
* \ \ / / \ \ / / __/
* \ \/ / /\ \ \/ / /
* \____/__/ \__\____/__/
*
* Copyright 2014-2017 Vavr, http://vavr.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.usi.inf.sdm.sdm04.math;
import ch.usi.si.codelounge.jsicko.Contract;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
@SuppressWarnings("unused") // methods used by jSicko
public interface IEEE754rUtilsContracts extends Contract {
@Pure
default boolean non_null_non_empty_arg(final double[] array) {
return array != null && array.length > 0;
}
@Pure
default boolean non_null_non_empty_arg_float(final float[] array) {
return array != null && array.length > 0;
}
@Pure
default boolean is_minimum_element(final double returns, final double[] array) {
return DoubleStream.of(array).filter(value -> !Double.isNaN(value)).allMatch(e -> returns <= e);
}
@Pure
default boolean is_maximum_element(final double returns, final double[] array) {
return DoubleStream.of(array).filter(value -> !Double.isNaN(value)).allMatch(e -> returns >= e);
}
@Pure
default boolean is_minimum_element_float(final float returns, final float[] array) {
return is_minimum_element(returns, IntStream.range(0, array.length).mapToDouble(i -> array[i]).toArray());
}
@Pure
default boolean is_maximum_element_float(final float returns, float[] array) {
return is_maximum_element(returns, IntStream.range(0, array.length).mapToDouble(i -> array[i]).toArray());
}
@Pure
default boolean is_minimum_element_2(final double returns, final double a, final double b) {
return is_minimum_element(returns, new double[] { a, b });
}
@Pure
default boolean is_minimum_element_3(final double returns, final double a, final double b, final double c) {
return is_minimum_element(returns, new double[] { a, b, c });
}
@Pure
default boolean is_minimum_element_2_float(final float returns, final float a, final float b) {
return is_minimum_element(returns, new double[] { a, b });
}
@Pure
default boolean is_minimum_element_3_float(final float returns, final float a, final float b, final float c) {
return is_minimum_element(returns, new double[] { a, b, c });
}
@Pure
default boolean is_maximum_element_2(final double returns, final double a, final double b) {
return is_maximum_element(returns, new double[] { a, b });
}
@Pure
default boolean is_maximum_element_3(final double returns, final double a, final double b, final double c) {
return is_maximum_element(returns, new double[] { a, b, c });
}
@Pure
default boolean is_maximum_element_2_float(final float returns, final float a, final float b) {
return is_maximum_element(returns, new double[] { a, b });
}
@Pure
default boolean is_maximum_element_3_float(final float returns, final float a, final float b, final float c) {
return is_maximum_element(returns, new double[] { a, b, c });
}
}

View File

@ -26,10 +26,9 @@ import java.util.stream.IntStream;
* <p>
* Originally from Apache Commons VFS with more added to act as a fluent replacement for {@link BitSet}.
* </p>
*
* @since 3.13.0
*/
public final class FluentBitSet implements Cloneable, Serializable {
public final class FluentBitSet implements Cloneable, Serializable, FluentBitSetContracts {
private static final long serialVersionUID = 1L;
@ -73,6 +72,7 @@ public final class FluentBitSet implements Cloneable, Serializable {
* @param set a bit set.
* @return this.
*/
@Ensures("and_bs")
public FluentBitSet and(final BitSet set) {
bitSet.and(set);
return this;
@ -86,6 +86,7 @@ public final class FluentBitSet implements Cloneable, Serializable {
* @param set a bit set.
* @return this.
*/
@Ensures("and_fbs")
public FluentBitSet and(final FluentBitSet set) {
bitSet.and(set.bitSet);
return this;
@ -97,6 +98,7 @@ public final class FluentBitSet implements Cloneable, Serializable {
* @param set the {@link BitSet} with which to mask this {@link BitSet}.
* @return this.
*/
@Ensures("and_not_bs")
public FluentBitSet andNot(final BitSet set) {
bitSet.andNot(set);
return this;
@ -108,6 +110,7 @@ public final class FluentBitSet implements Cloneable, Serializable {
* @param set the {@link BitSet} with which to mask this {@link BitSet}.
* @return this.
*/
@Ensures("and_not_fbs")
public FluentBitSet andNot(final FluentBitSet set) {
this.bitSet.andNot(set.bitSet);
return this;
@ -118,6 +121,8 @@ public final class FluentBitSet implements Cloneable, Serializable {
*
* @return the wrapped bit set.
*/
@Pure
@Override
public BitSet bitSet() {
return bitSet;
}
@ -127,6 +132,8 @@ public final class FluentBitSet implements Cloneable, Serializable {
*
* @return the number of bits set to {@code true} in this {@link BitSet}.
*/
@Pure
@Override
public int cardinality() {
return bitSet.cardinality();
}
@ -136,6 +143,7 @@ public final class FluentBitSet implements Cloneable, Serializable {
*
* @return this.
*/
@Ensures("clear_void")
public FluentBitSet clear() {
bitSet.clear();
return this;
@ -145,8 +153,8 @@ public final class FluentBitSet implements Cloneable, Serializable {
* Sets the bits specified by the indexes to {@code false}.
*
* @param bitIndexArray the index of the bit to be cleared.
* @return this.
* @throws IndexOutOfBoundsException if the specified index is negative.
* @return this.
*/
public FluentBitSet clear(final int... bitIndexArray) {
for (final int e : bitIndexArray) {
@ -159,8 +167,8 @@ public final class FluentBitSet implements Cloneable, Serializable {
* Sets the bit specified by the index to {@code false}.
*
* @param bitIndex the index of the bit to be cleared.
* @return this.
* @throws IndexOutOfBoundsException if the specified index is negative.
* @return this.
*/
public FluentBitSet clear(final int bitIndex) {
bitSet.clear(bitIndex);
@ -172,10 +180,10 @@ public final class FluentBitSet implements Cloneable, Serializable {
* {@code false}.
*
* @param fromIndex index of the first bit to be cleared.
* @param toIndex index after the last bit to be cleared.
* @return this.
* @param toIndex index after the last bit to be cleared.
* @throws IndexOutOfBoundsException if {@code fromIndex} is negative, or {@code toIndex} is negative, or
* {@code fromIndex} is larger than {@code toIndex}.
* {@code fromIndex} is larger than {@code toIndex}.
* @return this.
*/
public FluentBitSet clear(final int fromIndex, final int toIndex) {
bitSet.clear(fromIndex, toIndex);
@ -210,8 +218,8 @@ public final class FluentBitSet implements Cloneable, Serializable {
* Sets the bit at the specified index to the complement of its current value.
*
* @param bitIndex the index of the bit to flip.
* @return this.
* @throws IndexOutOfBoundsException if the specified index is negative.
* @return this.
*/
public FluentBitSet flip(final int bitIndex) {
bitSet.flip(bitIndex);
@ -223,10 +231,10 @@ public final class FluentBitSet implements Cloneable, Serializable {
* complement of its current value.
*
* @param fromIndex index of the first bit to flip.
* @param toIndex index after the last bit to flip.
* @return this.
* @param toIndex index after the last bit to flip.
* @throws IndexOutOfBoundsException if {@code fromIndex} is negative, or {@code toIndex} is negative, or
* {@code fromIndex} is larger than {@code toIndex}.
* {@code fromIndex} is larger than {@code toIndex}.
* @return this.
*/
public FluentBitSet flip(final int fromIndex, final int toIndex) {
bitSet.flip(fromIndex, toIndex);
@ -241,6 +249,9 @@ public final class FluentBitSet implements Cloneable, Serializable {
* @return the value of the bit with the specified index.
* @throws IndexOutOfBoundsException if the specified index is negative.
*/
@Pure
@Override
@Requires("get_int")
public boolean get(final int bitIndex) {
return bitSet.get(bitIndex);
}
@ -250,10 +261,10 @@ public final class FluentBitSet implements Cloneable, Serializable {
* {@code toIndex} (exclusive).
*
* @param fromIndex index of the first bit to include.
* @param toIndex index after the last bit to include.
* @param toIndex index after the last bit to include.
* @return a new {@link BitSet} from a range of this {@link BitSet}.
* @throws IndexOutOfBoundsException if {@code fromIndex} is negative, or {@code toIndex} is negative, or
* {@code fromIndex} is larger than {@code toIndex}.
* {@code fromIndex} is larger than {@code toIndex}.
*/
public FluentBitSet get(final int fromIndex, final int toIndex) {
return new FluentBitSet(bitSet.get(fromIndex, toIndex));
@ -271,6 +282,7 @@ public final class FluentBitSet implements Cloneable, Serializable {
* @param set {@link BitSet} to intersect with.
* @return boolean indicating whether this {@link BitSet} intersects the specified {@link BitSet}.
*/
@Pure
public boolean intersects(final BitSet set) {
return bitSet.intersects(set);
}
@ -291,6 +303,8 @@ public final class FluentBitSet implements Cloneable, Serializable {
*
* @return boolean indicating whether this {@link BitSet} is empty.
*/
@Pure
@Ensures("is_empty")
public boolean isEmpty() {
return bitSet.isEmpty();
}
@ -301,6 +315,8 @@ public final class FluentBitSet implements Cloneable, Serializable {
*
* @return the logical size of this {@link BitSet}.
*/
@Pure
@Override
public int length() {
return bitSet.length();
}
@ -349,6 +365,7 @@ public final class FluentBitSet implements Cloneable, Serializable {
* @param set a bit set.
* @return this.
*/
@Ensures("or_bs")
public FluentBitSet or(final BitSet set) {
bitSet.or(set);
return this;
@ -377,6 +394,7 @@ public final class FluentBitSet implements Cloneable, Serializable {
* @param set a bit set.
* @return this.
*/
@Ensures("or_fbs")
public FluentBitSet or(final FluentBitSet set) {
this.bitSet.or(set.bitSet);
return this;
@ -420,8 +438,8 @@ public final class FluentBitSet implements Cloneable, Serializable {
* Sets the bit at the specified indexes to {@code true}.
*
* @param bitIndexArray a bit index array.
* @return this.
* @throws IndexOutOfBoundsException if the specified index is negative.
* @return this.
*/
public FluentBitSet set(final int... bitIndexArray) {
for (final int e : bitIndexArray) {
@ -434,8 +452,8 @@ public final class FluentBitSet implements Cloneable, Serializable {
* Sets the bit at the specified index to {@code true}.
*
* @param bitIndex a bit index
* @return this.
* @throws IndexOutOfBoundsException if the specified index is negative
* @return this.
*/
public FluentBitSet set(final int bitIndex) {
bitSet.set(bitIndex);
@ -446,11 +464,10 @@ public final class FluentBitSet implements Cloneable, Serializable {
* Sets the bit at the specified index to the specified value.
*
* @param bitIndex a bit index.
* @param value a boolean value to set.
* @return this.
* @param value a boolean value to set.
* @throws IndexOutOfBoundsException if the specified index is negative.
* @return this.
*/
public FluentBitSet set(final int bitIndex, final boolean value) {
bitSet.set(bitIndex, value);
return this;
@ -461,10 +478,10 @@ public final class FluentBitSet implements Cloneable, Serializable {
* {@code true}.
*
* @param fromIndex index of the first bit to be set.
* @param toIndex index after the last bit to be set.
* @return this.
* @param toIndex index after the last bit to be set.
* @throws IndexOutOfBoundsException if {@code fromIndex} is negative, or {@code toIndex} is negative, or
* {@code fromIndex} is larger than {@code toIndex}.
* {@code fromIndex} is larger than {@code toIndex}.
* @return this.
*/
public FluentBitSet set(final int fromIndex, final int toIndex) {
bitSet.set(fromIndex, toIndex);
@ -476,11 +493,11 @@ public final class FluentBitSet implements Cloneable, Serializable {
* specified value.
*
* @param fromIndex index of the first bit to be set.
* @param toIndex index after the last bit to be set.
* @param value value to set the selected bits to.
* @return this.
* @param toIndex index after the last bit to be set.
* @param value value to set the selected bits to.
* @throws IndexOutOfBoundsException if {@code fromIndex} is negative, or {@code toIndex} is negative, or
* {@code fromIndex} is larger than {@code toIndex}.
* {@code fromIndex} is larger than {@code toIndex}.
* @return this.
*/
public FluentBitSet set(final int fromIndex, final int toIndex, final boolean value) {
bitSet.set(fromIndex, toIndex, value);
@ -492,10 +509,10 @@ public final class FluentBitSet implements Cloneable, Serializable {
* {@code true}.
*
* @param fromIndex index of the first bit to be set
* @param toIndex index of the last bit to be set
* @return this.
* @param toIndex index of the last bit to be set
* @throws IndexOutOfBoundsException if {@code fromIndex} is negative, or {@code toIndex} is negative, or
* {@code fromIndex} is larger than {@code toIndex}
* {@code fromIndex} is larger than {@code toIndex}
* @return this.
*/
public FluentBitSet setInclusive(final int fromIndex, final int toIndex) {
bitSet.set(fromIndex, toIndex + 1);

View File

@ -0,0 +1,133 @@
/* __ __ __ __ __ ___
* \ \ / / \ \ / / __/
* \ \/ / /\ \ \/ / /
* \____/__/ \__\____/__/
*
* Copyright 2014-2017 Vavr, http://vavr.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.usi.inf.sdm.sdm04.util;
import ch.usi.si.codelounge.jsicko.Contract;
import java.util.BitSet;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import static ch.usi.si.codelounge.jsicko.Contract.old;
@SuppressWarnings("unused") // methods used by jSicko
public interface FluentBitSetContracts extends FluentBitSetContractsContracts, Contract {
@Pure
boolean intersects(final FluentBitSet set);
@Pure
int cardinality();
@Pure
BitSet bitSet();
@Pure
int length();
@Pure
boolean get(final int bitIndex);
@Pure
default boolean test_cardinality(final FluentBitSet returns, final boolean oldShouldBeGreater) {
return old(this).cardinality() == this.cardinality() ||
oldShouldBeGreater == old(this).cardinality() > this.cardinality();
}
@Pure
@Requires("cardinality_positive")
default boolean test_lesser_cardinality(final FluentBitSet returns, final long cardinality) {
return returns.cardinality() == (old(this).cardinality() - cardinality);
}
@Pure
default boolean test_length(final FluentBitSet returns, final Supplier<Integer> paramLength) {
return old(this).length() >= this.length() || paramLength.get() >= this.length();
}
@Pure
default boolean and_fbs(final FluentBitSet returns, final FluentBitSet set) {
return test_cardinality(returns, true) && test_length(returns, set::length);
}
@Pure
default boolean or_fbs(final FluentBitSet returns, final FluentBitSet set) {
return test_cardinality(returns, false) && test_length(returns, set::length);
}
@Pure
default boolean and_not_fbs(final FluentBitSet returns, final FluentBitSet set) {
final FluentBitSet notSet = new FluentBitSet(set.bitSet()).flip(0, set.length());
return test_cardinality(returns, true) && test_length(returns, notSet::length);
}
@Pure
default boolean and_not_bs(final FluentBitSet returns, final BitSet set) {
final FluentBitSet notSet = new FluentBitSet(set).flip(0, set.length());
return test_cardinality(returns, true) && test_length(returns, notSet::length);
}
@Pure
default boolean and_bs(final FluentBitSet returns, final BitSet set) {
return test_cardinality(returns, true) && test_length(returns, set::length);
}
@Pure
default boolean or_bs(final FluentBitSet returns, final BitSet set) {
return test_cardinality(returns, false) && test_length(returns, set::length);
}
@Pure
default boolean clear_void(final FluentBitSet returns) {
return cardinality() == 0;
}
@Pure
default boolean clear_int(final FluentBitSet returns, final int bitIndex) {
return test_lesser_cardinality(returns, old(this).get(bitIndex) ? 1 : 0);
}
@Pure
default boolean clear_int_int(final FluentBitSet returns, final int fromIndex, final int toIndex) {
return test_lesser_cardinality(returns, IntStream.range(fromIndex, toIndex)
.filter(old(this)::get)
.count());
}
@Pure
default boolean is_empty(final boolean returns) {
return returns == (cardinality() == 0);
}
@Pure
default boolean get_int(final int bitIndex) {
return bitIndex >= 0;
}
@Pure
default boolean intersects_bs(final boolean returns, final BitSet set) {
return returns == set.intersects(this.bitSet());
}
@Pure
default boolean intersects_fbs(final boolean returns, final FluentBitSet set) {
return intersects_bs(returns, set.bitSet());
}
}

View File

@ -17,21 +17,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.usi.inf.sdm.sdm04;
package ch.usi.inf.sdm.sdm04.util;
import ch.usi.si.codelounge.jsicko.Contract;
import java.util.stream.DoubleStream;
@SuppressWarnings("unused") // methods used by jSicko
public interface IEEE754rUtilsContracts extends Contract {
@SuppressWarnings("unused") // used by jSicko
public interface FluentBitSetContractsContracts extends Contract {
@Pure
default boolean non_null_non_empty_arg(double[] array) {
return array != null && array.length > 0;
}
@Pure
default boolean is_minimum_element(final double returns, double[] array) {
return DoubleStream.of(array).filter(value -> !Double.isNaN(value)).allMatch(e -> returns <= e);
default boolean cardinality_positive(final FluentBitSet returns, final int cardinality) {
return cardinality >= 0;
}
}

View File

@ -18,19 +18,14 @@
*/
package ch.usi.inf.sdm.sdm04;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.commons.lang3.SerializationUtils;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.apache.commons.lang3.SerializationUtils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* Unit tests {@link CharRange}.
@ -307,8 +302,8 @@ public class CharRangeTest {
@Test
public void testContainsNullArg() {
final CharRange range = CharRange.is('a');
final NullPointerException e = assertThrows(NullPointerException.class, () -> range.contains(null));
assertEquals("range", e.getMessage());
// removed NPE message assertion as jSicko throws an error instead
assertThrows(Throwable.class, () -> range.contains(null));
}
@Test

View File

@ -1,105 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.usi.inf.sdm.sdm04;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/**
* Unit tests {@link org.apache.commons.lang3.math.IEEE754rUtils}.
*/
public class IEEE754rUtilsTest {
@Test
public void testConstructorExists() {
new IEEE754rUtils();
}
@Test
public void testEnforceExceptions() {
assertThrows(
NullPointerException.class,
() -> new IEEE754rUtils().min( (float[]) null),
"IllegalArgumentException expected for null input");
// Changed the exception type to throwable so a
assertThrows(
Throwable.class,
new IEEE754rUtils()::min,
"IllegalArgumentException expected for empty input");
assertThrows(
Throwable.class,
() -> new IEEE754rUtils().max((float[]) null),
"IllegalArgumentException expected for null input");
assertThrows(
Throwable.class,
new IEEE754rUtils()::max,
"IllegalArgumentException expected for empty input");
assertThrows(
Throwable.class,
() -> new IEEE754rUtils().min( (double[]) null),
"IllegalArgumentException expected for null input");
assertThrows(
Throwable.class,
new IEEE754rUtils()::min,
"IllegalArgumentException expected for empty input");
assertThrows(
Throwable.class,
() -> new IEEE754rUtils().max( (double[]) null),
"IllegalArgumentException expected for null input");
assertThrows(
Throwable.class,
new IEEE754rUtils()::max,
"IllegalArgumentException expected for empty input");
}
@Test
public void testLang381() {
assertEquals(1.2, new IEEE754rUtils().min(1.2, 2.5, Double.NaN), 0.01);
assertEquals(2.5, new IEEE754rUtils().max(1.2, 2.5, Double.NaN), 0.01);
assertTrue(Double.isNaN(new IEEE754rUtils().max(Double.NaN, Double.NaN, Double.NaN)));
assertEquals(1.2f, new IEEE754rUtils().min(1.2f, 2.5f, Float.NaN), 0.01);
assertEquals(2.5f, new IEEE754rUtils().max(1.2f, 2.5f, Float.NaN), 0.01);
assertTrue(Float.isNaN(new IEEE754rUtils().max(Float.NaN, Float.NaN, Float.NaN)));
final double[] a = { 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN };
assertEquals(42.0, new IEEE754rUtils().max(a), 0.01);
assertEquals(1.2, new IEEE754rUtils().min(a), 0.01);
final double[] b = { Double.NaN, 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN };
assertEquals(42.0, new IEEE754rUtils().max(b), 0.01);
assertEquals(1.2, new IEEE754rUtils().min(b), 0.01);
final float[] aF = { 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN };
assertEquals(1.2f, new IEEE754rUtils().min(aF), 0.01);
assertEquals(42.0f, new IEEE754rUtils().max(aF), 0.01);
final float[] bF = { Float.NaN, 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN };
assertEquals(1.2f, new IEEE754rUtils().min(bF), 0.01);
assertEquals(42.0f, new IEEE754rUtils().max(bF), 0.01);
}
}

View File

@ -34,71 +34,72 @@ public class IEEE754rUtilsTest {
@Test
public void testEnforceExceptions() {
// Changed the exception types to Throwable to allow jSicko assertion errors
assertThrows(
NullPointerException.class,
() -> IEEE754rUtils.min( (float[]) null),
Throwable.class,
() -> new IEEE754rUtils().min( (float[]) null),
"IllegalArgumentException expected for null input");
assertThrows(
IllegalArgumentException.class,
IEEE754rUtils::min,
Throwable.class,
new IEEE754rUtils()::min,
"IllegalArgumentException expected for empty input");
assertThrows(
NullPointerException.class,
() -> IEEE754rUtils.max( (float[]) null),
Throwable.class,
() -> new IEEE754rUtils().max((float[]) null),
"IllegalArgumentException expected for null input");
assertThrows(
IllegalArgumentException.class,
IEEE754rUtils::max,
Throwable.class,
new IEEE754rUtils()::max,
"IllegalArgumentException expected for empty input");
assertThrows(
NullPointerException.class,
() -> IEEE754rUtils.min( (double[]) null),
Throwable.class,
() -> new IEEE754rUtils().min( (double[]) null),
"IllegalArgumentException expected for null input");
assertThrows(
IllegalArgumentException.class,
IEEE754rUtils::min,
Throwable.class,
new IEEE754rUtils()::min,
"IllegalArgumentException expected for empty input");
assertThrows(
NullPointerException.class,
() -> IEEE754rUtils.max( (double[]) null),
Throwable.class,
() -> new IEEE754rUtils().max( (double[]) null),
"IllegalArgumentException expected for null input");
assertThrows(
IllegalArgumentException.class,
IEEE754rUtils::max,
Throwable.class,
new IEEE754rUtils()::max,
"IllegalArgumentException expected for empty input");
}
@Test
public void testLang381() {
assertEquals(1.2, IEEE754rUtils.min(1.2, 2.5, Double.NaN), 0.01);
assertEquals(2.5, IEEE754rUtils.max(1.2, 2.5, Double.NaN), 0.01);
assertTrue(Double.isNaN(IEEE754rUtils.max(Double.NaN, Double.NaN, Double.NaN)));
assertEquals(1.2f, IEEE754rUtils.min(1.2f, 2.5f, Float.NaN), 0.01);
assertEquals(2.5f, IEEE754rUtils.max(1.2f, 2.5f, Float.NaN), 0.01);
assertTrue(Float.isNaN(IEEE754rUtils.max(Float.NaN, Float.NaN, Float.NaN)));
assertEquals(1.2, new IEEE754rUtils().min(1.2, 2.5, Double.NaN), 0.01);
assertEquals(2.5, new IEEE754rUtils().max(1.2, 2.5, Double.NaN), 0.01);
assertTrue(Double.isNaN(new IEEE754rUtils().max(Double.NaN, Double.NaN, Double.NaN)));
assertEquals(1.2f, new IEEE754rUtils().min(1.2f, 2.5f, Float.NaN), 0.01);
assertEquals(2.5f, new IEEE754rUtils().max(1.2f, 2.5f, Float.NaN), 0.01);
assertTrue(Float.isNaN(new IEEE754rUtils().max(Float.NaN, Float.NaN, Float.NaN)));
final double[] a = { 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN };
assertEquals(42.0, IEEE754rUtils.max(a), 0.01);
assertEquals(1.2, IEEE754rUtils.min(a), 0.01);
assertEquals(42.0, new IEEE754rUtils().max(a), 0.01);
assertEquals(1.2, new IEEE754rUtils().min(a), 0.01);
final double[] b = { Double.NaN, 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN };
assertEquals(42.0, IEEE754rUtils.max(b), 0.01);
assertEquals(1.2, IEEE754rUtils.min(b), 0.01);
assertEquals(42.0, new IEEE754rUtils().max(b), 0.01);
assertEquals(1.2, new IEEE754rUtils().min(b), 0.01);
final float[] aF = { 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN };
assertEquals(1.2f, IEEE754rUtils.min(aF), 0.01);
assertEquals(42.0f, IEEE754rUtils.max(aF), 0.01);
assertEquals(1.2f, new IEEE754rUtils().min(aF), 0.01);
assertEquals(42.0f, new IEEE754rUtils().max(aF), 0.01);
final float[] bF = { Float.NaN, 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN };
assertEquals(1.2f, IEEE754rUtils.min(bF), 0.01);
assertEquals(42.0f, IEEE754rUtils.max(bF), 0.01);
assertEquals(1.2f, new IEEE754rUtils().min(bF), 0.01);
assertEquals(42.0f, new IEEE754rUtils().max(bF), 0.01);
}
}

View File

@ -17,19 +17,14 @@
package ch.usi.inf.sdm.sdm04.util;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.BitSet;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.BitSet;
import static org.junit.jupiter.api.Assertions.*;
/**
* Tests {@link FluentBitSet}.
* <p>
@ -381,17 +376,17 @@ public class FluentBitSetTest {
// test illegal args
bs = newInstance(10);
assertThrows(IndexOutOfBoundsException.class, () -> newInstance(10).clear(-1, 3),
"Test1: Attempt to flip with negative index failed to generate exception");
"Test1: Attempt to flip with negative index failed to generate exception");
assertThrows(IndexOutOfBoundsException.class, () -> newInstance(10).clear(2, -1),
"Test2: Attempt to flip with negative index failed to generate exception");
"Test2: Attempt to flip with negative index failed to generate exception");
bs.set(2, 4);
bs.clear(2, 2);
assertTrue(bs.get(2), "Bit got cleared incorrectly ");
assertThrows(IndexOutOfBoundsException.class, () -> newInstance(10).clear(4, 2),
"Test4: Attempt to flip with illegal args failed to generate exception");
"Test4: Attempt to flip with illegal args failed to generate exception");
bs = newInstance(0);
assertEquals(0, bs.length(), "Test1: Wrong length,");
@ -514,7 +509,7 @@ public class FluentBitSetTest {
}
/**
* Tests {@link FluentBitSet#equals(java.lang.Object)}.
* Tests {@link FluentBitSet#equals(Object)}.
*/
@Test
public void test_equals() {
@ -767,7 +762,8 @@ public class FluentBitSetTest {
assertTrue(eightFbs.get(3), "Get returned false for set value");
assertFalse(bs.get(0), "Get returned true for a non set value");
assertThrows(IndexOutOfBoundsException.class, () -> newInstance().get(-1), "Attempt to get at negative index failed to generate exception");
// changed to Throwable to allow jSicko errors
assertThrows(Throwable.class, () -> newInstance().get(-1), "Attempt to get at negative index failed to generate exception");
bs = newInstance(1);
assertFalse(bs.get(64), "Access greater than size");
@ -1119,7 +1115,7 @@ public class FluentBitSetTest {
// Test for method int java.util.BitSet.nextSetBit()
final FluentBitSet bs = newInstance(500);
bs.set(0, bs.size() - 1); // ensure all the bits from 0 to bs.size()
// -1
// -1
bs.set(bs.size() - 1); // are set to true
bs.clear(5);
bs.clear(32);