tarray4.pp 4.1 KB

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