tests.generics.arrayhelper.pas 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2018 by Maciej Izak (hnb),
  4. member of the Free Pascal development team
  5. It contains tests for the Free Pascal generics library
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. Acknowledgment
  12. Thanks to Sphere 10 Software (http://sphere10.com) for sponsoring
  13. many new types, tests and major refactoring of entire library
  14. **********************************************************************}
  15. unit tests.generics.arrayhelper;
  16. {$mode delphi}
  17. interface
  18. uses
  19. fpcunit, testregistry, testutils,
  20. Classes, SysUtils, 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);
  27. procedure CheckSearchResult(constref ASearchResult: TBinarySearchResult;
  28. AValue: Integer; ACandidateIndex, AFoundIndex: SizeInt; ACompareResult: Boolean);
  29. published
  30. procedure Test_BinarySearch_Integers;
  31. procedure Test_BinarySearch_EmptyArray;
  32. end;
  33. implementation
  34. { TTestArrayHelper }
  35. procedure TTestArrayHelper.CheckBinarySearch(constref AArray: TArray<Integer>;
  36. AValue: Integer; AExpectedResult: boolean; out
  37. ASearchResult: TBinarySearchResult);
  38. begin
  39. CheckEquals(AExpectedResult,
  40. TArrayHelper<Integer>.BinarySearch(AArray,AValue,ASearchResult),
  41. 'Wrong BinarySearch result for ' + AValue.ToString);
  42. end;
  43. procedure TTestArrayHelper.CheckSearchResult(constref
  44. ASearchResult: TBinarySearchResult; AValue: Integer; ACandidateIndex,
  45. AFoundIndex: SizeInt; ACompareResult: Boolean);
  46. begin
  47. with ASearchResult do
  48. begin
  49. CheckEquals(ACandidateIndex, CandidateIndex, 'Wrong binary search result (CandidateIndex) for ' + AValue.ToString);
  50. CheckEquals(AFoundIndex, FoundIndex, 'Wrong binary search result (FoundIndex) for ' + AValue.ToString);
  51. Check(ACompareResult, 'Wrong binary search result (CompareResult) for ' + AValue.ToString);
  52. end;
  53. end;
  54. procedure TTestArrayHelper.Test_BinarySearch_Integers;
  55. var
  56. a: TArray<Integer>;
  57. LSearchResult: TBinarySearchResult;
  58. begin
  59. a := TArray<Integer>.Create(1,3,5,7,9,11,13,15,20);
  60. CheckBinarySearch(a, 10, False, LSearchResult);
  61. CheckSearchResult(LSearchResult, 10, 5, -1, LSearchResult.CompareResult>0);
  62. CheckBinarySearch(a, 20, True, LSearchResult);
  63. CheckSearchResult(LSearchResult, 20, 8, 8, LSearchResult.CompareResult=0);
  64. end;
  65. procedure TTestArrayHelper.Test_BinarySearch_EmptyArray;
  66. var
  67. LSearchResult: TBinarySearchResult;
  68. begin
  69. CheckBinarySearch(nil, 1, False, LSearchResult);
  70. CheckSearchResult(LSearchResult, 1, -1, -1, LSearchResult.CompareResult=0);
  71. end;
  72. begin
  73. RegisterTest(TTestArrayHelper);
  74. end.