forked from synopse/mORMot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.core.base.pas
6154 lines (5995 loc) · 193 KB
/
test.core.base.pas
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// regression tests for most mormot.core.* units
// - this unit is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
unit test.core.base;
interface
{$I ..\src\mormot.defines.inc}
uses
sysutils,
classes,
mormot.core.base,
mormot.core.os,
mormot.core.text,
mormot.core.buffers,
mormot.core.unicode,
mormot.core.rtti,
mormot.core.variants,
mormot.core.json,
mormot.core.data,
mormot.core.datetime,
mormot.crypt.core,
mormot.crypt.secure,
mormot.core.perf,
mormot.core.search,
mormot.core.log,
mormot.core.test,
mormot.net.sock,
mormot.db.core,
mormot.orm.core,
mormot.rest.client;
const
{$ifdef OSWINDOWS}
HTTP_DEFAULTPORT = '888';
{$else}
HTTP_DEFAULTPORT = '8888'; // under Linux, port<1024 needs root user
{$endif OSWINDOWS}
type
/// a test class, used by TTestServiceOrientedArchitecture
// - to test TPersistent objects used as parameters for remote service calls
TComplexNumber = class(TPersistent)
private
fReal: Double;
fImaginary: Double;
public
/// create an instance to store a complex number
constructor Create(aReal, aImaginary: double); reintroduce;
published
/// the real part of this complex number
property Real: Double read fReal write fReal;
/// the imaginary part of this complex number
property Imaginary: Double read fImaginary write fImaginary;
end;
TComplexNumberObjArray = array of TComplexNumber;
// a record mapping used in the test classes of the framework
// - this class can be used for debugging purposes, with the database
// created by TTestFileBased from test.orm.sqlite3
// - this class will use 'People' as a table name
TOrmPeople = class(TOrm)
private
fData: RawBlob;
fFirstName: RawUtf8;
fLastName: RawUtf8;
fYearOfBirth: integer;
fYearOfDeath: word;
published
property FirstName: RawUtf8 read fFirstName write fFirstName;
property LastName: RawUtf8 read fLastName write fLastName;
property Data: RawBlob read fData write fData;
property YearOfBirth: integer read fYearOfBirth write fYearOfBirth;
property YearOfDeath: word read fYearOfDeath write fYearOfDeath;
public
/// method used to test the Client-Side
// ModelRoot/TableName/ID/MethodName RESTful request, i.e.
// ModelRoot/People/ID/DataAsHex in this case
// - this method calls the supplied TRestClient to retrieve its results,
// with the ID taken from the current TOrmPeole instance ID field
// - parameters and result types depends on the purpose of the function
// - TRestServerTest.DataAsHex published method implements the result
// calculation on the Server-Side
function DataAsHex(aClient: TRestClientUri): RawUtf8;
/// method used to test the Client-Side
// ModelRoot/MethodName RESTful request, i.e. ModelRoot/Sum in this case
// - this method calls the supplied TRestClient to retrieve its results
// - parameters and result types depends on the purpose of the function
// - TRestServerTest.Sum published method implements the result calculation
// on the Server-Side
// - this method doesn't expect any ID to be supplied, therefore will be
// called as class function - normally, it should be implement in a
// TRestClient descendant, and not as a TOrm, since it does't depend
// on TOrmPeople at all
// - you could also call the same servce from the ModelRoot/People/ID/Sum URL,
// but it won't make any difference)
class function Sum(aClient: TRestClientUri; a, b: double; Method2: boolean): double;
end;
/// a record used to test dynamic array serialization
TFV = packed record
Major, Minor, Release, Build: integer;
Main, Detailed: string;
BuildDateTime: TDateTime;
BuildYear: integer;
end;
PFV = ^TFV;
TFVs = array of TFV;
TPersistentAutoCreateFieldsTest = class(TPersistentAutoCreateFields)
private
fText: RawUtf8;
fValue1: TComplexNumber;
fValue2: TComplexNumber;
public
constructor CreateFake;
published
property Text: RawUtf8 read fText write fText;
property Value1: TComplexNumber read fValue1;
property Value2: TComplexNumber read fValue2;
end;
TPersistentAutoCreateFieldsTestObjArray = array of TPersistentAutoCreateFieldsTest;
TObjArrayTest = class(TPersistentAutoCreateFieldsTest)
private
fValues: TComplexNumberObjArray;
published
property values: TComplexNumberObjArray read fValues write fValues;
end;
TOrmArrayTest = class(TOrm)
private
fValues: TComplexNumberObjArray;
published
property values: TComplexNumberObjArray read fValues write fValues;
end;
function TOrmPeopleCompareByFirstName(const A, B): integer;
type
/// regression tests for most basic mormot.core.* features
TTestCoreBase = class(TSynTestCase)
protected
a: array of TOrmPeople;
fAdd, fDel: RawUtf8;
fQuickSelectValues: TIntegerDynArray;
function QuickSelectGT(IndexA, IndexB: PtrInt): boolean;
procedure intadd(const Sender; Value: integer);
procedure intdel(const Sender; Value: integer);
published
/// test the new RecordCopy() using our fast RTTI
procedure _RecordCopy;
/// test the TRawUtf8List class
procedure _TRawUtf8List;
/// test the TDynArray object and methods
procedure _TDynArray;
/// test the TDynArrayHashed object and methods (dictionary features)
// - this test will create an array of 200,000 items to test speed
procedure _TDynArrayHashed;
/// test the TSynDictionary class
procedure _TSynDictionary;
/// validate the TSynQueue class
procedure _TSynQueue;
/// test TSynNameValue class
procedure _TSynNameValue;
/// test TRawUtf8Interning process
procedure _TRawUtf8Interning;
/// test T*ObjArray types and the ObjArray*() wrappers
procedure _TObjArray;
{$ifdef CPUINTEL}
/// validate our optimized MoveFast/FillCharFast functions
procedure CustomRTL;
{$endif CPUINTEL}
/// test StrIComp() and AnsiIComp() functions
procedure FastStringCompare;
/// test IdemPropName() and IdemPropNameU() functions
procedure _IdemPropName;
/// test UrlEncode() and UrlDecode() functions
procedure UrlEncoding;
/// test our internal fast TGUID process functions
procedure _GUID;
/// test ParseCommandArguments() function
procedure _ParseCommandArguments;
/// test IsMatch() function
procedure _IsMatch;
/// test TExprParserMatch class
procedure _TExprParserMatch;
/// the Soundex search feature (i.e. TSynSoundex and all related
// functions)
procedure Soundex;
/// low level fast Integer or Floating-Point to/from string conversion
// - especially the RawUtf8 or PUtf8Char relative versions
procedure NumericalConversions;
/// test low-level integer/Int64 functions
procedure Integers;
/// test crc32c in both software and hardware (SSE4.2) implementations
procedure _crc32c;
/// test RDRAND Intel x86/x64 opcode if available, or fast gsl_rng_taus2
procedure _Random32;
/// test TSynBloomFilter class
procedure BloomFilters;
/// test DeltaCompress/DeltaExtract functions
procedure _DeltaCompress;
/// the new fast Currency to/from string conversion
procedure Curr64;
/// the camel-case / camel-uncase features, used for i18n from Delphi RTII
procedure _CamelCase;
/// the low-level bit management functions
procedure Bits;
/// the fast .ini file content direct access
procedure IniFiles;
/// test UTF-8 and Win-Ansi conversion (from or to, through RawUnicode)
procedure _UTF8;
/// test UrlEncode() and UrlDecode() functions
// - this method use some ISO-8601 encoded dates and times for the testing
procedure UrlDecoding;
/// test ASCII Baudot encoding
procedure BaudotCode;
/// the ISO-8601 date and time encoding
// - test especially the conversion to/from text
procedure Iso8601DateAndTime;
/// test the TSynTimeZone class and its cross-platform local time process
procedure TimeZones;
/// test mime types recognition
procedure MimeTypes;
/// validates the median computation using the "Quick Select" algorithm
procedure QuickSelect;
/// test the TSynCache class
procedure _TSynCache;
/// low-level TSynFilter classes
procedure _TSynFilter;
/// low-level TSynValidate classes
procedure _TSynValidate;
/// low-level TSynLogFile class
procedure _TSynLogFile;
/// client side geniune 64 bit identifiers generation
procedure _TSynUniqueIdentifier;
end;
implementation
{ TOrmPeople }
function TOrmPeople.DataAsHex(aClient: TRestClientUri): RawUtf8;
begin
Result := aClient.CallBackGetResult('DataAsHex', [], RecordClass, fID);
end;
class function TOrmPeople.Sum(aClient: TRestClientUri; a, b: double;
Method2: boolean): double;
var
err: integer;
const
METHOD: array[boolean] of RawUtf8 = ('sum', 'sum2');
begin
Result := GetExtended(pointer(
aClient.CallBackGetResult(METHOD[Method2], ['a', a, 'b', b])), err);
end;
{ TComplexNumber }
constructor TComplexNumber.Create(aReal, aImaginary: double);
begin
Real := aReal;
Imaginary := aImaginary;
end;
{ TPersistentAutoCreateFieldsTest }
constructor TPersistentAutoCreateFieldsTest.CreateFake;
begin
inherited Create;
text := 'text';
Value1.Real := 1.5;
Value1.Imaginary := 2.5;
Value2.Real := 1.7;
Value2.Imaginary := 2.7;
end;
{ TTestCoreBase }
procedure TTestCoreBase._CamelCase;
var
v: RawUtf8;
begin
v := UnCamelCase('On');
Check(v = 'On');
v := UnCamelCase('ON');
Check(v = 'ON');
v := UnCamelCase('OnLine');
Check(v = 'On line');
v := UnCamelCase('OnLINE');
Check(v = 'On LINE');
v := UnCamelCase('OnMyLINE');
Check(v = 'On my LINE');
v := UnCamelCase('On_MyLINE');
Check(v = 'On - My LINE');
v := UnCamelCase('On__MyLINE');
Check(v = 'On: My LINE');
v := UnCamelCase('Email1');
Check(v = 'Email 1');
v := UnCamelCase('Email12');
Check(v = 'Email 12');
v := UnCamelCase('KLMFlightNumber');
Check(v = 'KLM flight number');
v := UnCamelCase('GoodBBCProgram');
Check(v = 'Good BBC program');
end;
function GetBitsCount64(const Bits; Count: PtrInt): PtrInt;
begin
// reference implementation
result := 0;
while Count > 0 do
begin
dec(Count);
if Count in TBits64(Bits) then // bt dword[rdi],edx is slow in such a loop
inc(result); // ... but correct :)
end;
end;
function GetBitsCountPurePascal(value: PtrInt): PtrInt;
begin
result := value;
{$ifdef CPU64}
result := result - ((result shr 1) and $5555555555555555);
result := (result and $3333333333333333) +
((result shr 2) and $3333333333333333);
result := (result + (result shr 4)) and $0f0f0f0f0f0f0f0f;
inc(result, result shr 8); // avoid slow multiplication
inc(result, result shr 16);
inc(result, result shr 32);
result := result and $7f;
{$else}
result := result - ((result shr 1) and $55555555);
result := (result and $33333333) + ((result shr 2) and $33333333);
result := (result + (result shr 4)) and $0f0f0f0f;
inc(result, result shr 8);
inc(result, result shr 16);
result := result and $3f;
{$endif CPU64}
end;
procedure TTestCoreBase.Bits;
const
N = 1000000;
procedure TestPopCnt(const ctxt: string);
var
timer: TPrecisionTimer;
i, c: integer;
v: QWord;
begin
CheckEqual(GetBitsCountPtrInt(0), 0);
CheckEqual(GetBitsCountPtrInt($f), 4);
CheckEqual(GetBitsCountPtrInt($ff), 8);
CheckEqual(GetBitsCountPtrInt($fff), 12);
CheckEqual(GetBitsCountPtrInt($ffff), 16);
CheckEqual(GetBitsCountPtrInt(-1), POINTERBITS);
v := PtrUInt(-1);
CheckEqual(GetBitsCount(v, 0), 0);
CheckEqual(GetBitsCount64(v, 0), 0);
for i := 0 to POINTERBITS - 1 do
begin
CheckEqual(GetBitsCountPtrInt(PtrInt(1) shl i), 1);
if i < POINTERBITS - 1 then
begin
CheckEqual(GetBitsCountPtrInt(PtrInt(3) shl i), 2);
CheckEqual(GetBitsCountPtrInt((PtrInt(1) shl (i + 1)) - 1), i + 1);
end;
if i < POINTERBITS - 2 then
CheckEqual(GetBitsCountPtrInt(PtrInt(7) shl i), 3);
if i < POINTERBITS - 3 then
CheckEqual(GetBitsCountPtrInt(PtrInt(15) shl i), 4);
CheckEqual(GetBitsCount64(v, i + 1), i + 1);
CheckEqual(GetBitsCount(v, i + 1), i + 1);
end;
for i := 1 to 32 do
begin
v := ALLBITS_CARDINAL[i];
CheckEqual(GetBitsCountPtrInt(v), i);
CheckEqual(GetBitsCount(v, POINTERBITS), i);
CheckEqual(GetBitsCount(v, i), i);
end;
for i := 1 to 1000 do
begin
v := i;
c := GetBitsCount64(v, POINTERBITS);
CheckEqual(GetBitsCountPtrInt(v), c);
CheckEqual(GetBitsCount(v, POINTERBITS), c);
{$ifdef FPC}
CheckEqual(popcnt(v), c);
{$endif FPC}
v := v * v * 19;
c := GetBitsCount64(v, POINTERBITS);
CheckEqual(GetBitsCountPtrInt(v), c);
{$ifdef FPC}
CheckEqual(popcnt(v), c);
{$endif FPC}
v := random32;
{$ifdef CPU64}
v := v or (PtrUInt(Random32) shl 32);
{$endif CPU64}
c := GetBitsCount64(v, POINTERBITS);
CheckEqual(GetBitsCountPtrInt(v), c);
CheckEqual(GetBitsCount(v, POINTERBITS), c);
{$ifdef FPC}
CheckEqual(popcnt(v), c);
{$endif FPC}
end;
timer.Start;
for i := 1 to N do
GetBitsCountPtrInt(i);
NotifyTestSpeed(ctxt, N, N shl POINTERSHR, @timer, {onlylog=}true);
end;
var
Bits: array[byte] of byte;
Bits64: Int64 absolute Bits;
Si, i: integer;
c: cardinal;
{$ifdef FPC}
u: PtrUInt;
timer: TPrecisionTimer;
{$endif FPC}
begin
{$ifdef CPUINTEL}
GetBitsCountPtrInt := @GetBitsCountPurePascal;
TestPopCnt('pas');
GetBitsCountPtrInt := @GetBitsCountPas; // x86/x86_64 assembly
TestPopCnt('asm');
if cfPOPCNT in CpuFeatures then
begin
GetBitsCountPtrInt := @GetBitsCountSSE42;
TestPopCnt('sse4.2');
end;
{$else}
TestPopCnt('pas');
{$endif CPUINTEL}
{$ifdef FPC}
timer.Start;
for u := 1 to N do
i := popcnt(u);
NotifyTestSpeed('FPC', N, N shl POINTERSHR, @timer, {onlylog=}true);
{$endif FPC}
FillcharFast(Bits, sizeof(Bits), 0);
for i := 0 to high(Bits) * 8 + 7 do
begin
Check(not GetBit(Bits, i));
Check(not GetBitPtr(@Bits, i));
end;
RandSeed := 10; // will reproduce the same Random() values
for i := 1 to 100 do
begin
Si := Random(high(Bits));
SetBit(Bits, Si);
Check(GetBit(Bits, Si));
Check(GetBitPtr(@Bits, Si));
end;
RandSeed := 10;
for i := 1 to 100 do
Check(GetBit(Bits, Random(high(Bits))));
RandSeed := 10;
for i := 1 to 100 do
begin
Si := Random(high(Bits));
UnSetBit(Bits, Si);
Check(not GetBit(Bits, Si));
Check(not GetBitPtr(@Bits, Si));
end;
for i := 0 to high(Bits) * 8 + 7 do
Check(not GetBit(Bits, i));
for i := 0 to 63 do
Check(not GetBit64(Bits64, i));
RandSeed := 10;
for i := 1 to 30 do
begin
Si := Random(63);
SetBit64(Bits64, Si);
Check(GetBit64(Bits64, Si));
end;
RandSeed := 10;
for i := 1 to 30 do
Check(GetBit64(Bits64, Random(63)));
RandSeed := 10;
for i := 1 to 30 do
begin
Si := Random(63);
UnSetBit64(Bits64, Si);
Check(not GetBit64(Bits64, Si));
end;
for i := 0 to 63 do
Check(not GetBit64(Bits64, i));
c := 1;
for i := 1 to 32 do
begin
Check(GetAllBits($ffffffff, i));
Check(not GetAllBits(0, i));
Check(GetAllBits(c, i));
Check(not GetAllBits(c and - 2, i));
Check(GetAllBits(ALLBITS_CARDINAL[i], i));
c := c or (1 shl i);
end;
Randomize; // we fixed the RandSeed value above -> get true random now
end;
procedure TTestCoreBase.Curr64;
var
tmp: string[63];
i, err: Integer;
V1: currency;
V2: TSynExtended;
i64: Int64;
v: RawUtf8;
begin
Check(TruncTo2Digits(1) = 1);
Check(TruncTo2Digits(1.05) = 1.05);
Check(TruncTo2Digits(1.051) = 1.05);
Check(TruncTo2Digits(1.0599) = 1.05);
Check(TruncTo2Digits(-1) = -1);
Check(TruncTo2Digits(-1.05) = -1.05);
Check(TruncTo2Digits(-1.051) = -1.05);
Check(TruncTo2Digits(-1.0599) = -1.05);
Check(SimpleRoundTo2Digits(1) = 1);
Check(SimpleRoundTo2Digits(1.05) = 1.05);
Check(SimpleRoundTo2Digits(1.051) = 1.05);
Check(SimpleRoundTo2Digits(1.0549) = 1.05);
Check(SimpleRoundTo2Digits(1.0550) = 1.05);
Check(SimpleRoundTo2Digits(1.0551) = 1.06);
Check(SimpleRoundTo2Digits(1.0599) = 1.06);
Check(SimpleRoundTo2Digits(-1) = -1);
Check(SimpleRoundTo2Digits(-1.05) = -1.05);
Check(SimpleRoundTo2Digits(-1.051) = -1.05);
Check(SimpleRoundTo2Digits(-1.0549) = -1.05);
Check(SimpleRoundTo2Digits(-1.0550) = -1.05);
Check(SimpleRoundTo2Digits(-1.0551) = -1.06);
Check(SimpleRoundTo2Digits(-1.0599) = -1.06);
Check(StrToCurr64('.5') = 5000);
Check(StrToCurr64('.05') = 500);
Check(StrToCurr64('.005') = 50);
Check(StrToCurr64('.0005') = 5);
Check(StrToCurr64('.00005') = 0);
Check(StrToCurr64('0.5') = 5000);
Check(StrToCurr64('0.05') = 500);
Check(StrToCurr64('0.005') = 50);
Check(StrToCurr64('0.0005') = 5);
Check(StrToCurr64('0.00005') = 0);
Check(StrToCurr64('1.5') = 15000);
Check(StrToCurr64('1.05') = 10500);
Check(StrToCurr64('1.005') = 10050);
Check(StrToCurr64('1.0005') = 10005);
Check(StrToCurr64('1.00005') = 10000);
Check(StrToCurr64(pointer(Curr64ToStr(1))) = 1);
Check(StrToCurr64(pointer(Curr64ToStr(12))) = 12);
Check(StrToCurr64(pointer(Curr64ToStr(123))) = 123);
Check(StrToCurr64(pointer(Curr64ToStr(1234))) = 1234);
Check(StrToCurr64(pointer(Curr64ToStr(12345))) = 12345);
Check(StrToCurr64(pointer(Curr64ToStr(123456))) = 123456);
Check(StrToCurr64(pointer(Curr64ToStr(12340000))) = 12340000);
Check(StrToCurr64(pointer(Curr64ToStr(12345000))) = 12345000);
Check(StrToCurr64(pointer(Curr64ToStr(12345600))) = 12345600);
Check(StrToCurr64(pointer(Curr64ToStr(12345670))) = 12345670);
Check(StrToCurr64(pointer(Curr64ToStr(12345678))) = 12345678);
tmp[0] := AnsiChar(Curr64ToPChar(1, @tmp[1]));
Check(tmp = '0.0001');
tmp[0] := AnsiChar(Curr64ToPChar(12, @tmp[1]));
Check(tmp = '0.0012');
tmp[0] := AnsiChar(Curr64ToPChar(123, @tmp[1]));
Check(tmp = '0.0123');
tmp[0] := AnsiChar(Curr64ToPChar(1234, @tmp[1]));
Check(tmp = '0.1234');
for i := 0 to 5000 do
begin
if i < 500 then
V1 := i * 3
else
V1 := Random64 shr 28;
if Random32 and 3 = 0 then
V1 := -V1;
v := Curr64ToStr(PInt64(@V1)^);
tmp[0] := AnsiChar(Curr64ToPChar(PInt64(@V1)^, @tmp[1]));
Check(RawUtf8(tmp) = v);
V2 := GetExtended(pointer(v), err);
Check(err = 0);
CheckSame(V1, V2, 1E-4);
i64 := StrToCurr64(pointer(v));
Check(PInt64(@V1)^ = i64);
end;
end;
procedure TTestCoreBase.FastStringCompare;
begin
Check(CompareText('', '') = 0);
Check(CompareText('abcd', '') > 0);
Check(CompareText('', 'abcd') < 0);
Check(StrIComp(nil, nil) = 0);
Check(StrIComp(PAnsiChar('abcD'), nil) = 1);
Check(StrIComp(nil, PAnsiChar('ABcd')) = -1);
Check(StrIComp(PAnsiChar('abcD'), PAnsiChar('ABcd')) = 0);
Check(StrIComp(PAnsiChar('abcD'), PAnsiChar('ABcF')) =
StrComp(PAnsiChar('ABCD'), PAnsiChar('ABCF')));
Check(StrComp(PAnsiChar('abcD'), nil) = 1);
Check(StrComp(nil, PAnsiChar('ABcd')) = -1);
Check(StrComp(nil, nil) = 0);
Check(StrComp(PAnsiChar('ABCD'), PAnsiChar('ABCD')) = 0);
Check(StrComp(PAnsiChar('ABCD'), PAnsiChar('ABCE')) = -1);
Check(StrComp(PAnsiChar('ABCD'), PAnsiChar('ABCC')) = 1);
Check(AnsiIComp(pointer(PAnsiChar('abcD')), pointer(PAnsiChar('ABcd'))) = 0);
Check(AnsiIComp(pointer(PAnsiChar('abcD')), pointer(PAnsiChar('ABcF'))) =
StrComp(PAnsiChar('ABCD'), PAnsiChar('ABCF')));
Check(StrIComp(PAnsiChar('abcD'), PAnsiChar('ABcd')) =
AnsiIComp(PAnsiChar('abcD'), PAnsiChar('ABcd')));
Check(StrIComp(PAnsiChar('abcD'), PAnsiChar('ABcF')) =
AnsiIComp(PAnsiChar('ABCD'), PAnsiChar('ABCF')));
Check(strcspn(PAnsiChar('ab'), PAnsiChar('a'#0)) = 0);
Check(strcspn(PAnsiChar('ab'), PAnsiChar('b'#0)) = 1);
Check(strcspn(PAnsiChar('1234ab'), PAnsiChar('a'#0)) = 4);
Check(strcspn(PAnsiChar('12345ab'), PAnsiChar('a'#0)) = 5);
Check(strcspn(PAnsiChar('123456ab'), PAnsiChar('a'#0)) = 6);
Check(strcspn(PAnsiChar('1234567ab'), PAnsiChar('a'#0)) = 7);
Check(strcspn(PAnsiChar('12345678ab'), PAnsiChar('a'#0)) = 8);
Check(strcspn(PAnsiChar('1234ab'), PAnsiChar('c'#0)) = 6);
Check(strcspn(PAnsiChar('12345678901234567ab'),
PAnsiChar('cccccccccccccccccccd')) = 19);
Assert(strspn(PAnsiChar('abcdef'), PAnsiChar('debca')) = 5);
Assert(strspn(PAnsiChar('baabbaabcd'), PAnsiChar('ab')) = 8);
Assert(strspn(PAnsiChar('abcdef'), PAnsiChar('g'#0)) = 0);
Assert(strspn(PAnsiChar('abcdef'), PAnsiChar('a'#0)) = 1);
Assert(strspn(PAnsiChar('bbcdef'), PAnsiChar('b'#0)) = 2);
Assert(strspn(PAnsiChar('bbcdef'), PAnsiChar('bf')) = 2);
Assert(strspn(PAnsiChar('bcbdef'), PAnsiChar('cb')) = 3);
Assert(strspn(PAnsiChar('baabcd'), PAnsiChar('ab')) = 4);
Assert(strspn(PAnsiChar('abcdef'), PAnsiChar('debca')) = 5);
Assert(strspn(PAnsiChar('baabbaabcd'), PAnsiChar('ab')) = 8);
Assert(strspn(PAnsiChar('baabbaabbaabcd'), PAnsiChar('ab')) = 12);
Assert(strspn(PAnsiChar('baabbaabbaabbabcd'), PAnsiChar('ab')) = 15);
Assert(strspn(PAnsiChar('baabbaabbaabbaabcd'), PAnsiChar('ab')) = 16);
Assert(strspn(PAnsiChar('baabbaabbaababaabcd'), PAnsiChar('ab')) = 17);
end;
procedure TTestCoreBase.IniFiles;
var
Content, S, N, V: RawUtf8;
Si, Ni, Vi, i, j: integer;
P: PUtf8Char;
begin
Content := '';
for i := 1 to 1000 do
begin
Si := Random32(20);
Ni := Random32(50);
Vi := Si * Ni + Ni;
if Si = 0 then
S := ''
else
S := 'Section' + Int32ToUtf8(Si);
N := Int32ToUtf8(Ni);
V := Int32ToUtf8(Vi);
UpdateIniEntry(Content, S, N, V);
for j := 1 to 5 do
Check(FindIniEntry(Content, S, N) = V, 'FindIniEntry');
Check(FindIniEntry(Content, S, 'no') = '');
Check(FindIniEntry(Content, 'no', N) = '');
end;
Check(FileFromString(Content, WorkDir + 'test.ini'), 'test.ini');
Check(AlgoSynLZ.FileCompress(WorkDir + 'test.ini',
WorkDir + 'test.ini.synlz', $ABA51051), 'synLZ');
if CheckFailed(AlgoSynLZ.FileUnCompress(WorkDir + 'test.ini.synlz',
WorkDir + 'test2.ini', $ABA51051), 'unSynLZ') then
exit;
S := StringFromFile(WorkDir + 'test2.ini');
Check(S = Content, WorkDir + 'test2.ini');
Content := 'abc'#13#10'def'#10'ghijkl'#13'1234567890';
P := pointer(Content);
Check(GetNextLine(P, P) = 'abc');
Check(GetNextLine(P, P) = 'def');
Check(GetNextLine(P, P) = 'ghijkl');
Check(GetNextLine(P, P) = '1234567890');
Check(P = nil);
Check(FindNameValue(pointer(Content), 'A')^ = 'b');
Check(FindNameValue(pointer(Content), 'AB')^ = 'c');
Check(FindNameValue(pointer(Content), 'D')^ = 'e');
Check(FindNameValue(pointer(Content), '1')^ = '2');
Check(FindNameValue(pointer(Content), 'GHIJK')^ = 'l');
Check(FindNameValue(pointer(Content), 'B') = nil);
Check(FindNameValue(pointer(Content), 'L') = nil);
Check(FindNameValue(pointer(Content), '2') = nil);
Check(FindNameValue(pointer(Content), 'TOTO') = nil);
Check(FindNameValue(Content, 'AB', S));
Check(S = 'c');
Check(FindNameValue(Content, 'DEF', S));
Check(S = '');
Check(FindNameValue(Content, 'G', S));
Check(S = 'hijkl');
Check(FindNameValue(Content, '1234', S));
Check(S = '567890');
Check(not FindNameValue(Content, 'H', S));
Check(S = '');
end;
procedure TTestCoreBase.Soundex;
var
e: cardinal;
PC: PAnsiChar;
Soundex: TSynSoundEx;
s: WinAnsiString;
begin
Check(SoundExAnsi(PAnsiChar(' 120 ')) = 0);
if SOUNDEX_BITS = 8 then
{%H-}e := $2050206
else
e := $2526;
Check(SoundExAnsi(PAnsiChar('bonjour')) = e);
Check(SoundExAnsi(PAnsiChar(' 123 bonjour. m'), @PC) = e);
Check((PC <> nil) and
(PC^ = '.'));
s := ' 123 bonjourtreslongmotquidepasse m';
s[15] := #232;
s[28] := #233;
Check(SoundExAnsi(pointer(s), @PC) <> 0);
Check((PC <> nil) and
(PC^ = ' '));
Check(SoundExAnsi(PAnsiChar('BOnjour')) = e);
Check(SoundExAnsi(PAnsiChar('Bnjr')) = e);
Check(SoundExAnsi(PAnsiChar('bonchour')) = e);
Check(SoundExAnsi(PAnsiChar('mohammad')) =
SoundExAnsi(PAnsiChar('mohhhammeeet')));
if SOUNDEX_BITS = 8 then
{%H-}e := $2050206
else
e := $25262;
Check(SoundExAnsi(PAnsiChar('bonjours')) = e);
Check(SoundExAnsi(PAnsiChar('BOnjours')) = e);
Check(SoundExAnsi(PAnsiChar('Bnjrs')) = e);
Check(SoundExAnsi(PAnsiChar(' 120 ')) = 0);
if SOUNDEX_BITS = 8 then
{%H-}e := $2050206
else
e := $2526;
Check(SoundExUtf8('bonjour') = e);
Check(SoundExUtf8(' 123 bonjour. m', @PC) = e);
Check((PC <> nil) and
(PC^ = 'm'));
Check(SoundExUtf8(Pointer(WinAnsiToUtf8(s)), @PC) <> 0);
Check((PC <> nil) and
(PC^ = 'm'));
Check(SoundExUtf8('BOnjour') = e);
Check(SoundExUtf8('Bnjr') = e);
Check(SoundExUtf8('bonchour') = e);
Check(SoundExUtf8('mohammad') = SoundExUtf8('mohhhammeeet'));
if SOUNDEX_BITS = 8 then
{%H-}e := $2050206
else
e := $25262;
Check(SoundExUtf8('bonjours') = e);
Check(SoundExUtf8('BOnjours') = e);
Check(SoundExUtf8('Bnjrs') = e);
Check(Soundex.Prepare(PAnsiChar('mohamad'), sndxEnglish));
Check(Soundex.Ansi('moi rechercher mohammed ici'));
Check(Soundex.Utf8('moi rechercher mohammed ici'));
Check(Soundex.Ansi('moi mohammed'));
Check(Soundex.Utf8('moi mohammed'));
Check(not Soundex.Ansi('moi rechercher mouette ici'));
Check(not Soundex.Utf8('moi rechercher mouette ici'));
Check(not Soundex.Ansi('moi rechercher mouette'));
Check(not Soundex.Utf8('moi rechercher mouette'));
end;
procedure TTestCoreBase._TRawUtf8List;
const
MAX = 20000;
var
i, n: integer;
L: TRawUtf8List;
C: TComponent;
Rec: TSynFilterOrValidate;
s: RawUtf8;
begin
L := TRawUtf8List.Create([fObjectsOwned]);
try // no hash table involved
for i := 0 to MAX do
begin
C := TComponent.Create(nil);
C.Tag := i;
Check(L.AddObject(UInt32ToUtf8(i), C) = i);
end;
Check(L.Count = MAX + 1);
for i := 0 to MAX do
Check(GetInteger(Pointer(L[i])) = i);
for i := 0 to MAX do
Check(TComponent(L.Objects[i]).Tag = i);
Check(L.IndexOf('') < 0);
Check(L.IndexOf('5') = 5);
Check(L.IndexOf('999') = 999);
for i := MAX downto 0 do
if i and 1 = 0 then
L.Delete(i); // delete half the array
Check(L.Count = MAX div 2);
for i := 0 to L.Count - 1 do
Check(GetInteger(Pointer(L[i])) = TComponent(L.Objects[i]).Tag);
Check(L.IndexOf('5') = 2);
Check(L.IndexOf('6') < 0);
finally
L.Free;
end;
L := TRawUtf8List.Create([fObjectsOwned, fNoDuplicate, fCaseSensitive]);
try // with hash table
for i := 1 to MAX do
begin
Rec := TSynFilterOrValidate.create;
Rec.Parameters := Int32ToUtf8(i);
Check(L.AddObject(Rec.Parameters, Rec) = i - 1);
Check(L.IndexOf(Rec.Parameters) = i - 1);
end;
Check(L.IndexOf('') < 0);
Check(L.IndexOf('abcd') < 0);
Check(L.Count = MAX);
n := 0;
for i := 1 to MAX do
begin
UInt32ToUtf8(i, s);
Check(L.IndexOf(s) = n);
Check(TSynFilterOrValidate(L.Objects[n]).Parameters = s);
if i and 127 = 0 then
Check(L.Delete(s) = n)
else
inc(n);
end;
Check(L.Count = n);
for i := 1 to MAX do
begin
UInt32ToUtf8(i, s);
Check((L.IndexOf(s) >= 0) = (i and 127 <> 0));
end;
L.SaveToFile(WorkDir + 'utf8list.txt');
L.Clear;
Check(L.Count = 0);
L.LoadFromFile(WorkDir + 'utf8list.txt');
Check(L.Count = n);
for i := 1 to MAX do
begin
UInt32ToUtf8(i, s);
Check((L.IndexOf(s) >= 0) = (i and 127 <> 0));
end;
DeleteFile(WorkDir + 'utf8list.txt');
finally
L.Free;
end;
end;
type
TCity = record
Name: string;
Country: string;
Latitude: double;
Longitude: double;
end;
TCityDynArray = array of TCity;
TAmount = packed record
firmID: integer;
amount: RawUtf8;
end;
TAmountCollection = array of TAmount;
TAmountI = packed record
firmID: integer;
amount: integer;
end;
TAmountICollection = array of TAmountI;
procedure TTestCoreBase._TDynArrayHashed;
var
ACities: TDynArrayHashed;
Cities: TCityDynArray;
CitiesCount: integer;
City: TCity;
added: boolean;
N: string;
i, j: integer;
A: TAmount;
AI: TAmountI;
AmountCollection: TAmountCollection;
AmountICollection: TAmountICollection;
AmountDA, AmountIDA1, AmountIDA2: TDynArrayHashed;
const
CITIES_MAX = 200000;
begin
//FIXME - too slow on FullDebugMode exit;
// default Init() will hash and compare binary content before string, i.e. firmID
AmountDA.Init(TypeInfo(TAmountCollection), AmountCollection);
Check(AmountDA.Info.Parser = ptDynArray);
Check(AmountDA.Info.ArrayFirstField = ptInteger);
Check(@AmountDA.HashItem = @PT_HASH[false, ptInteger]);
for i := 1 to 100 do
begin
A.firmID := i;
A.amount := UInt32ToUtf8(i);
Check(AmountDA.Add(A) = i - 1);
end;
AmountDA.ReHash;
for i := 1 to length(AmountCollection) do
Check(AmountDA.FindHashed(i) = i - 1);
// default Init() will hash and compare the WHOLE binary content, i.e. 8 bytes
AmountIDA1.Init(TypeInfo(TAmountICollection), AmountICollection);
Check(AmountIDA1.Info.Parser = ptDynArray);
Check(AmountIDA1.Info.ArrayFirstField = ptInt64);
Check(@AmountIDA1.HashItem = @PT_HASH[false, ptInt64]);
for i := 1 to 100 do
begin
AI.firmID := i;
AI.amount := i * 2;
Check(AmountIDA1.Add(AI) = i - 1);
end;
AmountIDA1.ReHash;
for i := 1 to length(AmountICollection) do
begin
AI.firmID := i;
AI.amount := i * 2;
Check(AmountIDA1.FindHashed(AI) = i - 1);
end;
AmountIDA1.Clear;
// specific hash & compare of the firmID integer first field
AmountIDA2.InitSpecific(
TypeInfo(TAmountICollection), AmountICollection, ptInteger);
Check(AmountIDA2.Info.Parser = ptDynArray);
Check(AmountIDA2.Info.ArrayFirstField = ptInt64); // global TRttiCustom untouched
Check(@AmountIDA2.HashItem = @PT_HASH[false, ptInteger]);
for i := 1 to 100 do
begin
AI.firmID := i;
AI.amount := i * 2;
Check(AmountIDA2.Add(AI) = i - 1);
end;
AmountIDA2.ReHash;
for i := 1 to length(AmountICollection) do
Check(AmountIDA2.FindHashed(i) >= 0);
// valide generic-like features
// see http://docwiki.embarcadero.com/CodeExamples/en/Generics_Collections_TDictionary_(Delphi)
ACities.Init(TypeInfo(TCityDynArray), Cities, nil, nil, nil, @CitiesCount);
City.Name := 'Iasi';
City.Country := 'Romania';
City.Latitude := 47.16;
City.Longitude := 27.58;
ACities.Add(City);
City.Name := 'London';
City.Country := 'United Kingdom';
City.Latitude := 51.5;
City.Longitude := -0.17;
ACities.Add(City);
City.Name := 'Buenos Aires';
City.Country := 'Argentina';
City.Latitude := 0;
City.Longitude := 0;
ACities.Add(City);
Check(ACities.Count = 3);
ACities.ReHash; // will use default hash, and search by Name = 1st field
City.Name := 'Iasi';
Check(ACities.FindHashedAndFill(City) = 0);
Check(City.Name = 'Iasi');
Check(City.Country = 'Romania');
CheckSame(City.Latitude, 47.16);
CheckSame(City.Longitude, 27.58);
Check(ACities.FindHashedAndDelete(City) = 0);
Check(City.Name = 'Iasi');
Check(ACities.Scan(City) < 0);
Check(ACities.FindHashed(City) < 0);
City.Name := 'Buenos Aires';
City.Country := 'Argentina';
City.Latitude := -34.6;
City.Longitude := -58.45;
Check(ACities.FindHashedAndUpdate(City, {addifnotexisting=}false) >= 0);
City.Latitude := 0;
City.Longitude := 0;
Check(City.Name = 'Buenos Aires');
Check(ACities.FindHashedAndFill(City) >= 0);
CheckSame(City.Latitude, -34.6);
CheckSame(City.Longitude, -58.45);
Check(ACities.FindHashedForAdding(City, added) >= 0);
Check(not added);
City.Name := 'Iasi';
City.Country := 'Romania';
City.Latitude := 47.16;
City.Longitude := 27.58;
i := ACities.FindHashedForAdding(City, added);