1
exercism fetch go nucleotide-count

cases_test.go

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
package dna

// Source: exercism/problem-specifications
// Commit: 879a096 nucleotide-count: Apply new "input" policy
// Problem Specifications Version: 1.3.0

// count all nucleotides in a strand
var testCases = []struct {
	description   string
	strand        string
	expected      Histogram
	errorExpected bool
}{
	{
		description: "empty strand",
		strand:      "",
		expected:    Histogram{'A': 0, 'C': 0, 'G': 0, 'T': 0},
	},
	{
		description: "can count one nucleotide in single-character input",
		strand:      "G",
		expected:    Histogram{'A': 0, 'C': 0, 'G': 1, 'T': 0},
	},
	{
		description: "strand with repeated nucleotide",
		strand:      "GGGGGGG",
		expected:    Histogram{'A': 0, 'C': 0, 'G': 7, 'T': 0},
	},
	{
		description: "strand with multiple nucleotides",
		strand:      "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC",
		expected:    Histogram{'A': 20, 'C': 12, 'G': 17, 'T': 21},
	},
	{
		description:   "strand with invalid nucleotides",
		strand:        "AGXXACT",
		errorExpected: true,
	},
}

nucleotide_count_test.go

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
package dna

import (
	"reflect"
	"testing"
)

func TestCounts(t *testing.T) {
	for _, tc := range testCases {
		dna := DNA(tc.strand)
		s, err := dna.Counts()
		if tc.errorExpected {
			if err == nil {
				t.Fatalf("FAIL: %s\nCounts(%q)\nExpected error\nActual: %#v",
					tc.description, tc.strand, s)
			}
		} else if err != nil {
			t.Fatalf("FAIL: %s\nCounts(%q)\nExpected: %#v\nGot error: %q",
				tc.description, tc.strand, tc.expected, err)
		} else if !reflect.DeepEqual(s, tc.expected) {
			t.Fatalf("FAIL: %s\nCounts(%q)\nExpected: %#v\nActual: %#v",
				tc.description, tc.strand, tc.expected, s)
		}
		t.Logf("PASS: %s", tc.description)
	}
}