tarray3.pp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. { %KNOWN }
  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. {$P-}
  56. procedure testvarconv2(var st : string4);
  57. begin
  58. Writeln('st=',st);
  59. Writeln('Length(st)=',Length(st));
  60. If Length(st)>4 then
  61. Error('string length too big in calling var arg without openstring');
  62. end;
  63. begin
  64. { compare array of char to constant strings }
  65. Writeln('Testing if "',car4_1,'" is equal to "',cst4_1,'"');
  66. if car4_1<>cst4_1 then
  67. error('Comparison of array of char and string don''t work');
  68. Writeln('Testing if "',car4_1,'" is equal to "ABCD"');
  69. if car4_1<>'ABCD' then
  70. error('Comparison of array of char and constat string don''t work');
  71. Writeln('Testing if "',cst4_1,'" is equal to "ABCD"');
  72. if 'ABCD'<>cst4_1 then
  73. error('Comparison of string and constant string don''t work');
  74. car4_1:='AB'#0'D';
  75. if car4_1='AB' then
  76. Writeln('Anything beyond a #0 is ignored')
  77. else if car4_1='AB'#0'D' then
  78. Writeln('Chars after #0 are not ignored')
  79. else
  80. Error('problems if #0 in array of char');
  81. {$ifdef FPC this is not allowed in BP !}
  82. car4_1:=cst4_1;
  83. { if it is allowed then it must also work correctly !! }
  84. Writeln('Testing if "',car4_1,'" is equal to "',cst4_1,'"');
  85. if car4_1<>cst4_1 then
  86. error('Comparison of array of char and string don''t work');
  87. if string4(car6_2)<>'efgh' then
  88. error('typcasting to shorter strings leads to problems');
  89. ar4_2:='Test';
  90. ar4_1:=cst6_2;
  91. if ar4_2<>'Test' then
  92. error('overwriting beyond char array size');
  93. ar6_1:='Test'#0'T';
  94. st6_1:=ar6_1;
  95. if (st6_1<>ar6_1) or (st6_1='Test') then
  96. error('problems with #0');
  97. ar6_1:='AB';
  98. if ar6_1='AB'#0't'#0'T' then
  99. Error('assigning strings to array of char does not zero end of array if string is shorter');
  100. if ar6_1='AB'#0#0#0#0 then
  101. writeln('assigning shorter strings to array of char does zero fo tserarray')
  102. else
  103. error('assigning "AB" to ar6_1 gives '+ar6_1);
  104. {$endif}
  105. cst8_1:=car4_1;
  106. { if it is allowed then it must also work correctly !! }
  107. Writeln('Testing if "',car4_1,'" is equal to "',cst8_1,'"');
  108. if car4_1<>cst8_1 then
  109. error('Comparison of array of char and string don''t work');
  110. st4_2:='Test';
  111. st4_1:=car6_1;
  112. if (st4_2<>'Test') or (st4_1<>'EFGH') then
  113. error('problems when copying long char array to shorter string');
  114. testvalueconv('AB');
  115. testvalueconv('ABCDEFG');
  116. testvalueconv(car4_1);
  117. testvalueconv(car6_1);
  118. getmem(pc,256);
  119. pc:='Long Test';
  120. {$ifdef FPC this is not allowed in BP !}
  121. testvalueconv(pc);
  122. {$endif def FPC this is not allowed in BP !}
  123. testconstconv('AB');
  124. testconstconv('ABCDEFG');
  125. testconstconv(st4_1);
  126. testconstconv(cst6_2);
  127. {$ifdef FPC this is not allowed in BP !}
  128. testconstconv(pc);
  129. {$endif def FPC this is not allowed in BP !}
  130. testvarconv(st4_2);
  131. testvarconv(cst4_1);
  132. {$ifdef FPC this is not allowed in BP !}
  133. testvarconv(st6_1);
  134. testvarconv(cst8_1);
  135. {$endif def FPC this is not allowed in BP !}
  136. { testvarconv(pc); this one fails at compilation }
  137. testvarconv2(st4_2);
  138. testvarconv2(cst4_1);
  139. {$ifdef FPC this is not allowed in BP !}
  140. testvarconv2(st6_1);
  141. testvarconv2(cst8_1);
  142. {$endif def FPC this is not allowed in BP !}
  143. if has_errors then
  144. begin
  145. Writeln('There are still problems with arrays of char');
  146. Halt(1);
  147. end;
  148. end.