tarray18.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. program tarray18;
  2. {$mode objfpc}
  3. {$modeswitch advancedrecords}
  4. function CheckArray(aArr, aExpected: array of LongInt): Boolean;
  5. var
  6. i: LongInt;
  7. begin
  8. if Length(aArr) <> Length(aExpected) then
  9. Exit(False);
  10. for i := Low(aArr) to High(aArr) do
  11. if aArr[i] <> aExpected[i] then
  12. Exit(False);
  13. Result := True;
  14. end;
  15. type
  16. TTest1 = record
  17. f: array of LongInt;
  18. class operator := (a: array of LongInt): TTest1;
  19. end;
  20. TTest2 = record
  21. f: array of LongInt;
  22. class operator Explicit(a: array of LongInt): TTest2;
  23. end;
  24. TTest3 = record
  25. f: array of LongInt;
  26. end;
  27. TTest4 = record
  28. f: array of LongInt;
  29. end;
  30. function AssignArray(a: array of LongInt): specialize TArray<LongInt>;
  31. var
  32. i: LongInt;
  33. begin
  34. SetLength(Result, Length(a));
  35. for i := 0 to High(a) do
  36. Result[i] := a[i];
  37. end;
  38. class operator TTest1.:=(a: array of LongInt): TTest1;
  39. begin
  40. Result.f := AssignArray(a);
  41. end;
  42. class operator TTest2.Explicit(a: array of LongInt): TTest2;
  43. begin
  44. Result.f := AssignArray(a);
  45. end;
  46. operator :=(a: array of LongInt): TTest3;
  47. begin
  48. Result.f := AssignArray(a);
  49. end;
  50. operator :=(a: array of LongInt): TTest4;
  51. begin
  52. Result.f := AssignArray(a);
  53. end;
  54. procedure Test1(aRec: TTest1; a: array of LongInt; aCode: LongInt);
  55. begin
  56. if not CheckArray(aRec.f, a) then
  57. Halt(aCode);
  58. end;
  59. procedure Test2(aRec: TTest2; a: array of LongInt; aCode: LongInt);
  60. begin
  61. if not CheckArray(aRec.f, a) then
  62. Halt(aCode);
  63. end;
  64. procedure Test3(aRec: TTest3; a: array of LongInt; aCode: LongInt);
  65. begin
  66. if not CheckArray(aRec.f, a) then
  67. Halt(aCode);
  68. end;
  69. procedure Test4(aRec: TTest4; a: array of LongInt; aCode: LongInt);
  70. begin
  71. if not CheckArray(aRec.f, a) then
  72. Halt(aCode);
  73. end;
  74. var
  75. t1: TTest1;
  76. t2: TTest2;
  77. t3: TTest3;
  78. t4: TTest4;
  79. begin
  80. t1 := [];
  81. if not CheckArray(t1.f, []) then
  82. Halt(1);
  83. t1 := [2, 4];
  84. if not CheckArray(t1.f, [2, 4]) then
  85. Halt(2);
  86. t1 := TTest1([]);
  87. if not CheckArray(t1.f, []) then
  88. Halt(3);
  89. t1 := TTest1([2, 4]);
  90. if not CheckArray(t1.f, [2, 4]) then
  91. Halt(4);
  92. t2 := TTest2([]);
  93. if not CheckArray(t2.f, []) then
  94. Halt(5);
  95. t2 := TTest2([2, 4]);
  96. if not CheckArray(t2.f, [2, 4]) then
  97. Halt(6);
  98. t3 := [];
  99. if not CheckArray(t3.f, []) then
  100. Halt(7);
  101. t3 := [2, 4];
  102. if not CheckArray(t3.f, [2, 4]) then
  103. Halt(8);
  104. t3 := TTest3([]);
  105. if not CheckArray(t3.f, []) then
  106. Halt(9);
  107. t3 := TTest3([2, 4]);
  108. if not CheckArray(t3.f, [2, 4]) then
  109. Halt(10);
  110. t4 := TTest4([]);
  111. if not CheckArray(t4.f, []) then
  112. Halt(11);
  113. t4 := TTest4([2, 4]);
  114. if not CheckArray(t4.f, [2, 4]) then
  115. Halt(12);
  116. Test1([], [], 13);
  117. Test1([2, 4], [2, 4], 14);
  118. Test2(TTest2([]), [], 15);
  119. Test2(TTest2([2, 4]), [2, 4], 16);
  120. Test3([], [], 17);
  121. Test3([2, 4], [2, 4], 18);
  122. Test4(TTest4([]), [], 19);
  123. Test4(TTest4([2, 4]), [2, 4], 20);
  124. Writeln('ok');
  125. end.