summaryrefslogtreecommitdiff
path: root/tests/test/tarray13.pp
blob: db1fab078005b6b6a7c81796f84cbbf6c02d6249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
{ test for the various DynArray* functions }

program tarray13;

{$mode objfpc}

function TestDynArraySize: LongInt;
type
  TTest = record
    value1: QWord;
    value2: LongInt;
  end;

var
  arr1: array of LongInt;
  arr2: array of QWord;
  arr3: array of TTest;
begin
  SetLength(arr1, 5);
  SetLength(arr2, 8);
  SetLength(arr3, 6);

  if DynArraySize(Pointer(arr1)) <> 5 then
    Halt(1);
  if DynArraySize(@arr1[0]) <> 5 then
    Halt(2);
  if DynArraySize(Pointer(arr2)) <> 8 then
    Halt(3);
  if DynArraySize(@arr2[0]) <> 8 then
    Halt(4);
  if DynArraySize(Pointer(arr3)) <> 6 then
    Halt(5);
  if DynArraySize(@arr3[0]) <> 6 then
    Halt(6);

  Result := 7;
end;

function TestDynArrayIndexSingle(aBaseCode: LongInt): LongInt;
type
  TTest1 = record
    value1: QWord;
    value2: LongInt;
  end;

  TTest2 = packed record
    value1: LongInt;
    value2: QWord;
  end;

var
  arr1: array of Byte;
  arr2: array of LongInt;
  arr3: array of QWord;
  arr4: array of TTest1;
  arr5: array of TTest2;
  i, basecode: LongInt;
begin
  SetLength(arr1, 5);
  SetLength(arr2, 3);
  SetLength(arr3, 8);
  SetLength(arr4, 6);
  SetLength(arr5, 7);

  basecode := aBaseCode;
  for i := Low(arr1) to High(arr1) do begin
    if @arr1[i] <> DynArrayIndex(Pointer(arr1), [i], TypeInfo(arr1)) then
      Halt(basecode + i * 2);
    if @arr1[i] <> DynArrayIndex(@arr1[0], [i], TypeInfo(arr1)) then
      Halt(basecode + i * 2 + 1);
  end;

  basecode := basecode + 2 * Length(arr1);
  for i := Low(arr2) to High(arr2) do begin
    if @arr2[i] <> DynArrayIndex(Pointer(arr2), [i], TypeInfo(arr2)) then
      Halt(basecode + i * 2);
    if @arr2[i] <> DynArrayIndex(@arr2[0], [i], TypeInfo(arr2)) then
      Halt(basecode + i * 2 + 1);
  end;

  basecode := basecode + 2 * Length(arr2);
  for i := Low(arr3) to High(arr3) do begin
    if @arr3[i] <> DynArrayIndex(Pointer(arr3), [i], TypeInfo(arr3)) then
      Halt(basecode + i * 2);
    if @arr3[i] <> DynArrayIndex(@arr3[0], [i], TypeInfo(arr3)) then
      Halt(basecode + i * 2 + 1);
  end;

  basecode := basecode + 2 * Length(arr3);
  for i := Low(arr4) to High(arr4) do begin
    if @arr4[i] <> DynArrayIndex(Pointer(arr4), [i], TypeInfo(arr4)) then
      Halt(basecode + i * 2);
    if @arr4[i] <> DynArrayIndex(@arr4[0], [i], TypeInfo(arr4)) then
      Halt(basecode + i * 2 + 1);
  end;

  basecode := basecode + 2 * Length(arr4);
  for i := Low(arr5) to High(arr5) do begin
    if @arr5[i] <> DynArrayIndex(Pointer(arr5), [i], TypeInfo(arr5)) then
      Halt(basecode + i * 2);
    if @arr5[i] <> DynArrayIndex(@arr5[0], [i], TypeInfo(arr5)) then
      Halt(basecode + i * 2 + 1);
  end;

  Result := basecode + 2 * Length(arr5);
end;

function TestDynArrayIndexMulti(aBaseCode: LongInt): LongInt;
var
  arr1: array of array of LongInt;
  arr2: array of array of array of QWord;
  i, j, k, basecode: LongInt;
begin
  SetLength(arr1, 4, 8);
  SetLength(arr2, 3, 5, 9);

  basecode := aBaseCode;
  for i := Low(arr1) to High(arr1) do begin
    for j := Low(arr1[i]) to High(arr1[i]) do begin
      if @arr1[i, j] <> DynArrayIndex(Pointer(arr1), [i, j], TypeInfo(arr1)) then
        Halt(basecode + j * 2);
      if @arr1[i, j] <> DynArrayIndex(@arr1[0], [i, j], TypeInfo(arr1)) then
        Halt(basecode + j * 2 + 1);
      { Note: @arr1[0, 0] would be different from @arr1[0] or arr1! }
    end;
    basecode := basecode + Length(arr1[i]) * 3;
  end;

  for i := Low(arr2) to High(arr2) do begin
    for j := Low(arr2[i]) to High(arr2[i]) do begin
      for k := Low(arr2[i, j]) to High(arr2[i, j]) do begin
        if @arr2[i, j, k] <> DynArrayIndex(Pointer(arr2), [i, j, k], TypeInfo(arr2)) then
          Halt(basecode + k * 2);
        if @arr2[i, j, k] <> DynArrayIndex(@arr2[0], [i, j, k], TypeInfo(arr2)) then
          Halt(basecode + k * 2 + 1);
        { Note: @arr2[0, 0] and @arr2[0, 0, 0] would be different from @arr2[0] or arr2! }
      end;
      basecode := basecode + Length(arr2[i, j]) * 2;
    end;
  end;

  Result := basecode;
end;

var
  basecode: LongInt;
begin
  Writeln('TestDynArraySize errors starting at 1');
  basecode := TestDynArraySize;
  Writeln('TestDynArrayIndexSingle errors starting at ', basecode);
  basecode := TestDynArrayIndexSingle(basecode);
  Writeln('TestDynArrayIndexMulti errors starting at ', basecode);
  basecode := TestDynArrayIndexMulti(basecode);
  Writeln('ok');
end.