Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 package jme3test.app;
     34 
     35 import com.jme3.renderer.IDList;
     36 import java.util.*;
     37 
     38 public class TestIDList {
     39 
     40     static class StateCol {
     41 
     42         static Random rand = new Random();
     43 
     44         Map<Integer, Object> objs = new HashMap<Integer, Object>();
     45 
     46         public StateCol(){
     47             // populate with free ids
     48             List<Integer> freeIds = new ArrayList();
     49             for (int i = 0; i < 16; i++){
     50                 freeIds.add(i);
     51             }
     52 
     53             // create random
     54             int numStates = rand.nextInt(6) + 1;
     55             for (int i = 0; i < numStates; i++){
     56                 // remove a random id from free id list
     57                 int idx = rand.nextInt(freeIds.size());
     58                 int id = freeIds.remove(idx);
     59 
     60                 objs.put(id, new Object());
     61             }
     62         }
     63 
     64         public void print(){
     65             System.out.println("-----------------");
     66 
     67             Set<Integer> keys = objs.keySet();
     68             Integer[] keysArr = keys.toArray(new Integer[0]);
     69             Arrays.sort(keysArr);
     70             for (int i = 0; i < keysArr.length; i++){
     71                 System.out.println(keysArr[i]+" => "+objs.get(keysArr[i]).hashCode());
     72             }
     73         }
     74 
     75     }
     76 
     77     static IDList list = new IDList();
     78     static int boundSlot = 0;
     79 
     80     static Object[] slots = new Object[16];
     81     static boolean[] enabledSlots = new boolean[16];
     82 
     83     static void enable(int slot){
     84         System.out.println("Enabled SLOT["+slot+"]");
     85         if (enabledSlots[slot] == true){
     86             System.err.println("FAIL! Extra state change");
     87         }
     88         enabledSlots[slot] = true;
     89     }
     90 
     91     static void disable(int slot){
     92         System.out.println("Disabled SLOT["+slot+"]");
     93         if (enabledSlots[slot] == false){
     94             System.err.println("FAIL! Extra state change");
     95         }
     96         enabledSlots[slot] = false;
     97     }
     98 
     99     static void setSlot(int slot, Object val){
    100         if (!list.moveToNew(slot)){
    101             enable(slot);
    102         }
    103         if (slots[slot] != val){
    104             System.out.println("SLOT["+slot+"] = "+val.hashCode());
    105             slots[slot] = val;
    106         }
    107     }
    108 
    109     static void checkSlots(StateCol state){
    110         for (int i = 0; i < 16; i++){
    111             if (slots[i] != null && enabledSlots[i] == false){
    112                 System.err.println("FAIL! SLOT["+i+"] assigned, but disabled");
    113             }
    114             if (slots[i] == null && enabledSlots[i] == true){
    115                 System.err.println("FAIL! SLOT["+i+"] enabled, but not assigned");
    116             }
    117 
    118             Object val = state.objs.get(i);
    119             if (val != null){
    120                 if (slots[i] != val)
    121                     System.err.println("FAIL! SLOT["+i+"] does not contain correct value");
    122                 if (!enabledSlots[i])
    123                     System.err.println("FAIL! SLOT["+i+"] is not enabled");
    124             }else{
    125                 if (slots[i] != null)
    126                     System.err.println("FAIL! SLOT["+i+"] is not set");
    127                 if (enabledSlots[i])
    128                     System.err.println("FAIL! SLOT["+i+"] is enabled");
    129             }
    130         }
    131     }
    132 
    133     static void clearSlots(){
    134         for (int i = 0; i < list.oldLen; i++){
    135             int slot = list.oldList[i];
    136             disable(slot);
    137             slots[slot] = null;
    138         }
    139         list.copyNewToOld();
    140 //        context.attribIndexList.print();
    141     }
    142 
    143     static void setState(StateCol state){
    144         state.print();
    145         for (Map.Entry<Integer, Object> entry : state.objs.entrySet()){
    146             setSlot(entry.getKey(), entry.getValue());
    147         }
    148         clearSlots();
    149         checkSlots(state);
    150     }
    151 
    152     public static void main(String[] args){
    153         StateCol[] states = new StateCol[20];
    154         for (int i = 0; i < states.length; i++)
    155             states[i] = new StateCol();
    156 
    157         // shuffle would be useful here..
    158 
    159         for (int i = 0; i < states.length; i++){
    160             setState(states[i]);
    161         }
    162     }
    163 
    164 }
    165