蛋蛋星球-制度模式
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util_test.go 1.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package new_user_red_package
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "testing"
  6. "time"
  7. )
  8. func TestDistributeRedPacket(t *testing.T) {
  9. // 初始化随机数种子
  10. rand.Seed(time.Now().UnixNano())
  11. // 定义测试用例
  12. testCases := []struct {
  13. amount float64
  14. days int
  15. userChoices []bool // 每天用户是否选择翻倍
  16. }{
  17. {
  18. amount: 10.0,
  19. days: 5,
  20. userChoices: []bool{true, false, true, false, false},
  21. },
  22. {
  23. amount: 20.0,
  24. days: 7,
  25. userChoices: []bool{true, true, false, false, true, false, false},
  26. },
  27. }
  28. // 运行测试用例
  29. for _, tc := range testCases {
  30. runTest(tc.amount, tc.days, tc.userChoices)
  31. }
  32. }
  33. // runTest 运行测试用例
  34. func runTest(totalAmount float64, totalDays int, userChoices []bool) {
  35. fmt.Printf("\n测试: %.2f元 / %d天\n", totalAmount, totalDays)
  36. fmt.Printf("每日平均金额: %.2f元\n", totalAmount/float64(totalDays))
  37. fmt.Println("----------------------------------------")
  38. distributor := NewRedPacketDistributorForTest(totalAmount, totalDays)
  39. var totalDistributed float64
  40. for day := 1; day <= totalDays; day++ {
  41. // 获取用户选择
  42. userChoice := false
  43. if day <= len(userChoices) {
  44. userChoice = userChoices[day-1]
  45. }
  46. // 分发红包并打印结果
  47. packet, err := distributor.DistributeRedPacket(day, userChoice)
  48. if err != nil {
  49. println("err:::", err.Error())
  50. break
  51. }
  52. totalDistributed += packet.FinalAmount
  53. fmt.Println(formatRedPacketInfo(packet))
  54. }
  55. // 打印总结信息
  56. fmt.Println("----------------------------------------")
  57. fmt.Printf("总计分发: %.2f 元\n", totalDistributed)
  58. fmt.Printf("剩余金额: %.2f 元\n", distributor.RemainAmount)
  59. }