提交 e1b0eab3 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

generate random values in field section level, not just after get the limit values

上级 9a905f3d
package gen
import (
commonUtils "github.com/easysoft/zendata/src/utils/common"
constant "github.com/easysoft/zendata/src/utils/const"
)
//func GenerateByteItems(start byte, end byte, step interface{}, rand bool, repeat int) []interface{} {
// if !rand {
// return GenerateByteItemsByStep(start, end, step.(int), repeat)
// } else {
// return GenerateByteItemsRand(start, end, step.(int), repeat)
// }
//}
func GenerateByteItems(start byte, end byte, step int, rand bool, repeat int) []interface{} {
if !rand {
return GenerateByteItemsByStep(start, end, step, repeat)
} else{
return GenerateByteItemsRand(start, end, step, repeat)
}
}
func GenerateByteItemsByStep(start byte, end byte, step int, repeat int) []interface{} {
arr := make([]interface{}, 0)
......@@ -39,33 +40,33 @@ func GenerateByteItemsByStep(start byte, end byte, step int, repeat int) []inter
return arr
}
//func GenerateByteItemsRand(start byte, end byte, step int, repeat int) []interface{} {
// arr := make([]interface{}, 0)
//
// countInRound := int(int(end) - int(start)) / step
// total := 0
// for i := 0; i < countInRound; {
// rand := commonUtils.RandNum(countInRound)
// if step < 0 {
// rand = rand * -1
// }
//
// val := start + byte(rand)
//
// for round := 0; round < repeat; round++ {
// arr = append(arr, val)
//
// total++
// if total > constant.MaxNumb {
// break
// }
// }
//
// if total > constant.MaxNumb {
// break
// }
// i++
// }
//
// return arr
//}
\ No newline at end of file
func GenerateByteItemsRand(start byte, end byte, step int, repeat int) []interface{} {
arr := make([]interface{}, 0)
countInRound := (int(end) - int(start)) / step
total := 0
for i := 0; i < countInRound; {
rand := commonUtils.RandNum(countInRound)
if step < 0 {
rand = rand * -1
}
val := start + byte(rand)
for round := 0; round < repeat; round++ {
arr = append(arr, val)
total++
if total > constant.MaxNumb {
break
}
}
if total > constant.MaxNumb {
break
}
i++
}
return arr
}
\ No newline at end of file
package gen
import (
commonUtils "github.com/easysoft/zendata/src/utils/common"
constant "github.com/easysoft/zendata/src/utils/const"
"strconv"
"strings"
)
func GenerateFloatItemsByStep(start float64, end float64, step interface{}, repeat int) []interface{} {
func GenerateFloatItems(start float64, end float64, step interface{}, rand bool, repeat int) []interface{} {
if !rand {
return GenerateFloatItemsByStep(start, end, step.(float64), repeat)
} else{
return GenerateFloatItemsRand(start, end, step.(float64), repeat)
}
}
func GenerateFloatItemsByStep(start float64, end float64, step float64, repeat int) []interface{} {
arr := make([]interface{}, 0)
total := 0
for i := 0; true; {
val := start + float64(i) * step.(float64)
if (val > end && step.(float64) > 0) || (val < end && step.(float64) < 0) {
val := start + float64(i) * step
if (val > end && step > 0) || (val < end && step < 0) {
break
}
......@@ -34,36 +43,36 @@ func GenerateFloatItemsByStep(start float64, end float64, step interface{}, repe
return arr
}
//func GenerateFloatItemsRand(start float64, end float64, step float64, repeat int) []interface{} {
// arr := make([]interface{}, 0)
//
// countInRound := (end - start) / step
// total := 0
// for i := float64(0); i < countInRound; {
// rand := commonUtils.RandNum64(int64(countInRound))
// if step < 0 {
// rand = rand * -1
// }
//
// val := start + float64(rand) * step
//
// for round := 0; round < repeat; round++ {
// arr = append(arr, val)
//
// total++
// if total > constant.MaxNumb {
// break
// }
// }
//
// if total > constant.MaxNumb {
// break
// }
// i++
// }
//
// return arr
//}
func GenerateFloatItemsRand(start float64, end float64, step float64, repeat int) []interface{} {
arr := make([]interface{}, 0)
countInRound := (end - start) / step
total := 0
for i := float64(0); i < countInRound; {
rand := commonUtils.RandNum64(int64(countInRound))
if step < 0 {
rand = rand * -1
}
val := start + float64(rand) * step
for round := 0; round < repeat; round++ {
arr = append(arr, val)
total++
if total > constant.MaxNumb {
break
}
}
if total > constant.MaxNumb {
break
}
i++
}
return arr
}
func GetPrecision(base float64, step interface{}) (precision int, newStep float64) {
val := base
......
......@@ -206,7 +206,7 @@ func GenerateFieldValuesForDef(field *model.DefField) []string {
values = append(values, val)
count++
isRandomAndLoopEnd := !vari.ResLoading && // ignore rand in res
isRandomAndLoopEnd := !vari.ResLoading && // ignore rand in resource
!(*field).ReferToAnotherYaml && (*field).IsRand && (*field).LoopIndex == (*field).LoopEnd
// isNotRandomAndValOver := !(*field).IsRand && indexOfRow >= len(fieldWithValues.Values)
if count >= vari.Total || count >= len(fieldWithValues.Values) || isRandomAndLoopEnd {
......
......@@ -18,7 +18,7 @@ func GenerateIntItemsByStep(start int64, end int64, step int, repeat int) []inte
total := 0
for i := 0; true; {
val := start + int64(i*step)
val := start + int64(i * step)
if (val > end && step > 0) || (val < end && step < 0) {
break
}
......
......@@ -2,6 +2,7 @@ package gen
import (
"github.com/easysoft/zendata/src/model"
commonUtils "github.com/easysoft/zendata/src/utils/common"
constant "github.com/easysoft/zendata/src/utils/const"
"github.com/easysoft/zendata/src/utils/vari"
"strconv"
......@@ -171,7 +172,12 @@ func CreateValuesFromLiteral(field *model.DefField, desc string, stepStr string,
}
for i := 0; i < len(elemArr); {
val := elemArr[i]
idx := i
if field.Path == "" && stepStr == "r" {
idx = commonUtils.RandNum(len(elemArr)) // should set random here too
}
val := elemArr[idx]
for round := 0; round < repeat; round++ {
items = append(items, val)
......@@ -216,17 +222,17 @@ func CreateValuesFromInterval(field *model.DefField, desc, stepStr string, repea
startInt, _ := strconv.ParseInt(startStr, 0, 64)
endInt, _ := strconv.ParseInt(endStr, 0, 64)
items = GenerateIntItemsByStep(startInt, endInt, step.(int), repeat)
items = GenerateIntItems(startInt, endInt, step.(int), rand, repeat)
} else if dataType == "float" {
startFloat, _ := strconv.ParseFloat(startStr, 64)
endFloat, _ := strconv.ParseFloat(endStr, 64)
field.Precision = precision
items = GenerateFloatItemsByStep(startFloat, endFloat, step.(float64), repeat)
items = GenerateFloatItems(startFloat, endFloat, step, rand, repeat)
} else if dataType == "char" {
items = GenerateByteItemsByStep(startStr[0], endStr[0], step.(int), repeat)
items = GenerateByteItems(startStr[0], endStr[0], step.(int), rand, repeat)
} else if dataType == "string" {
if repeat == 0 { repeat = 1 }
......@@ -235,7 +241,7 @@ func CreateValuesFromInterval(field *model.DefField, desc, stepStr string, repea
}
}
if field.Path == "" && stepStr == "r" { // for ranges and instances, random
if field.Path == "" && stepStr == "r" { // for ranges and instances, random again
items = randomInterfaces(items)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册