Skip to content

Commit

Permalink
Merge pull request #2 from kyokomi/feature/addedNewDefaultFunc
Browse files Browse the repository at this point in the history
added NewDefault func
  • Loading branch information
kyokomi committed Dec 27, 2015
2 parents 22a1396 + 7860545 commit 519bfc5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lottery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package lottery
import (
"math/rand"
"sort"
"time"
)

//go:generate mockgen -package lottery -source lottery.go -destination lottery_mock.go
Expand Down Expand Up @@ -31,6 +32,11 @@ func (s lotterySort) Len() int { return len(s) }
func (s lotterySort) Less(i, j int) bool { return s[i].Prob() < s[j].Prob() }
func (s lotterySort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

// NewDefault return default rand lottery library.
func NewDefault() Lottery {
return New(rand.New(rand.NewSource(time.Now().UnixNano())))
}

// New return lottery library.
func New(rd *rand.Rand) Lottery {
return &lottery{
Expand Down
12 changes: 5 additions & 7 deletions lottery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package lottery_test

import (
"fmt"
"math/rand"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/kyokomi/lottery"
Expand Down Expand Up @@ -33,7 +31,7 @@ func (t Trap) Prob() int {
var _ lottery.Interface = (*Trap)(nil)

func TestLots(t *testing.T) {
l := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano())))
l := lottery.NewDefault()

dropItems := []lottery.Interface{
DropItem{ItemName: "エリクサ", DropProb: 5},
Expand Down Expand Up @@ -80,7 +78,7 @@ func TestLots(t *testing.T) {
}

func TestLot(t *testing.T) {
l := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano())))
l := lottery.NewDefault()

check := 1000000
prob := float64(4.0) // 4%
Expand All @@ -101,7 +99,7 @@ func TestLot(t *testing.T) {
}

func TestLotOf(t *testing.T) {
l := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano())))
l := lottery.NewDefault()

check := 1000000
prob := float64(0.5) // 0.5%
Expand All @@ -123,7 +121,7 @@ func TestLotOf(t *testing.T) {
}

func TestLot_0to100(t *testing.T) {
l := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano())))
l := lottery.NewDefault()

testCases := []struct {
prob int
Expand All @@ -143,7 +141,7 @@ func TestLot_0to100(t *testing.T) {
}

func TestLots_error(t *testing.T) {
l := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano())))
l := lottery.NewDefault()

ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down

0 comments on commit 519bfc5

Please sign in to comment.