2
0

CompactVector3ArrayTest.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package com.jme.animation;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertTrue;
  4. import static org.junit.Assert.fail;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.Arrays;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import com.jme3.animation.CompactVector3Array;
  11. import com.jme3.export.binary.BinaryExporter;
  12. import com.jme3.export.binary.BinaryImporter;
  13. import com.jme3.math.Vector3f;
  14. public class CompactVector3ArrayTest {
  15. private final Vector3f[] objArray1 = new Vector3f[] {
  16. new Vector3f(1, 0, 1), // 0
  17. new Vector3f(1, 1, 1), // 1
  18. new Vector3f(0, 1, 1), // 2
  19. new Vector3f(1, 1, 1), // 1
  20. new Vector3f(1, 0, 1), // 0
  21. };
  22. private final Vector3f[] objArray2 = new Vector3f[] {
  23. new Vector3f(1, 0, 2), // 3
  24. new Vector3f(1, 1, 1), // 1
  25. new Vector3f(0, 1, 1), // 2
  26. null, // -1
  27. new Vector3f(1, 0, 2), // 3
  28. };
  29. private static final int[] index1 = new int[] {0, 1, 2, 1, 0};
  30. private static final int[] index2 = new int[] {3, 1, 2, -1, 3};
  31. private int[] index12;
  32. private static final float[] serialData = new float[] {1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 2};
  33. CompactVector3Array compact;
  34. @Before
  35. public void setUp() throws Exception {
  36. compact = new CompactVector3Array();
  37. index12 = Arrays.copyOf(index1, index1.length+index2.length);
  38. System.arraycopy(index2, 0, index12, index1.length, index2.length);
  39. }
  40. @Test
  41. public void testCompactVector3ArrayAdd() {
  42. compact.add(objArray1);
  43. compact.add(objArray2);
  44. _testAdd();
  45. try {
  46. compact.freeze();
  47. compact.add(objArray1);
  48. fail();
  49. } catch (Exception e) {
  50. }
  51. }
  52. private void _testAdd() {
  53. assertTrue(Arrays.equals(compact.getIndex(objArray1), index1));
  54. assertTrue(Arrays.equals(compact.getIndex(objArray2), index2));
  55. assertTrue(Arrays.equals(compact.getSerializedData(), serialData));
  56. }
  57. @Test
  58. public void testCompactVector3ArrayFloatArrayIntArray() {
  59. int[] indexArray = index1;
  60. float[] dataArray = new float[] {1, 0, 1, 1, 1, 1, 0, 1, 1};
  61. Vector3f[] objArray = new Vector3f[] {
  62. new Vector3f(1, 0, 1),
  63. new Vector3f(1, 1, 1),
  64. new Vector3f(0, 1, 1),
  65. new Vector3f(1, 1, 1),
  66. new Vector3f(1, 0, 1),
  67. };
  68. CompactVector3Array compact = new CompactVector3Array(dataArray, indexArray);
  69. assertTrue(Arrays.deepEquals(compact.toObjectArray(), objArray));
  70. }
  71. @Test
  72. public void testGetTotalObjectSize() {
  73. compact.add(objArray1);
  74. assertTrue(compact.getTotalObjectSize() == 5);
  75. assertTrue(compact.getCompactObjectSize() == 3);
  76. compact.add(objArray2);
  77. _testSize();
  78. }
  79. private void _testSize() {
  80. assertTrue(compact.getTotalObjectSize() == 10);
  81. assertTrue(compact.getCompactObjectSize() == 4);
  82. }
  83. @Test
  84. public void testGet() {
  85. compact.add(objArray1);
  86. Vector3f v1 = compact.get(1, new Vector3f());
  87. assertEquals(new Vector3f(1, 1, 1), v1);
  88. compact.add(objArray2);
  89. _testGet();
  90. }
  91. private void _testGet() {
  92. Vector3f v2 = compact.get(1, new Vector3f());
  93. assertEquals(new Vector3f(1, 1, 1), v2);
  94. Vector3f v3 = compact.get(5, new Vector3f());
  95. assertEquals(new Vector3f(1, 0, 2), v3);
  96. }
  97. @Test
  98. public void testGetCompactIndex() {
  99. compact.add(objArray1);
  100. compact.add(objArray2);
  101. _testCompactIndex();
  102. }
  103. private void _testCompactIndex() {
  104. for (int i = 0; i < index12.length; i++) {
  105. assertEquals(index12[i], compact.getCompactIndex(i));
  106. }
  107. }
  108. @Test
  109. public void testGetIndex() {
  110. compact.add(objArray1);
  111. compact.add(objArray2);
  112. _testGetIndex();
  113. }
  114. private void _testGetIndex() {
  115. Vector3f[] reverse = new Vector3f[objArray1.length];
  116. int[] reverseIndex = new int[objArray1.length];
  117. for (int i = 0; i < objArray1.length; i++) {
  118. reverse[i] = objArray1[objArray1.length-1-i];
  119. reverseIndex[i] = index1[objArray1.length-1-i];
  120. }
  121. int[] index = compact.getIndex(reverse);
  122. for (int i = 0; i < index.length; i++) {
  123. assertEquals(reverseIndex[i], index[i]);
  124. }
  125. }
  126. @Test
  127. public void testRead() throws IOException {
  128. File file = File.createTempFile("compactArray", "test");
  129. BinaryImporter importer = new BinaryImporter();
  130. BinaryExporter exporter = new BinaryExporter();
  131. compact.add(objArray1);
  132. compact.add(objArray2);
  133. exporter.save(compact, file);
  134. compact = (CompactVector3Array) importer.load(file);
  135. _testSize();
  136. _testCompactIndex();
  137. _testGet();
  138. file.delete();
  139. }
  140. }