1
exercism fetch go book-store

book_store_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 bookstore

import (
	"testing"
)

func TestCost(t *testing.T) {
	for _, testCase := range testCases {
		cost := Cost(testCase.basket)
		if testCase.expected != cost {
			t.Fatalf("FAIL: %s\n\tCost(%v) expected %v, got %v",
				testCase.description, testCase.basket, testCase.expected, cost)
		}
		t.Logf("PASS: %s", testCase.description)
	}
}

func BenchmarkCost(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, testCase := range testCases {
			Cost(testCase.basket)
		}
	}
}

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

// Source: exercism/problem-specifications
// Commit: a636903 Update description per @insti comments
// Problem Specifications Version: 1.1.0

var testCases = []struct {
	description string
	basket      []int
	expected    float64
}{
	{
		description: "Only a single book",
		basket:      []int{1},
		expected:    8.00,
	},
	{
		description: "Two of the same book",
		basket:      []int{2, 2},
		expected:    16.00,
	},
	{
		description: "Empty basket",
		basket:      []int{},
		expected:    0.00,
	},
	{
		description: "Two different books",
		basket:      []int{1, 2},
		expected:    15.20,
	},
	{
		description: "Three different books",
		basket:      []int{1, 2, 3},
		expected:    21.60,
	},
	{
		description: "Four different books",
		basket:      []int{1, 2, 3, 4},
		expected:    25.60,
	},
	{
		description: "Five different books",
		basket:      []int{1, 2, 3, 4, 5},
		expected:    30.00,
	},
	{
		description: "Two groups of four is cheaper than group of five plus group of three",
		basket:      []int{1, 1, 2, 2, 3, 3, 4, 5},
		expected:    51.20,
	},
	{
		description: "Group of four plus group of two is cheaper than two groups of three",
		basket:      []int{1, 1, 2, 2, 3, 4},
		expected:    40.80,
	},
	{
		description: "Two each of first 4 books and 1 copy each of rest",
		basket:      []int{1, 1, 2, 2, 3, 3, 4, 4, 5},
		expected:    55.60,
	},
	{
		description: "Two copies of each book",
		basket:      []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5},
		expected:    60.00,
	},
	{
		description: "Three copies of first book and 2 each of remaining",
		basket:      []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1},
		expected:    68.00,
	},
	{
		description: "Three each of first 2 books and 2 each of remaining books",
		basket:      []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2},
		expected:    75.20,
	},
	{
		description: "Four groups of four are cheaper than two groups each of five and three",
		basket:      []int{1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5},
		expected:    102.40,
	},
}