1
exercism fetch delphi nucleotide-count

uNucleotideCountTest.pas

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
unit uNucleotideCountTest;

interface
uses
  DUnitX.TestFramework, System.Generics.Collections;

type

  [TestFixture('count all nucleotides in a strand')]
  NucleoTideCountTest = class(TObject)
  public
    [Test]
    procedure Validate_CompareDictionaries;

    [Test]
//  [Ignore('Comment the "[Ignore]" statement to run the test')]
    procedure empty_strand;

    [Test]
    [Ignore]
    procedure can_count_one_nucleotide_in_single_character_input;

    [Test]
    [Ignore]
    procedure strand_with_repeated_nucleotide;

    [Test]
    [Ignore]
    procedure Counts_a_nucleotide_only_once;

    [Test]
    [Ignore]
    procedure strand_with_invalid_nucleotides;

    [Test]
    [Ignore]
    procedure strand_with_multiple_nucleotides;
  end;

implementation
uses SysUtils, uNucleotideCount;

procedure CompareDictionaries(Expected, Actual: TDictionary<Char, Integer>);
var expectedArray,
    actualArray: TArray<TPair<char, integer>>;
    i: integer;
begin
  Assert.AreEqual(Expected.Count, Actual.Count);
  expectedArray := Expected.ToArray;
  actualArray := Actual.ToArray;
  for i := Low(expectedArray) to High(expectedArray) do
  begin
    Assert.AreEqual(expectedArray[i].Key, actualArray[i].Key,'Keys don''t match');
    Assert.AreEqual(expectedArray[i].Value, actualArray[i].Value, format('Key %s count is wrong',[expectedArray[i].Key]));
  end;
end;

procedure NucleoTideCountTest.Validate_CompareDictionaries;
var expected, actual: TDictionary<char, integer>;
begin
  expected := TDictionary<char, integer>.Create;
  expected.Add('r',5);
  expected.Add('a',10);
  expected.Add('n',15);
  expected.Add('d',20);
  expected.Add('o',25);
  expected.Add('m',30);

  actual := TDictionary<char, integer>.create(expected);

  CompareDictionaries(expected, actual);
end;

procedure NucleoTideCountTest.empty_strand;
var dna: TDNA;
    expected: TDictionary<char, integer>;
begin
  expected := TDictionary<char, integer>.Create;
  expected.Add('A',0);
  expected.Add('T',0);
  expected.Add('C',0);
  expected.Add('G',0);

  dna := TDNA.Create('');

  CompareDictionaries(expected, dna.NucleotideCounts);
end;

procedure NucleoTideCountTest.strand_with_repeated_nucleotide;
var dna: TDNA;
    expected: TDictionary<char, integer>;
    inStr: string;
begin
  expected := TDictionary<char, integer>.Create();
  expected.Add('A',0);
  expected.Add('T',0);
  expected.Add('C',0);
  expected.Add('G',7);
  inStr := 'GGGGGGG';

  dna := TDNA.Create(inStr);

  CompareDictionaries(expected, dna.NucleotideCounts);
end;

procedure NucleoTideCountTest.Counts_a_nucleotide_only_once;
var dna: TDNA;
    inStr: string;
begin
  inStr := 'GGTTGG';
  dna := TDNA.Create(inStr);
  dna.Count('T');

  Assert.AreEqual(2, dna.Count('T'));
end;

procedure NucleoTideCountTest.strand_with_invalid_nucleotides;
var MyProc: TTestLocalMethod;
begin
  MyProc := procedure
            var dna: TDNA;
                inStr: string;
            begin
              inStr := 'AGXXACT';
              dna := TDNA.Create(inStr);
              dna.Count('X');
            end;
  Assert.WillRaiseWithMessage(MyProc,EInvalidNucleotideException,'Invalid nucleotide in strand');
end;

procedure NucleoTideCountTest.can_count_one_nucleotide_in_single_character_input;
var dna: TDNA;
    expected: TDictionary<char, integer>;
    inStr: string;
begin
  expected := TDictionary<char, integer>.Create;
  expected.Add('A',0);
  expected.Add('T',0);
  expected.Add('C',0);
  expected.Add('G',1);
  inStr := 'G';

  dna := TDNA.Create(inStr);

  CompareDictionaries(expected, dna.NucleotideCounts);
end;

procedure NucleoTideCountTest.strand_with_multiple_nucleotides;
var dna: TDNA;
    expected: TDictionary<char, integer>;
    inStr: string;
begin
  expected := TDictionary<char, integer>.Create;
  expected.Add('A',20);
  expected.Add('T',21);
  expected.Add('C',12);
  expected.Add('G',17);
  inStr := 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC';

  dna := TDNA.Create(inStr);

  CompareDictionaries(expected, dna.NucleotideCounts);
end;

initialization
  TDUnitX.RegisterTestFixture(NucleoTideCountTest);
end.