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

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

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