PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/test-suite/d/li_std_vector_runme.1.d

#
D | 219 lines | 172 code | 35 blank | 12 comment | 33 complexity | 21dd9d2fecfbf5ab07142283f6790276 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. module li_std_vector_runme;
  2. import tango.core.Exception;
  3. import tango.io.Stdout;
  4. import Integer = tango.text.convert.Integer;
  5. import li_std_vector.li_std_vector;
  6. import li_std_vector.DoubleVector;
  7. import li_std_vector.IntVector;
  8. import li_std_vector.IntPtrVector;
  9. import li_std_vector.IntConstPtrVector;
  10. import li_std_vector.RealVector;
  11. import li_std_vector.Struct;
  12. import li_std_vector.StructVector;
  13. import li_std_vector.StructPtrVector;
  14. import li_std_vector.StructConstPtrVector;
  15. const size_t SIZE = 20;
  16. void main() {
  17. // Basic functionality tests.
  18. {
  19. auto vector = new IntVector();
  20. for (size_t i = 0; i < SIZE; ++i) {
  21. vector ~= i * 10;
  22. }
  23. if (vector.length != SIZE) {
  24. throw new Exception("length test failed.");
  25. }
  26. vector[0] = 200;
  27. if (vector[0] != 200) {
  28. throw new Exception("indexing test failed");
  29. }
  30. vector[0] = 0 * 10;
  31. try {
  32. vector[vector.length] = 777;
  33. throw new Exception("out of range test failed");
  34. } catch (NoSuchElementException) {
  35. }
  36. foreach (i, value; vector) {
  37. if (value != (i * 10)) {
  38. throw new Exception("foreach test failed, i: " ~ Integer.toString(i));
  39. }
  40. }
  41. vector.clear();
  42. if (vector.size != 0) {
  43. throw new Exception("clear test failed");
  44. }
  45. }
  46. // Slice tests.
  47. {
  48. auto dVector = new DoubleVector();
  49. for (size_t i = 0; i < SIZE; ++i) {
  50. dVector ~= i * 10.1f;
  51. }
  52. double[] dArray = dVector[];
  53. foreach (i, value; dArray) {
  54. if (dVector[i] != value) {
  55. throw new Exception("slice test 1 failed, i: " ~ Integer.toString(i));
  56. }
  57. }
  58. auto sVector = new StructVector();
  59. for (size_t i = 0; i < SIZE; i++) {
  60. sVector ~= new Struct(i / 10.0);
  61. }
  62. Struct[] array = sVector[];
  63. for (size_t i = 0; i < SIZE; i++) {
  64. // Make sure that a shallow copy has been made.
  65. void* aPtr = Struct.swigGetCPtr(array[i]);
  66. void* vPtr = Struct.swigGetCPtr(sVector[i]);
  67. if (aPtr != vPtr) {
  68. throw new Exception("slice test 2 failed, i: " ~
  69. Integer.toString(i));
  70. }
  71. }
  72. }
  73. // remove() tests.
  74. {
  75. auto iVector = new IntVector();
  76. for (int i = 0; i < SIZE; i++) {
  77. iVector ~= i;
  78. }
  79. iVector.remove(iVector.length - 1);
  80. iVector.remove(SIZE / 2);
  81. iVector.remove(0);
  82. try {
  83. iVector.remove(iVector.size);
  84. throw new Exception("remove test failed");
  85. } catch (NoSuchElementException) {
  86. }
  87. }
  88. // Capacity tests.
  89. {
  90. auto dv = new DoubleVector(10);
  91. if ((dv.capacity != 10) || (dv.length != 0)) {
  92. throw new Exception("constructor setting capacity test failed");
  93. }
  94. // TODO: Is this really required (and spec'ed) behavior?
  95. dv.capacity = 20;
  96. if (dv.capacity != 20) {
  97. throw new Exception("capacity test 1 failed");
  98. }
  99. dv ~= 1.11;
  100. try {
  101. dv.capacity = dv.length - 1;
  102. throw new Exception("capacity test 2 failed");
  103. } catch (IllegalArgumentException) {
  104. }
  105. }
  106. // Test the methods being wrapped.
  107. {
  108. auto iv = new IntVector();
  109. for (int i=0; i<4; i++) {
  110. iv ~= i;
  111. }
  112. double x = average(iv);
  113. x += average(new IntVector([1, 2, 3, 4]));
  114. RealVector rv = half(new RealVector([10.0f, 10.5f, 11.0f, 11.5f]));
  115. auto dv = new DoubleVector();
  116. for (size_t i = 0; i < SIZE; i++) {
  117. dv ~= i / 2.0;
  118. }
  119. halve_in_place(dv);
  120. RealVector v0 = vecreal(new RealVector());
  121. float flo = 123.456f;
  122. v0 ~= flo;
  123. flo = v0[0];
  124. IntVector v1 = vecintptr(new IntVector());
  125. IntPtrVector v2 = vecintptr(new IntPtrVector());
  126. IntConstPtrVector v3 = vecintconstptr(new IntConstPtrVector());
  127. v1 ~= 123;
  128. v2.clear();
  129. v3.clear();
  130. StructVector v4 = vecstruct(new StructVector());
  131. StructPtrVector v5 = vecstructptr(new StructPtrVector());
  132. StructConstPtrVector v6 = vecstructconstptr(new StructConstPtrVector());
  133. v4 ~= new Struct(123);
  134. v5 ~= new Struct(123);
  135. v6 ~= new Struct(123);
  136. }
  137. // Test vectors of pointers.
  138. {
  139. auto vector = new StructPtrVector();
  140. for (size_t i = 0; i < SIZE; i++) {
  141. vector ~= new Struct(i / 10.0);
  142. }
  143. Struct[] array = vector[];
  144. for (size_t i = 0; i < SIZE; i++) {
  145. // Make sure that a shallow copy has been made.
  146. void* aPtr = Struct.swigGetCPtr(array[i]);
  147. void* vPtr = Struct.swigGetCPtr(vector[i]);
  148. if (aPtr != vPtr) {
  149. throw new Exception("StructPtrVector test 1 failed, i: " ~
  150. Integer.toString(i));
  151. }
  152. }
  153. }
  154. // Test vectors of const pointers.
  155. {
  156. auto vector = new StructConstPtrVector();
  157. for (size_t i = 0; i < SIZE; i++) {
  158. vector ~= new Struct(i / 10.0);
  159. }
  160. Struct[] array = vector[];
  161. for (size_t i = 0; i < SIZE; i++) {
  162. // Make sure that a shallow copy has been made.
  163. void* aPtr = Struct.swigGetCPtr(array[i]);
  164. void* vPtr = Struct.swigGetCPtr(vector[i]);
  165. if (aPtr != vPtr) {
  166. throw new Exception("StructConstPtrVector test 1 failed, i: " ~
  167. Integer.toString(i));
  168. }
  169. }
  170. }
  171. // Test vectors destroyed via dispose().
  172. {
  173. {
  174. scope vector = new StructVector();
  175. vector ~= new Struct(0.0);
  176. vector ~= new Struct(11.1);
  177. }
  178. {
  179. scope vector = new DoubleVector();
  180. vector ~= 0.0;
  181. vector ~= 11.1;
  182. }
  183. }
  184. }