|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package new_user_red_package
-
- import (
- "fmt"
- "math/rand"
- "testing"
- "time"
- )
-
- func TestDistributeRedPacket(t *testing.T) {
- // 初始化随机数种子
- rand.Seed(time.Now().UnixNano())
- // 定义测试用例
- testCases := []struct {
- amount float64
- days int
- userChoices []bool // 每天用户是否选择翻倍
- }{
- {
- amount: 10.0,
- days: 5,
- userChoices: []bool{true, false, true, false, false},
- },
- {
- amount: 20.0,
- days: 7,
- userChoices: []bool{true, true, false, false, true, false, false},
- },
- }
-
- // 运行测试用例
- for _, tc := range testCases {
- runTest(tc.amount, tc.days, tc.userChoices)
- }
- }
-
- // runTest 运行测试用例
- func runTest(totalAmount float64, totalDays int, userChoices []bool) {
- fmt.Printf("\n测试: %.2f元 / %d天\n", totalAmount, totalDays)
- fmt.Printf("每日平均金额: %.2f元\n", totalAmount/float64(totalDays))
- fmt.Println("----------------------------------------")
-
- distributor := NewRedPacketDistributorForTest(totalAmount, totalDays)
- var totalDistributed float64
-
- for day := 1; day <= totalDays; day++ {
- // 获取用户选择
- userChoice := false
- if day <= len(userChoices) {
- userChoice = userChoices[day-1]
- }
-
- // 分发红包并打印结果
- packet, err := distributor.DistributeRedPacket(day, userChoice)
- if err != nil {
- println("err:::", err.Error())
- break
- }
- totalDistributed += packet.FinalAmount
- fmt.Println(formatRedPacketInfo(packet))
- }
-
- // 打印总结信息
- fmt.Println("----------------------------------------")
- fmt.Printf("总计分发: %.2f 元\n", totalDistributed)
- fmt.Printf("剩余金额: %.2f 元\n", distributor.RemainAmount)
- }
|