Skip to content

Commit 7797e67

Browse files
committed
Merge pull request #215 from rpottsoh/updateBinarySearch
2 parents 7624d9c + 01027c3 commit 7797e67

File tree

2 files changed

+59
-37
lines changed

2 files changed

+59
-37
lines changed

exercises/binary-search/uBinarySearchExample.pas

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ implementation
1515
class function BinarySearch.SearchHelper(input: TArray<Integer>; target: Integer; minIndex: Integer; maxIndex: Integer): integer;
1616
var middleIndex: integer;
1717
begin
18+
if minIndex > maxIndex then
19+
begin
20+
result := -1;
21+
exit;
22+
end;
23+
1824
middleIndex := (minIndex + maxIndex) div 2;
1925

2026
if (input[middleIndex] = target) then

exercises/binary-search/uBinarySearchTest.pas

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,66 @@ interface
44
uses
55
DUnitX.TestFramework;
66

7+
const
8+
CanonicalVersion = '1.1.0';
9+
710
type
811

912
[TestFixture]
1013
TBinarySearchTest = class(TObject)
1114
public
1215
[Test]
13-
// [Ignore('Comment the "[Ignore]" statement to run the test')]
14-
procedure Should_return_minus_one_when_an_empty_array_is_searched;
16+
// [Ignore('Comment the "[Ignore]" statement to run the test')]
17+
procedure Finds_a_value_in_an_array_with_one_element;
1518

1619
[Test]
1720
[Ignore]
18-
procedure Should_be_able_to_find_a_value_in_a_single_element_array_with_one_access;
21+
procedure Finds_a_value_in_the_middle_of_an_array;
1922

2023
[Test]
2124
[Ignore]
22-
procedure Should_return_minus_one_if_a_value_is_less_than_the_element_in_a_single_element_array;
25+
procedure Finds_a_value_at_the_beginning_of_an_array;
2326

2427
[Test]
2528
[Ignore]
26-
procedure Should_return_minus_one_if_a_value_is_greater_than_the_element_in_a_single_element_array;
29+
procedure Finds_a_value_at_the_end_of_an_array;
2730

2831
[Test]
2932
[Ignore]
30-
procedure Should_find_an_element_in_a_longer_array;
33+
procedure Finds_a_value_in_an_array_of_odd_length;
3134

3235
[Test]
3336
[Ignore]
34-
procedure Should_find_elements_at_the_beginning_of_an_array;
37+
procedure Finds_a_value_in_an_array_of_even_length;
3538

3639
[Test]
3740
[Ignore]
38-
procedure Should_find_elements_at_the_end_of_an_array;
41+
procedure Identifies_that_a_value_is_not_included_in_the_array;
3942

4043
[Test]
4144
[Ignore]
42-
procedure Should_return_minus_one_if_a_value_is_less_than_all_elements_in_a_long_array;
45+
procedure A_value_smaller_than_the_arrays_smallest_value_is_not_included;
4346

4447
[Test]
4548
[Ignore]
46-
procedure Should_return_minus_one_if_a_value_is_greater_than_all_elements_in_a_long_array;
49+
procedure A_value_larger_than_the_arrays_largest_value_is_not_included;
50+
51+
[Test]
52+
[Ignore]
53+
procedure Nothing_is_included_in_an_empty_array;
4754
end;
4855

4956
implementation
5057
uses System.Generics.Collections, uBinarySearch;
5158

52-
procedure TBinarySearchTest.Should_return_minus_one_when_an_empty_array_is_searched;
59+
procedure TBinarySearchTest.Nothing_is_included_in_an_empty_array;
5360
var input: TArray<Integer>;
5461
begin
5562
SetLength(input, 0);
5663
Assert.AreEqual(-1, BinarySearch.Search(input, 6));
5764
end;
5865

59-
procedure TBinarySearchTest.Should_be_able_to_find_a_value_in_a_single_element_array_with_one_access;
66+
procedure TBinarySearchTest.Finds_a_value_in_an_array_with_one_element;
6067
var input: TArray<integer>;
6168
begin
6269
SetLength(input, 1);
@@ -65,67 +72,76 @@ procedure TBinarySearchTest.Should_be_able_to_find_a_value_in_a_single_element_a
6572
Assert.AreEqual(0, BinarySearch.Search(input, 6));
6673
end;
6774

68-
procedure TBinarySearchTest.Should_return_minus_one_if_a_value_is_less_than_the_element_in_a_single_element_array;
69-
var input: TArray<integer>;
75+
procedure TBinarySearchTest.Finds_a_value_in_the_middle_of_an_array;
76+
var inputList: TList<integer>;
7077
begin
71-
SetLength(input, 1);
72-
input[0] := 94;
78+
inputList := TList<integer>.Create;
79+
inputList.AddRange([1, 3, 4, 6, 8, 9, 11]);
7380

74-
Assert.AreEqual(-1, BinarySearch.Search(input, 6));
81+
Assert.AreEqual(3, BinarySearch.Search(inputList.ToArray, 6));
7582
end;
7683

77-
procedure TBinarySearchTest.Should_return_minus_one_if_a_value_is_greater_than_the_element_in_a_single_element_array;
78-
var input: TArray<integer>;
84+
procedure TBinarySearchTest.Finds_a_value_at_the_beginning_of_an_array;
85+
var inputList: TList<integer>;
7986
begin
80-
SetLength(input, 1);
81-
input[0] := 94;
87+
inputList := TList<integer>.Create;
88+
inputList.AddRange([1, 3, 4, 6, 8, 9, 11]);
89+
90+
Assert.AreEqual(0, BinarySearch.Search(inputList.ToArray, 1));
91+
end;
92+
93+
procedure TBinarySearchTest.Finds_a_value_at_the_end_of_an_array;
94+
var inputList: TList<integer>;
95+
begin
96+
inputList := TList<integer>.Create;
97+
inputList.AddRange([1, 3, 4, 6, 8, 9, 11]);
8298

83-
Assert.AreEqual(-1, BinarySearch.Search(input, 602));
99+
Assert.AreEqual(6, BinarySearch.Search(inputList.ToArray, 11));
84100
end;
85101

86-
procedure TBinarySearchTest.Should_find_an_element_in_a_longer_array;
102+
procedure TBinarySearchTest.Finds_a_value_in_an_array_of_odd_length;
87103
var inputList: TList<integer>;
88104
begin
89105
inputList := TList<integer>.Create;
90-
inputList.AddRange([6, 67, 123, 345, 456, 457, 490, 2002, 54321, 54322]);
106+
inputList.AddRange([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634]);
91107

92-
Assert.AreEqual(7, BinarySearch.Search(inputList.ToArray, 2002));
108+
Assert.AreEqual(9, BinarySearch.Search(inputList.ToArray, 144));
93109
end;
94110

95-
procedure TBinarySearchTest.Should_find_elements_at_the_beginning_of_an_array;
111+
procedure TBinarySearchTest.Finds_a_value_in_an_array_of_even_length;
96112
var inputList: TList<integer>;
97113
begin
98114
inputList := TList<integer>.Create;
99-
inputList.AddRange([6, 67, 123, 345, 456, 457, 490, 2002, 54321, 54322]);
115+
inputList.AddRange([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]);
100116

101-
Assert.AreEqual(0, BinarySearch.Search(inputList.ToArray, 6));
117+
Assert.AreEqual(5, BinarySearch.Search(inputList.ToArray, 21));
102118
end;
103119

104-
procedure TBinarySearchTest.Should_find_elements_at_the_end_of_an_array;
120+
procedure TBinarySearchTest.Identifies_that_a_value_is_not_included_in_the_array;
105121
var inputList: TList<integer>;
106122
begin
107123
inputList := TList<integer>.Create;
108-
inputList.AddRange([6, 67, 123, 345, 456, 457, 490, 2002, 54321, 54322]);
124+
inputList.AddRange([1, 3, 4, 6, 8, 9, 11]);
109125

110-
Assert.AreEqual(9, BinarySearch.Search(inputList.ToArray, 54322));
126+
Assert.AreEqual(-1, BinarySearch.Search(inputList.ToArray, 7));
111127
end;
112128

113-
procedure TBinarySearchTest.Should_return_minus_one_if_a_value_is_less_than_all_elements_in_a_long_array;
129+
procedure TBinarySearchTest.A_value_larger_than_the_arrays_largest_value_is_not_included;
114130
var inputList: TList<integer>;
115131
begin
116132
inputList := TList<integer>.Create;
117-
inputList.AddRange([6, 67, 123, 345, 456, 457, 490, 2002, 54321, 54322]);
133+
inputList.AddRange([1, 3, 4, 6, 8, 9, 11]);
118134

119-
Assert.AreEqual(-1, BinarySearch.Search(inputList.ToArray, 2));
135+
Assert.AreEqual(-1, BinarySearch.Search(inputList.ToArray, 13));
120136
end;
121137

122-
procedure TBinarySearchTest.Should_return_minus_one_if_a_value_is_greater_than_all_elements_in_a_long_array;
138+
procedure TBinarySearchTest.A_value_smaller_than_the_arrays_smallest_value_is_not_included;
123139
var inputList: TList<integer>;
124140
begin
125141
inputList := TList<integer>.Create;
126-
inputList.AddRange([6, 67, 123, 345, 456, 457, 490, 2002, 54321, 54322]);
142+
inputList.AddRange([1, 3, 4, 6, 8, 9, 11]);
127143

128-
Assert.AreEqual(-1, BinarySearch.Search(inputList.ToArray, 54323));
144+
Assert.AreEqual(-1, BinarySearch.Search(inputList.ToArray, 0));
129145
end;
130146

131147
initialization

0 commit comments

Comments
 (0)