123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package utils_test
  2. import (
  3. "spaceofcheng/services/utils"
  4. "testing"
  5. )
  6. func TestGetRand(t *testing.T) {
  7. res1 := utils.GetRand(6)
  8. res2 := utils.GetRand(6)
  9. if res1 == res2 {
  10. t.Fatalf("TestGetRand error, %s, %s", res1, res2)
  11. }
  12. }
  13. func TestLuckyDraw(t *testing.T) {
  14. prizeList := []map[string]interface{}{
  15. map[string]interface{}{
  16. "prize": "A",
  17. "prob": 20,
  18. },
  19. map[string]interface{}{
  20. "prize": "B",
  21. "prob": 20,
  22. },
  23. map[string]interface{}{
  24. "prize": "C",
  25. "prob": 30,
  26. },
  27. map[string]interface{}{
  28. "prize": "D",
  29. "prob": 30,
  30. },
  31. }
  32. theA := 0
  33. theB := 0
  34. theC := 0
  35. theD := 0
  36. for i := 0; i < 10000; i++ {
  37. res := utils.LuckyDraw(prizeList).(string)
  38. switch res {
  39. case "A":
  40. theA += 1
  41. case "B":
  42. theB += 1
  43. case "C":
  44. theC += 1
  45. case "D":
  46. theD += 1
  47. default:
  48. }
  49. // bingoList = append(bingoList, utils.LuckyDraw(prizeList))
  50. }
  51. resStr := `A ==> %d B ==> %d C ==> %d D ==> %d`
  52. t.Fatalf(resStr, theA, theB, theC, theD)
  53. }