summaryrefslogtreecommitdiff
path: root/go/hamming/hamming_test.go
blob: bfee7056f0ea8dc0492189eeda86cf3ce724af3f (plain)
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
package hamming

import "testing"

const targetTestVersion = 4

func TestHamming(t *testing.T) {
	if testVersion != targetTestVersion {
		t.Errorf("Found testVersion = %v, want %v.", testVersion, targetTestVersion)
	}
	for _, tc := range testCases {
		switch got, err := Distance(tc.s1, tc.s2); {
		case err != nil:
			var _ error = err
			if tc.want >= 0 {
				t.Fatalf("Distance(%q, %q) returned error: %v",
					tc.s1, tc.s2, err)
			}
		case tc.want < 0:
			t.Fatalf("Distance(%q, %q) = %d.  Expected error.",
				tc.s1, tc.s2, got)
		case got != tc.want:
			t.Fatalf("Distance(%q, %q) = %d, want %d.",
				tc.s1, tc.s2, got, tc.want)
		}
	}
}

func BenchmarkHamming(b *testing.B) {
	// bench combined time to run through all test cases
	for i := 0; i < b.N; i++ {
		for _, tc := range testCases {
			Distance(tc.s1, tc.s2)
		}
	}
}