tarray3.pp 5.1 KB

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