tcgenarrayhelper.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. {
  2. This file is part of the Free Pascal/NewPascal run time library.
  3. Copyright (c) 2018 by Maciej Izak (hnb),
  4. member of the NewPascal development team (http://newpascal.org)
  5. Copyright(c) 2004-2018 DaThoX
  6. It contains tests for the Free Pascal generics library
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. Acknowledgment
  13. Thanks to Sphere 10 Software (http://sphere10.com) for sponsoring
  14. many new types, tests and major refactoring of entire library
  15. **********************************************************************}
  16. unit tcgenarrayhelper;
  17. {$mode delphi}
  18. interface
  19. uses
  20. fpcunit, testregistry, Classes, SysUtils, Generics.Defaults, Generics.Collections;
  21. type
  22. { TTestArrayHelper }
  23. TTestArrayHelper = class(TTestCase)
  24. protected
  25. procedure CheckBinarySearch(constref AArray: TArray<Integer>;
  26. AValue: Integer; AExpectedResult: boolean; out ASearchResult: TBinarySearchResult); overload;
  27. procedure CheckBinarySearch(constref AArray: TArray<Integer>;
  28. AValue: Integer; AExpectedResult: boolean; out ASearchResult: TBinarySearchResult; const AComparer: IComparer<Integer>);overload;
  29. procedure CheckSearchResult(constref ASearchResult: TBinarySearchResult;
  30. AValue: Integer; ACandidateIndex, AFoundIndex: SizeInt; ACompareResult: Boolean);
  31. published
  32. procedure Test_BinarySearch_IntegersCompare;
  33. procedure Test_BinarySearch_EmptyArrayCompare;
  34. procedure Test_BinarySearch_Integers;
  35. procedure Test_BinarySearch_EmptyArray;
  36. end;
  37. implementation
  38. { TTestArrayHelper }
  39. procedure TTestArrayHelper.CheckBinarySearch(constref AArray: TArray<Integer>;
  40. AValue: Integer; AExpectedResult: boolean; out
  41. ASearchResult: TBinarySearchResult; const AComparer: IComparer<Integer>);
  42. begin
  43. CheckEquals(AExpectedResult,
  44. TArrayHelper<Integer>.BinarySearch(AArray,AValue,ASearchResult,aComparer,0,Length(AArray)),
  45. 'Wrong BinarySearch result for ' + AValue.ToString);
  46. end;
  47. procedure TTestArrayHelper.CheckBinarySearch(constref AArray: TArray<Integer>;
  48. AValue: Integer; AExpectedResult: boolean; out
  49. ASearchResult: TBinarySearchResult);
  50. begin
  51. Fail('Overload without comparer not supported');
  52. (*
  53. *)
  54. end;
  55. procedure TTestArrayHelper.CheckSearchResult(constref
  56. ASearchResult: TBinarySearchResult; AValue: Integer; ACandidateIndex,
  57. AFoundIndex: SizeInt; ACompareResult: Boolean);
  58. begin
  59. with ASearchResult do
  60. begin
  61. CheckEquals(ACandidateIndex, CandidateIndex, 'Wrong binary search result (CandidateIndex) for ' + AValue.ToString);
  62. CheckEquals(AFoundIndex, FoundIndex, 'Wrong binary search result (FoundIndex) for ' + AValue.ToString);
  63. Check(ACompareResult, 'Wrong binary search result (CompareResult) for ' + AValue.ToString);
  64. end;
  65. end;
  66. procedure TTestArrayHelper.Test_BinarySearch_IntegersCompare;
  67. var
  68. a: TArray<Integer>;
  69. LSearchResult: TBinarySearchResult;
  70. aComparer : IComparer<Integer>;
  71. begin
  72. a:=[1,3,5,7,9,11,13,15,20];
  73. aComparer:=TComparer<Integer>.Construct(function (Const a,b : integer) : integer
  74. begin
  75. Result:=a-b;
  76. end);
  77. CheckBinarySearch(a, 10, False, LSearchResult, aComparer);
  78. CheckSearchResult(LSearchResult, 10, 5, -1, LSearchResult.CompareResult>0);
  79. CheckBinarySearch(a, 20, True, LSearchResult,aComparer);
  80. CheckSearchResult(LSearchResult, 20, 8, 8, LSearchResult.CompareResult=0);
  81. end;
  82. procedure TTestArrayHelper.Test_BinarySearch_EmptyArrayCompare;
  83. var
  84. LSearchResult: TBinarySearchResult;
  85. aComparer : IComparer<Integer>;
  86. begin
  87. aComparer:=TComparer<Integer>.Construct(function (Const a,b : integer) : integer
  88. begin
  89. Result:=a-b;
  90. end);
  91. CheckBinarySearch(nil, 1, False, LSearchResult,aComparer);
  92. CheckSearchResult(LSearchResult, 1, -1, -1, LSearchResult.CompareResult=0);
  93. end;
  94. procedure TTestArrayHelper.Test_BinarySearch_Integers;
  95. var
  96. a: TArray<Integer>;
  97. LSearchResult: TBinarySearchResult;
  98. begin
  99. a:=[1,3,5,7,9,11,13,15,20];
  100. CheckBinarySearch(a, 10, False, LSearchResult);
  101. CheckSearchResult(LSearchResult, 10, 5, -1, LSearchResult.CompareResult>0);
  102. CheckBinarySearch(a, 20, True, LSearchResult);
  103. CheckSearchResult(LSearchResult, 20, 8, 8, LSearchResult.CompareResult=0);
  104. end;
  105. procedure TTestArrayHelper.Test_BinarySearch_EmptyArray;
  106. var
  107. LSearchResult: TBinarySearchResult;
  108. begin
  109. CheckBinarySearch(nil, 1, False, LSearchResult);
  110. CheckSearchResult(LSearchResult, 1, -1, -1, LSearchResult.CompareResult=0);
  111. end;
  112. begin
  113. RegisterTest(TTestArrayHelper);
  114. end.