All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.assertj.api.AbstractFloatArrayAssert Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/**
 * 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.
 *
 * Copyright 2012-2016 the original author or authors.
 */
package com.fitbur.assertj.api;

import java.util.Comparator;

import com.fitbur.assertj.data.Index;
import com.fitbur.assertj.data.Offset;
import com.fitbur.assertj.internal.ComparatorBasedComparisonStrategy;
import com.fitbur.assertj.internal.FloatArrays;
import com.fitbur.assertj.util.VisibleForTesting;

public abstract class AbstractFloatArrayAssert>
    extends AbstractArrayAssert {

  @VisibleForTesting
  protected FloatArrays arrays = FloatArrays.instance();

  private final ComparatorFactory floatComparator = ComparatorFactory.INSTANCE;

  public AbstractFloatArrayAssert(float[] actual, Class selfType) {
    super(actual, selfType);
  }

  /** {@inheritDoc} */
  @Override
  public void isNullOrEmpty() {
    arrays.assertNullOrEmpty(info, actual);
  }

  /** {@inheritDoc} */
  @Override
  public void isEmpty() {
    arrays.assertEmpty(info, actual);
  }

  /** {@inheritDoc} */
  @Override
  public S isNotEmpty() {
    arrays.assertNotEmpty(info, actual);
    return myself;
  }

  /** 
   * {@inheritDoc} 
   * 

* Examples: *

 // assertion will pass
   * assertThat(new float[] { 1.0f, 2.0f, 3.0f }).hasSize(3);
   * 
   * // assertions will fail
   * assertThat(new float[] { 1.0f, 2.0f, 1.0f }).hasSize(2);
*/ @Override public S hasSize(int expected) { arrays.assertHasSize(info, actual, expected); return myself; } /** * Verifies that the actual group has the same size as given {@link Iterable}. *

* Examples: *

 // assertion will pass
   * assertThat(new float[] { 1.0f, 2.0f, 3.0f }).hasSameSizeAs(Arrays.asList(1, 2, 3));
   * 
   * // assertion will fail
   * assertThat(new float[] { 1.0f, 2.0f, 1.0f }).hasSameSizeAs(Arrays.asList(1, 2));
*/ @Override public S hasSameSizeAs(Iterable other) { arrays.assertHasSameSizeAs(info, actual, other); return myself; } /** * Verifies that the actual array contains the given values, in any order. *

* If you want to set a precision for the comparison either use {@link #contains(float[], Offset)} * or {@link #usingComparatorWithPrecision(Float)} before calling the assertion. *

* Examples : *

 float[] values = new float[] {1.0f, 2.0f, 3.0f};
   * 
   * // assertions will pass
   * assertThat(values).contains(1.0f, 3.0f, 2.0f)
   *                   .contains(3.0f, 1.0f)
   *                   .usingComparatorWithPrecision(0.5f)
   *                   .contains(1.1f, 2.1f);
   * 
   * // assertions will fail
   * assertThat(values).contains(1.0f, 4.0f);
   * assertThat(values).usingComparatorWithPrecision(0.01f)
   *                   .contains(1.1f, 2.1f);
* * @param values the given values. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual array does not contain the given values. */ public S contains(float... values) { arrays.assertContains(info, actual, values); return myself; } /** * Verifies that the actual array contains the given values, in any order, * the comparison is done at the given precision/offset set with {@link Assertions#withPrecision(Float)}. *

* Examples : *

 float[] values = new float[] {1.0f, 2.0f, 3.0f};
   * 
   * // assertion will pass
   * assertThat(values).contains(new float[] {1.01f, 3.01f, 2.0f}, withPrecision(0.02f));
   *
   * // assertions will fail
   * assertThat(values).contains(new float[] {1.0f, 4.0f}, withPrecision(0.5f));
   * assertThat(values).contains(new float[] {4.0f, 7.0f}, withPrecision(2f));
* * @param values the given values. * @param precision the precision under which the values may vary. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual array does not contain the given values. */ public S contains(float[] values, Offset precision) { usingComparatorWithPrecision(precision.value); return contains(values); } /** * Verifies that the actual array contains only the given values and nothing else, in any order. *

* If you want to set a precision for the comparison either use {@link #containsOnly(float[], Offset)} * or {@link #usingComparatorWithPrecision(Float)} before calling the assertion. *

* Examples : *

 float[] values = new double[] {1.0f, 2.0f, 3.0f};
   * 
   * // assertions will pass
   * assertThat(values).containsOnly(1.0f, 2.0f, 3.0f)
   *                   .containsOnly(2.0f, 3.0f, 1.0f)
   *                   .usingComparatorWithPrecision(0.5f)
   *                   .containsOnly(1.1f, 3.1f, 2.1f);
  
   * // assertions will fail
   * assertThat(values).containsOnly(1.0f, 4.0f, 2.0f, 3.0f);
   * assertThat(values).containsOnly(4.0f, 7.0f);
   * assertThat(values).containsOnly(1.1f, 2.1f, 3.1f);
   * assertThat(values).usingComparatorWithPrecision(0.01f)
   *                   .containsOnly(1.1f, 2.1f, 3.1)f;
* * @param values the given values. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual array does not contain the given values, i.e. the actual array contains some * or none of the given values, or the actual array contains more values than the given ones. */ public S containsOnly(float... values) { arrays.assertContainsOnly(info, actual, values); return myself; } /** * Verifies that the actual array contains only the given values and nothing else, in any order. * The comparison is done at the given precision/offset set with {@link Assertions#withPrecision(Float)}. *

* Examples : *

 float[] values = new float[] {1.0f, 2.0f, 3.0f};
   * 
   * // assertion will pass
   * assertThat(values).containsOnly(new float[] {1.0f, 2.0f, 3.0f }, withPrecision(0.00001f))
   *                   .containsOnly(new float[] {2.0,f 3.0f, 0.7f}, withPrecision(0.5f));
   *
   * // assertions will fail
   * assertThat(values).containsOnly(new float[] {1.0f, 4.0f, 2.0f, 3.0f}, withPrecision(0.5f));
   * assertThat(values).containsOnly(new float[] {4.0f, 7.0f}, withPrecision(0.2f));
* * @param values the given values. * @param precision the precision under which the values may vary. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual array does not contain the given values, i.e. the actual array contains some * or none of the given values, or the actual array contains more values than the given ones. */ public S containsOnly(float[] values, Offset precision) { usingComparatorWithPrecision(precision.value); return containsOnly(values); } /** * Verifies that the actual array contains the given values only once. *

* If you want to set a precision for the comparison either use {@link #containsOnlyOnce(float[], Offset)} * or {@link #usingComparatorWithPrecision(Float)} before calling the assertion. *

* Examples : *

 // assertions will pass
   * assertThat(new float[] { 1.0f, 2.0f, 3.0f }).containsOnlyOnce(1.0f, 2.0f)
   *                                           .usingComparatorWithPrecision(0.5f)
   *                                           .containsOnlyOnce(1.1f, 3.1f, 2.1f);
   * 
   * // assertions will fail
   * assertThat(new float[] { 1.0f, 2.0f, 1.0f }).containsOnlyOnce(1.0f);
   * assertThat(new float[] { 1.0f, 2.0f, 1.0f }).containsOnlyOnce(1.0f, 2.0f);
   * assertThat(new float[] { 1.0f, 2.0f, 3.0f }).containsOnlyOnce(4.0f);
   * assertThat(new float[] { 1.0f, 2.0f, 3.0f }).usingComparatorWithPrecision(0.05f)
   *                                           .containsOnlyOnce(1.1f, 2.1f);
* * @param values the given values. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual group does not contain the given values, i.e. the actual group contains some * or none of the given values, or the actual group contains more than once these values. */ public S containsOnlyOnce(float... values) { arrays.assertContainsOnlyOnce(info, actual, values); return myself; } /** * Verifies that the actual array contains the given values only once. * The comparison is done at the given precision/offset set with {@link Assertions#withPrecision(Float)}. *

* Examples : *

 // assertion will pass
  * assertThat(new float[] { 1.0f, 2.0f, 3.0f }).containsOnlyOnce(new float[] {1.1f, 2.0f}, withPrecision(0.2f));
  *
  * // assertions will fail
  * assertThat(new float[] { 1.0f, 2.0f, 1.0f }).containsOnlyOnce(new float[] {1.05f}, withPrecision(0.1f));
  * assertThat(new float[] { 1.0f, 2.0f, 3.0f }).containsOnlyOnce(new float[] {4.0f}, withPrecision(0.1f));
  * assertThat(new float[] { 1.0f, 2.0f, 3.0f, 3.0f }).containsOnlyOnce(new float[] {0.1f, 0.9f, 2.0f, 3.11f, 4.0f, 5.0f}, withPrecision(0.2f));
* * @param values the given values. * @param precision the precision under which the values may vary. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual group does not contain the given values, i.e. the actual group contains some * or none of the given values, or the actual group contains more than once these values. */ public S containsOnlyOnce(float[] values, Offset precision) { usingComparatorWithPrecision(precision.value); return containsOnlyOnce(values); } /** * Verifies that the actual array contains the given sequence, without any other values between them. *

* If you want to set a precision for the comparison either use {@link #containsSequence(float[], Offset)} * or {@link #usingComparatorWithPrecision(Float)} before calling the assertion. *

* Examples : *

 float[] values = new float[] { 1.0f, 2.0f, 3.0f };
   * 
   * // assertion will pass
   * assertThat(values).containsSequence(1.0f, 2.0f)
   *                   .containsSequence(1.0f, 2.0f, 3.0f)
   *                   .containsSequence(2.0f, 3.0f)
   *                   .usingComparatorWithPrecision(0.5f)
   *                   .containsSequence(1.1f, 2.1f);
   * 
   * // assertions will fail
   * assertThat(values).containsSequence(1.0f, 3.0f);
   * assertThat(values).containsSequence(4.0f, 7.0f);
   * assertThat(values).usingComparatorWithPrecision(0.01f)
   *                   .containsSequence(1.1f, 2.0f, 3.0f);
* * @param sequence the sequence of values to look for. * @return {@code this} assertion object. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the given array is {@code null}. * @throws AssertionError if the actual array does not contain the given sequence. */ public S containsSequence(float... sequence) { arrays.assertContainsSequence(info, actual, sequence); return myself; } /** * Verifies that the actual array contains the given sequence, without any other values between them. * The comparison is done at the given precision/offset set with {@link Assertions#withPrecision(Float)}. *

* Examples : *

 float[] values = new float[] {1.0f, 2.0f, 3.0f};
   * 
   * // assertions will pass
   * assertThat(values).containsSequence(new float[] {1.07f, 2.0f}, withPrecision(0.1f))
   *                   .containsSequence(new float[] {1.1f, 2.1f, 3.0f}, withPrecision(0.2f))
   *                   .containsSequence(new float[] {2.2f, 3.0f}, withPrecision(0.3f));
   *
   * // assertions will fail
   * assertThat(values).containsSequence(new float[] {1.0f, 3.0f}, withPrecision(0.2f));
   * assertThat(values).containsSequence(new float[] {4.0f, 7.0f}, withPrecision(0.1f));
* * @param sequence the sequence of values to look for. * @param precision the precision under which the values may vary. * @return {@code this} assertion object. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the given array is {@code null}. * @throws AssertionError if the actual array does not contain the given sequence. */ public S containsSequence(float[] sequence, Offset precision) { usingComparatorWithPrecision(precision.value); return containsSequence(sequence); } /** * Verifies that the actual array contains the given subsequence (possibly with other values between them). *

* If you want to set a precision for the comparison either use {@link #containsSubsequence(float[], Offset)} * or {@link #usingComparatorWithPrecision(Float)} before calling the assertion. *

* Examples : *

 float[] values = new float[] { 1.0f, 2.0f, 3.0f };
   * 
   * // assertion will pass
   * assertThat(values).containsSubsequence(1.0f, 2.0f);
   *                   .containsSubsequence(1.0f, 2.0f, 3.0f)
   *                   .containsSubsequence(1.0f, 3.0f)
   *                   .usingComparatorWithPrecision(0.5f)
   *                   .containsSubsequence(1.1f, 2.1f);
   * 
   * // assertions will fail
   * assertThat(values).containsSubsequence(3.0f, 1.0f);
   * assertThat(values).containsSubsequence(4.0f, 7.0f);
   * assertThat(values).usingComparatorWithPrecision(0.01f)
   *                   .containsSubsequence(1.1f, 2.0f);
* * @param subsequence the subsequence of values to look for. * @return {@code this} assertion object. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the given array is {@code null}. * @throws AssertionError if the actual array does not contain the given subsequence. */ public S containsSubsequence(float... subsequence) { arrays.assertContainsSubsequence(info, actual, subsequence); return myself; } /** * Verifies that the actual array contains the given subsequence (possibly with other values between them). * The comparison is done at the given precision/offset set with {@link Assertions#withPrecision(Float)}. *

* Examples : *

 float[] values = new float[] {1.0f, 2.0f, 3.0f};
   * 
   * // assertions will pass
   * assertThat(values).containsSubsequence(new float[] {1.0f, 2.0f}, withPrecision(0.1f))
   *                   .containsSubsequence(new float[] {1.0f, 2.07f, 3.0f}, withPrecision(0.1f))
   *                   .containsSubsequence(new float[] {2.1f, 2.9f}, withPrecision(0.2f));
   *
   * // assertions will fail
   * assertThat(values).containsSubsequence(new float[] {1.0f, 3.0f}, withPrecision(0.1f));
   * assertThat(values).containsSubsequence(new float[] {4.0f, 7.0f}, withPrecision(0.1f));
* * @param subsequence the subsequence of values to look for. * @param precision the precision under which the values may vary. * @return {@code this} assertion object. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the given array is {@code null}. * @throws AssertionError if the actual array does not contain the given subsequence. */ public S containsSubsequence(float[] subsequence, Offset precision) { usingComparatorWithPrecision(precision.value); return containsSubsequence(subsequence); } /** * Verifies that the actual array contains the given value at the given index. *

* If you want to set a precision for the comparison either use {@link #contains(float, Index, Offset)} * or {@link #usingComparatorWithPrecision(Float)} before calling the assertion. *

* Example: *

 float[] values = new float[] { 1.0f, 2.0f, 3.0f };
   * 
   * // assertion will pass
   * assertThat(values).contains(1.0f, atIndex(O))
   *                   .contains(3.0f, atIndex(2))
   *                   .usingComparatorWithPrecision(0.5f)
   *                   .contains(3.1f, atIndex(2));
   * 
   * // assertions will fail
   * assertThat(values).contains(1.0f, atIndex(1));
   * assertThat(values).contains(4.0f, atIndex(2));
   * assertThat(values).usingComparatorWithPrecision(0.01f)
   *                   .contains(3.1f, atIndex(2));
* * @param value the value to look for. * @param index the index where the value should be stored in the actual array. * @return {@code this} assertion object. * @throws AssertionError if the actual array is {@code null} or empty. * @throws NullPointerException if the given {@code Index} is {@code null}. * @throws IndexOutOfBoundsException if the value of the given {@code Index} is equal to or greater than the size of * the actual array. * @throws AssertionError if the actual array does not contain the given value at the given index. */ public S contains(float value, Index index) { arrays.assertContains(info, actual, value, index); return myself; } /** * Verifies that the actual array contains the given value at the given index. * The comparison is done at the given precision/offset set with {@link Assertions#withPrecision(Float)}. *

* Example: *

 float[] values = new float[] {1.0f, 2.0f, 3.0f};
   * 
   * // assertions will pass
   * assertThat(values).contains(1.0f, atIndex(O), withPrecision(0.01f))
   *                   .contains(3.3f, atIndex(2), withPrecision(0.5f));
   *
   * // assertions will fail
   * assertThat(values).contains(1.0f, atIndex(1), withPrecision(0.2f));
   * assertThat(values).contains(4.5f, atIndex(2), withPrecision(0.1f));
* * @param value the value to look for. * @param index the index where the value should be stored in the actual array. * @param precision the precision which the value may vary. * @return {@code this} assertion object. * @throws AssertionError if the actual array is {@code null} or empty. * @throws NullPointerException if the given {@code Index} is {@code null}. * @throws IndexOutOfBoundsException if the value of the given {@code Index} is equal to or greater than the size of * the actual array. * @throws AssertionError if the actual array does not contain the given value at the given index. */ public S contains(float value, Index index, Offset precision) { usingComparatorWithPrecision(precision.value); return contains(value, index); } /** * Verifies that the actual array does not contain the given values. *

* If you want to set a precision for the comparison either use {@link #doesNotContain(float[], Offset)} * or {@link #usingComparatorWithPrecision(Float)} before calling the assertion. *

* Example: *

 float[] values = new float[] { 1.0f, 2.0f, 3.0f };
   * 
   * // assertion will pass
   * assertThat(values).doesNotContain(4.0f, 8.0f)
   *                   .usingComparatorWithPrecision(0.0001f)
   *                   .doesNotContain(1.01f, 2.01f);
   *                   
   * // assertions will fail
   * assertThat(values).doesNotContain(1.0f, 4.0f, 8.0f);
   * assertThat(values).usingComparatorWithPrecision(0.1f)
   *                   .doesNotContain(1.001f, 2.001f);
* * @param values the given values. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual array contains any of the given values. */ public S doesNotContain(float... values) { arrays.assertDoesNotContain(info, actual, values); return myself; } /** * Verifies that the actual array does not contain the given values. * The comparison is done at the given precision/offset set with {@link Assertions#withPrecision(Float)}. *

* Example: *

 float[] values = new float[] {1.0f, 2.0f, 3.0f};
   * 
   * // assertion will pass
   * assertThat(values).doesNotContain(new float[] {4.0f, 8.0f}, withPrecision(0.5f));
   *
   * // assertion will fail
   * assertThat(values).doesNotContain(new float[] {1.05f, 4.0f, 8.0f}, withPrecision(0.1f));
* * @param values the given values. * @param precision the precision under which the values may vary. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual array contains any of the given values. */ public S doesNotContain(float[] values, Offset precision) { usingComparatorWithPrecision(precision.value); arrays.assertDoesNotContain(info, actual, values); return myself; } /** * Verifies that the actual array does not contain the given value at the given index. *

* If you want to set a precision for the comparison either use {@link #doesNotContain(float, Index, Offset)} * or {@link #usingComparatorWithPrecision(Float)} before calling the assertion. *

* Example: *

 float[] values = new float[] { 1.0f, 2.0f, 3.0f };
   * 
   * // assertion will pass
   * assertThat(values).doesNotContain(1.0f, atIndex(1))
   *                   .doesNotContain(2.0f, atIndex(0))
   *                   .usingComparatorWithPrecision(0.001)
   *                   .doesNotContain(1.1f, atIndex(0));
   * 
   * // assertions will fail
   * assertThat(values).doesNotContain(1.0f, atIndex(0));
   * assertThat(values).usingComparatorWithPrecision(0.1)
   *                   .doesNotContain(1.001f, atIndex(0));
* * @param value the value to look for. * @param index the index where the value should be stored in the actual array. * @return {@code this} assertion object. * @throws AssertionError if the actual array is {@code null}. * @throws NullPointerException if the given {@code Index} is {@code null}. * @throws AssertionError if the actual array contains the given value at the given index. */ public S doesNotContain(float value, Index index) { arrays.assertDoesNotContain(info, actual, value, index); return myself; } /** * Verifies that the actual array does not contain the given value at the given index. * The comparison is done at the given precision/offset set with {@link Assertions#withPrecision(Float)}. *

* Example: *

 float[] values = new float[] {1.0f, 2.0f, 3.0f};
   * 
   * // assertions will pass
   * assertThat(values).doesNotContain(1.01f, atIndex(1), withPrecision(0.0001f))
   *                   .doesNotContain(2.05f, atIndex(0), withPrecision(0.1f));
   *
   * // assertion will fail
   * assertThat(values).doesNotContain(1.01f, atIndex(0), withPrecision(0.1f));
* * @param value the value to look for. * @param index the index where the value should be stored in the actual array. * @param precision the precision under which the value may vary. * @return {@code this} assertion object. * @throws AssertionError if the actual array is {@code null}. * @throws NullPointerException if the given {@code Index} is {@code null}. * @throws AssertionError if the actual array contains the given value at the given index. */ public S doesNotContain(float value, Index index, Offset precision) { usingComparatorWithPrecision(precision.value); return doesNotContain(value, index); } /** * Verifies that the actual array does not contain duplicates. *

* If you want to set a precision for the comparison either use {@link #doesNotHaveDuplicates(Offset)} * or {@link #usingComparatorWithPrecision(Float)} before calling the assertion. *

* Example: *

 // assertions will pass
   * assertThat(new float[] { 1.0f, 2.0f, 3.0f }).doesNotHaveDuplicates();
   * assertThat(new float[] { 1.0f, 1.1f }).usingComparatorWithPrecision(0.01f)
   *                                      .doesNotHaveDuplicates();
   * 
   * // assertions will fail
   * assertThat(new float[] { 1.0f, 1.0f, 2.0f, 3.0f }).doesNotHaveDuplicates();
   * assertThat(new float[] { 1.0f, 1.1f }).usingComparatorWithPrecision(0.5f)
   *                                      .doesNotHaveDuplicates();
* * @return {@code this} assertion object. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual array contains duplicates. */ public S doesNotHaveDuplicates() { arrays.assertDoesNotHaveDuplicates(info, actual); return myself; } /** * Verifies that the actual array does not contain duplicates. * The comparison is done at the given precision/offset set with {@link Assertions#withPrecision(Float)}. *

* Example: *

 // assertions will pass
   * assertThat(new float[] {1.0f, 2.0f, 3.0f}).doesNotHaveDuplicates(withPrecision(0.1f));
   * assertThat(new float[] {1.1f, 1.2f, 1.3f}).doesNotHaveDuplicates(withPrecision(0.05f));
   * 
   * // assertion will fail
   * assertThat(new float[] {1.0f, 1.01f, 2.0f}).doesNotHaveDuplicates(withPrecision(0.1f));
* * @param precision the precision under which the values may vary. * @return {@code this} assertion object. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual array contains duplicates. */ public S doesNotHaveDuplicates(Offset precision) { usingComparatorWithPrecision(precision.value); return doesNotHaveDuplicates(); } /** * Verifies that the actual array starts with the given sequence of values, without any other values between them. * Similar to {@link #containsSequence(float...)}, but it also verifies that the first element in the * sequence is also first element of the actual array. *

* If you want to set a precision for the comparison either use {@link #startsWith(float[], Offset)} * or {@link #usingComparatorWithPrecision(Float)} before calling the assertion. *

* Example: *

 float[] values = new float[] { 1.0f, 2.0f, 3.0f };
   * 
   * // assertion will pass
   * assertThat(values).startsWith(1.0f, 2.0f)
   *                   .usingComparatorWithPrecision(0.5f)
   *                   .startsWith(1.1f, 2.1f);
   * 
   * // assertion will fail
   * assertThat(values).startsWith(2.0f, 3.0f);
* * @param sequence the sequence of values to look for. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual array does not start with the given sequence. */ public S startsWith(float... sequence) { arrays.assertStartsWith(info, actual, sequence); return myself; } /** * Verifies that the actual array starts with the given sequence of values, without any other values between them. * Similar to {@link #containsSequence(float...)}, but it also verifies that the first element in the * sequence is also first element of the actual array. *

* The comparison is done at the given precision/offset set with {@link Assertions#withPrecision(Float)}. *

* Example: *

 float[] values = new float[] {1.0f, 2.0f, 3.0f};
   * 
   * // assertion will pass
   * assertThat(values).startsWith(new float[] {1.01f, 2.01f}, withPrecision(0.1f));
   * 
   * // assertions will fail
   * assertThat(values).startsWith(new float[] {2.0f, 1.0f}, withPrecision(0.1f))
   * assertThat(values).startsWith(new float[] {1.1f, 2.1f}, withPrecision(0.5f))
* * @param sequence the sequence of values to look for. * @param precision the precision under which the values may vary. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual array does not end with the given sequence. */ public S startsWith(float[] values, Offset precision) { usingComparatorWithPrecision(precision.value); return startsWith(values); } /** * Verifies that the actual array ends with the given sequence of values, without any other values between them. * Similar to {@link #containsSequence(float...)}, but it also verifies that the last element in the * sequence is also last element of the actual array. *

* If you want to set a precision for the comparison either use {@link #endsWith(float[], Offset)} * or {@link #usingComparatorWithPrecision(Float)} before calling the assertion. *

* Example: *

 float[] values = new float[] { 1.0f, 2.0f, 3.0f };
   * 
   * // assertion will pass
   * assertThat(values).endsWith(2.0f, 3.0f)
   *                   .usingComparatorWithPrecision(0.5f)
   *                   .endsWith(2.1f, 3.1f);
   * 
   * // assertion will fail
   * assertThat(values).endsWith(1.0f, 3.0f);
* * @param sequence the sequence of values to look for. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual array does not end with the given sequence. */ public S endsWith(float... sequence) { arrays.assertEndsWith(info, actual, sequence); return myself; } /** * Verifies that the actual array ends with the given sequence of values, without any other values between them. * Similar to {@link #containsSequence(float...)}, but it also verifies that the last element in the * sequence is also last element of the actual array. *

* The comparison is done at the given precision/offset set with {@link Assertions#withPrecision(Float)}. *

* Example: *

 float[] values = new float[] {1.0f, 2.0f, 3.0f};
   * 
   * // assertion will pass
   * assertThat(values).endsWith(new float[] {2.01f, 3.01f}, withPrecision(0.1f));
   * 
   * // assertions will fail
   * assertThat(values).endsWith(new float[] {3.0f, 2.0f}, withPrecision(0.1f))
   * assertThat(values).endsWith(new float[] {2.1f, 3.1f}, withPrecision(0.5f))
* * @param sequence the sequence of values to look for. * @param precision the precision under which the values may vary. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the actual array is {@code null}. * @throws AssertionError if the actual array does not end with the given sequence. */ public S endsWith(float[] values, Offset precision) { usingComparatorWithPrecision(precision.value); return endsWith(values); } /** {@inheritDoc} */ @Override public S isSorted() { arrays.assertIsSorted(info, actual); return myself; } /** {@inheritDoc} */ @Override public S isSortedAccordingTo(Comparator comparator) { arrays.assertIsSortedAccordingToComparator(info, actual, comparator); return myself; } /** {@inheritDoc} */ @Override public S usingElementComparator(Comparator customComparator) { this.arrays = new FloatArrays(new ComparatorBasedComparisonStrategy(customComparator)); return myself; } /** {@inheritDoc} */ @Override public S usingDefaultElementComparator() { this.arrays = FloatArrays.instance(); return myself; } /** * Verifies that the actual group contains only the given values and nothing else, in order. *

* If you want to set a precision for the comparison either use {@link #containsExactly(float[], Offset)} * or {@link #usingComparatorWithPrecision(Float)} before calling the assertion. *

* Example : *

 float[] values = new float[] { 1.0f, 2.0f, 3.0f };
   * 
   * // assertion will pass
   * assertThat(values).containsExactly(1.0f, 2.0f, 3.0f)
   *                   .usingComparatorWithPrecision(0.2f)
   *                   .containsExactly(1.1f, 2.1f, 2.9f);
   * 
   * // assertion will fail as actual and expected order differ
   * assertThat(values).containsExactly(2.0f, 1.0f, 3.0f);
* * @param values the given values. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws AssertionError if the actual group is {@code null}. * @throws AssertionError if the actual group does not contain the given values with same order, i.e. the actual group * contains some or none of the given values, or the actual group contains more values than the given ones * or values are the same but the order is not. */ public S containsExactly(float... values) { arrays.assertContainsExactly(info, actual, values); return myself; } /** * Verifies that the actual group contains only the given values and nothing else, in order. * The values may vary with a specified precision. *

* Example : *

 float[] values = new float[] {1.0f, 2.0f, 3.0f};
   * 
   * // assertion will pass
   * assertThat(values).containsExactly(new float[] {1.0f, 1.98f, 3.01f}, withPrecision(0.05f));
   *
   * // assertion fails because |1.0 - 1.1| > 0.05 (precision)
   * assertThat(values).containsExactly(new float[] {1.1f, 2.0f, 3.01f}, withPrecision(0.05f));
   * 
   * // assertion will fail as actual and expected order differ
   * assertThat(values).containsExactly(new float[] {1.98f, 1.0f, 3.01f}, withPrecision(0.05f));
* * @param values the given values. * @param precision the precision under which the values may vary. * @return {@code this} assertion object. * @throws NullPointerException if the given argument is {@code null}. * @throws AssertionError if the actual group is {@code null}. * @throws AssertionError if the actual group does not contain the given values within the specified precision * with same order, i.e. the actual group contains some or none of the given values, or the actual group contains * more values than the given ones or values are the same but the order is not. */ public S containsExactly(float[] values, Offset precision) { usingComparatorWithPrecision(precision.value); return containsExactly(values); } /** * Create a {@link Float} comparator which compares floats at the given precision and pass it to {@link #usingElementComparator(Comparator)}. * All the following assertions will use this comparator to compare float[] elements. * * @param precision precisin used to compare {@link Float}. * @return {@code this} assertion object. */ public S usingComparatorWithPrecision(Float precision) { return usingElementComparator(floatComparator.floatComparatorWithPrecision(precision)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy