From e1b0eab3fc96b8a0f718ed195c607f6a9b144caf Mon Sep 17 00:00:00 2001 From: aaron <462826@qq.com> Date: Fri, 18 Sep 2020 11:33:18 +0800 Subject: [PATCH] generate random values in field section level, not just after get the limit values --- src/gen/charVal.go | 75 ++++++++++++++++++++++---------------------- src/gen/floatVal.go | 75 +++++++++++++++++++++++++------------------- src/gen/generator.go | 2 +- src/gen/intVal.go | 2 +- src/gen/list.go | 16 +++++++--- 5 files changed, 93 insertions(+), 77 deletions(-) diff --git a/src/gen/charVal.go b/src/gen/charVal.go index f12aede..277b05a 100644 --- a/src/gen/charVal.go +++ b/src/gen/charVal.go @@ -1,16 +1,17 @@ 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 diff --git a/src/gen/floatVal.go b/src/gen/floatVal.go index a7a1101..91af759 100644 --- a/src/gen/floatVal.go +++ b/src/gen/floatVal.go @@ -1,19 +1,28 @@ 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 diff --git a/src/gen/generator.go b/src/gen/generator.go index 6d36d28..6e8cea6 100644 --- a/src/gen/generator.go +++ b/src/gen/generator.go @@ -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 { diff --git a/src/gen/intVal.go b/src/gen/intVal.go index a14330d..cc36a7c 100644 --- a/src/gen/intVal.go +++ b/src/gen/intVal.go @@ -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 } diff --git a/src/gen/list.go b/src/gen/list.go index 8c9859a..dfadfcd 100644 --- a/src/gen/list.go +++ b/src/gen/list.go @@ -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) } -- GitLab