1 /*
   2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.control.skin;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertFalse;
  30 import static org.junit.Assert.assertNull;
  31 import static org.junit.Assert.assertTrue;
  32 
  33 import org.junit.Before;
  34 import org.junit.Test;
  35 
  36 import static javafx.scene.control.skin.VirtualFlow.ArrayLinkedList;
  37 
  38 public class ArrayLinkedListTest {
  39     private ArrayLinkedList<String> list;
  40     private String a = "a";
  41     private String b = "b";
  42     private String c = "c";
  43 
  44     @Before public void setUp() {
  45         list = new ArrayLinkedList<String>();
  46     }
  47     
  48     @Test public void testArrayLinkedList_Empty_GetFirstReturnsNull() {
  49         assertNull(list.getFirst());
  50     }
  51 
  52     @Test public void testArrayLinkedList_Empty_GetLastReturnsNull() {
  53         assertNull(list.getLast());
  54     }
  55 
  56     @Test public void testArrayLinkedList_Empty_AddFirst() {
  57         list.addFirst(a);
  58         assertEquals(1, list.size());
  59         assertEquals(a, list.getFirst());
  60         assertEquals(a, list.getLast());
  61         assertEquals(a, list.get(0));
  62         assertFalse(list.isEmpty());
  63     }
  64 
  65     @Test public void testArrayLinkedList_Empty_AddLast() {
  66         list.addLast(c);
  67         assertEquals(1, list.size());
  68         assertEquals(c, list.getFirst());
  69         assertEquals(c, list.getLast());
  70         assertEquals(c, list.get(0));
  71         assertFalse(list.isEmpty());
  72     }
  73 
  74     @Test public void testArrayLinkedList_Empty_SizeIsZero() {
  75         assertEquals(0, list.size());
  76     }
  77 
  78     @Test public void testArrayLinkedList_Empty_IsEmpty() {
  79         assertTrue(list.isEmpty());
  80     }
  81 
  82     @Test public void testArrayLinkedList_Empty_ClearHasNoEffect() {
  83         list.clear();
  84         assertTrue(list.isEmpty());
  85         assertEquals(0, list.size());
  86     }
  87 
  88     @Test public void testArrayLinkedList_Empty_GetResultsInArrayIndexOutOfBounds() {
  89         try {
  90             list.get(0);
  91             assertTrue("get didn't return an ArrayIndexOutOfBoundsException", false);
  92         } catch (ArrayIndexOutOfBoundsException e) {
  93             assertTrue(true);
  94         }
  95     }
  96 
  97     @Test public void testArrayLinkedList_Empty_RemoveFirstIsNoOp() {
  98         list.removeFirst();
  99     }
 100 
 101     @Test public void testArrayLinkedList_Empty_RemoveLastIsNoOp() {
 102         list.removeLast();
 103     }
 104 
 105     @Test public void testArrayLinkedList_Empty_RemoveResultsInArrayIndexOutOfBounds() {
 106         try {
 107             list.remove(0);
 108             assertTrue("remove didn't return an ArrayIndexOutOfBoundsException", false);
 109         } catch (ArrayIndexOutOfBoundsException e) {
 110             assertTrue(true);
 111         }
 112     }
 113 
 114     @Test public void testArrayLinkedList_GetFirst_AfterAddLast() {
 115         list.addLast(a);
 116         list.addLast(b);
 117         list.addLast(c);
 118         assertEquals(a, list.getFirst());
 119     }
 120 
 121     @Test public void testArrayLinkedList_GetFirst_AfterAddFirst() {
 122         list.addFirst(c);
 123         list.addFirst(b);
 124         list.addFirst(a);
 125         assertEquals(a, list.getFirst());
 126     }
 127 
 128     @Test public void testArrayLinkedList_GetFirst_AfterAddFirstAndAddLast() {
 129         list.addFirst(b);
 130         list.addLast(c);
 131         list.addFirst(a);
 132         assertEquals(a, list.getFirst());
 133     }
 134 
 135     @Test public void testArrayLinkedList_GetFirst_AfterRemoveFirst() {
 136         list.addLast(a);
 137         list.addLast(b);
 138         list.addLast(c);
 139         list.removeFirst();
 140         assertEquals(b, list.getFirst());
 141         list.removeFirst();
 142         assertEquals(c, list.getFirst());
 143         list.removeFirst();
 144         assertNull(list.getFirst());
 145     }
 146 
 147     @Test public void testArrayLinkedList_GetFirst_AfterRemoveLast() {
 148         list.addLast(a);
 149         list.addLast(b);
 150         list.addLast(c);
 151         list.removeLast();
 152         assertEquals(a, list.getFirst());
 153         list.removeLast();
 154         assertEquals(a, list.getFirst());
 155         list.removeLast();
 156         assertNull(list.getFirst());
 157     }
 158 
 159     @Test public void testArrayLinkedList_GetLast_AfterAddLast() {
 160         list.addLast(a);
 161         list.addLast(b);
 162         list.addLast(c);
 163         assertEquals(c, list.getLast());
 164     }
 165 
 166     @Test public void testArrayLinkedList_GetLast_AfterAddFirst() {
 167         list.addFirst(c);
 168         list.addFirst(b);
 169         list.addFirst(a);
 170         assertEquals(c, list.getLast());
 171     }
 172 
 173     @Test public void testArrayLinkedList_GetLast_AfterAddFirstAndAddLast() {
 174         list.addFirst(b);
 175         list.addLast(c);
 176         list.addFirst(a);
 177         assertEquals(c, list.getLast());
 178     }
 179 
 180     @Test public void testArrayLinkedList_GetLast_AfterRemoveFirst() {
 181         list.addLast(a);
 182         list.addLast(b);
 183         list.addLast(c);
 184         list.removeFirst();
 185         assertEquals(c, list.getLast());
 186         list.removeFirst();
 187         assertEquals(c, list.getLast());
 188         list.removeFirst();
 189         assertNull(list.getLast());
 190     }
 191 
 192     @Test public void testArrayLinkedList_GetLast_AfterRemoveLast() {
 193         list.addLast(a);
 194         list.addLast(b);
 195         list.addLast(c);
 196         list.removeLast();
 197         assertEquals(b, list.getLast());
 198         list.removeLast();
 199         assertEquals(a, list.getLast());
 200         list.removeLast();
 201         assertNull(list.getLast());
 202     }
 203 
 204     @Test public void testArrayLinkedList_Get_AfterAddLast() {
 205         list.addLast(a);
 206         list.addLast(b);
 207         list.addLast(c);
 208         assertEquals(a, list.get(0));
 209         assertEquals(b, list.get(1));
 210         assertEquals(c, list.get(2));
 211     }
 212 
 213     @Test public void testArrayLinkedList_Get_AfterAddFirst() {
 214         list.addFirst(c);
 215         list.addFirst(b);
 216         list.addFirst(a);
 217         assertEquals(a, list.get(0));
 218         assertEquals(b, list.get(1));
 219         assertEquals(c, list.get(2));
 220     }
 221 
 222     @Test public void testArrayLinkedList_Get_AfterAddFirstAndAddLast() {
 223         list.addFirst(b);
 224         list.addLast(c);
 225         list.addFirst(a);
 226         assertEquals(a, list.get(0));
 227         assertEquals(b, list.get(1));
 228         assertEquals(c, list.get(2));
 229     }
 230 
 231     @Test public void testArrayLinkedList_Get_AfterRemoveFirst() {
 232         list.addLast(a);
 233         list.addLast(b);
 234         list.addLast(c);
 235         list.removeFirst();
 236         assertEquals(b, list.get(0));
 237         assertEquals(c, list.get(1));
 238         list.removeFirst();
 239         assertEquals(c, list.get(0));
 240     }
 241 
 242     @Test public void testArrayLinkedList_Get_AfterRemoveLast() {
 243         list.addLast(a);
 244         list.addLast(b);
 245         list.addLast(c);
 246         list.removeLast();
 247         assertEquals(a, list.get(0));
 248         assertEquals(b, list.get(1));
 249         list.removeLast();
 250         assertEquals(a, list.get(0));
 251     }
 252 
 253 //    // using a combination of addFirst, addLast, removeFirst, removeLast,
 254 //    // and remove calls, check the status of the list and ensure it is
 255 //    // always in the expected state
 256 //    @Test public void testArrayLinkedList_StressTest() {
 257 //        // This is our "control" in the classical scientific testing sense.
 258 //        // If the values / sizes / results in the list match the control, then
 259 //        // we know the VirtualFlow.ArrayLinkedList implementation is working
 260 //        List control = new LinkedList();
 261 //        // Iterate a bunch of times
 262 //        for (int i = 0; i < 5000; i++) {
 263 //            // there are 1 of 6 different possible outcomes here:
 264 //            //  1) addFirst
 265 //            //  2) addLast
 266 //            //  3) removeFirst
 267 //            //  4) removeLast
 268 //            //  5) remove
 269 //            //  6) clear
 270 //            // but since I want to clear very infrequently, I weight everything
 271 //            // between 0 and 60 and the "clear" option has to be an even 59.
 272 //            int choice = (int) (Math.random() * 50);
 273 //            if (choice < 10) {
 274 //                char input = Character.toChars((Math.random() * 26) + 65)[0];
 275 //                IndexedCell cell = new CellStub("input");
 276 //                control.addFirst(cell);
 277 //                list.addFirst(cell);
 278 //                assertMatch(control, list);
 279 //            } else if (choice < 20) {
 280 //                char input = Character.toChars((Math.random() * 26) + 65)[0];
 281 //                IndexedCell cell = new CellStub("input");
 282 //                control.addLast(cell);
 283 //                list.addLast(cell);
 284 //                assertMatch(control, list);
 285 //            } else if (choice < 30) {
 286 //                if (control.size() == 0) continue;
 287 //                IndexedCell cell = (IndexedCell) control.removeFirst();
 288 //                IndexedCell cell2 = list.removeFirst();
 289 //                assertSame(cell, cell2);
 290 //                assertMatch(control, list);
 291 //            } else if (choice < 40) {
 292 //                if (control.size() == 0) continue;
 293 //                IndexedCell cell = control.removeLast();
 294 //                IndexedCell cell2 = list.removeLast();
 295 //                assertSame(cell, cell2);
 296 //                assertMatch(control, list);
 297 //            } else if (choice < 49) {
 298 //                if (control.size() == 0) continue;
 299 //                int index = (Math.random() * control.size());
 300 //                if (index == control.size()) index--;
 301 //                IndexedCell cell = control.remove(index);
 302 //                def cell2 = list.remove(index);
 303 //                assertSame(cell, cell2);
 304 //                assertMatch(control, list);
 305 //            } else if (choice < 50) {
 306 //                control.clear();
 307 //                list.clear();
 308 //                assertMatch(control, list);
 309 //            }
 310 //        }
 311 //    }
 312 }