func TestLots(t *testing.T) { l := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano()))) dropItems := []lottery.Interface{ DropItem{ItemName: "エリクサ", DropProb: 5}, DropItem{ItemName: "エーテル", DropProb: 10}, DropItem{ItemName: "ポーション", DropProb: 20}, DropItem{ItemName: "ハズレ", DropProb: 50}, Trap{TrapName: "地雷", prob: 5}, Trap{TrapName: "トラバサミ", prob: 10}, } check := 2000000 countMap := map[lottery.Interface]int{} for i := 0; i < check; i++ { lotIdx := l.Lots(dropItems...) if lotIdx == -1 { t.Fatal("lot error") } switch d := dropItems[lotIdx].(type) { case DropItem, Trap: countMap[d]++ } } for item, count := range countMap { result := float64(count) / float64(check) * 100 prob := float64(item.Prob()) name := "" switch t := item.(type) { case DropItem: name = t.ItemName case Trap: name = t.TrapName } // 0.1 check if (prob-0.1) <= result && result < (prob+0.1) { fmt.Printf("ok %3.5f%%(%7d) : %s\n", result, count, name) } else { t.Errorf("error %3.5f%%(%7d) : %s\n", result, count, name) } } }
func TestLot_0to100(t *testing.T) { l := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano()))) testCases := []struct { prob int result bool }{ {prob: 120, result: true}, {prob: 100, result: true}, {prob: 0, result: false}, {prob: -1, result: false}, } for _, testCase := range testCases { if l.Lot(testCase.prob) != testCase.result { t.Errorf("lottery error not %f%%", testCase.prob) } } }
func TestLot(t *testing.T) { l := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano()))) check := 1000000 prob := float64(4.0) // 4% count := 0 for i := 0; i < check; i++ { if l.Lot(int(prob)) { count++ } } result := float64(count) / float64(check) * 100 // 0.1 check if (prob-0.1) <= result && result < (prob+0.1) { fmt.Printf("lottery ok %f%%\n", result) } else { t.Errorf("lottery error %f%%", result) } }
func main() { lot := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano()))) check := 1000 count := 0 for i := 0; i < check; i++ { if lot.LotOf(1, 5) { // 1/5の確率 fmt.Print("アタリ") count++ } else { fmt.Print("ハズレ") } } fmt.Println(float32(count)/float32(check)*100, "%") }
func TestExploration(t *testing.T) { log.SetFlags(log.Llongfile) lot := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano()))) // init repository playerRepository := repository.NewPlayer() explorationRepository := repository.NewExploration() // init factory messageFactory := dMessage.NewFactory() lotteryFactory := dLottery.NewFactory(&lot) enemyFactory := dCharacter.NewEnemyFactory() p := dCharacter.NewPlayer("test player") e := dExploration.New(dExploration.Identifier("uuid-uuid-uuid"), dDungeon.Tutorial, p.ID) // init repository store explorationRepository.Store(e) playerRepository.Store(p) // init service explorationService := sExploration.New( explorationRepository, playerRepository, enemyFactory, lotteryFactory, ) explorationLogService := sExplorationlog.New( explorationRepository, playerRepository, messageFactory, ) // start nowTime, _ := time.ParseInLocation("2006-01-02 15:04:05", "2015-07-20 17:42:40", time.UTC) // 探索開始時間(5時間前) startTime := nowTime.Add(-5 * time.Hour) if err := explorationService.Start(e.ID, startTime); err != nil { t.Errorf("exploration start error : %s", err.Error()) } progress, err := explorationService.Progress(e.ID, nowTime) if err != nil { t.Errorf("exploration progress error : %s", err.Error()) } fmt.Printf("progress count : %s - %s => %d\n", startTime, nowTime, progress) e, err = explorationRepository.Resolve(e.ID) if err != nil { t.Errorf("exploration find error : %s", err.Error()) } pp.Println(e.StartTime, e.PlayState, e.EndTime, e.ProgressCount) p, err = playerRepository.Resolve(p.ID) if err != nil { t.Errorf("player find error : %s", err.Error()) } pp.Println(p) messages := explorationLogService.EventLog(e.ID) for _, ms := range messages { fmt.Printf("%s\n", ms) } explorationMessage := explorationLogService.ExplorationLog(e.ID) line := buildMaxLenSizeLine(explorationMessage) fmt.Println(line) fmt.Printf("%s\n", explorationMessage) fmt.Println(line) }