CompactVector3ArrayTest.java 5.0 KB

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