| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial design and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.tests.util; |
| 14 | |
| 15 | import java.util.Arrays; |
| 16 | |
| 17 | import junit.framework.TestCase; |
| 18 | |
| 19 | import org.eclipse.linuxtools.tmf.util.TmfFixedArray; |
| 20 | |
| 21 | public class TmfFixedArrayTest extends TestCase { |
| 22 | |
| 23 | // ------------------------------------------------------------------------ |
| 24 | // Field(s) |
| 25 | // ------------------------------------------------------------------------ |
| 26 | TmfFixedArray<String> fFixedArray1 = null; |
| 27 | TmfFixedArray<String> fFixedArray2 = null; |
| 28 | String fString1, fString2, fString3, fString4, fString5; |
| 29 | |
| 30 | // ------------------------------------------------------------------------ |
| 31 | // Housekeeping |
| 32 | // ------------------------------------------------------------------------ |
| 33 | |
| 34 | public TmfFixedArrayTest() { |
| 35 | fString1 = "First String"; |
| 36 | fString2 = "Second String"; |
| 37 | fString3 = "Third String"; |
| 38 | fString4 = "Fourth String"; |
| 39 | fString5 = "Fifth String"; |
| 40 | fFixedArray1 = new TmfFixedArray<String>(fString1, fString2, fString3); |
| 41 | fFixedArray2 = new TmfFixedArray<String>(); // Empty array at the beginning |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | protected void setUp() throws Exception { |
| 46 | super.setUp(); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | protected void tearDown() throws Exception { |
| 51 | super.tearDown(); |
| 52 | } |
| 53 | |
| 54 | // ------------------------------------------------------------------------ |
| 55 | // Get |
| 56 | // ------------------------------------------------------------------------ |
| 57 | |
| 58 | public void testGet() throws Exception { |
| 59 | assertTrue("get", fString1.equals(fFixedArray1.get(0))); |
| 60 | assertTrue("get", fString2.equals(fFixedArray1.get(1))); |
| 61 | assertTrue("get", fString3.equals(fFixedArray1.get(2))); |
| 62 | |
| 63 | try { |
| 64 | fFixedArray2.get(0); |
| 65 | fail(); |
| 66 | } |
| 67 | catch (ArrayIndexOutOfBoundsException e) { |
| 68 | // Success |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public void testGetArray() throws Exception { |
| 73 | String[] stringArray = fFixedArray1.getArray(); |
| 74 | assertNotNull("getArray", stringArray); |
| 75 | assertTrue("getArray", fString1.equals(stringArray[0])); |
| 76 | assertTrue("getArray", fString2.equals(stringArray[1])); |
| 77 | assertTrue("getArray", fString3.equals(stringArray[2])); |
| 78 | } |
| 79 | |
| 80 | // ------------------------------------------------------------------------ |
| 81 | // Equals |
| 82 | // ------------------------------------------------------------------------ |
| 83 | |
| 84 | public void testEqualsReflexivity() throws Exception { |
| 85 | assertTrue("equals", fFixedArray1.equals(fFixedArray1)); |
| 86 | assertTrue("equals", fFixedArray2.equals(fFixedArray2)); |
| 87 | |
| 88 | assertTrue("equals", !fFixedArray1.equals(fFixedArray2)); |
| 89 | assertTrue("equals", !fFixedArray2.equals(fFixedArray1)); |
| 90 | } |
| 91 | |
| 92 | @SuppressWarnings("unchecked") |
| 93 | public void testEqualsSymmetry() throws Exception { |
| 94 | TmfFixedArray<String> fixedArray1 = (TmfFixedArray<String>) fFixedArray1.clone(); |
| 95 | TmfFixedArray<String> fixedArray2 = (TmfFixedArray<String>) fFixedArray2.clone(); |
| 96 | |
| 97 | assertTrue("equals", fixedArray1.equals(fFixedArray1)); |
| 98 | assertTrue("equals", fFixedArray1.equals(fixedArray1)); |
| 99 | |
| 100 | assertTrue("equals", fixedArray2.equals(fFixedArray2)); |
| 101 | assertTrue("equals", fFixedArray2.equals(fixedArray2)); |
| 102 | } |
| 103 | |
| 104 | @SuppressWarnings("unchecked") |
| 105 | public void testEqualsTransivity() throws Exception { |
| 106 | TmfFixedArray<String> fixedArray1 = (TmfFixedArray<String>) fFixedArray1.clone(); |
| 107 | TmfFixedArray<String> fixedArray2 = (TmfFixedArray<String>) fFixedArray1.clone(); |
| 108 | TmfFixedArray<String> fixedArray3 = (TmfFixedArray<String>) fFixedArray1.clone(); |
| 109 | |
| 110 | assertTrue("equals", fixedArray1.equals(fixedArray2)); |
| 111 | assertTrue("equals", fixedArray2.equals(fixedArray3)); |
| 112 | assertTrue("equals", fixedArray1.equals(fixedArray3)); |
| 113 | } |
| 114 | |
| 115 | public void testEqualsNull() throws Exception { |
| 116 | assertTrue("equals", !fFixedArray1.equals(null)); |
| 117 | assertTrue("equals", !fFixedArray2.equals(null)); |
| 118 | } |
| 119 | |
| 120 | // ------------------------------------------------------------------------ |
| 121 | // Append |
| 122 | // ------------------------------------------------------------------------ |
| 123 | |
| 124 | public void testAppend() { |
| 125 | TmfFixedArray<String> fixedArray = new TmfFixedArray<String>(); |
| 126 | |
| 127 | fixedArray = fixedArray.append(fString1, fString2, fString3); |
| 128 | assertEquals("append", 3, fixedArray.size()); |
| 129 | assertTrue("append", fString1.equals(fixedArray.get(0))); |
| 130 | assertTrue("append", fString2.equals(fixedArray.get(1))); |
| 131 | assertTrue("append", fString3.equals(fixedArray.get(2))); |
| 132 | |
| 133 | fixedArray = fixedArray.append(fString4); |
| 134 | assertEquals("append", 4, fixedArray.size()); |
| 135 | assertTrue("append", fString4.equals(fixedArray.get(3))); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | @SuppressWarnings("unchecked") |
| 140 | public void testAppendFixedArray() throws Exception { |
| 141 | TmfFixedArray<String> fixedArrayToAppend1 = new TmfFixedArray<String>(fString4); |
| 142 | TmfFixedArray<String> fixedArrayToAppend2 = new TmfFixedArray<String>(fString5); |
| 143 | TmfFixedArray<String> fixedArray = new TmfFixedArray<String>(); |
| 144 | |
| 145 | fixedArray = fixedArray.append(fFixedArray1, fixedArrayToAppend1); |
| 146 | assertEquals("append", 4, fixedArray.size()); |
| 147 | assertTrue("append", fString1.equals(fixedArray.get(0))); |
| 148 | assertTrue("append", fString2.equals(fixedArray.get(1))); |
| 149 | assertTrue("append", fString3.equals(fixedArray.get(2))); |
| 150 | assertTrue("append", fString4.equals(fixedArray.get(3))); |
| 151 | |
| 152 | fixedArray = fixedArray.append(fixedArrayToAppend2); |
| 153 | assertEquals("append", 5, fixedArray.size()); |
| 154 | assertTrue("append", fString5.equals(fixedArray.get(4))); |
| 155 | } |
| 156 | |
| 157 | // ------------------------------------------------------------------------ |
| 158 | // hashCode |
| 159 | // ------------------------------------------------------------------------ |
| 160 | |
| 161 | @SuppressWarnings("unchecked") |
| 162 | public void testHashCode() throws Exception { |
| 163 | TmfFixedArray<String> fixedArray1 = (TmfFixedArray<String>) fFixedArray1.clone(); |
| 164 | TmfFixedArray<String> fixedArray2 = (TmfFixedArray<String>) fFixedArray2.clone(); |
| 165 | |
| 166 | assertTrue("hashCode", fixedArray1.hashCode() == fFixedArray1.hashCode()); |
| 167 | assertTrue("hashCode", fFixedArray2.hashCode() == fixedArray2.hashCode()); |
| 168 | |
| 169 | assertTrue("hashCode", fFixedArray1.hashCode() != fixedArray2.hashCode()); |
| 170 | assertTrue("hashCode", fFixedArray2.hashCode() != fixedArray1.hashCode()); |
| 171 | } |
| 172 | |
| 173 | // ------------------------------------------------------------------------ |
| 174 | // toArray |
| 175 | // ------------------------------------------------------------------------ |
| 176 | |
| 177 | public void testToArray() { |
| 178 | String[] expected1 = {fString1, fString2, fString3}; |
| 179 | assertTrue("toArray", Arrays.equals(expected1, fFixedArray1.toArray())); |
| 180 | |
| 181 | String[] expected2 = {}; |
| 182 | assertTrue("toArray", Arrays.equals(expected2, fFixedArray2.toArray())); |
| 183 | } |
| 184 | |
| 185 | public void testToArrayArg() throws Exception { |
| 186 | String[] stringArray = new String[3]; |
| 187 | fFixedArray1.toArray(stringArray); |
| 188 | assertTrue("toArrayArg", stringArray[0].equals(fFixedArray1.get(0))); |
| 189 | assertTrue("toArrayArg", stringArray[1].equals(fFixedArray1.get(1))); |
| 190 | assertTrue("toArrayArg", stringArray[2].equals(fFixedArray1.get(2))); |
| 191 | |
| 192 | String[] stringBigArray = new String[10]; |
| 193 | fFixedArray1.toArray(stringBigArray); |
| 194 | assertNull("toArrayArg", stringBigArray[3]); |
| 195 | |
| 196 | TmfFixedArray<Object> fFixedArrayObject = new TmfFixedArray<Object>(fString1); |
| 197 | stringArray = fFixedArrayObject.toArray(new String[0]); |
| 198 | assertTrue("toArrayArg", stringArray[0].equals(fString1)); |
| 199 | } |
| 200 | |
| 201 | // ------------------------------------------------------------------------ |
| 202 | // Size |
| 203 | // ------------------------------------------------------------------------ |
| 204 | |
| 205 | public void testSize() { |
| 206 | assertEquals("toArray", 3, fFixedArray1.size()); |
| 207 | |
| 208 | assertEquals("toArray", 0, fFixedArray2.size()); |
| 209 | } |
| 210 | |
| 211 | // ------------------------------------------------------------------------ |
| 212 | // SubArray |
| 213 | // ------------------------------------------------------------------------ |
| 214 | |
| 215 | public void testSubArray() throws Exception { |
| 216 | TmfFixedArray<String> subArray = fFixedArray1.subArray(1); |
| 217 | |
| 218 | assertEquals("SubArray", 2, subArray.size()); |
| 219 | assertTrue("SubArray", fString2.equals(subArray.get(0))); |
| 220 | assertTrue("SubArray", fString3.equals(subArray.get(1))); |
| 221 | } |
| 222 | |
| 223 | public void testSubArray2() { |
| 224 | TmfFixedArray<String> subArray = fFixedArray1.subArray(1, 2); |
| 225 | |
| 226 | assertEquals("SubArray", 2, subArray.size()); |
| 227 | assertTrue("SubArray", fString2.equals(subArray.get(0))); |
| 228 | assertTrue("SubArray", fString3.equals(subArray.get(1))); |
| 229 | } |
| 230 | |
| 231 | // ------------------------------------------------------------------------ |
| 232 | // Set |
| 233 | // ------------------------------------------------------------------------ |
| 234 | |
| 235 | public void testSet() throws Exception { |
| 236 | String[] newString = {"new FirstString", "new SecondString", "new ThirdString"}; |
| 237 | |
| 238 | fFixedArray1.set(0, newString[0]); |
| 239 | assertTrue("getArray", newString[0].equals(newString[0])); |
| 240 | |
| 241 | fFixedArray1.set(1, newString[1]); |
| 242 | assertTrue("getArray", newString[1].equals(newString[1])); |
| 243 | |
| 244 | fFixedArray1.set(2, newString[2]); |
| 245 | assertTrue("getArray", newString[2].equals(newString[2])); |
| 246 | |
| 247 | try { |
| 248 | fFixedArray2.set(0, "newString"); |
| 249 | fail(); |
| 250 | } catch (Exception e) { |
| 251 | // Success |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // ------------------------------------------------------------------------ |
| 256 | // toString |
| 257 | // ------------------------------------------------------------------------ |
| 258 | |
| 259 | public void testToString() { |
| 260 | String expected1 = Arrays.asList(fString1, fString2, fString3).toString(); |
| 261 | assertEquals("toString", expected1, fFixedArray1.toString()); |
| 262 | |
| 263 | String expected2 = Arrays.asList().toString(); |
| 264 | assertEquals("toString", expected2, fFixedArray2.toString()); |
| 265 | } |
| 266 | } |