testchr2.pp 4.1 KB

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