tarray7.pp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. program array7;
  2. {This test checks for open array functionality.}
  3. function average(const row:array of integer):real;
  4. var i:longint;
  5. temp:real;
  6. begin
  7. temp:=Row[0];
  8. for i:=1 to high(row) do
  9. temp:=temp+row[i];
  10. average:=temp/(high(row)+1);
  11. end;
  12. procedure uppercase(var u:array of char);
  13. var i:longint;
  14. begin
  15. for i:=low(u) to high(u) do
  16. u[i]:=upcase(u[i]);
  17. end;
  18. var a:array[-1000..1000] of integer;
  19. b:Pinteger;
  20. c:array of integer;
  21. d:array[1..10] of char;
  22. e:Pchar;
  23. f:string;
  24. g:ansistring;
  25. i:longint;
  26. s:string[31];
  27. begin
  28. {Integer stuff.}
  29. {First try it with a static array.}
  30. for i:=low(a) to high(a) do
  31. a[i]:=i xor 99;
  32. str(average(a):4:3,s);
  33. if s<>'-0.046' then
  34. halt(1);
  35. str(average(a[-1000..0]):4:3,s);
  36. if s<>'-502.203' then
  37. halt(2);
  38. str(average(a[0..1000]):4:3,s);
  39. if s<>'502.209' then
  40. halt(3);
  41. {Now try it with a heap block.}
  42. getmem(b,2001*sizeof(integer));
  43. for i:=-1000 to 1000 do
  44. b[i+1000]:=i xor 99;
  45. str(average(b[0..2000]):4:3,s);
  46. if s<>'-0.046' then
  47. halt(4);
  48. dispose(b);
  49. {And now try it with a dynamic array.}
  50. setlength(c,2001);
  51. for i:=-1000 to 1000 do
  52. c[i+1000]:=i xor 99;
  53. str(average(c):4:3,s);
  54. if s<>'-0.046' then
  55. halt(5);
  56. str(average(c[0..1000]):4:3,s);
  57. if s<>'-502.203' then
  58. halt(6);
  59. str(average(c[1000..2000]):4:3,s);
  60. if s<>'502.209' then
  61. halt(7);
  62. setlength(c,0);
  63. {Character stuff.}
  64. {First with a static array.}
  65. d:='abcdefghij';
  66. uppercase(d);
  67. if d<>'ABCDEFGHIJ' then
  68. halt(8);
  69. {Now with a heap block.}
  70. getmem(e,10);
  71. move(d,e^,10);
  72. uppercase(e[0..9]);
  73. move(e^,d,10);
  74. if d<>'ABCDEFGHIJ' then
  75. halt(9);
  76. dispose(e);
  77. {Then a shortstring.}
  78. f:='abcdefghij';
  79. uppercase(f[1..10]);
  80. if f<>'ABCDEFGHIJ' then
  81. halt(10);
  82. {And finish with an ansistring.}
  83. g:='abcdefghij';
  84. uppercase(g[1..10]);
  85. if g<>'ABCDEFGHIJ' then
  86. halt(11);
  87. end.