修复GetPricePercentage中的bug
This commit is contained in:
@@ -279,7 +279,7 @@ func GetPricePercentage(l model.PricePercentagePack, price int, defPricePercenta
|
|||||||
mid := 0
|
mid := 0
|
||||||
for low <= high {
|
for low <= high {
|
||||||
mid = low + (high-low)/2
|
mid = low + (high-low)/2
|
||||||
if mid == 0 || mid == itemLen-1 {
|
if mid < 0 || mid >= itemLen-1 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if price >= l[mid].BeginPrice {
|
if price >= l[mid].BeginPrice {
|
||||||
@@ -292,8 +292,10 @@ func GetPricePercentage(l model.PricePercentagePack, price int, defPricePercenta
|
|||||||
high = mid - 1
|
high = mid - 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pricePercentage = l[mid].PricePercentage
|
if mid >= 0 && mid <= itemLen-1 && low <= high {
|
||||||
priceAdd = l[mid].PriceAdd
|
pricePercentage = l[mid].PricePercentage
|
||||||
|
priceAdd = l[mid].PriceAdd
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return pricePercentage, priceAdd
|
return pricePercentage, priceAdd
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,6 +173,7 @@ func TestGetPricePercentage(t *testing.T) {
|
|||||||
SpecUnit string
|
SpecUnit string
|
||||||
Unit string
|
Unit string
|
||||||
}
|
}
|
||||||
|
|
||||||
l := []*model.PricePercentageItem{
|
l := []*model.PricePercentageItem{
|
||||||
&model.PricePercentageItem{
|
&model.PricePercentageItem{
|
||||||
BeginPrice: 0,
|
BeginPrice: 0,
|
||||||
@@ -194,12 +195,21 @@ func TestGetPricePercentage(t *testing.T) {
|
|||||||
PricePercentage: 30,
|
PricePercentage: 30,
|
||||||
PriceAdd: 3,
|
PriceAdd: 3,
|
||||||
},
|
},
|
||||||
|
&model.PricePercentageItem{
|
||||||
|
BeginPrice: 60,
|
||||||
|
PricePercentage: 60,
|
||||||
|
PriceAdd: 6,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range [][]int{
|
for _, v := range [][]int{
|
||||||
|
[]int{88, 0, -1, 88},
|
||||||
[]int{0, 0, 0, 0},
|
[]int{0, 0, 0, 0},
|
||||||
[]int{30, 3, 40, 0},
|
[]int{30, 3, 40, 0},
|
||||||
[]int{20, 2, 25, 0},
|
[]int{20, 2, 25, 0},
|
||||||
[]int{10, 1, 10, 0},
|
[]int{10, 1, 10, 0},
|
||||||
|
[]int{60, 6, 60, 0},
|
||||||
|
[]int{60, 6, 1000, 10},
|
||||||
} {
|
} {
|
||||||
pricePercentage, priceAdd := GetPricePercentage(l, v[2], v[3])
|
pricePercentage, priceAdd := GetPricePercentage(l, v[2], v[3])
|
||||||
if pricePercentage != v[0] || priceAdd != v[1] {
|
if pricePercentage != v[0] || priceAdd != v[1] {
|
||||||
|
|||||||
Reference in New Issue
Block a user