diff --git a/exercises/hamming/uHammingExample.pas b/exercises/hamming/uHammingExample.pas index f9459dfb..22511ac0 100644 --- a/exercises/hamming/uHammingExample.pas +++ b/exercises/hamming/uHammingExample.pas @@ -12,12 +12,18 @@ implementation uses System.SysUtils; class function THamming.Distance(strand1, strand2: string): integer; -var i: integer; begin + if strand1.IsEmpty and not strand2.IsEmpty then + raise EArgumentException.Create('error: left strand must not be empty'); + + if not strand1.IsEmpty and strand2.IsEmpty then + raise EArgumentException.Create('error: right strand must not be empty'); + if strand1.Length <> strand2.Length then raise EArgumentException.Create('error: left and right strands must be of equal length'); + result := 0; - for i := Low(strand1) to High(strand1) do + for var i := Low(strand1) to High(strand1) do result := result + ord(strand1[i] <> strand2[i]); end; diff --git a/exercises/hamming/uHammingTests.pas b/exercises/hamming/uHammingTests.pas index 46f84ec7..74fdccde 100644 --- a/exercises/hamming/uHammingTests.pas +++ b/exercises/hamming/uHammingTests.pas @@ -5,14 +5,14 @@ interface DUnitX.TestFramework; const - CanonicalVersion = '2.2.0'; + CanonicalVersion = '2.3.0'; type [TestFixture] HammingTests = class(TObject) public [Test] -// [Ignore('Comment the "[Ignore]" statement to run the test')] +// [Ignore('Comment the "[Ignore]" statement to run the test')] procedure empty_strands; [Test] @@ -38,6 +38,14 @@ HammingTests = class(TObject) [Test] [Ignore] procedure disallow_second_strand_longer; + + [Test] + [Ignore] + procedure disallow_left_empty_strand; + + [Test] + [Ignore] + procedure disallow_right_empty_strand; end; implementation @@ -78,6 +86,26 @@ procedure HammingTests.disallow_first_strand_longer; Assert.WillRaiseWithMessage(MyProc, EArgumentException, 'error: left and right strands must be of equal length'); end; +procedure HammingTests.disallow_left_empty_strand; +var MyProc: TTestLocalMethod; +begin + MyProc := procedure + begin + THamming.Distance('', 'G'); + end; + Assert.WillRaiseWithMessage(MyProc, EArgumentException, 'error: left strand must not be empty'); +end; + +procedure HammingTests.disallow_right_empty_strand; +var MyProc: TTestLocalMethod; +begin + MyProc := procedure + begin + THamming.Distance('G', ''); + end; + Assert.WillRaiseWithMessage(MyProc, EArgumentException, 'error: right strand must not be empty'); +end; + procedure HammingTests.disallow_second_strand_longer; var MyProc: TTestLocalMethod; begin