tests.generics.arrayhelper.pas 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 tests.generics.arrayhelper;
  17. {$mode delphi}
  18. interface
  19. uses
  20. fpcunit, testregistry, testutils,
  21. Classes, SysUtils, Generics.Collections;
  22. type
  23. { TTestArrayHelper }
  24. TTestArrayHelper = class(TTestCase)
  25. protected
  26. procedure CheckBinarySearch(const AArray: TArray<Integer>;
  27. AValue: Integer; AExpectedResult: boolean; out ASearchResult: TBinarySearchResult);
  28. procedure CheckSearchResult(const ASearchResult: TBinarySearchResult;
  29. AValue: Integer; ACandidateIndex, AFoundIndex: SizeInt; ACompareResult: Boolean);
  30. published
  31. procedure Test_BinarySearch_Integers;
  32. procedure Test_BinarySearch_EmptyArray;
  33. end;
  34. implementation
  35. { TTestArrayHelper }
  36. procedure TTestArrayHelper.CheckBinarySearch(const AArray: TArray<Integer>;
  37. AValue: Integer; AExpectedResult: boolean; out
  38. ASearchResult: TBinarySearchResult);
  39. begin
  40. CheckEquals(AExpectedResult,
  41. TArrayHelper<Integer>.BinarySearch(AArray,AValue,ASearchResult),
  42. 'Wrong BinarySearch result for ' + AValue.ToString);
  43. end;
  44. procedure TTestArrayHelper.CheckSearchResult(const
  45. ASearchResult: TBinarySearchResult; AValue: Integer; ACandidateIndex,
  46. AFoundIndex: SizeInt; ACompareResult: Boolean);
  47. begin
  48. with ASearchResult do
  49. begin
  50. CheckEquals(ACandidateIndex, CandidateIndex, 'Wrong binary search result (CandidateIndex) for ' + AValue.ToString);
  51. CheckEquals(AFoundIndex, FoundIndex, 'Wrong binary search result (FoundIndex) for ' + AValue.ToString);
  52. Check(ACompareResult, 'Wrong binary search result (CompareResult) for ' + AValue.ToString);
  53. end;
  54. end;
  55. procedure TTestArrayHelper.Test_BinarySearch_Integers;
  56. var
  57. a: TArray<Integer>;
  58. LSearchResult: TBinarySearchResult;
  59. begin
  60. a := TArray<Integer>.Create(1,3,5,7,9,11,13,15,20);
  61. CheckBinarySearch(a, 10, False, LSearchResult);
  62. CheckSearchResult(LSearchResult, 10, 5, -1, LSearchResult.CompareResult>0);
  63. CheckBinarySearch(a, 20, True, LSearchResult);
  64. CheckSearchResult(LSearchResult, 20, 8, 8, LSearchResult.CompareResult=0);
  65. end;
  66. procedure TTestArrayHelper.Test_BinarySearch_EmptyArray;
  67. var
  68. LSearchResult: TBinarySearchResult;
  69. begin
  70. CheckBinarySearch(nil, 1, False, LSearchResult);
  71. CheckSearchResult(LSearchResult, 1, -1, -1, LSearchResult.CompareResult=0);
  72. end;
  73. begin
  74. RegisterTest(TTestArrayHelper);
  75. end.