tcsyshelpers.pp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. unit tcsyshelpers;
  2. {$mode objfpc}{$h+}
  3. interface
  4. uses
  5. SysUtils, fpcunit, testregistry;
  6. Type
  7. { TTestHelpers }
  8. TTestHelpers = class(TTestCase)
  9. private
  10. procedure EqualGUID(Msg: String; Expected, Actual: TGUID);
  11. procedure EqualGUIDSwap(Msg: String; Expected, Actual: TGUID);
  12. procedure GetGUID(out G: TGUID);
  13. Published
  14. // Public
  15. procedure TestGUIDHelperCreateUntypedData;
  16. procedure TestGUIDHelperCreateUntypedDataEndian;
  17. procedure TestGUIDHelperCreateInteger;
  18. procedure TestGUIDHelperCreateIntegerBytes;
  19. procedure TestGUIDHelperCreateNew;
  20. procedure TestGUIDHelperCreateString;
  21. procedure TestGUIDHelperCreateTBytes;
  22. procedure TestGUIDHelperCreateTBytesAtIndex;
  23. procedure TestGUIDHelperCreateWords;
  24. procedure TestGUIDHelperToByteArray;
  25. procedure TestGUIDHelperToString;
  26. procedure TestByteHelper;
  27. procedure TestCardinalHelper;
  28. procedure TestLongintHelper;
  29. procedure TestNegLongintHelper;
  30. procedure TestNegShortIntHelper;
  31. procedure TestNegSmallintHelper;
  32. procedure TestShortIntHelper;
  33. procedure TestSmallintHelper;
  34. procedure TestWordHelper;
  35. procedure TestIsNanDouble;
  36. procedure TestByteClearBit;
  37. procedure TestByteSetBit;
  38. procedure TestByteTestBit;
  39. procedure TestByteToggleBit;
  40. procedure TestShortIntSetBit;
  41. procedure TestShortIntToggleBit;
  42. procedure TestCardinalClearBit;
  43. procedure TestCardinalSetBit;
  44. procedure TestCardinalTestBit;
  45. procedure TestCardinalToggleBit;
  46. procedure TestLongintClearBit;
  47. procedure TestLongintSetBit;
  48. procedure TestLongintTestBit;
  49. procedure TestLongintToggleBit;
  50. procedure TestShortIntClearBit;
  51. procedure TestShortIntTestBit;
  52. procedure TestSmallIntClearBit;
  53. procedure TestSmallIntSetBit;
  54. procedure TestSmallIntTestBit;
  55. procedure TestSmallIntToggleBit;
  56. procedure TestWordClearBit;
  57. procedure TestWordSetBit;
  58. procedure TestWordTestBit;
  59. procedure TestWordToggleBit;
  60. procedure TestNativeUintSetBit;
  61. procedure TestNativeUIntToggleBit;
  62. procedure TestNativeIntHelper;
  63. procedure TestNativeUintHelper;
  64. procedure TestNativeUIntTestBit;
  65. procedure TestNativeIntClearBit;
  66. procedure TestNativeIntSetBit;
  67. procedure TestNativeIntToggleBit;
  68. procedure TestNativeIntTestBit;
  69. end;
  70. Implementation
  71. Procedure TTestHelpers.TestByteHelper;
  72. Const
  73. Value = 123;
  74. ValueAsString = '123';
  75. ValueAsHex = '7B';
  76. ValueAsHexDig = 4;
  77. ValueAsHexDigString = '007B';
  78. Var
  79. V : Byte;
  80. begin
  81. {$i tohelper.inc}
  82. end;
  83. Procedure TTestHelpers.TestShortIntHelper;
  84. Const
  85. Value = 123;
  86. ValueAsString = '123';
  87. ValueAsHex = '7B';
  88. ValueAsHexDig = 4;
  89. ValueAsHexDigString = '007B';
  90. Var
  91. V : ShortInt;
  92. begin
  93. {$i tohelper.inc}
  94. end;
  95. Procedure TTestHelpers.TestNegShortIntHelper;
  96. Const
  97. Value = -123;
  98. ValueAsString = '-123';
  99. ValueAsHex = '85';
  100. ValueAsHexDig = 4;
  101. ValueAsHexDigString = 'FF85';
  102. Var
  103. V : ShortInt;
  104. begin
  105. {$i tohelper.inc}
  106. end;
  107. Procedure TTestHelpers.TestWordHelper;
  108. Const
  109. Value = 1024;
  110. ValueAsString = '1024';
  111. ValueAsHex = '0400';
  112. ValueAsHexDig = 6;
  113. ValueAsHexDigString = '000400';
  114. Var
  115. V : Word;
  116. begin
  117. {$i tohelper.inc}
  118. end;
  119. Procedure TTestHelpers.TestSmallintHelper;
  120. Const
  121. Value = 1024;
  122. ValueAsString = '1024';
  123. ValueAsHex = '0400';
  124. ValueAsHexDig = 6;
  125. ValueAsHexDigString = '000400';
  126. Var
  127. V : Smallint;
  128. begin
  129. {$i tohelper.inc}
  130. end;
  131. Procedure TTestHelpers.TestNegSmallintHelper;
  132. Const
  133. Value = -1024;
  134. ValueAsString = '-1024';
  135. ValueAsHex = 'FC00';
  136. ValueAsHexDig = 6;
  137. ValueAsHexDigString = 'FFFC00';
  138. Var
  139. V : Smallint;
  140. begin
  141. {$i tohelper.inc}
  142. end;
  143. Procedure TTestHelpers.TestCardinalHelper;
  144. Const
  145. Value = 131072;
  146. ValueAsString = '131072';
  147. ValueAsHex = '00020000';
  148. ValueAsHexDig = 10;
  149. ValueAsHexDigString = '0000020000';
  150. Var
  151. V : Cardinal;
  152. begin
  153. {$i tohelper.inc}
  154. end;
  155. Procedure TTestHelpers.TestLongintHelper;
  156. Const
  157. Value = 131072;
  158. ValueAsString = '131072';
  159. ValueAsHex = '00020000';
  160. ValueAsHexDig = 10;
  161. ValueAsHexDigString = '0000020000';
  162. Var
  163. V : Longint;
  164. begin
  165. {$i tohelper.inc}
  166. end;
  167. Procedure TTestHelpers.TestNegLongintHelper;
  168. Const
  169. Value = -131072;
  170. ValueAsString = '-131072';
  171. ValueAsHex = 'FFFE0000';
  172. ValueAsHexDig = 10;
  173. ValueAsHexDigString = '00FFFE0000';
  174. Var
  175. V : Longint;
  176. begin
  177. {$i tohelper.inc}
  178. end;
  179. Procedure TTestHelpers.TestNativeUintHelper;
  180. Const
  181. Value = 17179869184; // 2^34
  182. ValueAsString = '17179869184';
  183. ValueAsHex = '0000000400000000';
  184. ValueAsHexDig = 18;
  185. ValueAsHexDigString = '000000000400000000';
  186. Var
  187. V : NativeUInt;
  188. begin
  189. Fail('Not implemented yet');
  190. // {$i tohelper.inc}
  191. end;
  192. Procedure TTestHelpers.TestNativeIntHelper;
  193. Const
  194. Value = 17179869184; // 2^34
  195. ValueAsString = '17179869184';
  196. ValueAsHex = '0000000400000000';
  197. ValueAsHexDig = 18;
  198. ValueAsHexDigString = '000000000400000000';
  199. Var
  200. V : NativeInt;
  201. begin
  202. Fail('Notimplemented');
  203. // {$i tohelper.inc}
  204. end;
  205. Procedure TTestHelpers.GetGUID(out G : TGUID);
  206. Var
  207. I : Integer;
  208. begin
  209. G.D1:=$DDCCBBAA;
  210. G.D2:=$EEFF;
  211. G.D3:=$CAAC;
  212. For I:=0 to 7 do
  213. G.D4[i]:=(1 shl i) and $FF;
  214. end;
  215. Procedure TTestHelpers.EqualGUID(Msg : String;Expected,Actual : TGUID);
  216. Var
  217. I : Integer;
  218. begin
  219. AssertEquals(Msg+' D1 equal',Expected.D1,Actual.D1);
  220. AssertEquals(Msg+' D2 equal',Expected.D2,Actual.D2);
  221. AssertEquals(Msg+' D2 equal',Expected.D3,Actual.D3);
  222. For I:=0 to 7 do
  223. AssertEquals(Msg+' D4['+IntToStr(I)+'] equal',Expected.D4[i],Actual.D4[i]);
  224. end;
  225. Procedure TTestHelpers.EqualGUIDSwap(Msg : String;Expected,Actual : TGUID);
  226. Var
  227. I : Integer;
  228. begin
  229. AssertEquals(Msg+' D1 equal',SwapEndian(Expected.D1),Actual.D1);
  230. AssertEquals(Msg+' D2 equal',SwapEndian(Expected.D2),Actual.D2);
  231. AssertEquals(Msg+' D2 equal',SwapEndian(Expected.D3),Actual.D3);
  232. For I:=0 to 7 do
  233. AssertEquals(Msg+' D4['+IntToStr(I)+'] equal',Expected.D4[i],Actual.D4[i]);
  234. end;
  235. Procedure TTestHelpers.TestGUIDHelperCreateUntypedData;
  236. Var
  237. Src,Dest : TGUID;
  238. begin
  239. GetGUID(Src);
  240. Dest:=TGUID.Create(Src, True);
  241. if CPUEndian = TEndian.Big then
  242. EqualGUID('BE CPU: Create(Data,True)',Src,Dest)
  243. else
  244. EqualGUIDSwap('LE CPU: Create(Data,True)',Src,Dest);
  245. Dest:=TGUID.Create(Src, False);
  246. if CPUEndian = TEndian.Big then
  247. EqualGUIDSwap('BE CPU: Create(Data,False)',Src,Dest)
  248. else
  249. EqualGUID('LE CPU : Create(Data,False)',Src,Dest);
  250. end;
  251. Procedure TTestHelpers.TestGUIDHelperCreateUntypedDataEndian;
  252. Var
  253. Src,Dest : TGUID;
  254. begin
  255. GetGUID(Src);
  256. Dest:=TGUID.Create(Src, True);
  257. if CPUEndian = TEndian.Big then
  258. EqualGUID('BE CPU: Create(Data,True)',Src,Dest)
  259. else
  260. EqualGUIDSwap('LE CPU: Create(Data,True)',Src,Dest);
  261. Dest:=TGUID.Create(Src, False);
  262. if CPUEndian = TEndian.Big then
  263. EqualGUIDSwap('BE CPU: Create(Data,False)',Src,Dest)
  264. else
  265. EqualGUID('LE CPU : Create(Data,False)',Src,Dest);
  266. end;
  267. Procedure TTestHelpers.TestGUIDHelperCreateTBytes;
  268. Var
  269. Src,Dest : TGUID;
  270. SrcBytes : TBytes;
  271. D,I : Cardinal;
  272. begin
  273. GetGUID(Src);
  274. SrcBytes:=[];
  275. SetLength(SrcBytes,16);
  276. D:=Src.D1;
  277. SrcBytes[0]:=D shr 24;
  278. SrcBytes[1]:=(D shr 16) and $FF;
  279. SrcBytes[2]:=(D shr 8) and $FF;
  280. SrcBytes[3]:=(D and $FF);
  281. D:=Src.D2;
  282. SrcBytes[4]:=(D shr 8) and $FF;
  283. SrcBytes[5]:=(D and $FF);
  284. D:=Src.D3;
  285. SrcBytes[6]:=(D shr 8) and $FF;
  286. SrcBytes[7]:=(D and $FF);
  287. For I:=0 to 7 do
  288. SrcBytes[8+i]:=Src.D4[i];
  289. Dest:=TGUID.Create(SrcBytes, TEndian.Big);
  290. if CPUEndian = TEndian.Big then
  291. EqualGUID('BE CPU: Create(Data,True)',Src,Dest)
  292. else
  293. EqualGUIDSwap('LE CPU: Create(Data,True)',Src,Dest);
  294. Dest:=TGUID.Create(SrcBytes, TEndian.Little);
  295. if CPUEndian = TEndian.Big then
  296. EqualGUIDSwap('BE CPU: Create(Data,False)',Src,Dest)
  297. else
  298. EqualGUID('LE CPU : Create(Data,False)',Src,Dest);
  299. end;
  300. Procedure TTestHelpers.TestGUIDHelperCreateTBytesAtIndex;
  301. Var
  302. Src,Dest : TGUID;
  303. SrcBytes : TBytes;
  304. I,D : Cardinal;
  305. begin
  306. GetGUID(Src);
  307. SrcBytes:=[];
  308. SetLength(SrcBytes,32);
  309. D:=Src.D1;
  310. SrcBytes[4]:=D shr 24;
  311. SrcBytes[5]:=(D shr 16) and $FF;
  312. SrcBytes[6]:=(D shr 8) and $FF;
  313. SrcBytes[7]:=(D and $FF);
  314. D:=Src.D2;
  315. SrcBytes[8]:=(D shr 8) and $FF;
  316. SrcBytes[9]:=(D and $FF);
  317. D:=Src.D3;
  318. SrcBytes[10]:=(D shr 8) and $FF;
  319. SrcBytes[11]:=(D and $FF);
  320. For I:=0 to 7 do
  321. SrcBytes[12+i]:=Src.D4[i];
  322. Dest:=TGUID.Create(SrcBytes, 4, TEndian.Big);
  323. if CPUEndian = TEndian.Big then
  324. EqualGUID('BE CPU: Create(Data,True)',Src,Dest)
  325. else
  326. EqualGUIDSwap('LE CPU: Create(Data,True)',Src,Dest);
  327. Dest:=TGUID.Create(SrcBytes, 4, TEndian.Little);
  328. if CPUEndian = TEndian.Big then
  329. EqualGUIDSwap('BE CPU: Create(Data,False)',Src,Dest)
  330. else
  331. EqualGUID('LE CPU : Create(Data,False)',Src,Dest);
  332. end;
  333. Procedure TTestHelpers.TestGUIDHelperCreateString;
  334. Var
  335. Src,Dest : TGUID;
  336. begin
  337. GetGUID(Src);
  338. Dest:=TGUID.Create(GUIDToString(Src));
  339. EqualGUID('Check equals',Src,Dest);
  340. end;
  341. Procedure TTestHelpers.TestGUIDHelperCreateIntegerBytes;
  342. // Class Function Create(A: Integer; B: SmallInt; C: SmallInt; const D: TBytes): TGUID; overload; static;
  343. Var
  344. A,I : Integer;
  345. B,C : Smallint;
  346. D : TBytes;
  347. Dest : TGUID;
  348. begin
  349. A:=1;
  350. B:=2;
  351. C:=3;
  352. D:=Nil;
  353. SetLength(D,8);
  354. For I:=0 to 7 do
  355. D[i]:=4+I;
  356. Dest:=TGuid.Create(A,B,C,D);
  357. AssertEquals('D1',1,Dest.D1);
  358. AssertEquals('D2',2,Dest.D2);
  359. AssertEquals('D3',3,Dest.D3);
  360. For I:=0 to 7 do
  361. AssertEquals('D4['+IntToStr(i)+']',I+4,Dest.D4[i]);
  362. end;
  363. Procedure TTestHelpers.TestGUIDHelperCreateWords;
  364. // Class Function Create(A: Cardinal; B: Word; C: Word; D, E, F, G, H, I, J, K: Byte): TGUID; overload; static;
  365. Var
  366. A,I : Cardinal;
  367. B,C : Word;
  368. Dest : TGUID;
  369. begin
  370. A:=1;
  371. B:=Word($FFFE);
  372. C:=Word($FFFF);
  373. Dest:=TGuid.Create(A,B,C,4,5,6,7,8,9,10,11);
  374. AssertEquals('D1',1,Dest.D1);
  375. AssertEquals('D2',$FFFE,Dest.D2);
  376. AssertEquals('D3',$FFFF,Dest.D3);
  377. For I:=0 to 7 do
  378. AssertEquals('D4['+IntToStr(i)+']',I+4,Dest.D4[i]);
  379. end;
  380. Procedure TTestHelpers.TestGUIDHelperCreateInteger;
  381. // Class Function Create(A: Integer; B: SmallInt; C: SmallInt; D, E, F, G, H, I, J, K: Byte): TGUID; overload; static;
  382. Var
  383. A,I : Integer;
  384. B,C : Smallint;
  385. Dest : TGUID;
  386. begin
  387. A:=1;
  388. B:=Smallint($FFFE);
  389. C:=Smallint($FFFF);
  390. Dest:=TGuid.Create(A,B,C,4,5,6,7,8,9,10,11);
  391. AssertEquals('D1',1,Dest.D1);
  392. AssertEquals('D2',$FFFE,Dest.D2);
  393. AssertEquals('D3',$FFFF,Dest.D3);
  394. For I:=0 to 7 do
  395. AssertEquals('D4['+IntToStr(i)+']',I+4,Dest.D4[i]);
  396. end;
  397. Procedure TTestHelpers.TestGUIDHelperCreateNew;
  398. // Class Function NewGuid: TGUID; static;
  399. Var
  400. Src,Dest : TGuid;
  401. I,J : integer;
  402. begin
  403. // All we can do is check that you don't get the same GUID twice.
  404. Src:=TGuid.NewGuid;
  405. Dest:=TGuid.NewGuid;
  406. I:=0;
  407. Inc(I,Ord(Src.D1<>Dest.D1));
  408. Inc(I,Ord(Src.D2<>Dest.D2));
  409. Inc(I,Ord(Src.D3<>Dest.D3));
  410. For J:=0 to 7 do
  411. Inc(I,Ord(Src.D4[i]<>Dest.D4[i]));
  412. AssertTrue('D1<>D2',I>0);
  413. end;
  414. Procedure TTestHelpers.TestGUIDHelperToByteArray;
  415. Var
  416. Src,Dest : TGuid;
  417. D : TBytes;
  418. begin
  419. // All we can do is check that you don't get the same GUID twice.
  420. Src:=TGuid.NewGuid;
  421. D:=Src.ToByteArray(CPUEndian);
  422. Dest:=TGUID.Create(D,CPUEndian);
  423. EqualGUID('Check equals',Src,Dest);
  424. if CPUEndian=TEndian.Big then
  425. Dest:=TGUID.Create(D,TEndian.Little)
  426. else
  427. Dest:=TGUID.Create(D,TEndian.Big);
  428. EqualGUIDSwap('Swapped, Check equals',Src,Dest);
  429. end;
  430. Procedure TTestHelpers.TestGUIDHelperToString;
  431. // Function ToString: string;
  432. Var
  433. Src : TGuid;
  434. S : String;
  435. begin
  436. CreateGUID(Src);
  437. S:=GuidToString(Src);
  438. AssertEquals('Equal',S,Src.ToString);
  439. Delete(S,1,1);
  440. Delete(S,Length(S),1);
  441. AssertEquals('Equal',S,Src.ToString(True));
  442. end;
  443. Procedure TTestHelpers.TestIsNanDouble;
  444. var
  445. Value: Double;
  446. begin
  447. asm
  448. Value = Number.NaN; // Double.NaN;
  449. end;
  450. AssertEquals('Is Nan',True,Value.IsNan);
  451. end;
  452. Procedure TTestHelpers.TestByteSetBit;
  453. var
  454. Index: TByteBitIndex;
  455. B: Byte;
  456. const
  457. Expected: array[TByteBitIndex] of byte = ($01,$03,$07,$0F,$1F,$3F,$7F,$FF);
  458. begin
  459. // writeln('TestByteSetBit Start');
  460. B := 0;
  461. for Index in TByteBitIndex do
  462. begin
  463. B.SetBit(Index);
  464. AssertEquals('Bit '+IntToStr(Index),Expected[Index],B);
  465. end;
  466. // writeln('TestByteSetBit: OK');
  467. end;
  468. Procedure TTestHelpers.TestByteToggleBit;
  469. var
  470. Index: TByteBitIndex;
  471. B: Byte;
  472. const
  473. Expected: array[TByteBitIndex] of byte = ($01,$03,$07,$0F,$1F,$3F,$7F,$FF);
  474. begin
  475. // writeln('TestByteToggleBit Start');
  476. B := 0;
  477. for Index in TByteBitIndex do
  478. begin
  479. B.ToggleBit(Index);
  480. AssertEquals('Bit '+IntToStr(Index),Expected[Index],B);
  481. end;
  482. // writeln('TestByteToggleBit: OK');
  483. end;
  484. Procedure TTestHelpers.TestByteClearBit;
  485. var
  486. Index: TByteBitIndex;
  487. B: Byte;
  488. const
  489. Expected: array[TByteBitIndex] of byte = ($FE,$FD,$FB,$F7,$EF,$DF,$BF,$7F);
  490. begin
  491. // writeln('TestByteClearBit Start');
  492. for Index in TByteBitIndex do
  493. begin
  494. B := High(Byte);
  495. B.ClearBit(Index);
  496. AssertEquals('Bit '+IntToStr(Index),Expected[Index],B);
  497. end;
  498. // writeln('TestByteClearBit: OK');
  499. end;
  500. Procedure TTestHelpers.TestByteTestBit;
  501. var
  502. Index: TByteBitIndex;
  503. B: Byte;
  504. const
  505. Expected: array[TByteBitIndex] of Boolean = (True,False,True,False,True,False,True,False);
  506. begin
  507. // writeln('TestByteTestBit Start');
  508. B := $55;
  509. for Index in TByteBitIndex do
  510. AssertEquals('Bit '+IntToStr(Index),Expected[Index],B.TestBit(Index));
  511. // writeln('TestByteTestBit: OK');
  512. end;
  513. Procedure TTestHelpers.TestShortIntSetBit;
  514. var
  515. Index: TShortIntBitIndex;
  516. S: ShortInt;
  517. const
  518. Expected: array[TByteBitIndex] of ShortInt = (
  519. ShortInt($01),ShortInt($03),ShortInt($07),ShortInt($0F),
  520. ShortInt($1F),ShortInt($3F),ShortInt($7F),ShortInt($FF));
  521. begin
  522. // writeln('TestShortIntSetBit Start');
  523. S := 0;
  524. for Index in TShortIntBitIndex do
  525. begin
  526. S.SetBit(Index);
  527. AssertEquals('Bit '+IntToStr(Index),Expected[Index],S);
  528. end;
  529. // writeln('TestShortIntSetBit: OK');
  530. end;
  531. Procedure TTestHelpers.TestShortIntToggleBit;
  532. var
  533. Index: TShortIntBitIndex;
  534. S: ShortInt;
  535. const
  536. Expected: array[TByteBitIndex] of ShortInt = (
  537. ShortInt($01),ShortInt($03),ShortInt($07),ShortInt($0F),
  538. ShortInt($1F),ShortInt($3F),ShortInt($7F),ShortInt($FF));
  539. begin
  540. // writeln('TestShortIntToggleBit Start');
  541. S := 0;
  542. for Index in TShortIntBitIndex do
  543. begin
  544. S.ToggleBit(Index);
  545. AssertEquals('Bit '+IntToStr(Index),Expected[Index],S);
  546. end;
  547. // writeln('TestShortIntToggleBit: OK');
  548. end;
  549. Procedure TTestHelpers.TestShortIntClearBit;
  550. var
  551. Index: TShortIntBitIndex;
  552. S: ShortInt;
  553. const
  554. Expected: array[TByteBitIndex] of ShortInt = (
  555. ShortInt($FE),ShortInt($FD),ShortInt($FB),ShortInt($F7),
  556. ShortInt($EF),ShortInt($DF),ShortInt($BF),ShortInt($7F));
  557. begin
  558. // writeln('TestShortIntClearBit Start');
  559. for Index in TShortIntBitIndex do
  560. begin
  561. S := ShortInt($FF);
  562. S.ClearBit(Index);// was Togglebit ?
  563. AssertEquals('Bit '+IntToStr(Index),Expected[Index],S);
  564. end;
  565. // writeln('TestShortIntClearBit: OK');
  566. end;
  567. Procedure TTestHelpers.TestShortIntTestBit;
  568. var
  569. Index: TShortIntBitIndex;
  570. S: ShortInt;
  571. const
  572. Expected: array[TByteBitIndex] of Boolean = (True,False,True,False,True,False,True,False);
  573. begin
  574. // writeln('TestShortIntTestBit Start');
  575. S := ShortInt($55);
  576. for Index in TShortIntBitIndex do
  577. AssertEquals('Bit '+IntToStr(Index),Expected[Index],S.TestBit(Index));
  578. // writeln('TestShortIntTestBit: OK');
  579. end;
  580. Procedure TTestHelpers.TestWordSetBit;
  581. var
  582. Index: TWordBitIndex;
  583. W: Word;
  584. const
  585. Expected: array[TWordBitIndex] of Word = (
  586. $0001,$0003,$0007,$000F,$001F,$003F,$007F,$00FF,
  587. $01FF,$03FF,$07FF,$0FFF,$1FFF,$3FFF,$7FFF,$FFFF);
  588. begin
  589. // writeln('TestWordSetBit Start');
  590. W := 0;
  591. for Index in TWordBitIndex do
  592. begin
  593. W.SetBit(Index);
  594. AssertEquals('Bit '+IntToStr(Index),Expected[Index],W);
  595. end;
  596. // writeln('TestWordSetBit: OK');
  597. end;
  598. Procedure TTestHelpers.TestWordToggleBit;
  599. var
  600. Index: TWordBitIndex;
  601. W: Word;
  602. const
  603. Expected: array[TWordBitIndex] of Word = (
  604. $0001,$0003,$0007,$000F,$001F,$003F,$007F,$00FF,
  605. $01FF,$03FF,$07FF,$0FFF,$1FFF,$3FFF,$7FFF,$FFFF);
  606. begin
  607. // writeln('TestWordToggleBit Start');
  608. W := 0;
  609. for Index in TWordBitIndex do
  610. begin
  611. W.ToggleBit(Index);
  612. AssertEquals('Bit '+IntToStr(Index),Expected[Index],W);
  613. end;
  614. // writeln('TestWordToggleBit: OK');
  615. end;
  616. Procedure TTestHelpers.TestWordClearBit;
  617. var
  618. Index: TWordBitIndex;
  619. W: Word;
  620. const
  621. Expected: array[TWordBitIndex] of Word = (
  622. $FFFE,$FFFD,$FFFB,$FFF7,$FFEF,$FFDF,$FFBF,$FF7F,
  623. $FEFF,$FDFF,$FBFF,$F7FF,$EFFF,$DFFF,$BFFF,$7FFF);
  624. begin
  625. // writeln('TestWordClearBit Start');
  626. for Index in TWordBitIndex do
  627. begin
  628. W := High(Word);
  629. W.ClearBit(Index);
  630. AssertEquals('Bit '+IntToStr(Index),Expected[Index],W);
  631. end;
  632. // writeln('TestWordClearBit: OK');
  633. end;
  634. Procedure TTestHelpers.TestWordTestBit;
  635. var
  636. Index: TWordBitIndex;
  637. W: Word;
  638. const
  639. Expected: array[TWordBitIndex] of Boolean = (True,False,True,False,True,False,True,False,
  640. True,False,True,False,True,False,True,False);
  641. begin
  642. // writeln('TestWordTestBit Start');
  643. W := $5555;
  644. for Index in TWordBitIndex do
  645. AssertEquals('Bit '+IntToStr(Index),Expected[Index],W.TestBit(Index));
  646. // writeln('TestWordTestBit: OK');
  647. end;
  648. Procedure TTestHelpers.TestSmallIntSetBit;
  649. var
  650. Index: TSmallIntBitIndex;
  651. S: SmallInt;
  652. const
  653. Expected: array[TSmallIntBitIndex] of SmallInt = (
  654. SmallInt($0001),SmallInt($0003),SmallInt($0007),SmallInt($000F),
  655. SmallInt($001F),SmallInt($003F),SmallInt($007F),SmallInt($00FF),
  656. SmallInt($01FF),SmallInt($03FF),SmallInt($07FF),SmallInt($0FFF),
  657. SmallInt($1FFF),SmallInt($3FFF),SmallInt($7FFF),SmallInt($FFFF));
  658. begin
  659. // writeln('TestSmallIntSetBit Start');
  660. S := 0;
  661. for Index in TSmallIntBitIndex do
  662. begin
  663. S.SetBit(Index);
  664. AssertEquals('Bit '+IntToStr(Index),Expected[Index],S);
  665. end;
  666. // writeln('TestSmallIntSetBit: OK');
  667. end;
  668. Procedure TTestHelpers.TestSmallIntToggleBit;
  669. var
  670. Index: TSmallIntBitIndex;
  671. S: SmallInt;
  672. const
  673. Expected: array[TSmallIntBitIndex] of SmallInt = (
  674. SmallInt($0001),SmallInt($0003),SmallInt($0007),SmallInt($000F),
  675. SmallInt($001F),SmallInt($003F),SmallInt($007F),SmallInt($00FF),
  676. SmallInt($01FF),SmallInt($03FF),SmallInt($07FF),SmallInt($0FFF),
  677. SmallInt($1FFF),SmallInt($3FFF),SmallInt($7FFF),SmallInt($FFFF));
  678. begin
  679. // writeln('TestSmallIntToggleBit Start');
  680. S := 0;
  681. for Index in TSmallIntBitIndex do
  682. begin
  683. S.ToggleBit(Index);
  684. AssertEquals('Bit '+IntToStr(Index),Expected[Index],S);
  685. end;
  686. // writeln('TestSmallIntToggleBit: OK');
  687. end;
  688. Procedure TTestHelpers.TestSmallIntClearBit;
  689. var
  690. Index: TSmallIntBitIndex;
  691. S: SmallInt;
  692. const
  693. Expected: array[TSmallIntBitIndex] of SmallInt = (
  694. SmallInt($FFFE),SmallInt($FFFD),SmallInt($FFFB),SmallInt($FFF7),
  695. SmallInt($FFEF),SmallInt($FFDF),SmallInt($FFBF),SmallInt($FF7F),
  696. SmallInt($FEFF),SmallInt($FDFF),SmallInt($FBFF),SmallInt($F7FF),
  697. SmallInt($EFFF),SmallInt($DFFF),SmallInt($BFFF),SmallInt($7FFF));
  698. begin
  699. // writeln('TestSmallIntClearBit Start');
  700. for Index in TSmallIntBitIndex do
  701. begin
  702. S := SmallInt($FFFF);
  703. S.ClearBit(Index);
  704. AssertEquals('Bit '+IntToStr(Index),Expected[Index],S);
  705. end;
  706. // writeln('TestSmallIntClearBit: OK');
  707. end;
  708. Procedure TTestHelpers.TestSmallIntTestBit;
  709. var
  710. Index: TSmallIntBitIndex;
  711. S: SmallInt;
  712. const
  713. Expected: array[TSmallIntBitIndex] of Boolean = (True,False,True,False,True,False,True,False,
  714. True,False,True,False,True,False,True,False);
  715. begin
  716. // writeln('TestSmallIntTestBit Start');
  717. S := SMallInt($5555);
  718. for Index in TSmallIntBitIndex do
  719. AssertEquals('Bit '+IntToStr(Index),Expected[Index],S.TestBit(Index));
  720. // writeln('TestSmallIntTestBit: OK');
  721. end;
  722. Procedure TTestHelpers.TestCardinalSetBit;
  723. var
  724. Index: TCardinalBitIndex;
  725. C: Cardinal;
  726. const
  727. Expected: array[TCardinalBitIndex] of Cardinal = (
  728. $00000001,$00000003,$00000007,$0000000F,
  729. $0000001F,$0000003F,$0000007F,$000000FF,
  730. $000001FF,$000003FF,$000007FF,$00000FFF,
  731. $00001FFF,$00003FFF,$00007FFF,$0000FFFF,
  732. $0001FFFF,$0003FFFF,$0007FFFF,$000FFFFF,
  733. $001FFFFF,$003FFFFF,$007FFFFF,$00FFFFFF,
  734. $01FFFFFF,$03FFFFFF,$07FFFFFF,$0FFFFFFF,
  735. $1FFFFFFF,$3FFFFFFF,$7FFFFFFF,$FFFFFFFF);
  736. begin
  737. // writeln('TestCardinalSetBit Start');
  738. C := 0;
  739. for Index in TCardinalBitIndex do
  740. begin
  741. C.SetBit(Index);
  742. AssertEquals('Bit '+IntToStr(Index),Expected[Index],C);
  743. end;
  744. // writeln('TestCardinalSetBit: OK');
  745. end;
  746. Procedure TTestHelpers.TestCardinalToggleBit;
  747. var
  748. Index: TCardinalBitIndex;
  749. C: Cardinal;
  750. const
  751. Expected: array[TCardinalBitIndex] of Cardinal = (
  752. $00000001,$00000003,$00000007,$0000000F,
  753. $0000001F,$0000003F,$0000007F,$000000FF,
  754. $000001FF,$000003FF,$000007FF,$00000FFF,
  755. $00001FFF,$00003FFF,$00007FFF,$0000FFFF,
  756. $0001FFFF,$0003FFFF,$0007FFFF,$000FFFFF,
  757. $001FFFFF,$003FFFFF,$007FFFFF,$00FFFFFF,
  758. $01FFFFFF,$03FFFFFF,$07FFFFFF,$0FFFFFFF,
  759. $1FFFFFFF,$3FFFFFFF,$7FFFFFFF,$FFFFFFFF);
  760. begin
  761. // writeln('TestCardinalToggleBit Start');
  762. C := 0;
  763. for Index in TCardinalBitIndex do
  764. begin
  765. C.ToggleBit(Index);
  766. AssertEquals('Bit '+IntToStr(Index),Expected[Index],C);
  767. end;
  768. // writeln('TestCardinalToggleBit: OK');
  769. end;
  770. Procedure TTestHelpers.TestCardinalClearBit;
  771. var
  772. Index: TCardinalBitIndex;
  773. C: Cardinal;
  774. const
  775. Expected: array[TCardinalBitIndex] of Cardinal = (
  776. $FFFFFFFE,$FFFFFFFD,$FFFFFFFB,$FFFFFFF7,
  777. $FFFFFFEF,$FFFFFFDF,$FFFFFFBF,$FFFFFF7F,
  778. $FFFFFEFF,$FFFFFDFF,$FFFFFBFF,$FFFFF7FF,
  779. $FFFFEFFF,$FFFFDFFF,$FFFFBFFF,$FFFF7FFF,
  780. $FFFEFFFF,$FFFDFFFF,$FFFBFFFF,$FFF7FFFF,
  781. $FFEFFFFF,$FFDFFFFF,$FFBFFFFF,$FF7FFFFF,
  782. $FEFFFFFF,$FDFFFFFF,$FBFFFFFF,$F7FFFFFF,
  783. $EFFFFFFF,$DFFFFFFF,$BFFFFFFF,$7FFFFFFF);
  784. begin
  785. // writeln('TestCardinalClearBit Start');
  786. for Index in TCardinalBitIndex do
  787. begin
  788. C := High(Cardinal);
  789. C.ClearBit(Index);
  790. AssertEquals('Bit '+IntToStr(Index),Expected[Index],C);
  791. end;
  792. // writeln('TestCardinalClearBit: OK');
  793. end;
  794. Procedure TTestHelpers.TestCardinalTestBit;
  795. var
  796. Index: TCardinalBitIndex;
  797. C: Cardinal;
  798. const
  799. Expected: array[TCardinalBitIndex] of Boolean = (
  800. True,False,True,False,True,False,True,False,
  801. True,False,True,False,True,False,True,False,
  802. True,False,True,False,True,False,True,False,
  803. True,False,True,False,True,False,True,False);
  804. begin
  805. // writeln('TestCardinalTestBit Start');
  806. C := $55555555;
  807. for Index in TCardinalBitIndex do
  808. AssertEquals('Bit '+IntToStr(Index),Expected[Index],C.TestBit(Index));
  809. // writeln('TestCardinalTestBit: OK');
  810. end;
  811. Procedure TTestHelpers.TestLongintSetBit;
  812. var
  813. Index: TLongintBitIndex;
  814. L: Longint;
  815. const
  816. Expected: array[TLongintBitIndex] of Longint = (
  817. Longint($00000001),Longint($00000003),Longint($00000007),Longint($0000000F),
  818. Longint($0000001F),Longint($0000003F),Longint($0000007F),Longint($000000FF),
  819. Longint($000001FF),Longint($000003FF),Longint($000007FF),Longint($00000FFF),
  820. Longint($00001FFF),Longint($00003FFF),Longint($00007FFF),Longint($0000FFFF),
  821. Longint($0001FFFF),Longint($0003FFFF),Longint($0007FFFF),Longint($000FFFFF),
  822. Longint($001FFFFF),Longint($003FFFFF),Longint($007FFFFF),Longint($00FFFFFF),
  823. Longint($01FFFFFF),Longint($03FFFFFF),Longint($07FFFFFF),Longint($0FFFFFFF),
  824. Longint($1FFFFFFF),Longint($3FFFFFFF),Longint($7FFFFFFF),Longint($FFFFFFFF));
  825. begin
  826. // writeln('TestLongintSetBit Start');
  827. L := 0;
  828. for Index in TLongintBitIndex do
  829. begin
  830. L.SetBit(Index);
  831. AssertEquals('Bit '+IntToStr(Index),Expected[Index],L);
  832. end;
  833. // writeln('TestLongintSetBit: OK');
  834. end;
  835. Procedure TTestHelpers.TestLongintToggleBit;
  836. var
  837. Index: TLongintBitIndex;
  838. L: Longint;
  839. const
  840. Expected: array[TLongintBitIndex] of Longint = (
  841. Longint($00000001),Longint($00000003),Longint($00000007),Longint($0000000F),
  842. Longint($0000001F),Longint($0000003F),Longint($0000007F),Longint($000000FF),
  843. Longint($000001FF),Longint($000003FF),Longint($000007FF),Longint($00000FFF),
  844. Longint($00001FFF),Longint($00003FFF),Longint($00007FFF),Longint($0000FFFF),
  845. Longint($0001FFFF),Longint($0003FFFF),Longint($0007FFFF),Longint($000FFFFF),
  846. Longint($001FFFFF),Longint($003FFFFF),Longint($007FFFFF),Longint($00FFFFFF),
  847. Longint($01FFFFFF),Longint($03FFFFFF),Longint($07FFFFFF),Longint($0FFFFFFF),
  848. Longint($1FFFFFFF),Longint($3FFFFFFF),Longint($7FFFFFFF),Longint($FFFFFFFF));
  849. begin
  850. // writeln('TestLongintToggleBit Start');
  851. L := 0;
  852. for Index in TLongintBitIndex do
  853. begin
  854. L.ToggleBit(Index);
  855. AssertEquals('Bit '+IntToStr(Index),Expected[Index],L);
  856. end;
  857. // writeln('TestLongintToggleBit: OK');
  858. end;
  859. Procedure TTestHelpers.TestLongintClearBit;
  860. var
  861. Index: TLongintBitIndex;
  862. L: Longint;
  863. const
  864. Expected: array[TLongintBitIndex] of Longint = (
  865. Longint($FFFFFFFE),Longint($FFFFFFFD),Longint($FFFFFFFB),Longint($FFFFFFF7),
  866. Longint($FFFFFFEF),Longint($FFFFFFDF),Longint($FFFFFFBF),Longint($FFFFFF7F),
  867. Longint($FFFFFEFF),Longint($FFFFFDFF),Longint($FFFFFBFF),Longint($FFFFF7FF),
  868. Longint($FFFFEFFF),Longint($FFFFDFFF),Longint($FFFFBFFF),Longint($FFFF7FFF),
  869. Longint($FFFEFFFF),Longint($FFFDFFFF),Longint($FFFBFFFF),Longint($FFF7FFFF),
  870. Longint($FFEFFFFF),Longint($FFDFFFFF),Longint($FFBFFFFF),Longint($FF7FFFFF),
  871. Longint($FEFFFFFF),Longint($FDFFFFFF),Longint($FBFFFFFF),Longint($F7FFFFFF),
  872. Longint($EFFFFFFF),Longint($DFFFFFFF),Longint($BFFFFFFF),Longint($7FFFFFFF));
  873. begin
  874. // writeln('TestLongintClearBit Start');
  875. for Index in TLongintBitIndex do
  876. begin
  877. L := Longint($FFFFFFFF);
  878. L.ClearBit(Index);
  879. AssertEquals('Bit '+IntToStr(Index),Expected[Index],L);
  880. end;
  881. // writeln('TestLongintClearBit: OK');
  882. end;
  883. Procedure TTestHelpers.TestLongintTestBit;
  884. var
  885. Index: TLongintBitIndex;
  886. L: Longint;
  887. const
  888. Expected: array[TLongintBitIndex] of Boolean = (
  889. True,False,True,False,True,False,True,False,
  890. True,False,True,False,True,False,True,False,
  891. True,False,True,False,True,False,True,False,
  892. True,False,True,False,True,False,True,False);
  893. begin
  894. // writeln('TestLongintTestBit Start');
  895. L := Longint($55555555);
  896. for Index in TLongintBitIndex do
  897. AssertEquals('Bit '+IntToStr(Index),Expected[Index],L.TestBit(Index));
  898. // writeln('TestLongintTestBit: OK');
  899. end;
  900. Procedure TTestHelpers.TestNativeUintSetBit;
  901. var
  902. Index: TQWordBitIndex;
  903. Q: NativeUInt;
  904. const
  905. Expected: array[TQWordBitIndex] of NativeUInt = (
  906. $0000000000000001,$0000000000000003,$0000000000000007,$000000000000000F,
  907. $000000000000001F,$000000000000003F,$000000000000007F,$00000000000000FF,
  908. $00000000000001FF,$00000000000003FF,$00000000000007FF,$0000000000000FFF,
  909. $0000000000001FFF,$0000000000003FFF,$0000000000007FFF,$000000000000FFFF,
  910. $000000000001FFFF,$000000000003FFFF,$000000000007FFFF,$00000000000FFFFF,
  911. $00000000001FFFFF,$00000000003FFFFF,$00000000007FFFFF,$0000000000FFFFFF,
  912. $0000000001FFFFFF,$0000000003FFFFFF,$0000000007FFFFFF,$000000000FFFFFFF,
  913. $000000001FFFFFFF,$000000003FFFFFFF,$000000007FFFFFFF,$00000000FFFFFFFF,
  914. $00000001FFFFFFFF,$00000003FFFFFFFF,$00000007FFFFFFFF,$0000000FFFFFFFFF,
  915. $0000001FFFFFFFFF,$0000003FFFFFFFFF,$0000007FFFFFFFFF,$000000FFFFFFFFFF,
  916. $000001FFFFFFFFFF,$000003FFFFFFFFFF,$000007FFFFFFFFFF,$00000FFFFFFFFFFF,
  917. $00001FFFFFFFFFFF,$00003FFFFFFFFFFF,$00007FFFFFFFFFFF,$0000FFFFFFFFFFFF,
  918. $0001FFFFFFFFFFFF,$0003FFFFFFFFFFFF,$0007FFFFFFFFFFFF,$000FFFFFFFFFFFFF,
  919. $001FFFFFFFFFFFFF{,$003FFFFFFFFFFFFF,$007FFFFFFFFFFFFF,$00FFFFFFFFFFFFFF,
  920. $01FFFFFFFFFFFFFF,$03FFFFFFFFFFFFFF,$07FFFFFFFFFFFFFF,$0FFFFFFFFFFFFFFF,
  921. $1FFFFFFFFFFFFFFF,$3FFFFFFFFFFFFFFF,$7FFFFFFFFFFFFFFF,QWORD($FFFFFFFFFFFFFFFF)});
  922. begin
  923. Fail('Not implemented');
  924. // writeln('TestQWordSetBit Start');
  925. Q := 0;
  926. for Index in TQWordBitIndex do
  927. begin
  928. // TODO Q.SetBit(Index);
  929. // AssertEquals('Bit '+IntToStr(Index),Expected[Index],S);
  930. end;
  931. // writeln('TestQWordSetBit: OK');
  932. end;
  933. Procedure TTestHelpers.TestNativeUIntToggleBit;
  934. var
  935. Index: TQWordBitIndex;
  936. Q: NativeUint;
  937. const
  938. Expected: array[TQWordBitIndex] of NativeUInt = (
  939. $0000000000000001,$0000000000000003,$0000000000000007,$000000000000000F,
  940. $000000000000001F,$000000000000003F,$000000000000007F,$00000000000000FF,
  941. $00000000000001FF,$00000000000003FF,$00000000000007FF,$0000000000000FFF,
  942. $0000000000001FFF,$0000000000003FFF,$0000000000007FFF,$000000000000FFFF,
  943. $000000000001FFFF,$000000000003FFFF,$000000000007FFFF,$00000000000FFFFF,
  944. $00000000001FFFFF,$00000000003FFFFF,$00000000007FFFFF,$0000000000FFFFFF,
  945. $0000000001FFFFFF,$0000000003FFFFFF,$0000000007FFFFFF,$000000000FFFFFFF,
  946. $000000001FFFFFFF,$000000003FFFFFFF,$000000007FFFFFFF,$00000000FFFFFFFF,
  947. $00000001FFFFFFFF,$00000003FFFFFFFF,$00000007FFFFFFFF,$0000000FFFFFFFFF,
  948. $0000001FFFFFFFFF,$0000003FFFFFFFFF,$0000007FFFFFFFFF,$000000FFFFFFFFFF,
  949. $000001FFFFFFFFFF,$000003FFFFFFFFFF,$000007FFFFFFFFFF,$00000FFFFFFFFFFF,
  950. $00001FFFFFFFFFFF,$00003FFFFFFFFFFF,$00007FFFFFFFFFFF,$0000FFFFFFFFFFFF,
  951. $0001FFFFFFFFFFFF,$0003FFFFFFFFFFFF,$0007FFFFFFFFFFFF,$000FFFFFFFFFFFFF,
  952. $001FFFFFFFFFFFFF{,$003FFFFFFFFFFFFF,$007FFFFFFFFFFFFF,$00FFFFFFFFFFFFFF,
  953. $01FFFFFFFFFFFFFF,$03FFFFFFFFFFFFFF,$07FFFFFFFFFFFFFF,$0FFFFFFFFFFFFFFF,
  954. $1FFFFFFFFFFFFFFF,$3FFFFFFFFFFFFFFF,$7FFFFFFFFFFFFFFF,QWORD($FFFFFFFFFFFFFFFF)});
  955. begin
  956. Fail('Not implemented');
  957. // writeln('TestQWordToggleBit Start');
  958. Q := 0;
  959. for Index in TQWordBitIndex do
  960. begin
  961. // TODO Q.ToggleBit(Index);
  962. AssertEquals('Bit '+IntToStr(Index),Expected[Index],Q);
  963. end;
  964. // writeln('TestQWordToggleBit: OK');
  965. end;
  966. Procedure TTestHelpers.TestNativeUIntTestBit;
  967. var
  968. Index: TQWordBitIndex;
  969. Q: NativeUint;
  970. const
  971. Expected: array[TQWordBitIndex] of Boolean = (True,False,True,False,True,False,True,False,
  972. True,False,True,False,True,False,True,False,
  973. True,False,True,False,True,False,True,False,
  974. True,False,True,False,True,False,True,False,
  975. True,False,True,False,True,False,True,False,
  976. True,False,True,False,True,False,True,False,
  977. True,False,True,False,True);
  978. begin
  979. Fail('NotImplemented');
  980. // writeln('TestQWordTestBit Start');
  981. Q := $5555555555;
  982. for Index in TQWordBitIndex do
  983. // AssertEquals('Bit '+IntToStr(Index),Expected[Index],Q.TestBit(Index));
  984. // writeln('TestQWordTestBit: OK');
  985. end;
  986. Procedure TTestHelpers.TestNativeIntSetBit;
  987. var
  988. Index: TInt64BitIndex;
  989. I64: NativeInt;
  990. const
  991. Expected: array[TNativeIntBitIndex] of NativeInt = (
  992. NativeInt($0000000000000001),NativeInt($0000000000000003),NativeInt($0000000000000007),NativeInt($000000000000000F),
  993. NativeInt($000000000000001F),NativeInt($000000000000003F),NativeInt($000000000000007F),NativeInt($00000000000000FF),
  994. NativeInt($00000000000001FF),NativeInt($00000000000003FF),NativeInt($00000000000007FF),NativeInt($0000000000000FFF),
  995. NativeInt($0000000000001FFF),NativeInt($0000000000003FFF),NativeInt($0000000000007FFF),NativeInt($000000000000FFFF),
  996. NativeInt($000000000001FFFF),NativeInt($000000000003FFFF),NativeInt($000000000007FFFF),NativeInt($00000000000FFFFF),
  997. NativeInt($00000000001FFFFF),NativeInt($00000000003FFFFF),NativeInt($00000000007FFFFF),NativeInt($0000000000FFFFFF),
  998. NativeInt($0000000001FFFFFF),NativeInt($0000000003FFFFFF),NativeInt($0000000007FFFFFF),NativeInt($000000000FFFFFFF),
  999. NativeInt($000000001FFFFFFF),NativeInt($000000003FFFFFFF),NativeInt($000000007FFFFFFF),NativeInt($00000000FFFFFFFF),
  1000. NativeInt($00000001FFFFFFFF),NativeInt($00000003FFFFFFFF),NativeInt($00000007FFFFFFFF),NativeInt($0000000FFFFFFFFF),
  1001. NativeInt($0000001FFFFFFFFF),NativeInt($0000003FFFFFFFFF),NativeInt($0000007FFFFFFFFF),NativeInt($000000FFFFFFFFFF),
  1002. NativeInt($000001FFFFFFFFFF),NativeInt($000003FFFFFFFFFF),NativeInt($000007FFFFFFFFFF),NativeInt($00000FFFFFFFFFFF),
  1003. NativeInt($00001FFFFFFFFFFF),NativeInt($00003FFFFFFFFFFF),NativeInt($00007FFFFFFFFFFF),NativeInt($0000FFFFFFFFFFFF),
  1004. NativeInt($0001FFFFFFFFFFFF),NativeInt($0003FFFFFFFFFFFF),NativeInt($0007FFFFFFFFFFFF),NativeInt($000FFFFFFFFFFFFF),
  1005. NativeInt($001FFFFFFFFFFFFF));
  1006. begin
  1007. Fail('Not implemented');
  1008. // writeln('TestNativeIntSetBit Start');
  1009. I64 := 0;
  1010. for Index in TNativeIntBitIndex do
  1011. begin
  1012. // TODO I64.SetBit(Index);
  1013. AssertEquals('Bit '+IntToStr(Index),Expected[Index],I64);
  1014. end;
  1015. // writeln('TestNativeIntSetBit: OK');
  1016. end;
  1017. Procedure TTestHelpers.TestNativeIntToggleBit;
  1018. var
  1019. Index: TNativeIntBitIndex;
  1020. I64: NativeInt;
  1021. const
  1022. Expected: array[TNativeIntBitIndex] of NativeInt = (
  1023. NativeInt($0000000000000001),NativeInt($0000000000000003),NativeInt($0000000000000007),NativeInt($000000000000000F),
  1024. NativeInt($000000000000001F),NativeInt($000000000000003F),NativeInt($000000000000007F),NativeInt($00000000000000FF),
  1025. NativeInt($00000000000001FF),NativeInt($00000000000003FF),NativeInt($00000000000007FF),NativeInt($0000000000000FFF),
  1026. NativeInt($0000000000001FFF),NativeInt($0000000000003FFF),NativeInt($0000000000007FFF),NativeInt($000000000000FFFF),
  1027. NativeInt($000000000001FFFF),NativeInt($000000000003FFFF),NativeInt($000000000007FFFF),NativeInt($00000000000FFFFF),
  1028. NativeInt($00000000001FFFFF),NativeInt($00000000003FFFFF),NativeInt($00000000007FFFFF),NativeInt($0000000000FFFFFF),
  1029. NativeInt($0000000001FFFFFF),NativeInt($0000000003FFFFFF),NativeInt($0000000007FFFFFF),NativeInt($000000000FFFFFFF),
  1030. NativeInt($000000001FFFFFFF),NativeInt($000000003FFFFFFF),NativeInt($000000007FFFFFFF),NativeInt($00000000FFFFFFFF),
  1031. NativeInt($00000001FFFFFFFF),NativeInt($00000003FFFFFFFF),NativeInt($00000007FFFFFFFF),NativeInt($0000000FFFFFFFFF),
  1032. NativeInt($0000001FFFFFFFFF),NativeInt($0000003FFFFFFFFF),NativeInt($0000007FFFFFFFFF),NativeInt($000000FFFFFFFFFF),
  1033. NativeInt($000001FFFFFFFFFF),NativeInt($000003FFFFFFFFFF),NativeInt($000007FFFFFFFFFF),NativeInt($00000FFFFFFFFFFF),
  1034. NativeInt($00001FFFFFFFFFFF),NativeInt($00003FFFFFFFFFFF),NativeInt($00007FFFFFFFFFFF),NativeInt($0000FFFFFFFFFFFF),
  1035. NativeInt($0001FFFFFFFFFFFF),NativeInt($0003FFFFFFFFFFFF),NativeInt($0007FFFFFFFFFFFF),NativeInt($000FFFFFFFFFFFFF),
  1036. NativeInt($001FFFFFFFFFFFFF));
  1037. begin
  1038. Fail('Not implemented');
  1039. // writeln('TestNativeIntToggleBit Start');
  1040. I64 := 0;
  1041. for Index in TNativeIntBitIndex do
  1042. begin
  1043. //I64.ToggleBit(Index);
  1044. AssertEquals('Bit '+IntToStr(Index),Expected[Index],I64);
  1045. end;
  1046. // writeln('TestNativeIntToggleBit: OK');
  1047. end;
  1048. Procedure TTestHelpers.TestNativeIntClearBit;
  1049. var
  1050. Index: TNativeIntBitIndex;
  1051. I64: NativeInt;
  1052. begin
  1053. Fail('NotImplemented');
  1054. // for Index in TNativeIntBitIndex do
  1055. begin
  1056. // I64 := NativeInt($FFFFFFFFFFFFFFFF);
  1057. // I64.ClearBit(Index);
  1058. // AssertEquals('Bit '+IntToStr(Index),Expected[Index],I64);
  1059. end;
  1060. // writeln('TestNativeIntClearBit: OK');
  1061. end;
  1062. Procedure TTestHelpers.TestNativeIntTestBit;
  1063. var
  1064. Index: TNativeIntBitIndex;
  1065. I64: NativeInt;
  1066. const
  1067. Expected: array[TNativeIntBitIndex] of Boolean = (True,False,True,False,True,False,True,False,
  1068. True,False,True,False,True,False,True,False,
  1069. True,False,True,False,True,False,True,False,
  1070. True,False,True,False,True,False,True,False,
  1071. True,False,True,False,True,False,True,False,
  1072. True,False,True,False,True,False,True,False,
  1073. True,False,True,False,True);
  1074. begin
  1075. // writeln('TestNativeIntTestBit Start');
  1076. I64 := NativeInt($5555555555555);
  1077. for Index in TNativeIntBitIndex do
  1078. // AssertEquals('Bit '+IntToStr(Index),Expected[Index],I64.TestBit(Index));
  1079. // writeln('TestNativeIntTestBit: OK');
  1080. end;
  1081. initialization
  1082. RegisterTest(TTestHelpers);
  1083. end.