testchar.pp 4.5 KB

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