1 /* 2 * Copyright 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.hardware.camera2.cts.helpers; 18 19 import static junit.framework.Assert.*; 20 21 import java.util.ArrayList; 22 import java.util.Arrays; 23 import java.util.Collection; 24 import java.util.List; 25 26 /** 27 * Helper set of methods to add extra useful assert functionality missing in junit. 28 */ 29 public class AssertHelpers { 30 31 private static final int MAX_FORMAT_STRING = 50; 32 33 /** 34 * Assert that at least one of the elements in data is non-zero. 35 * 36 * <p>An empty or a null array always fails.</p> 37 */ assertArrayNotAllZeroes(String message, byte[] data)38 public static void assertArrayNotAllZeroes(String message, byte[] data) { 39 int size = data.length; 40 41 int i = 0; 42 for (i = 0; i < size; ++i) { 43 if (data[i] != 0) { 44 break; 45 } 46 } 47 48 assertTrue(message, i < size); 49 } 50 51 /** 52 * Assert that every element in left is less than or equals to the corresponding element in 53 * right. 54 * 55 * <p>Array sizes must match.</p> 56 * 57 * @param message Message to use in case the assertion fails 58 * @param left Left array 59 * @param right Right array 60 */ assertArrayNotGreater(String message, float[] left, float[] right)61 public static void assertArrayNotGreater(String message, float[] left, float[] right) { 62 assertEquals("Array lengths did not match", left.length, right.length); 63 64 String leftString = Arrays.toString(left); 65 String rightString = Arrays.toString(right); 66 67 for (int i = 0; i < left.length; ++i) { 68 String msg = String.format( 69 "%s: (%s should be less than or equals than %s; item index %d; left = %s; " + 70 "right = %s)", 71 message, left[i], right[i], i, leftString, rightString); 72 73 assertTrue(msg, left[i] <= right[i]); 74 } 75 } 76 77 /** 78 * Assert that every element in the value array is greater than the lower bound (exclusive). 79 * 80 * @param value an array of items 81 * @param lowerBound the exclusive lower bound 82 */ assertArrayWithinLowerBound(String message, float[] value, float lowerBound)83 public static void assertArrayWithinLowerBound(String message, float[] value, float lowerBound) 84 { 85 for (int i = 0; i < value.length; ++i) { 86 assertTrue( 87 String.format("%s: (%s should be greater than than %s; item index %d in %s)", 88 message, value[i], lowerBound, i, Arrays.toString(value)), 89 value[i] > lowerBound); 90 } 91 } 92 93 /** 94 * Assert that every element in the value array is less than the upper bound (exclusive). 95 * 96 * @param value an array of items 97 * @param upperBound the exclusive upper bound 98 */ assertArrayWithinUpperBound(String message, float[] value, float upperBound)99 public static void assertArrayWithinUpperBound(String message, float[] value, float upperBound) 100 { 101 for (int i = 0; i < value.length; ++i) { 102 assertTrue( 103 String.format("%s: (%s should be less than than %s; item index %d in %s)", 104 message, value[i], upperBound, i, Arrays.toString(value)), 105 value[i] < upperBound); 106 } 107 } 108 109 /** 110 * Assert that {@code low <= value <= high} 111 */ 112 public static void assertInRange(float value, float low, float high) { 113 assertTrue( 114 String.format("Value %s must be greater or equal to %s, but was lower", value, low), 115 value >= low); 116 assertTrue( 117 String.format("Value %s must be less than or equal to %s, but was higher", 118 value, high), 119 value <= high); 120 121 // TODO: generic by using comparators 122 } 123 124 /** 125 * Assert that the given array contains the given value. 126 * 127 * @param message message to print on failure. 128 * @param actual array to test. 129 * @param checkVals value to check for array membership. 130 */ assertArrayContains(String message, T[] actual, T checkVals)131 public static <T> void assertArrayContains(String message, T[] actual, T checkVals) { 132 assertCollectionContainsAnyOf(message, buildList(actual), Arrays.asList(checkVals)); 133 } 134 135 136 /** 137 * Assert that the given array contains the given value. 138 * 139 * @param message message to print on failure. 140 * @param actual array to test. 141 * @param checkVals value to check for array membership. 142 */ assertArrayContains(String message, int[] actual, int checkVals)143 public static void assertArrayContains(String message, int[] actual, int checkVals) { 144 assertCollectionContainsAnyOf(message, buildList(actual), Arrays.asList(checkVals)); 145 } 146 147 /** 148 * Assert that the given array contains at least one of the given values. 149 * 150 * @param message message to print on failure. 151 * @param actual array to test 152 * @param checkVals values to check for array membership. 153 * @return the value contained, or null. 154 */ assertArrayContainsAnyOf(String message, T[] actual, T[] checkVals)155 public static <T> T assertArrayContainsAnyOf(String message, T[] actual, T[] checkVals) { 156 return assertCollectionContainsAnyOf(message, buildList(actual), buildList(checkVals)); 157 } 158 159 /** 160 * Assert that the given array contains at least one of the given values. 161 * 162 * @param message message to print on failure. 163 * @param actual array to test 164 * @param checkVals values to check for array membership. 165 * @return the value contained. 166 */ assertArrayContainsAnyOf(String message, int[] actual, int[] checkVals)167 public static int assertArrayContainsAnyOf(String message, int[] actual, int[] checkVals) { 168 return assertCollectionContainsAnyOf(message, buildList(actual), buildList(checkVals)); 169 } 170 171 /** 172 * Assert that the given {@link Collection} contains at least one of the given values. 173 * 174 * @param message message to print on failure. 175 * @param actual {@link Collection} to test. 176 * @param checkVals a {@link Collection} of values to check for membership. 177 * @return the value contained, or null. 178 */ assertCollectionContainsAnyOf(String message, Collection<T> actual, Collection<T> checkVals)179 public static <T> T assertCollectionContainsAnyOf(String message, Collection<T> actual, 180 Collection<T> checkVals) { 181 boolean contains = false; 182 T selected = null; 183 for (T check : checkVals) { 184 contains = actual.contains(check); 185 if (contains) { 186 selected = check; 187 break; 188 } 189 } 190 191 if (!contains) { 192 fail(String.format("%s : No elements from %s in %s", message, 193 formatCollection(actual, MAX_FORMAT_STRING), 194 formatCollection(checkVals, MAX_FORMAT_STRING))); 195 } 196 return selected; 197 } 198 buildList(T[] array)199 private static <T> List<T> buildList(T[] array) { 200 return new ArrayList<T>(Arrays.asList(array)); 201 } 202 buildList(int[] array)203 private static List<Integer> buildList(int[] array) { 204 List<Integer> list = new ArrayList<Integer>(array.length); 205 for (Integer val : array) { 206 list.add(val); 207 } 208 return list; 209 } 210 formatCollection(Collection<T> collection, int maxLen)211 private static <T> String formatCollection(Collection<T> collection, int maxLen) { 212 StringBuilder builder = new StringBuilder(); 213 builder.append("["); 214 215 boolean first = true; 216 for (T elem : collection) { 217 String val = (first ? "" : ", ") + elem; 218 first = false; 219 if ((builder.length() + val.length()) > maxLen - "...]".length()) { 220 builder.append("..."); 221 break; 222 } else { 223 builder.append(val); 224 } 225 } 226 builder.append("]"); 227 return builder.toString(); 228 } 229 230 231 // Suppress default constructor for noninstantiability AssertHelpers()232 private AssertHelpers() { throw new AssertionError(); } 233 } 234