tarray9.pp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. { %OPT=-gh }
  2. { Test correct RTTI handling of open arrays with managed elements.
  3. When a part (slice or range) of array is passed as an out-parameter open array
  4. to a procedure, the entire array should NOT be finalized, only part that is actually passed should. }
  5. {$mode objfpc}{$h+}
  6. uses SysUtils;
  7. procedure test3(out arr: array of string);
  8. var
  9. i: Integer;
  10. begin
  11. { implicit initialization happens here }
  12. for i := 0 to High(arr) do
  13. begin
  14. Pointer(arr[i]):=nil; // if array initialization was correct, this will be a no-op
  15. // otherwise, it will trigger a memory leak
  16. arr[i] := 'tested'+IntToStr(i);
  17. end;
  18. end;
  19. procedure test_entire_openarray(var arr: array of string);
  20. begin
  21. test3(arr);
  22. end;
  23. procedure test_openarray_subrange(var arr: array of string);
  24. begin
  25. test3(arr[1..2]);
  26. end;
  27. procedure test_openarray_slice(var arr: array of string);
  28. begin
  29. test3(slice(arr,2));
  30. end;
  31. var
  32. sarr: array[0..3] of string;
  33. darr: array of string;
  34. procedure prepare;
  35. var
  36. i: Integer;
  37. begin
  38. for i := 0 to 3 do
  39. begin
  40. sarr[i] := 'static'+IntToStr(i);
  41. darr[i] := 'dynamic'+IntToStr(i);
  42. end;
  43. end;
  44. begin
  45. HaltOnNotReleased := True;
  46. SetLength(darr,4);
  47. prepare;
  48. test_entire_openarray(sarr);
  49. if sarr[0] <> 'tested0' then Halt(1);
  50. if sarr[1] <> 'tested1' then Halt(2);
  51. if sarr[2] <> 'tested2' then Halt(3);
  52. if sarr[3] <> 'tested3' then Halt(4);
  53. prepare;
  54. test_openarray_subrange(sarr); // must leave elements 0 and 3 intact
  55. if sarr[0] <> 'static0' then Halt(11);
  56. if sarr[1] <> 'tested0' then Halt(12);
  57. if sarr[2] <> 'tested1' then Halt(13);
  58. if sarr[3] <> 'static3' then Halt(14);
  59. prepare;
  60. test_openarray_slice(sarr); // must leave elements 2 and 3 intact
  61. if sarr[0] <> 'tested0' then Halt(21);
  62. if sarr[1] <> 'tested1' then Halt(22);
  63. if sarr[2] <> 'static2' then Halt(23);
  64. if sarr[3] <> 'static3' then Halt(24);
  65. prepare;
  66. test3(sarr); // entire static array
  67. if sarr[0] <> 'tested0' then Halt(31);
  68. if sarr[1] <> 'tested1' then Halt(32);
  69. if sarr[2] <> 'tested2' then Halt(33);
  70. if sarr[3] <> 'tested3' then Halt(34);
  71. prepare;
  72. test3(sarr[1..2]); // static array subrange
  73. if sarr[0] <> 'static0' then Halt(41);
  74. if sarr[1] <> 'tested0' then Halt(42);
  75. if sarr[2] <> 'tested1' then Halt(43);
  76. if sarr[3] <> 'static3' then Halt(44);
  77. prepare;
  78. test3(slice(sarr,2)); // static array slice
  79. if sarr[0] <> 'tested0' then Halt(51);
  80. if sarr[1] <> 'tested1' then Halt(52);
  81. if sarr[2] <> 'static2' then Halt(53);
  82. if sarr[3] <> 'static3' then Halt(54);
  83. prepare;
  84. test3(darr); // entire dynamic array
  85. if darr[0] <> 'tested0' then Halt(61);
  86. if darr[1] <> 'tested1' then Halt(62);
  87. if darr[2] <> 'tested2' then Halt(63);
  88. if darr[3] <> 'tested3' then Halt(64);
  89. prepare;
  90. test3(darr[1..2]); // dynamic array subrange
  91. if darr[0] <> 'dynamic0' then Halt(71);
  92. if darr[1] <> 'tested0' then Halt(72);
  93. if darr[2] <> 'tested1' then Halt(73);
  94. if darr[3] <> 'dynamic3' then Halt(74);
  95. prepare;
  96. test3(slice(darr,2)); // dynamic array slice
  97. if darr[0] <> 'tested0' then Halt(81);
  98. if darr[1] <> 'tested1' then Halt(82);
  99. if darr[2] <> 'dynamic2' then Halt(83);
  100. if darr[3] <> 'dynamic3' then Halt(84);
  101. end.