tarray13.pp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. { test for the various DynArray* functions }
  2. program tarray13;
  3. {$mode objfpc}
  4. function TestDynArraySize: LongInt;
  5. type
  6. TTest = record
  7. value1: QWord;
  8. value2: LongInt;
  9. end;
  10. var
  11. arr1: array of LongInt;
  12. arr2: array of QWord;
  13. arr3: array of TTest;
  14. begin
  15. SetLength(arr1, 5);
  16. SetLength(arr2, 8);
  17. SetLength(arr3, 6);
  18. if DynArraySize(Pointer(arr1)) <> 5 then
  19. Halt(1);
  20. if DynArraySize(@arr1[0]) <> 5 then
  21. Halt(2);
  22. if DynArraySize(Pointer(arr2)) <> 8 then
  23. Halt(3);
  24. if DynArraySize(@arr2[0]) <> 8 then
  25. Halt(4);
  26. if DynArraySize(Pointer(arr3)) <> 6 then
  27. Halt(5);
  28. if DynArraySize(@arr3[0]) <> 6 then
  29. Halt(6);
  30. Result := 7;
  31. end;
  32. function TestDynArrayIndexSingle(aBaseCode: LongInt): LongInt;
  33. type
  34. TTest1 = record
  35. value1: QWord;
  36. value2: LongInt;
  37. end;
  38. TTest2 = packed record
  39. value1: LongInt;
  40. value2: QWord;
  41. end;
  42. var
  43. arr1: array of Byte;
  44. arr2: array of LongInt;
  45. arr3: array of QWord;
  46. arr4: array of TTest1;
  47. arr5: array of TTest2;
  48. i, basecode: LongInt;
  49. begin
  50. SetLength(arr1, 5);
  51. SetLength(arr2, 3);
  52. SetLength(arr3, 8);
  53. SetLength(arr4, 6);
  54. SetLength(arr5, 7);
  55. basecode := aBaseCode;
  56. for i := Low(arr1) to High(arr1) do begin
  57. if @arr1[i] <> DynArrayIndex(Pointer(arr1), [i], TypeInfo(arr1)) then
  58. Halt(basecode + i * 2);
  59. if @arr1[i] <> DynArrayIndex(@arr1[0], [i], TypeInfo(arr1)) then
  60. Halt(basecode + i * 2 + 1);
  61. end;
  62. basecode := basecode + 2 * Length(arr1);
  63. for i := Low(arr2) to High(arr2) do begin
  64. if @arr2[i] <> DynArrayIndex(Pointer(arr2), [i], TypeInfo(arr2)) then
  65. Halt(basecode + i * 2);
  66. if @arr2[i] <> DynArrayIndex(@arr2[0], [i], TypeInfo(arr2)) then
  67. Halt(basecode + i * 2 + 1);
  68. end;
  69. basecode := basecode + 2 * Length(arr2);
  70. for i := Low(arr3) to High(arr3) do begin
  71. if @arr3[i] <> DynArrayIndex(Pointer(arr3), [i], TypeInfo(arr3)) then
  72. Halt(basecode + i * 2);
  73. if @arr3[i] <> DynArrayIndex(@arr3[0], [i], TypeInfo(arr3)) then
  74. Halt(basecode + i * 2 + 1);
  75. end;
  76. basecode := basecode + 2 * Length(arr3);
  77. for i := Low(arr4) to High(arr4) do begin
  78. if @arr4[i] <> DynArrayIndex(Pointer(arr4), [i], TypeInfo(arr4)) then
  79. Halt(basecode + i * 2);
  80. if @arr4[i] <> DynArrayIndex(@arr4[0], [i], TypeInfo(arr4)) then
  81. Halt(basecode + i * 2 + 1);
  82. end;
  83. basecode := basecode + 2 * Length(arr4);
  84. for i := Low(arr5) to High(arr5) do begin
  85. if @arr5[i] <> DynArrayIndex(Pointer(arr5), [i], TypeInfo(arr5)) then
  86. Halt(basecode + i * 2);
  87. if @arr5[i] <> DynArrayIndex(@arr5[0], [i], TypeInfo(arr5)) then
  88. Halt(basecode + i * 2 + 1);
  89. end;
  90. Result := basecode + 2 * Length(arr5);
  91. end;
  92. function TestDynArrayIndexMulti(aBaseCode: LongInt): LongInt;
  93. var
  94. arr1: array of array of LongInt;
  95. arr2: array of array of array of QWord;
  96. i, j, k, basecode: LongInt;
  97. begin
  98. SetLength(arr1, 4, 8);
  99. SetLength(arr2, 3, 5, 9);
  100. basecode := aBaseCode;
  101. for i := Low(arr1) to High(arr1) do begin
  102. for j := Low(arr1[i]) to High(arr1[i]) do begin
  103. if @arr1[i, j] <> DynArrayIndex(Pointer(arr1), [i, j], TypeInfo(arr1)) then
  104. Halt(basecode + j * 2);
  105. if @arr1[i, j] <> DynArrayIndex(@arr1[0], [i, j], TypeInfo(arr1)) then
  106. Halt(basecode + j * 2 + 1);
  107. { Note: @arr1[0, 0] would be different from @arr1[0] or arr1! }
  108. end;
  109. basecode := basecode + Length(arr1[i]) * 3;
  110. end;
  111. for i := Low(arr2) to High(arr2) do begin
  112. for j := Low(arr2[i]) to High(arr2[i]) do begin
  113. for k := Low(arr2[i, j]) to High(arr2[i, j]) do begin
  114. if @arr2[i, j, k] <> DynArrayIndex(Pointer(arr2), [i, j, k], TypeInfo(arr2)) then
  115. Halt(basecode + k * 2);
  116. if @arr2[i, j, k] <> DynArrayIndex(@arr2[0], [i, j, k], TypeInfo(arr2)) then
  117. Halt(basecode + k * 2 + 1);
  118. { Note: @arr2[0, 0] and @arr2[0, 0, 0] would be different from @arr2[0] or arr2! }
  119. end;
  120. basecode := basecode + Length(arr2[i, j]) * 2;
  121. end;
  122. end;
  123. Result := basecode;
  124. end;
  125. var
  126. basecode: LongInt;
  127. begin
  128. Writeln('TestDynArraySize errors starting at 1');
  129. basecode := TestDynArraySize;
  130. Writeln('TestDynArrayIndexSingle errors starting at ', basecode);
  131. basecode := TestDynArrayIndexSingle(basecode);
  132. Writeln('TestDynArrayIndexMulti errors starting at ', basecode);
  133. basecode := TestDynArrayIndexMulti(basecode);
  134. Writeln('ok');
  135. end.