1var
2  V1, V2: TFastVector;
3  RV1, RV2: TGenericVector;
4
5  procedure Check(Vector: TGenericVector; const Values: array of const);
6
7    procedure DoCheck;
8    var
9      I, J, MinI, MaxI: Integer;
10      Sum, SqrSum: Float64;
11      Min, Max, T: NumberType;
12    begin
13      if Vector.Count <> High(Values) + 1 then Error(EWrongResult);
14      Sum:=0;
15      SqrSum:=0;
16      Min:=Values[0].VInteger;
17      Max:=Min;
18      MinI:=0;
19      MaxI:=0;
20      for I:=0 to High(Values) do begin
21        if Vector[I] <> Values[I].VInteger then Error(EWrongResult);
22        Sum:=Sum + Vector[I];
23        SqrSum:=SqrSum + Sqr(Vector[I]);
24        if Vector[I] > Max then begin
25          Max:=Vector[I];
26          MaxI:=I;
27        end;
28        if Vector[I] < Min then begin
29          Min:=Vector[I];
30          MinI:=I;
31        end;
32      end;
33      if Abs(Sum - Vector.Sum) > MinFloat64 then Error(EWrongResult);
34      if Abs(SqrSum - Vector.SqrSum) > MinFloat64 then Error(EWrongResult);
35      if Min <> Vector.Min then Error(EWrongResult);
36      if Max <> Vector.Max then Error(EWrongResult);
37      if (Vector.MinIndex(T) <> MinI) or (T <> Min) then Error(EWrongResult);
38      if (Vector.MaxIndex(T) <> MaxI) or (T <> Max) then Error(EWrongResult);
39      Vector.Reverse;
40      J:=0;
41      for I:=High(Values) downto 0 do begin
42        if Vector[I] <> Values[J].VInteger then Error(EWrongResult);
43        Vector.IncItem(I, 2);
44        if Vector[I] <> Values[J].VInteger + 2 then Error(EWrongResult);
45        Vector.DecItem(I, 2);
46        if Vector[I] <> Values[J].VInteger then Error(EWrongResult);
47        Vector.MulItem(I, 3);
48        if Vector[I] <> Values[J].VInteger * 3 then Error(EWrongResult);
49        Vector.DivItem(I, 3);
50        if Vector[I] <> Values[J].VInteger then Error(EWrongResult);
51        Inc(J);
52      end;
53      Vector.Reverse;
54    end;
55
56  var
57    T: TVMemStream;
58  begin
59    DoCheck;
60    T:=TVMemStream.Create;
61    try
62      Vector.WriteToStream(T);
63      T.Seek(0);
64      Vector.ReadFromStream(T);
65      DoCheck;
66    finally
67      T.Free;
68    end;
69  end;
70
71  procedure SetItems(Vector: TGenericVector);
72  var
73    I: Integer;
74  begin
75    for I:=0 to Vector.Count - 1 do begin
76      Vector[I]:=I;
77      if Vector[I] <> I then Error(EWrongResult);
78    end;
79  end;
80
81  procedure Grow(Vector: TGenericVector; N: Integer);
82  var
83    I: Integer;
84  begin
85    for I:=1 to N do Vector.Add(I);
86  end;
87
88  procedure Diminish(Vector: TGenericVector; N: Integer);
89  var
90    I: Integer;
91  begin
92    for I:=1 to N do Vector.Delete(Vector.Count - 1);
93{    for I:=1 to N do Vector.Pop;}
94  end;
95
96  procedure Test(V: TGenericVector);
97  begin
98    V.Clear;
99    V.Count:=4;
100    V[1]:=0;
101    V[2]:=3;
102    V[3]:=4;
103    V.Count:=12;
104    V[5]:=6;
105    V[9]:=2;
106    V[10]:=8;
107    if V.IndexFrom(3, 6) <> 5 then Error(EWrongResult);
108    if V.IndexFrom(3, 2) <> 9 then Error(EWrongResult);
109    if V.IndexFrom(3, 1) <> 4 then Error(EWrongResult);
110    if V.IndexFrom(10, 1) <> 11 then Error(EWrongResult);
111    if V.IndexFrom(100, 1) <> -1 then Error(EWrongResult);
112    if V.IndexFrom(-1, 1) <> 0 then Error(EWrongResult);
113    if V.IndexFrom(-1, 101) <> -1 then Error(EWrongResult);
114    if V.LastIndexFrom(10, 6) <> 5 then Error(EWrongResult);
115    if V.LastIndexFrom(3, 3) <> 2 then Error(EWrongResult);
116    if V.LastIndexFrom(2, 1) <> 0 then Error(EWrongResult);
117    if V.LastIndexFrom(100, 1) <> 11 then Error(EWrongResult);
118    if V.LastIndexFrom(-1, 1) <> -1 then Error(EWrongResult);
119    if V.LastIndexFrom(100, 101) <> -1 then Error(EWrongResult);
120  end;
121
122begin
123  V1:=TFastVector.Create(5, 0);
124  RV1:=TSparseVector.Create(5, 0);
125  V2:=TFastVector.Create(5, 1);
126  RV2:=TSparseVector.Create(5, 1);
127  try
128    Check(V1, [0, 0, 0, 0, 0]);
129
130    V1[0]:=5;
131    V1[3]:=8;
132    V1[4]:=7;
133    Check(V1, [5, 0, 0, 8, 7]);
134    V1.SetToDefault;
135
136    if V1.NumberOfValues(0) <> V1.Count then Error(EWrongResult);
137    if V1.NumberOfValues(1) <> 0 then Error(EWrongResult);
138    if V2.NumberOfValues(0) <> 0 then Error(EWrongResult);
139    if V2.NumberOfValues(1) <> V2.Count then Error(EWrongResult);
140
141    if RV1.NumberOfValues(0) <> RV1.Count then Error(EWrongResult);
142    RV1.Pack;
143    if RV1.NumberOfValues(1) <> 0 then Error(EWrongResult);
144    if RV2.NumberOfValues(0) <> 0 then Error(EWrongResult);
145    if RV2.NumberOfValues(1) <> RV2.Count then Error(EWrongResult);
146
147    if V1.IndexOf(0) <> 0 then Error(EWrongResult);
148    if V1.LastIndexOf(0) <> 4 then Error(EWrongResult);
149    if V1.IndexOf(1) <> -1 then Error(EWrongResult);
150    if V1.LastIndexOf(1) <> -1 then Error(EWrongResult);
151    Check(RV1, [0, 0, 0, 0, 0]);
152    if RV1.IndexOf(0) <> 0 then Error(EWrongResult);
153    if RV1.LastIndexOf(0) <> 4 then Error(EWrongResult);
154    if RV1.IndexOf(1) <> -1 then Error(EWrongResult);
155    if RV1.LastIndexOf(1) <> -1 then Error(EWrongResult);
156    Check(V2, [1, 1, 1, 1, 1]);
157    if V2.IndexOf(0) <> -1 then Error(EWrongResult);
158    if V2.LastIndexOf(0) <> -1 then Error(EWrongResult);
159    if V2.IndexOf(1) <> 0 then Error(EWrongResult);
160    if V2.LastIndexOf(1) <> 4 then Error(EWrongResult);
161    Check(RV2, [1, 1, 1, 1, 1]);
162    RV2.Pack;
163    if RV2.IndexOf(0) <> -1 then Error(EWrongResult);
164    if RV2.LastIndexOf(0) <> -1 then Error(EWrongResult);
165    if RV2.IndexOf(1) <> 0 then Error(EWrongResult);
166    if RV2.LastIndexOf(1) <> 4 then Error(EWrongResult);
167
168    SetItems(V1);
169    Check(V1, [0, 1, 2, 3, 4]);
170    V1.AddScalar(1);
171    V1.Divide(V1);
172    Check(V1, [1, 1, 1, 1, 1]);
173    SetItems(V1);
174    V1.Divide(V2);
175    Check(V1, [0, 1, 2, 3, 4]);
176    V1.Divide(RV2);
177    Check(V1, [0, 1, 2, 3, 4]);
178    if V1.IndexFrom(1, 1) <> 1 then Error(EWrongResult);
179    if V1.IndexFrom(2, 1) <> -1 then Error(EWrongResult);
180    if V1.LastIndexFrom(4, 4) <> 4 then Error(EWrongResult);
181    if V1.LastIndexFrom(3, 4) <> -1 then Error(EWrongResult);
182    SetItems(RV1);
183    Check(RV1, [0, 1, 2, 3, 4]);
184    RV1.Divide(V2);
185    Check(RV1, [0, 1, 2, 3, 4]);
186    RV1.Divide(RV2);
187    Check(RV1, [0, 1, 2, 3, 4]);
188    if RV1.IndexFrom(1, 1) <> 1 then Error(EWrongResult);
189    if RV1.IndexFrom(2, 1) <> -1 then Error(EWrongResult);
190    if RV1.LastIndexFrom(4, 4) <> 4 then Error(EWrongResult);
191    if RV1.LastIndexFrom(3, 4) <> -1 then Error(EWrongResult);
192    V1.Mul(V1);
193    Check(V1, [0, 1, 4, 9, 16]);
194    SetItems(V1);
195    V1.Mul(RV1);
196    Check(V1, [0, 1, 4, 9, 16]);
197    SetItems(V1);
198    SetItems(RV1);
199    SetItems(V2);
200    Check(V2, [0, 1, 2, 3, 4]);
201    SetItems(RV2);
202    RV2.Pack;
203    Check(RV2, [0, 1, 2, 3, 4]);
204
205    V1.SortDesc;
206    Check(V1, [4, 3, 2, 1, 0]);
207    V1.Sort;
208    Check(V1, [0, 1, 2, 3, 4]);
209    if V1.FindInSorted(2) <> 2 then Error(EWrongResult);
210    if V1.FindInSorted(5) <> -1 then Error(EWrongResult);
211    RV1.SortDesc;
212    Check(RV1, [4, 3, 2, 1, 0]);
213    RV1.Sort;
214    Check(RV1, [0, 1, 2, 3, 4]);
215
216    V1.SetToDefault;
217    Check(V1, [0, 0, 0, 0, 0]);
218    RV1.SetToDefault;
219    Check(RV1, [0, 0, 0, 0, 0]);
220    V2.SetToDefault;
221    V2.Pack;
222    Check(V2, [1, 1, 1, 1, 1]);
223    RV2.SetToDefault;
224    Check(RV2, [1, 1, 1, 1, 1]);
225
226    V1.Count:=10;
227    Check(V1, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
228    RV1.Count:=10;
229    Check(RV1, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
230    V2.Count:=10;
231    Check(V2, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
232    RV2.Count:=10;
233    Check(RV2, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
234
235    V1.Count:=5;
236    Check(V1, [0, 0, 0, 0, 0]);
237    V1.Clear;
238    V1.Count:=5;
239    Check(V1, [0, 0, 0, 0, 0]);
240    RV1.Count:=5;
241    Check(RV1, [0, 0, 0, 0, 0]);
242    RV1.Clear;
243    RV1.Count:=5;
244    Check(RV1, [0, 0, 0, 0, 0]);
245    V2.Count:=5;
246    Check(V2, [1, 1, 1, 1, 1]);
247    V2.Clear;
248    V2.Count:=5;
249    Check(V2, [1, 1, 1, 1, 1]);
250    RV2.Count:=5;
251    Check(RV2, [1, 1, 1, 1, 1]);
252    RV2.Clear;
253    RV2.Count:=5;
254    Check(RV2, [1, 1, 1, 1, 1]);
255
256    SetItems(V2);
257    if V1.EqualTo(V2) then Error(EWrongResult);
258    V1.Assign(V2);
259    if not V1.EqualTo(V2) then Error(EWrongResult);
260    if not V2.EqualTo(V1) then Error(EWrongResult);
261    Check(V1, [0, 1, 2, 3, 4]);
262    V1.Count:=6;
263    Check(V1, [0, 1, 2, 3, 4, 1]); { DefaultValue changes on Assign }
264    V1.Sort;
265    Check(V1, [0, 1, 1, 2, 3, 4]);
266    if V1.NumberOfValues(1) <> 2 then Error(EWrongResult);
267    if V1.NumberOfValues(2) <> 1 then Error(EWrongResult);
268    SetItems(RV2);
269    RV2.SortDesc;
270    if V1.EqualTo(RV2) then Error(EWrongResult);
271    V1.Assign(RV2);
272    if not V1.EqualTo(RV2) then Error(EWrongResult);
273    if not RV2.EqualTo(V1) then Error(EWrongResult);
274    RV2.Sort;
275    V1.Assign(RV2);
276    V1.Count:=6;
277    Check(V1, [0, 1, 2, 3, 4, 1]);
278    SetItems(RV1);
279    if V1.EqualTo(RV1) then Error(EWrongResult);
280    V1.Assign(RV1);
281    if not V1.EqualTo(RV1) then Error(EWrongResult);
282    if not RV1.EqualTo(V1) then Error(EWrongResult);
283    V1.Count:=6;
284    Check(V1, [0, 1, 2, 3, 4, 0]);
285
286    RV1.Assign(V1);
287    if not RV1.EqualTo(V1) then Error(EWrongResult);
288    if not V1.EqualTo(RV1) then Error(EWrongResult);
289    Check(RV1, [0, 1, 2, 3, 4, 0]);
290
291    V1.Count:=15;
292
293    V1.AddScalar(1);
294    Check(V1, [1, 2, 3, 4, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
295    V1.SubScalar(1);
296    V1[V1.Count - 1]:=2;
297    Check(V1, [0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]);
298    V1.MulScalar(10);
299    Check(V1, [0, 10, 20, 30, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20]);
300    V1.DivScalar(10);
301    Check(V1, [0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]);
302
303    V1[V1.Count - 1]:=1;
304    V1.AddVector(V1);
305    Check(V1, [0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]);
306
307    RV1.Count:=15;
308
309    RV1.AddScalar(1);
310    Check(RV1, [1, 2, 3, 4, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
311    RV1.SubScalar(1);
312    Check(RV1, [0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
313    if RV1.NumberOfValues(0) <> 11 then Error(EWrongResult);
314    RV1.MulScalar(10);
315    Check(RV1, [0, 10, 20, 30, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
316    RV1.DivScalar(10);
317    Check(RV1, [0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
318
319    RV1.AddVector(RV1);
320    Check(RV1, [0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
321
322    if V1.DotProduct(RV1) <> 120 then Error(EWrongResult);
323
324    if RV1.DotProduct(V1) <> 120 then Error(EWrongResult);
325
326    if V1.DotProduct(V1) <> 124 then Error(EWrongResult);
327
328    V1.SubVector(V1);
329    Check(V1, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
330
331    RV1.SubVector(RV1);
332    Check(RV1, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
333
334    V1.Count:=34;
335    SetItems(V1);
336    Check(V1, [0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14,
337               15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
338               30, 31, 32, 33]);
339    V2.Count:=34;
340    SetItems(V2);
341    V2.SortDesc;
342    V2.CopyRange(4, V1, 5, 5);
343    V2.CopyRange(10, V1, 23, 29);
344    V2.CopyRange(40, V1, 23, 29);
345    V2.ExchangeRange(0, 35, 3);
346    Check(V2, [1,  1,  1,  30, 5, 28, 27, 26, 25, 24, 23, 24, 25, 26, 27, 28,
347               29, 16, 15, 14, 13, 12, 11, 10, 9,  8,  7,  6,  5,  4,  3,  2,
348               1,  0,  1,  33, 32, 31, 1,  1, 23, 24, 25, 26, 27, 28, 29]);
349    V2.Count:=34;
350    SetItems(V2);
351    V2.SortDesc;
352    Check(V2, [33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19,
353               18, 17, 16, 15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,
354                3,  2,  1,  0]);
355    V1.AddVector(V2);
356    Check(V1, [33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33,
357               33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33,
358               33, 33, 33, 33]);
359
360    V1.Count:=0;
361    if (V1.Sum <> 0) or (V1.SqrSum <> 0) or (V1.DotProduct(V1) <> 0) then Error(EWrongResult);
362    Grow(V1, 8);
363    Check(V1, [1, 2, 3, 4, 5, 6, 7, 8]);
364    V1.Delete(3);
365    Check(V1, [1, 2, 3, 5, 6, 7, 8]);
366    V1.Insert(3, 4);
367    Check(V1, [1, 2, 3, 4, 5, 6, 7, 8]);
368    V1.Delete(7);
369    Check(V1, [1, 2, 3, 4, 5, 6, 7]);
370    V1.DeleteRange(7, 0);
371    Check(V1, [1, 2, 3, 4, 5, 6, 7]);
372    V1.DeleteRange(7, -1);
373    Check(V1, [1, 2, 3, 4, 5, 6, 7]);
374    V1.DeleteRange(3, 4);
375    Check(V1, [1, 2, 3]);
376    V1.Add(4);
377    V1.Add(5);
378    V1.Add(6);
379    V1.Add(7);
380
381    RV2.Count:=0;
382    Grow(RV2, 8);
383    Check(RV2, [1, 2, 3, 4, 5, 6, 7, 8]);
384    RV2.Delete(3);
385    Check(RV2, [1, 2, 3, 5, 6, 7, 8]);
386    RV2.Insert(3, 4);
387    Check(RV2, [1, 2, 3, 4, 5, 6, 7, 8]);
388    RV2.Insert(8, 1);
389    Check(RV2, [1, 2, 3, 4, 5, 6, 7, 8, 1]);
390    if RV2.Add(0) <> 9 then Error(EWrongResult);
391    Check(RV2, [1, 2, 3, 4, 5, 6, 7, 8, 1, 0]);
392    RV2.Delete(9);
393    Check(RV2, [1, 2, 3, 4, 5, 6, 7, 8, 1]);
394    RV2.Delete(7);
395    Check(RV2, [1, 2, 3, 4, 5, 6, 7, 1]);
396    RV2.Delete(7);
397    Check(RV2, [1, 2, 3, 4, 5, 6, 7]);
398    RV2.DeleteRange(7, 0);
399    Check(RV2, [1, 2, 3, 4, 5, 6, 7]);
400    RV2.DeleteRange(7, -1);
401    Check(RV2, [1, 2, 3, 4, 5, 6, 7]);
402    RV2.DeleteRange(3, 4);
403    Check(RV2, [1, 2, 3]);
404    RV2.Add(4);
405    RV2.Add(5);
406    RV2.Add(6);
407    RV2.Add(7);
408
409    V1.MulScalar(2);
410    V1.SubVector(RV2);
411    Check(V1, [1, 2, 3, 4, 5, 6, 7]);
412
413    RV2.MulScalar(2);
414    RV2.SubVector(V1);
415    Check(RV2, [1, 2, 3, 4, 5, 6, 7]);
416
417    if V1.Remove(5) <> 4 then Error(EWrongResult);
418    Check(V1, [1, 2, 3, 4, 6, 7]);
419    if V1.Remove(5) <> -1 then Error(EWrongResult);
420    V1[3]:=2;
421    if V1.RemoveLast(2) <> 3 then Error(EWrongResult);
422    Check(V1, [1, 2, 3, 6, 7]);
423    V1[3]:=2;
424    if V1.RemoveFrom(2, 2) <> 3 then Error(EWrongResult);
425    V1.Add(3);
426    V1.Insert(2, 4);
427    Check(V1, [1, 2, 4, 3, 7, 3]);
428    if V1.RemoveLastFrom(3, 3) <> 3 then Error(EWrongResult);
429    Check(V1, [1, 2, 4, 7, 3]);
430
431    V1.Move(3, 1);
432    Check(V1, [1, 7, 2, 4, 3]);
433    V1.Move(0, 4);
434    Check(V1, [7, 2, 4, 3, 1]);
435
436    V1.AddScaled(2, V1);
437    Check(V1, [3 * 7, 3 * 2, 3 * 4, 3 * 3, 3 * 1]);
438
439    if RV2.Remove(5) <> 4 then Error(EWrongResult);
440    Check(RV2, [1, 2, 3, 4, 6, 7]);
441    if RV2.Remove(5) <> -1 then Error(EWrongResult);
442    RV2[3]:=2;
443    if RV2.RemoveLast(2) <> 3 then Error(EWrongResult);
444    Check(RV2, [1, 2, 3, 6, 7]);
445    RV2[3]:=2;
446    if RV2.RemoveFrom(2, 2) <> 3 then Error(EWrongResult);
447    RV2.Add(3);
448    RV2.Insert(2, 4);
449    Check(RV2, [1, 2, 4, 3, 7, 3]);
450    if RV2.RemoveLastFrom(3, 3) <> 3 then Error(EWrongResult);
451    Check(RV2, [1, 2, 4, 7, 3]);
452
453    RV2.Move(3, 1);
454    Check(RV2, [1, 7, 2, 4, 3]);
455    RV2.Move(0, 4);
456    Check(RV2, [7, 2, 4, 3, 1]);
457    if RV1.EqualTo(RV2) then Error(EWrongResult);
458    RV1.Assign(RV2);
459    Check(RV1, [7, 2, 4, 3, 1]);
460    if not RV1.EqualTo(RV2) then Error(EWrongResult);
461    if not RV2.EqualTo(RV1) then Error(EWrongResult);
462
463    Test(V2);
464    Test(RV2);
465
466    V1.Count:=1;
467    V1[0]:=5;
468    RV2.Assign(V1);
469    RV2.Count:=2;
470    Check(RV2, [5, 0]);
471  finally
472    V1.Free;
473    RV1.Free;
474    V2.Free;
475    RV2.Free;
476  end;
477end;
478