testinterlocked.pp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. program TInterlocked_tests;
  2. {$mode Delphi}
  3. uses
  4. SysUtils, SyncObjs, Classes;
  5. var
  6. i32: Longint;
  7. New32, Old32: Longint;
  8. i64: Int64;
  9. New64, Old64: Int64;
  10. Changed, OldBitValue: Boolean;
  11. list1, list2, oldlist: TStringList;
  12. d1, d2, dOld: Double;
  13. s1, s2, sOld: Single;
  14. begin
  15. writeln('start testing of TInterlocked methods');
  16. {* test all kinds of Longint usage *}
  17. i32 := 12;
  18. New32 := TInterlocked.Increment(i32);
  19. if New32 <> 13 then halt(1);
  20. if i32 <> 13 then halt(2);
  21. New32 := TInterlocked.Decrement(i32);
  22. if New32 <> 12 then halt(3);
  23. if i32 <> 12 then halt(4);
  24. New32 := TInterlocked.Add(i32, 12);
  25. if New32 <> 24 then halt(5);
  26. if i32 <> 24 then halt(6);
  27. Old32 := TInterlocked.CompareExchange(i32, 36, 24);
  28. if Old32 <> 24 then halt(7);
  29. if i32 <> 36 then halt(8);
  30. Old32 := TInterlocked.CompareExchange(i32, 48, 36, Changed);
  31. if Old32 <> 36 then halt(9);
  32. if Changed <> True then halt(10);
  33. if i32 <> 48 then halt(11);
  34. Old32 := TInterlocked.CompareExchange(i32, 123, 96, Changed);
  35. if Old32 <> 48 then halt(12);
  36. if Changed <> False then halt(13);
  37. if i32 <> 48 then halt(14);
  38. Old32 := TInterlocked.Exchange(i32, 96);
  39. if Old32 <> 48 then halt(15);
  40. if i32 <> 96 then halt(15);
  41. {$ifdef cpu64}
  42. {* test all kinds of Int64 usage *}
  43. i64 := 12;
  44. New64 := TInterlocked.Increment(i64);
  45. if New64 <> 13 then halt(20);
  46. if i64 <> 13 then halt(21);
  47. New64 := TInterlocked.Decrement(i64);
  48. if New64 <> 12 then halt(22);
  49. if i64 <> 12 then halt(23);
  50. New64 := TInterlocked.Add(i64, 12);
  51. if New64 <> 24 then halt(24);
  52. if i64 <> 24 then halt(25);
  53. Old64 := TInterlocked.CompareExchange(i64, 36, 24);
  54. if Old64 <> 24 then halt(26);
  55. if i64 <> 36 then halt(27);
  56. Old64 := TInterlocked.Exchange(i64, 48);
  57. if Old64 <> 36 then halt(28);
  58. if i64 <> 48 then halt(29);
  59. Old64 := TInterlocked.Read(i64);
  60. if Old64 <> 48 then halt(30);
  61. if i64 <> 48 then halt(31);
  62. {$endif}
  63. {* test all kinds of TObject and generic class usage *}
  64. list1 := TStringList.Create;
  65. list2 := TStringList.Create;
  66. try
  67. list1.Add('A');
  68. list2.Add('B');
  69. list2.Add('C');
  70. { TObject }
  71. oldlist := TStringList(TInterlocked.CompareExchange(TObject(list1), TObject(list2), TObject(list1)));
  72. if list1 <> list2 then halt(32);
  73. if oldlist.Count = list1.Count then halt(33);
  74. if oldlist.Count = list2.Count then halt(34);
  75. oldlist := TStringList(TInterlocked.Exchange(TObject(list1), TObject(oldlist)));
  76. if oldlist <> list2 then halt(35);
  77. if list1.Count <> 1 then halt(36);
  78. if list2.Count <> 2 then halt(37);
  79. { generic class }
  80. oldlist := TInterlocked.CompareExchange<TStringList>(list1, list2, list1);
  81. if list1 <> list2 then halt(38);
  82. if oldlist.Count = list1.Count then halt(39);
  83. if oldlist.Count = list2.Count then halt(40);
  84. oldlist := TInterlocked.Exchange<TStringList>(list1, oldlist);
  85. if oldlist <> list2 then halt(41);
  86. if list1.Count <> 1 then halt(42);
  87. if list2.Count <> 2 then halt(43);
  88. finally
  89. list1.Free;
  90. list2.Free;
  91. end;
  92. writeln('tests passed so far');
  93. {$ifdef cpu64}
  94. {* test all kinds of Double usage *}
  95. d1 := Double(3.14);
  96. d2 := Double(6.28);
  97. dOld := TInterlocked.CompareExchange(d1, d2, d1);
  98. if dOld <> Double(3.14) then halt(44);
  99. if d1 = Double(3.14) then halt(45);
  100. if d1 <> d2 then halt(46);
  101. d1 := dOld;
  102. dOld := TInterlocked.Exchange(d1, d2);
  103. if dOld <> Double(3.14) then halt(47);
  104. if d1 <> Double(6.28) then halt(48);
  105. if d1 <> d2 then halt(49);
  106. dOld := TInterlocked.CompareExchange(d1, dOld, d2);
  107. if dOld <> Double(6.28) then halt(50);
  108. if d1 <> Double(3.14) then halt(51);
  109. if d1 = d2 then halt(52);
  110. {$endif}
  111. {* test all kinds of Single usage *}
  112. s1 := Single(3.14);
  113. s2 := Single(6.28);
  114. sOld := TInterlocked.CompareExchange(s1, s2, s1);
  115. if sOld <> Single(3.14) then halt(53);
  116. if s1 = Single(3.14) then halt(54);
  117. if s1 <> s2 then halt(55);
  118. sOld := TInterlocked.CompareExchange(s1, sOld, s2);
  119. if sOld <> Single(6.28) then halt(56);
  120. if s1 <> Single(3.14) then halt(57);
  121. if s1 = s2 then halt(58);
  122. sOld := TInterlocked.Exchange(s2, s1);
  123. if sOld <> Single(6.28) then halt(59);
  124. if s1 <> Single(3.14) then halt(60);
  125. if s1 <> s2 then halt(61);
  126. {* test BitTestAndClear usage *}
  127. i32 := 96;
  128. OldBitValue := TInterlocked.BitTestAndClear(i32, 6);
  129. if OldBitValue <> True then halt(62);
  130. if i32 <> 32 then halt(63);
  131. OldBitValue := TInterlocked.BitTestAndClear(i32, 6);
  132. if OldBitValue <> False then halt(64);
  133. if i32 <> 32 then halt(65);
  134. {* test BitTestAndSet usage *}
  135. OldBitValue := TInterlocked.BitTestAndSet(i32, 6);
  136. if OldBitValue <> False then halt(66);
  137. if i32 <> 96 then halt(67);
  138. OldBitValue := TInterlocked.BitTestAndSet(i32, 6);
  139. if OldBitValue <> True then halt(68);
  140. if i32 <> 96 then halt(69);
  141. writeln('testing of TInterlocked methods ended');
  142. end.