How to use String method of rand Package

Best Testkube code snippet using rand.String

quick.go

Source:quick.go Github

copy

Full Screen

...119 return reflect.Value{}, false120 }121 v.Index(i).Set(elem)122 }123 case reflect.String:124 numChars := rand.Intn(complexSize)125 codePoints := make([]rune, numChars)126 for i := 0; i < numChars; i++ {127 codePoints[i] = rune(rand.Intn(0x10ffff))128 }129 v.SetString(string(codePoints))130 case reflect.Struct:131 for i := 0; i < v.NumField(); i++ {132 elem, ok := Value(concrete.Field(i).Type, rand)133 if !ok {134 return reflect.Value{}, false135 }136 v.Field(i).Set(elem)137 }138 default:139 return reflect.Value{}, false140 }141 return v, true142}143// A Config structure contains options for running a test.144type Config struct {145 // MaxCount sets the maximum number of iterations. If zero,146 // MaxCountScale is used.147 MaxCount int148 // MaxCountScale is a non-negative scale factor applied to the default149 // maximum. If zero, the default is unchanged.150 MaxCountScale float64151 // If non-nil, rand is a source of random numbers. Otherwise a default152 // pseudo-random source will be used.153 Rand *rand.Rand154 // If non-nil, the Values function generates a slice of arbitrary155 // reflect.Values that are congruent with the arguments to the function156 // being tested. Otherwise, the top-level Value function is used157 // to generate them.158 Values func([]reflect.Value, *rand.Rand)159}160var defaultConfig Config161// getRand returns the *rand.Rand to use for a given Config.162func (c *Config) getRand() *rand.Rand {163 if c.Rand == nil {164 return rand.New(rand.NewSource(0))165 }166 return c.Rand167}168// getMaxCount returns the maximum number of iterations to run for a given169// Config.170func (c *Config) getMaxCount() (maxCount int) {171 maxCount = c.MaxCount172 if maxCount == 0 {173 if c.MaxCountScale != 0 {174 maxCount = int(c.MaxCountScale * float64(*defaultMaxCount))175 } else {176 maxCount = *defaultMaxCount177 }178 }179 return180}181// A SetupError is the result of an error in the way that check is being182// used, independent of the functions being tested.183type SetupError string184func (s SetupError) Error() string { return string(s) }185// A CheckError is the result of Check finding an error.186type CheckError struct {187 Count int188 In []interface{}189}190func (s *CheckError) Error() string {191 return fmt.Sprintf("#%d: failed on input %s", s.Count, toString(s.In))192}193// A CheckEqualError is the result CheckEqual finding an error.194type CheckEqualError struct {195 CheckError196 Out1 []interface{}197 Out2 []interface{}198}199func (s *CheckEqualError) Error() string {200 return fmt.Sprintf("#%d: failed on input %s. Output 1: %s. Output 2: %s", s.Count, toString(s.In), toString(s.Out1), toString(s.Out2))201}202// Check looks for an input to f, any function that returns bool,203// such that f returns false. It calls f repeatedly, with arbitrary204// values for each argument. If f returns false on a given input,205// Check returns that input as a *CheckError.206// For example:207//208// func TestOddMultipleOfThree(t *testing.T) {209// f := func(x int) bool {210// y := OddMultipleOfThree(x)211// return y%2 == 1 && y%3 == 0212// }213// if err := quick.Check(f, nil); err != nil {214// t.Error(err)215// }216// }217func Check(f interface{}, config *Config) (err error) {218 if config == nil {219 config = &defaultConfig220 }221 fVal, fType, ok := functionAndType(f)222 if !ok {223 err = SetupError("argument is not a function")224 return225 }226 if fType.NumOut() != 1 {227 err = SetupError("function does not return one value")228 return229 }230 if fType.Out(0).Kind() != reflect.Bool {231 err = SetupError("function does not return a bool")232 return233 }234 arguments := make([]reflect.Value, fType.NumIn())235 rand := config.getRand()236 maxCount := config.getMaxCount()237 for i := 0; i < maxCount; i++ {238 err = arbitraryValues(arguments, fType, config, rand)239 if err != nil {240 return241 }242 if !fVal.Call(arguments)[0].Bool() {243 err = &CheckError{i + 1, toInterfaces(arguments)}244 return245 }246 }247 return248}249// CheckEqual looks for an input on which f and g return different results.250// It calls f and g repeatedly with arbitrary values for each argument.251// If f and g return different answers, CheckEqual returns a *CheckEqualError252// describing the input and the outputs.253func CheckEqual(f, g interface{}, config *Config) (err error) {254 if config == nil {255 config = &defaultConfig256 }257 x, xType, ok := functionAndType(f)258 if !ok {259 err = SetupError("f is not a function")260 return261 }262 y, yType, ok := functionAndType(g)263 if !ok {264 err = SetupError("g is not a function")265 return266 }267 if xType != yType {268 err = SetupError("functions have different types")269 return270 }271 arguments := make([]reflect.Value, xType.NumIn())272 rand := config.getRand()273 maxCount := config.getMaxCount()274 for i := 0; i < maxCount; i++ {275 err = arbitraryValues(arguments, xType, config, rand)276 if err != nil {277 return278 }279 xOut := toInterfaces(x.Call(arguments))280 yOut := toInterfaces(y.Call(arguments))281 if !reflect.DeepEqual(xOut, yOut) {282 err = &CheckEqualError{CheckError{i + 1, toInterfaces(arguments)}, xOut, yOut}283 return284 }285 }286 return287}288// arbitraryValues writes Values to args such that args contains Values289// suitable for calling f.290func arbitraryValues(args []reflect.Value, f reflect.Type, config *Config, rand *rand.Rand) (err error) {291 if config.Values != nil {292 config.Values(args, rand)293 return294 }295 for j := 0; j < len(args); j++ {296 var ok bool297 args[j], ok = Value(f.In(j), rand)298 if !ok {299 err = SetupError(fmt.Sprintf("cannot create arbitrary value of type %s for argument %d", f.In(j), j))300 return301 }302 }303 return304}305func functionAndType(f interface{}) (v reflect.Value, t reflect.Type, ok bool) {306 v = reflect.ValueOf(f)307 ok = v.Kind() == reflect.Func308 if !ok {309 return310 }311 t = v.Type()312 return313}314func toInterfaces(values []reflect.Value) []interface{} {315 ret := make([]interface{}, len(values))316 for i, v := range values {317 ret[i] = v.Interface()318 }319 return ret320}321func toString(interfaces []interface{}) string {322 s := make([]string, len(interfaces))323 for i, v := range interfaces {324 s[i] = fmt.Sprintf("%#v", v)325 }326 return strings.Join(s, ", ")327}...

Full Screen

Full Screen

handshake_messages_test.go

Source:handshake_messages_test.go Github

copy

Full Screen

...86 r[i] = byte(rand.Int31())87 }88 return r89}90func randomString(n int, rand *rand.Rand) string {91 b := randomBytes(n, rand)92 return string(b)93}94func (*clientHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {95 m := &clientHelloMsg{}96 m.vers = uint16(rand.Intn(65536))97 m.random = randomBytes(32, rand)98 m.sessionId = randomBytes(rand.Intn(32), rand)99 m.cipherSuites = make([]uint16, rand.Intn(63)+1)100 for i := 0; i < len(m.cipherSuites); i++ {101 m.cipherSuites[i] = uint16(rand.Int31())102 }103 m.compressionMethods = randomBytes(rand.Intn(63)+1, rand)104 if rand.Intn(10) > 5 {105 m.nextProtoNeg = true106 }107 if rand.Intn(10) > 5 {108 m.serverName = randomString(rand.Intn(255), rand)109 }110 m.ocspStapling = rand.Intn(10) > 5111 m.supportedPoints = randomBytes(rand.Intn(5)+1, rand)112 m.supportedCurves = make([]CurveID, rand.Intn(5)+1)113 for i := range m.supportedCurves {114 m.supportedCurves[i] = CurveID(rand.Intn(30000))115 }116 if rand.Intn(10) > 5 {117 m.ticketSupported = true118 if rand.Intn(10) > 5 {119 m.sessionTicket = randomBytes(rand.Intn(300), rand)120 }121 }122 if rand.Intn(10) > 5 {123 m.signatureAndHashes = supportedSignatureAlgorithms124 }125 m.alpnProtocols = make([]string, rand.Intn(5))126 for i := range m.alpnProtocols {127 m.alpnProtocols[i] = randomString(rand.Intn(20)+1, rand)128 }129 if rand.Intn(10) > 5 {130 m.scts = true131 }132 return reflect.ValueOf(m)133}134func (*serverHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {135 m := &serverHelloMsg{}136 m.vers = uint16(rand.Intn(65536))137 m.random = randomBytes(32, rand)138 m.sessionId = randomBytes(rand.Intn(32), rand)139 m.cipherSuite = uint16(rand.Int31())140 m.compressionMethod = uint8(rand.Intn(256))141 if rand.Intn(10) > 5 {142 m.nextProtoNeg = true143 n := rand.Intn(10)144 m.nextProtos = make([]string, n)145 for i := 0; i < n; i++ {146 m.nextProtos[i] = randomString(20, rand)147 }148 }149 if rand.Intn(10) > 5 {150 m.ocspStapling = true151 }152 if rand.Intn(10) > 5 {153 m.ticketSupported = true154 }155 m.alpnProtocol = randomString(rand.Intn(32)+1, rand)156 if rand.Intn(10) > 5 {157 numSCTs := rand.Intn(4)158 m.scts = make([][]byte, numSCTs)159 for i := range m.scts {160 m.scts[i] = randomBytes(rand.Intn(500), rand)161 }162 }163 return reflect.ValueOf(m)164}165func (*certificateMsg) Generate(rand *rand.Rand, size int) reflect.Value {166 m := &certificateMsg{}167 numCerts := rand.Intn(20)168 m.certificates = make([][]byte, numCerts)169 for i := 0; i < numCerts; i++ {170 m.certificates[i] = randomBytes(rand.Intn(10)+1, rand)171 }172 return reflect.ValueOf(m)173}174func (*certificateRequestMsg) Generate(rand *rand.Rand, size int) reflect.Value {175 m := &certificateRequestMsg{}176 m.certificateTypes = randomBytes(rand.Intn(5)+1, rand)177 numCAs := rand.Intn(100)178 m.certificateAuthorities = make([][]byte, numCAs)179 for i := 0; i < numCAs; i++ {180 m.certificateAuthorities[i] = randomBytes(rand.Intn(15)+1, rand)181 }182 return reflect.ValueOf(m)183}184func (*certificateVerifyMsg) Generate(rand *rand.Rand, size int) reflect.Value {185 m := &certificateVerifyMsg{}186 m.signature = randomBytes(rand.Intn(15)+1, rand)187 return reflect.ValueOf(m)188}189func (*certificateStatusMsg) Generate(rand *rand.Rand, size int) reflect.Value {190 m := &certificateStatusMsg{}191 if rand.Intn(10) > 5 {192 m.statusType = statusTypeOCSP193 m.response = randomBytes(rand.Intn(10)+1, rand)194 } else {195 m.statusType = 42196 }197 return reflect.ValueOf(m)198}199func (*clientKeyExchangeMsg) Generate(rand *rand.Rand, size int) reflect.Value {200 m := &clientKeyExchangeMsg{}201 m.ciphertext = randomBytes(rand.Intn(1000)+1, rand)202 return reflect.ValueOf(m)203}204func (*finishedMsg) Generate(rand *rand.Rand, size int) reflect.Value {205 m := &finishedMsg{}206 m.verifyData = randomBytes(12, rand)207 return reflect.ValueOf(m)208}209func (*nextProtoMsg) Generate(rand *rand.Rand, size int) reflect.Value {210 m := &nextProtoMsg{}211 m.proto = randomString(rand.Intn(255), rand)212 return reflect.ValueOf(m)213}214func (*newSessionTicketMsg) Generate(rand *rand.Rand, size int) reflect.Value {215 m := &newSessionTicketMsg{}216 m.ticket = randomBytes(rand.Intn(4), rand)217 return reflect.ValueOf(m)218}219func (*sessionState) Generate(rand *rand.Rand, size int) reflect.Value {220 s := &sessionState{}221 s.vers = uint16(rand.Intn(10000))222 s.cipherSuite = uint16(rand.Intn(10000))223 s.masterSecret = randomBytes(rand.Intn(100), rand)224 numCerts := rand.Intn(20)225 s.certificates = make([][]byte, numCerts)...

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 rand.Seed(time.Now().UnixNano())4 fmt.Println(rand.Intn(100))5 fmt.Println(rand.Intn(100))6 fmt.Println(rand.Float64())7 fmt.Println((rand.Float64() * 5) + 5)

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(rand.Int())4 fmt.Println(rand.Intn(100))5 fmt.Println(rand.Float32())6 fmt.Println(rand.Float64())7 fmt.Println(rand.Float64() * 5 + 5)8 fmt.Println(rand.Perm(5))9}10import (11func main() {12 fmt.Println(rand.Int())13 fmt.Println(rand.Intn(100))14 fmt.Println(rand.Float32())15 fmt.Println(rand.Float64())16 fmt.Println(rand.Float64() * 5 + 5)17 fmt.Println(rand.Perm(5))18}19import (20func main() {21 fmt.Println(rand.Int())22 fmt.Println(rand.Intn(100))23 fmt.Println(rand.Float32())24 fmt.Println(rand.Float64())25 fmt.Println(rand.Float64() * 5 + 5)26 fmt.Println(rand.Perm(5))27}28import (29func main() {30 fmt.Println(rand.Int())31 fmt.Println(rand.Intn(100))32 fmt.Println(rand.Float32())33 fmt.Println(rand.Float64())34 fmt.Println(rand.Float64() * 5 + 5)35 fmt.Println(rand.Perm(5))36}37import (38func main() {39 fmt.Println(rand.Int())40 fmt.Println(rand.Intn(100))41 fmt.Println(rand.Float32())42 fmt.Println(rand.Float64())43 fmt.Println(rand.Float64() * 5 + 5)44 fmt.Println(rand.Perm(5))45}46import (47func main() {48 fmt.Println(rand.Int())49 fmt.Println(rand.Intn(100))50 fmt.Println(rand.Float32())51 fmt.Println(rand.Float64())52 fmt.Println(rand.Float64() * 5 + 5)53 fmt.Println(rand.Perm(5))54}55import (

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 rand.Seed(time.Now().Unix())4 fmt.Println(rand.Intn(100))5 fmt.Println(rand.Float64())6 fmt.Println((rand.Float64() * 5) + 5)7 fmt.Println(rand.Intn(100) +

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(randString(10))4}5func randString(n int) string {6 b := make([]byte, n)7 for i := range b {8 b[i] = letterBytes[rand.Intn(len(letterBytes))]9 }10 return string(b)11}

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(rand.String(10))4}5import (6func main() {7 fmt.Println(rand.Int())8}9import (10func main() {11 fmt.Println(rand.Intn(100))12}13import (14func main() {15 fmt.Println(rand.Intn(100))16}17import (18func main() {19 fmt.Println(rand.Intn(100))20}21import (22func main() {23 fmt.Println(rand.Intn(100))24}25import (26func main() {27 fmt.Println(rand.Intn(100))28}29import (30func main() {31 fmt.Println(rand.Intn(100))32}33import (34func main() {35 fmt.Println(rand.Intn(100))36}37import (38func main() {39 fmt.Println(rand.Intn(100))40}41import (42func main() {43 fmt.Println(rand.Int

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(rand.String(6))4 fmt.Println(rand.String(10))5}6import (7func main() {8 fmt.Println(rand.String(6))9 fmt.Println(rand.String(10))10}11import (12func main() {13 fmt.Println(rand.String(6))14 fmt.Println(rand.String(10))15}16import (17func main() {18 fmt.Println(rand.String(6))19 fmt.Println(rand.String(10))20}21import (22func main() {23 fmt.Println(rand.String(6))24 fmt.Println(rand.String(10))25}26import (27func main() {28 fmt.Println(rand.String(6))29 fmt.Println(rand.String(10))30}31import (32func main() {33 fmt.Println(rand.String(6))34 fmt.Println(rand.String(10))35}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testkube automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful