UXInteract / task_046 /tests /validate_enhanced_cases.sh
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
3.87 kB
#!/usr/bin/env bash
set -euo pipefail
tmp_test="zz_enhanced_cases_test.go"
trap 'rm -f "$tmp_test"' EXIT
cat > "$tmp_test" <<'GOEOF'
package promotion
import ("testing"; "math"; "math/rand")
func enhancedClose(t *testing.T, got,want float64){t.Helper();if math.IsNaN(got)||math.IsInf(got,0)||got<want-0.0001||got>want+0.0001{t.Fatalf("got %.4f want %.4f",got,want)}}
// ENHANCED_CASE_1: full-reduction boundaries distinguish each exact threshold from the value immediately below it.
func TestEnhancedCase1FullReductionBoundaries(t *testing.T){s:=FullReductionStrategy{}; for _,c:=range []struct{a,w float64}{{199.99,0},{200,20},{499.99,20},{500,50},{999.99,50},{1000,100},{1400,100}}{enhancedClose(t,s.Discount(Order{OriginalAmount:c.a}),c.w)}}
// ENHANCED_CASE_2: every user level and category computes its discount from a non-round original price.
func TestEnhancedCase2PercentageTables(t *testing.T){a:=137.5; u:=UserLevelDiscountStrategy{}; c:=CategoryDiscountStrategy{}; for _,x:=range []struct{v string;w float64}{{"bronze",0},{"silver",6.875},{"gold",13.75},{"platinum",20.625}}{enhancedClose(t,u.Discount(Order{OriginalAmount:a,UserLevel:x.v}),x.w)}; for _,x:=range []struct{v string;w float64}{{"electronics",11},{"clothing",16.5},{"books",27.5}}{enhancedClose(t,c.Discount(Order{OriginalAmount:a,Category:x.v}),x.w)}}
// ENHANCED_CASE_3: combined strategies independently sum discounts based on original price.
func TestEnhancedCase3OriginalPriceStacking(t *testing.T){calc:=NewPromotionCalculator(FullReductionStrategy{},UserLevelDiscountStrategy{},CategoryDiscountStrategy{}); enhancedClose(t,calc.FinalAmount(Order{OriginalAmount:750,UserLevel:"platinum",Category:"clothing"}),497.5)}
type enhancedCustomStrategy struct{}
func (enhancedCustomStrategy) Discount(order Order) float64 { return order.OriginalAmount * .07 }
// ENHANCED_CASE_4: the calculator composes an external strategy and never returns a negative payable amount.
func TestEnhancedCase4ExtensibilityAndFloor(t *testing.T){
order:=Order{OriginalAmount:315,UserLevel:"gold",Category:"books"}
enhancedClose(t,NewPromotionCalculator().FinalAmount(order),315)
enhancedClose(t,NewPromotionCalculator(enhancedCustomStrategy{}).FinalAmount(order),292.95)
enhancedClose(t,NewPromotionCalculator(FullReductionStrategy{},UserLevelDiscountStrategy{},CategoryDiscountStrategy{},enhancedCustomStrategy{}).FinalAmount(order),178.45)
many:=make([]Strategy,15);for i:=range many{many[i]=enhancedCustomStrategy{}}
enhancedClose(t,NewPromotionCalculator(many...).FinalAmount(order),0)
}
func TestEnhancedCase5UnseenAmounts(t *testing.T){
rng:=rand.New(rand.NewSource(90472))
levels:=[]string{"bronze","silver","gold","platinum","unknown",""}
categories:=[]string{"electronics","clothing","books","unknown",""}
for i:=0;i<180;i++{
amount:=rng.Float64()*4000
level:=levels[i%len(levels)];category:=categories[i%len(categories)]
order:=Order{OriginalAmount:amount,UserLevel:level,Category:category}
reduction:=0.0;if amount>=1000{reduction=100}else if amount>=500{reduction=50}else if amount>=200{reduction=20}
user:=amount*map[string]float64{"silver":.05,"gold":.1,"platinum":.15}[level]
item:=amount*map[string]float64{"electronics":.08,"clothing":.12,"books":.2}[category]
enhancedClose(t,FullReductionStrategy{}.Discount(order),reduction)
enhancedClose(t,UserLevelDiscountStrategy{}.Discount(order),user)
enhancedClose(t,CategoryDiscountStrategy{}.Discount(order),item)
enhancedClose(t,NewPromotionCalculator(CategoryDiscountStrategy{},FullReductionStrategy{},UserLevelDiscountStrategy{}).FinalAmount(order),amount-reduction-user-item)
}
enhancedClose(t,NewPromotionCalculator(FullReductionStrategy{},UserLevelDiscountStrategy{},CategoryDiscountStrategy{}).FinalAmount(Order{}),0)
}
GOEOF
GOTOOLCHAIN=local GOPROXY=off go test -v -count=1 -timeout=20s -run '^TestEnhancedCase' .