testinterlocked.pp 4.5 KB

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