1
exercism fetch go secret-handshake

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

// Source: exercism/problem-specifications
// Commit: 8acd78c Replace x-common with problem-specifications
// Problem Specifications Version: 1.1.0

type secretHandshakeTest struct {
	code uint
	h    []string
}

var tests = []secretHandshakeTest{
	{1, []string{"wink"}},
	{2, []string{"double blink"}},
	{4, []string{"close your eyes"}},
	{8, []string{"jump"}},
	{3, []string{"wink", "double blink"}},
	{19, []string{"double blink", "wink"}},
	{24, []string{"jump"}},
	{16, []string{}},
	{15, []string{"wink", "double blink", "close your eyes", "jump"}},
	{31, []string{"jump", "close your eyes", "double blink", "wink"}},
	{0, []string{}},
}

secret_handshake_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
package secret

import (
	"reflect"
	"testing"
)

func TestHandshake(t *testing.T) {
	for _, test := range tests {
		h := Handshake(test.code)
		// use len() to allow either nil or empty list, because
		// they are not equal by DeepEqual
		if len(h) == 0 && len(test.h) == 0 {
			continue
		}
		if !reflect.DeepEqual(h, test.h) {
			t.Fatalf("Handshake(%d) = %q, want %q.", test.code, h, test.h)
		}
	}
}

func BenchmarkHandshake(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for j := uint(0); j < 32; j++ {
			Handshake(j)
		}
	}
}