|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 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 | +package com.jme3.light; |
| 33 | + |
| 34 | +import com.jme3.math.Vector3f; |
| 35 | +import com.jme3.scene.Geometry; |
| 36 | +import com.jme3.scene.Mesh; |
| 37 | +import org.junit.jupiter.api.Assertions; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | + |
| 40 | +/** |
| 41 | + * Test cases for LightList. |
| 42 | + */ |
| 43 | +public class LightListTest { |
| 44 | + |
| 45 | + /** |
| 46 | + * Verify sort() remains correct after the list previously held more lights. |
| 47 | + */ |
| 48 | + @Test |
| 49 | + public void testSortAfterRetainedCapacity() { |
| 50 | + Geometry owner = new Geometry("owner", new Mesh()); |
| 51 | + LightList list = new LightList(owner); |
| 52 | + |
| 53 | + for (int i = 0; i < 32; i++) { |
| 54 | + list.add(new PointLight(new Vector3f(100f + i, 0f, 0f))); |
| 55 | + } |
| 56 | + list.sort(true); |
| 57 | + list.clear(); |
| 58 | + |
| 59 | + PointLight far = new PointLight(new Vector3f(10f, 0f, 0f)); |
| 60 | + PointLight near = new PointLight(new Vector3f(1f, 0f, 0f)); |
| 61 | + PointLight middle = new PointLight(new Vector3f(5f, 0f, 0f)); |
| 62 | + |
| 63 | + list.add(far); |
| 64 | + list.add(near); |
| 65 | + list.add(middle); |
| 66 | + list.sort(true); |
| 67 | + |
| 68 | + Assertions.assertSame(near, list.get(0)); |
| 69 | + Assertions.assertSame(middle, list.get(1)); |
| 70 | + Assertions.assertSame(far, list.get(2)); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Verify indexed removal preserves order for front, middle, and tail removals. |
| 75 | + */ |
| 76 | + @Test |
| 77 | + public void testRemovePreservesOrder() { |
| 78 | + LightList list = new LightList(new Geometry("owner", new Mesh())); |
| 79 | + AmbientLight first = new AmbientLight(); |
| 80 | + AmbientLight second = new AmbientLight(); |
| 81 | + AmbientLight third = new AmbientLight(); |
| 82 | + AmbientLight fourth = new AmbientLight(); |
| 83 | + |
| 84 | + list.add(first); |
| 85 | + list.add(second); |
| 86 | + list.add(third); |
| 87 | + list.add(fourth); |
| 88 | + |
| 89 | + list.remove(0); |
| 90 | + Assertions.assertEquals(3, list.size()); |
| 91 | + Assertions.assertSame(second, list.get(0)); |
| 92 | + Assertions.assertSame(third, list.get(1)); |
| 93 | + Assertions.assertSame(fourth, list.get(2)); |
| 94 | + |
| 95 | + list.remove(1); |
| 96 | + Assertions.assertEquals(2, list.size()); |
| 97 | + Assertions.assertSame(second, list.get(0)); |
| 98 | + Assertions.assertSame(fourth, list.get(1)); |
| 99 | + |
| 100 | + list.remove(1); |
| 101 | + Assertions.assertEquals(1, list.size()); |
| 102 | + Assertions.assertSame(second, list.get(0)); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Verify unfiltered world-list updates preserve local-then-parent ordering. |
| 107 | + */ |
| 108 | + @Test |
| 109 | + public void testUpdateCopiesLocalAndParentLights() { |
| 110 | + LightList local = new LightList(new Geometry("local", new Mesh())); |
| 111 | + LightList parent = new LightList(new Geometry("parent", new Mesh())); |
| 112 | + LightList world = new LightList(new Geometry("world", new Mesh())); |
| 113 | + |
| 114 | + AmbientLight localFirst = new AmbientLight(); |
| 115 | + AmbientLight localSecond = new AmbientLight(); |
| 116 | + AmbientLight parentFirst = new AmbientLight(); |
| 117 | + AmbientLight parentSecond = new AmbientLight(); |
| 118 | + |
| 119 | + local.add(localFirst); |
| 120 | + local.add(localSecond); |
| 121 | + parent.add(parentFirst); |
| 122 | + parent.add(parentSecond); |
| 123 | + |
| 124 | + world.update(local, parent); |
| 125 | + |
| 126 | + Assertions.assertEquals(4, world.size()); |
| 127 | + Assertions.assertSame(localFirst, world.get(0)); |
| 128 | + Assertions.assertSame(localSecond, world.get(1)); |
| 129 | + Assertions.assertSame(parentFirst, world.get(2)); |
| 130 | + Assertions.assertSame(parentSecond, world.get(3)); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Verify filtered world-list updates keep only accepted local lights. |
| 135 | + */ |
| 136 | + @Test |
| 137 | + public void testUpdateFiltersLocalLights() { |
| 138 | + LightList local = new LightList(new Geometry("local", new Mesh())); |
| 139 | + LightList parent = new LightList(new Geometry("parent", new Mesh())); |
| 140 | + LightList world = new LightList(new Geometry("world", new Mesh())); |
| 141 | + |
| 142 | + AmbientLight keep = new AmbientLight(); |
| 143 | + AmbientLight discard = new AmbientLight(); |
| 144 | + AmbientLight parentLight = new AmbientLight(); |
| 145 | + |
| 146 | + local.add(keep); |
| 147 | + local.add(discard); |
| 148 | + parent.add(parentLight); |
| 149 | + |
| 150 | + world.update(local, parent, light -> light != discard); |
| 151 | + |
| 152 | + Assertions.assertEquals(2, world.size()); |
| 153 | + Assertions.assertSame(keep, world.get(0)); |
| 154 | + Assertions.assertSame(parentLight, world.get(1)); |
| 155 | + } |
| 156 | +} |
0 commit comments