001 /* 002 * Created on Dec 20, 2010 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 005 * the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 011 * specific language governing permissions and limitations under the License. 012 * 013 * Copyright @2010-2011 the original author or authors. 014 */ 015 package org.fest.assertions.internal; 016 017 import java.util.Comparator; 018 019 import org.fest.assertions.core.ArraySortedAssert; 020 import org.fest.assertions.core.AssertionInfo; 021 import org.fest.assertions.data.Index; 022 import org.fest.util.ComparisonStrategy; 023 import org.fest.util.StandardComparisonStrategy; 024 import org.fest.util.VisibleForTesting; 025 026 /** 027 * Reusable assertions for arrays of {@code double}s. 028 * 029 * @author Alex Ruiz 030 */ 031 public class DoubleArrays { 032 033 private static final DoubleArrays INSTANCE = new DoubleArrays(); 034 035 /** 036 * Returns the singleton instance of this class. 037 * @return the singleton instance of this class. 038 */ 039 public static DoubleArrays instance() { 040 return INSTANCE; 041 } 042 043 private Arrays arrays = Arrays.instance(); 044 045 @VisibleForTesting Failures failures = Failures.instance(); 046 047 @VisibleForTesting DoubleArrays() { 048 this(StandardComparisonStrategy.instance()); 049 } 050 051 public DoubleArrays(ComparisonStrategy comparisonStrategy) { 052 this.arrays = new Arrays(comparisonStrategy); 053 } 054 055 @VisibleForTesting 056 public Comparator<?> getComparator() { 057 return arrays.getComparator(); 058 } 059 060 /** 061 * Asserts that the given array is {@code null} or empty. 062 * @param info contains information about the assertion. 063 * @param actual the given array. 064 * @throws AssertionError if the given array is not {@code null} *and* contains one or more elements. 065 */ 066 public void assertNullOrEmpty(AssertionInfo info, double[] actual) { 067 arrays.assertNullOrEmpty(info, failures, actual); 068 } 069 070 /** 071 * Asserts that the given array is empty. 072 * @param info contains information about the assertion. 073 * @param actual the given array. 074 * @throws AssertionError if the given array is {@code null}. 075 * @throws AssertionError if the given array is not empty. 076 */ 077 public void assertEmpty(AssertionInfo info, double[] actual) { 078 arrays.assertEmpty(info, failures, actual); 079 } 080 081 /** 082 * Asserts that the given array is not empty. 083 * @param info contains information about the assertion. 084 * @param actual the given array. 085 * @throws AssertionError if the given array is {@code null}. 086 * @throws AssertionError if the given array is empty. 087 */ 088 public void assertNotEmpty(AssertionInfo info, double[] actual) { 089 arrays.assertNotEmpty(info, failures, actual); 090 } 091 092 /** 093 * Asserts that the number of elements in the given array is equal to the expected one. 094 * @param info contains information about the assertion. 095 * @param actual the given array. 096 * @param expectedSize the expected size of {@code actual}. 097 * @throws AssertionError if the given array is {@code null}. 098 * @throws AssertionError if the number of elements in the given array is different than the expected one. 099 */ 100 public void assertHasSize(AssertionInfo info, double[] actual, int expectedSize) { 101 arrays.assertHasSize(info, failures, actual, expectedSize); 102 } 103 104 /** 105 * Asserts that the given array contains the given values, in any order. 106 * @param info contains information about the assertion. 107 * @param actual the given array. 108 * @param values the values that are expected to be in the given array. 109 * @throws NullPointerException if the array of values is {@code null}. 110 * @throws IllegalArgumentException if the array of values is empty. 111 * @throws AssertionError if the given array is {@code null}. 112 * @throws AssertionError if the given array does not contain the given values. 113 */ 114 public void assertContains(AssertionInfo info, double[] actual, double[] values) { 115 arrays.assertContains(info, failures, actual, values); 116 } 117 118 /** 119 * Verifies that the given array contains the given value at the given index. 120 * @param info contains information about the assertion. 121 * @param actual the given array. 122 * @param value the value to look for. 123 * @param index the index where the value should be stored in the given array. 124 * @throws AssertionError if the given array is {@code null} or empty. 125 * @throws NullPointerException if the given {@code Index} is {@code null}. 126 * @throws IndexOutOfBoundsException if the value of the given {@code Index} is equal to or greater than the size of 127 * the given array. 128 * @throws AssertionError if the given array does not contain the given value at the given index. 129 */ 130 public void assertContains(AssertionInfo info, double[] actual, double value, Index index) { 131 arrays.assertContains(info, failures, actual, value, index); 132 } 133 134 /** 135 * Verifies that the given array does not contain the given value at the given index. 136 * @param info contains information about the assertion. 137 * @param actual the given array. 138 * @param value the value to look for. 139 * @param index the index where the value should be stored in the given array. 140 * @throws AssertionError if the given array is {@code null}. 141 * @throws NullPointerException if the given {@code Index} is {@code null}. 142 * @throws AssertionError if the given array contains the given value at the given index. 143 */ 144 public void assertDoesNotContain(AssertionInfo info, double[] actual, double value, Index index) { 145 arrays.assertDoesNotContain(info, failures, actual, value, index); 146 } 147 148 /** 149 * Asserts that the given array contains only the given values and nothing else, in any order. 150 * @param info contains information about the assertion. 151 * @param actual the given array. 152 * @param values the values that are expected to be in the given array. 153 * @throws NullPointerException if the array of values is {@code null}. 154 * @throws IllegalArgumentException if the array of values is empty. 155 * @throws AssertionError if the given array is {@code null}. 156 * @throws AssertionError if the given array does not contain the given values or if the given 157 * array contains values that are not in the given array. 158 */ 159 public void assertContainsOnly(AssertionInfo info, double[] actual, double[] values) { 160 arrays.assertContainsOnly(info, failures, actual, values); 161 } 162 163 /** 164 * Verifies that the given array contains the given sequence of values, without any other values between them. 165 * @param info contains information about the assertion. 166 * @param actual the given array. 167 * @param sequence the sequence of values to look for. 168 * @throws AssertionError if the given array is {@code null}. 169 * @throws NullPointerException if the given sequence is {@code null}. 170 * @throws IllegalArgumentException if the given sequence is empty. 171 * @throws AssertionError if the given array does not contain the given sequence of values. 172 */ 173 public void assertContainsSequence(AssertionInfo info, double[] actual, double[] sequence) { 174 arrays.assertContainsSequence(info, failures, actual, sequence); 175 } 176 177 /** 178 * Asserts that the given array does not contain the given values. 179 * @param info contains information about the assertion. 180 * @param actual the given array. 181 * @param values the values that are expected not to be in the given array. 182 * @throws NullPointerException if the array of values is {@code null}. 183 * @throws IllegalArgumentException if the array of values is empty. 184 * @throws AssertionError if the given array is {@code null}. 185 * @throws AssertionError if the given array contains any of given values. 186 */ 187 public void assertDoesNotContain(AssertionInfo info, double[] actual, double[] values) { 188 arrays.assertDoesNotContain(info, failures, actual, values); 189 } 190 191 /** 192 * Asserts that the given array does not have duplicate values. 193 * @param info contains information about the assertion. 194 * @param actual the given array. 195 * @throws NullPointerException if the array of values is {@code null}. 196 * @throws IllegalArgumentException if the array of values is empty. 197 * @throws AssertionError if the given array is {@code null}. 198 * @throws AssertionError if the given array contains duplicate values. 199 */ 200 public void assertDoesNotHaveDuplicates(AssertionInfo info, double[] actual) { 201 arrays.assertDoesNotHaveDuplicates(info, failures, actual); 202 } 203 204 /** 205 * Verifies that the given array starts with the given sequence of values, without any other values between them. 206 * Similar to <code>{@link #assertContainsSequence(AssertionInfo, double[], double[])}</code>, but it also verifies 207 * that the first element in the sequence is also the first element of the given array. 208 * @param info contains information about the assertion. 209 * @param actual the given array. 210 * @param sequence the sequence of values to look for. 211 * @throws NullPointerException if the given argument is {@code null}. 212 * @throws IllegalArgumentException if the given argument is an empty array. 213 * @throws AssertionError if the given array is {@code null}. 214 * @throws AssertionError if the given array does not start with the given sequence of values. 215 */ 216 public void assertStartsWith(AssertionInfo info, double[] actual, double[] sequence) { 217 arrays.assertStartsWith(info, failures, actual, sequence); 218 } 219 220 /** 221 * Verifies that the given array ends with the given sequence of values, without any other values between them. 222 * Similar to <code>{@link #assertContainsSequence(AssertionInfo, double[], double[])}</code>, but it also verifies 223 * that the last element in the sequence is also the last element of the given array. 224 * @param info contains information about the assertion. 225 * @param actual the given array. 226 * @param sequence the sequence of values to look for. 227 * @throws NullPointerException if the given argument is {@code null}. 228 * @throws IllegalArgumentException if the given argument is an empty array. 229 * @throws AssertionError if the given array is {@code null}. 230 * @throws AssertionError if the given array does not end with the given sequence of values. 231 */ 232 public void assertEndsWith(AssertionInfo info, double[] actual, double[] sequence) { 233 arrays.assertEndsWith(info, failures, actual, sequence); 234 } 235 236 /** 237 * Concrete implementation of {@link ArraySortedAssert#isSorted()}. 238 * 239 * @param info contains information about the assertion. 240 * @param actual the given array. 241 */ 242 public void assertIsSorted(AssertionInfo info, double[] actual) { 243 arrays.assertIsSorted(info, failures, actual); 244 } 245 246 /** 247 * Concrete implementation of {@link ArraySortedAssert#isSortedAccordingTo(Comparator)}. 248 * 249 * @param info contains information about the assertion. 250 * @param actual the given array. 251 * @param comparator the {@link Comparator} used to compare array elements 252 */ 253 public void assertIsSortedAccordingToComparator(AssertionInfo info, double[] actual, 254 Comparator<? extends Double> comparator) { 255 Arrays.assertIsSortedAccordingToComparator(info, failures, actual, comparator); 256 } 257 }