From 46c708f7bba473b537b07f0239bc4696528efa59 Mon Sep 17 00:00:00 2001 From: Kishan B Date: Sat, 4 Aug 2018 18:48:28 +0530 Subject: [PATCH 1/5] Add travis, codecov integration and benchmarks --- .gitignore | 2 ++ .travis.yml | 8 ++++++++ README.md | 19 +++++++++++++++++++ combos_benchmark_test.go | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 .travis.yml create mode 100644 combos_benchmark_test.go diff --git a/.gitignore b/.gitignore index a1338d6..db04a2f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 .glide/ + +coverage.txt \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..6a02a0e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +sudo: false +language: go +go: +- 1.10.x +script: +- go test -race -coverprofile=coverage.txt -covermode=atomic -v ./... +after_success: +- curl -sL https://codecov.io/bash | bash \ No newline at end of file diff --git a/README.md b/README.md index 7331168..145c853 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # combos +[![Build Status](https://travis-ci.org/notnil/combos.svg?branch=master)](https://travis-ci.org/notnil/combos) +[![Go Doc](https://godoc.org/github.com/notnil/combos?status.svg)](https://godoc.org/github.com/notnil/combos) +[![Go Report Card](https://goreportcard.com/badge/github.com/notnil/combos)](https://goreportcard.com/report/github.com/notnil/combos) +[![codecov](https://codecov.io/gh/notnil/combos/branch/master/graph/badge.svg)](https://codecov.io/gh/notnil/combos) + combos is a simple combinations package ## Usage @@ -18,3 +23,17 @@ func main() { // [[0 1 2 3 4] [0 1 2 3 5] [0 1 2 3 6] [0 1 2 4 5] [0 1 2 4 6] [0 1 25 6] [0 1 3 4 5] [0 1 3 4 6] [0 1 3 5 6] [0 1 4 5 6] [0 2 3 4 5] [0 2 3 4 6] [0 2 3 5 6] [0 2 4 5 6] [0 3 4 5 6] [1 2 3 4 5] [1 2 3 4 6] [1 2 3 5 6] [1 2 4 5 6] [1 3 4 5 6] [23 4 5 6]] } ``` + +## Benchmark + +```bash +~ go test -bench=. +goos: darwin +goarch: amd64 +pkg: github.com/notnil/combos +Benchmark10C2-8 500000 2627 ns/op +Benchmark100C2-8 10000 220249 ns/op +Benchmark1000C2-8 20 84906653 ns/op +PASS +ok github.com/notnil/combos 6.263s +``` \ No newline at end of file diff --git a/combos_benchmark_test.go b/combos_benchmark_test.go new file mode 100644 index 0000000..fc35834 --- /dev/null +++ b/combos_benchmark_test.go @@ -0,0 +1,19 @@ +package combos_test + +import ( + "testing" + + "github.com/notnil/combos" +) + +func Benchmark10C2(b *testing.B) { benchmarknCr(10, 2, b) } + +func Benchmark100C2(b *testing.B) { benchmarknCr(100, 2, b) } + +func Benchmark1000C2(b *testing.B) { benchmarknCr(1000, 2, b) } + +func benchmarknCr(n int, r int, b *testing.B) { + for i := 0 ; i < b.N; i++ { + combos.New(n, r) + } +} \ No newline at end of file From cd117101c6e12d63dc4da5eb12efcae6f402411f Mon Sep 17 00:00:00 2001 From: Kishan B Date: Sat, 4 Aug 2018 19:18:27 +0530 Subject: [PATCH 2/5] Add more test cases and formatting corrections --- combos_benchmark_test.go | 10 +++++----- combos_test.go | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/combos_benchmark_test.go b/combos_benchmark_test.go index fc35834..fedab60 100644 --- a/combos_benchmark_test.go +++ b/combos_benchmark_test.go @@ -6,14 +6,14 @@ import ( "github.com/notnil/combos" ) -func Benchmark10C2(b *testing.B) { benchmarknCr(10, 2, b) } +func Benchmark10C2(b *testing.B) { benchmarknCr(10, 2, b) } -func Benchmark100C2(b *testing.B) { benchmarknCr(100, 2, b) } +func Benchmark100C2(b *testing.B) { benchmarknCr(100, 2, b) } -func Benchmark1000C2(b *testing.B) { benchmarknCr(1000, 2, b) } +func Benchmark1000C2(b *testing.B) { benchmarknCr(1000, 2, b) } func benchmarknCr(n int, r int, b *testing.B) { - for i := 0 ; i < b.N; i++ { + for i := 0; i < b.N; i++ { combos.New(n, r) } -} \ No newline at end of file +} diff --git a/combos_test.go b/combos_test.go index b24205d..a9d87d5 100644 --- a/combos_test.go +++ b/combos_test.go @@ -6,9 +6,25 @@ import ( "github.com/notnil/combos" ) +type combinationTest struct { + n int + r int + expected int +} + +var combinationTests = []combinationTest{ + {7, 5, 21}, + {7, 4, 35}, + {10, 2, 45}, + {100, 2, 4950}, + {1000, 2, 499500}, +} + func TestCombinations(t *testing.T) { - results := combos.New(7, 5) - if len(results) != 21 { - t.Fatalf("expected %d results but got %d", 21, len(results)) + for _, ct := range combinationTests { + actual := combos.New(ct.n, ct.r) + if len(actual) != ct.expected { + t.Errorf("%dC%d: expected %d, actual %d", ct.n, ct.r, ct.expected, len(actual)) + } } } From 79dc91788b9c3322f869a3e831906151711bc62a Mon Sep 17 00:00:00 2001 From: Kishan B Date: Sat, 4 Aug 2018 19:29:37 +0530 Subject: [PATCH 3/5] Add consumed memory to benchmarks --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 145c853..2ac82ed 100644 --- a/README.md +++ b/README.md @@ -27,13 +27,13 @@ func main() { ## Benchmark ```bash -~ go test -bench=. +~ go test -bench=. -benchmem goos: darwin goarch: amd64 pkg: github.com/notnil/combos -Benchmark10C2-8 500000 2627 ns/op -Benchmark100C2-8 10000 220249 ns/op -Benchmark1000C2-8 20 84906653 ns/op +Benchmark10C2-8 500000 2628 ns/op 4080 B/op 62 allocs/op +Benchmark100C2-8 10000 219910 ns/op 556416 B/op 4980 allocs/op +Benchmark1000C2-8 20 89627977 ns/op 78746426 B/op 499554 allocs/op PASS -ok github.com/notnil/combos 6.263s +ok github.com/notnil/combos 5.582s ``` \ No newline at end of file From 3528e9b281bcdd43e4859591cf0c4106be2936ff Mon Sep 17 00:00:00 2001 From: Kishan B Date: Sun, 5 Aug 2018 07:58:31 +0530 Subject: [PATCH 4/5] Add test go check if the combination generated is sorted --- combos_test.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/combos_test.go b/combos_test.go index a9d87d5..a344553 100644 --- a/combos_test.go +++ b/combos_test.go @@ -1,6 +1,7 @@ package combos_test import ( + "sort" "testing" "github.com/notnil/combos" @@ -20,7 +21,7 @@ var combinationTests = []combinationTest{ {1000, 2, 499500}, } -func TestCombinations(t *testing.T) { +func TestCombinationsCount(t *testing.T) { for _, ct := range combinationTests { actual := combos.New(ct.n, ct.r) if len(actual) != ct.expected { @@ -28,3 +29,15 @@ func TestCombinations(t *testing.T) { } } } +} + +func TestCombinationsOrdering(t *testing.T) { + for _, ct := range combinationTests { + actual := combos.New(ct.n, ct.r) + for _, combination := range actual { + if !sort.IntsAreSorted(combination) { + t.Errorf("%v combination is not in ascending order", combination) + } + } + } +} From 81356d3f986cfb40c401828ee5cae8ce4359d59b Mon Sep 17 00:00:00 2001 From: Kishan B Date: Sun, 5 Aug 2018 08:01:01 +0530 Subject: [PATCH 5/5] Change 23 to 2 3 in readm and 25 to 2 5 in readme output --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2ac82ed..b18301d 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ import ( func main() { fmt.Println(combos.New(7, 5)) - // [[0 1 2 3 4] [0 1 2 3 5] [0 1 2 3 6] [0 1 2 4 5] [0 1 2 4 6] [0 1 25 6] [0 1 3 4 5] [0 1 3 4 6] [0 1 3 5 6] [0 1 4 5 6] [0 2 3 4 5] [0 2 3 4 6] [0 2 3 5 6] [0 2 4 5 6] [0 3 4 5 6] [1 2 3 4 5] [1 2 3 4 6] [1 2 3 5 6] [1 2 4 5 6] [1 3 4 5 6] [23 4 5 6]] + // [[0 1 2 3 4] [0 1 2 3 5] [0 1 2 3 6] [0 1 2 4 5] [0 1 2 4 6] [0 1 2 5 6] [0 1 3 4 5] [0 1 3 4 6] [0 1 3 5 6] [0 1 4 5 6] [0 2 3 4 5] [0 2 3 4 6] [0 2 3 5 6] [0 2 4 5 6] [0 3 4 5 6] [1 2 3 4 5] [1 2 3 4 6] [1 2 3 5 6] [1 2 4 5 6] [1 3 4 5 6] [2 3 4 5 6]] } ```