tarray3.pp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. {$j+}
  2. {$P+}
  3. type
  4. CharA4 = array [1..4] of char;
  5. CharA6 = array [1..6] of char;
  6. String4 = String[4];
  7. String5 = String[5];
  8. String6 = String[6];
  9. String8 = String[8];
  10. const
  11. car4_1 : CharA4 = 'ABCD';
  12. car4_2 : CharA4 = 'abcd';
  13. car6_1 : CharA6 = 'EFGHIJ';
  14. car6_2 : CharA6 = 'efghij';
  15. cst4_1 : String4 = 'ABCD';
  16. cst6_2 : string6 = 'EFGHIJ';
  17. cst8_1 : string8 = 'abcd';
  18. cst8_2 : string8 = 'efghij';
  19. var
  20. ar4_1, ar4_2 : CharA4;
  21. ar6_1, ar6_2 : CharA6;
  22. st4_1, st4_2 : string4;
  23. st5_1, st5_2 : string5;
  24. st6_1, st6_2 : string6;
  25. st8_1, st8_2 : string8;
  26. pc : pchar;
  27. const
  28. has_errors : boolean = false;
  29. procedure error(const st : string);
  30. begin
  31. Writeln('Error: ',st);
  32. has_errors:=true;
  33. end;
  34. procedure testvalueconv(st : string4);
  35. begin
  36. Writeln('st=',st);
  37. Writeln('Length(st)=',Length(st));
  38. If Length(st)>4 then
  39. Error('string length too big in calling value arg');
  40. end;
  41. procedure testconstconv(const st : string4);
  42. begin
  43. Writeln('st=',st);
  44. Writeln('Length(st)=',Length(st));
  45. If Length(st)>4 then
  46. Error('string length too big in calling const arg');
  47. end;
  48. procedure testvarconv(var st : string4);
  49. begin
  50. Writeln('st=',st);
  51. Writeln('Length(st)=',Length(st));
  52. If Length(st)>4 then
  53. Error('string length too big in calling var arg');
  54. end;
  55. { is global switch, can't turn off here }
  56. {P-}
  57. procedure testvarconv2(var st : string4);
  58. begin
  59. Writeln('st=',st);
  60. Writeln('Length(st)=',Length(st));
  61. If Length(st)>4 then
  62. Error('string length too big in calling var arg without openstring');
  63. end;
  64. begin
  65. { compare array of char to constant strings }
  66. Writeln('Testing if "',car4_1,'" is equal to "',cst4_1,'"');
  67. if car4_1<>cst4_1 then
  68. error('Comparison of array of char and string don''t work');
  69. Writeln('Testing if "',car4_1,'" is equal to "ABCD"');
  70. if car4_1<>'ABCD' then
  71. error('Comparison of array of char and constat string don''t work');
  72. Writeln('Testing if "',cst4_1,'" is equal to "ABCD"');
  73. if 'ABCD'<>cst4_1 then
  74. error('Comparison of string and constant string don''t work');
  75. car4_1:='AB'#0'D';
  76. if car4_1='AB' then
  77. Writeln('Anything beyond a #0 is ignored')
  78. else if car4_1='AB'#0'D' then
  79. Writeln('Chars after #0 are not ignored')
  80. else
  81. Error('problems if #0 in array of char');
  82. {$ifdef FPC this is not allowed in BP !}
  83. car4_1:=cst4_1;
  84. { if it is allowed then it must also work correctly !! }
  85. Writeln('Testing if "',car4_1,'" is equal to "',cst4_1,'"');
  86. if car4_1<>cst4_1 then
  87. error('Comparison of array of char and string don''t work');
  88. {$ifdef test_known_problems}
  89. if string4(car6_2)<>'efgh' then
  90. error('typcasting to shorter strings leads to problems');
  91. {$endif}
  92. ar4_2:='Test';
  93. ar4_1:=cst6_2;
  94. if ar4_2<>'Test' then
  95. error('overwriting beyond char array size');
  96. ar6_1:='Test'#0'T';
  97. st6_1:=ar6_1;
  98. if (st6_1<>ar6_1) or (st6_1='Test') then
  99. error('problems with #0');
  100. ar6_1:='AB';
  101. if ar6_1='AB'#0't'#0'T' then
  102. Error('assigning strings to array of char does not zero end of array if string is shorter');
  103. if ar6_1='AB'#0#0#0#0 then
  104. writeln('assigning shorter strings to array of char does zero rest of array')
  105. else
  106. error('assigning "AB" to ar6_1 gives '+ar6_1);
  107. {$endif}
  108. cst8_1:=car4_1;
  109. { if it is allowed then it must also work correctly !! }
  110. Writeln('Testing if "',car4_1,'" is equal to "',cst8_1,'"');
  111. if car4_1<>cst8_1 then
  112. error('Comparison of array of char and string don''t work');
  113. st4_2:='Test';
  114. st4_1:=car6_1;
  115. if (st4_2<>'Test') or (st4_1<>'EFGH') then
  116. error('problems when copying long char array to shorter string');
  117. testvalueconv('AB');
  118. testvalueconv('ABCDEFG');
  119. testvalueconv(car4_1);
  120. testvalueconv(car6_1);
  121. getmem(pc,256);
  122. pc:='Long Test';
  123. {$ifdef FPC this is not allowed in BP !}
  124. testvalueconv(pc);
  125. {$endif def FPC this is not allowed in BP !}
  126. testconstconv('AB');
  127. {$ifdef test_known_problems}
  128. testconstconv('ABCDEFG');
  129. {$endif}
  130. testconstconv(st4_1);
  131. {$ifdef test_known_problems}
  132. testconstconv(cst6_2);
  133. {$endif}
  134. {$ifdef FPC this is not allowed in BP !}
  135. {$ifdef test_known_problems}
  136. testconstconv(pc);
  137. {$endif}
  138. {$endif def FPC this is not allowed in BP !}
  139. testvarconv(st4_2);
  140. testvarconv(cst4_1);
  141. {$ifdef FPC this is not allowed in BP !}
  142. {$ifdef test_known_problems}
  143. testvarconv(st6_1);
  144. testvarconv(cst8_1);
  145. {$endif}
  146. {$endif def FPC this is not allowed in BP !}
  147. { testvarconv(pc); this one fails at compilation }
  148. testvarconv2(st4_2);
  149. testvarconv2(cst4_1);
  150. {$ifdef FPC this is not allowed in BP !}
  151. {$ifdef test_known_problems}
  152. testvarconv2(st6_1);
  153. testvarconv2(cst8_1);
  154. {$endif}
  155. {$endif def FPC this is not allowed in BP !}
  156. if has_errors then
  157. begin
  158. Writeln('There are still problems with arrays of char');
  159. Halt(1);
  160. end;
  161. end.