How to use init method of rand Package

Best Testkube code snippet using rand.init

messages_test.go

Source:messages_test.go Github

copy

Full Screen

1// Copyright 2011 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4package ssh5import (6 "bytes"7 "math/big"8 "math/rand"9 "reflect"10 "testing"11 "testing/quick"12)13var intLengthTests = []struct {14 val, length int15}{16 {0, 4 + 0},17 {1, 4 + 1},18 {127, 4 + 1},19 {128, 4 + 2},20 {-1, 4 + 1},21}22func TestIntLength(t *testing.T) {23 for _, test := range intLengthTests {24 v := new(big.Int).SetInt64(int64(test.val))25 length := intLength(v)26 if length != test.length {27 t.Errorf("For %d, got length %d but expected %d", test.val, length, test.length)28 }29 }30}31type msgAllTypes struct {32 Bool bool `sshtype:"21"`33 Array [16]byte34 Uint64 uint6435 Uint32 uint3236 Uint8 uint837 String string38 Strings []string39 Bytes []byte40 Int *big.Int41 Rest []byte `ssh:"rest"`42}43func (t *msgAllTypes) Generate(rand *rand.Rand, size int) reflect.Value {44 m := &msgAllTypes{}45 m.Bool = rand.Intn(2) == 146 randomBytes(m.Array[:], rand)47 m.Uint64 = uint64(rand.Int63n(1<<63 - 1))48 m.Uint32 = uint32(rand.Intn((1 << 31) - 1))49 m.Uint8 = uint8(rand.Intn(1 << 8))50 m.String = string(m.Array[:])51 m.Strings = randomNameList(rand)52 m.Bytes = m.Array[:]53 m.Int = randomInt(rand)54 m.Rest = m.Array[:]55 return reflect.ValueOf(m)56}57func TestMarshalUnmarshal(t *testing.T) {58 rand := rand.New(rand.NewSource(0))59 iface := &msgAllTypes{}60 ty := reflect.ValueOf(iface).Type()61 n := 10062 if testing.Short() {63 n = 564 }65 for j := 0; j < n; j++ {66 v, ok := quick.Value(ty, rand)67 if !ok {68 t.Errorf("failed to create value")69 break70 }71 m1 := v.Elem().Interface()72 m2 := iface73 marshaled := Marshal(m1)74 if err := Unmarshal(marshaled, m2); err != nil {75 t.Errorf("Unmarshal %#v: %s", m1, err)76 break77 }78 if !reflect.DeepEqual(v.Interface(), m2) {79 t.Errorf("got: %#v\nwant:%#v\n%x", m2, m1, marshaled)80 break81 }82 }83}84func TestUnmarshalEmptyPacket(t *testing.T) {85 var b []byte86 var m channelRequestSuccessMsg87 if err := Unmarshal(b, &m); err == nil {88 t.Fatalf("unmarshal of empty slice succeeded")89 }90}91func TestUnmarshalUnexpectedPacket(t *testing.T) {92 type S struct {93 I uint32 `sshtype:"43"`94 S string95 B bool96 }97 s := S{11, "hello", true}98 packet := Marshal(s)99 packet[0] = 42100 roundtrip := S{}101 err := Unmarshal(packet, &roundtrip)102 if err == nil {103 t.Fatal("expected error, not nil")104 }105}106func TestMarshalPtr(t *testing.T) {107 s := struct {108 S string109 }{"hello"}110 m1 := Marshal(s)111 m2 := Marshal(&s)112 if !bytes.Equal(m1, m2) {113 t.Errorf("got %q, want %q for marshaled pointer", m2, m1)114 }115}116func TestBareMarshalUnmarshal(t *testing.T) {117 type S struct {118 I uint32119 S string120 B bool121 }122 s := S{42, "hello", true}123 packet := Marshal(s)124 roundtrip := S{}125 Unmarshal(packet, &roundtrip)126 if !reflect.DeepEqual(s, roundtrip) {127 t.Errorf("got %#v, want %#v", roundtrip, s)128 }129}130func TestBareMarshal(t *testing.T) {131 type S2 struct {132 I uint32133 }134 s := S2{42}135 packet := Marshal(s)136 i, rest, ok := parseUint32(packet)137 if len(rest) > 0 || !ok {138 t.Errorf("parseInt(%q): parse error", packet)139 }140 if i != s.I {141 t.Errorf("got %d, want %d", i, s.I)142 }143}144func TestUnmarshalShortKexInitPacket(t *testing.T) {145 // This used to panic.146 // Issue 11348147 packet := []byte{0x14, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xff, 0xff, 0xff, 0xff}148 kim := &kexInitMsg{}149 if err := Unmarshal(packet, kim); err == nil {150 t.Error("truncated packet unmarshaled without error")151 }152}153func TestMarshalMultiTag(t *testing.T) {154 var res struct {155 A uint32 `sshtype:"1|2"`156 }157 good1 := struct {158 A uint32 `sshtype:"1"`159 }{160 1,161 }162 good2 := struct {163 A uint32 `sshtype:"2"`164 }{165 1,166 }167 if e := Unmarshal(Marshal(good1), &res); e != nil {168 t.Errorf("error unmarshaling multipart tag: %v", e)169 }170 if e := Unmarshal(Marshal(good2), &res); e != nil {171 t.Errorf("error unmarshaling multipart tag: %v", e)172 }173 bad1 := struct {174 A uint32 `sshtype:"3"`175 }{176 1,177 }178 if e := Unmarshal(Marshal(bad1), &res); e == nil {179 t.Errorf("bad struct unmarshaled without error")180 }181}182func randomBytes(out []byte, rand *rand.Rand) {183 for i := 0; i < len(out); i++ {184 out[i] = byte(rand.Int31())185 }186}187func randomNameList(rand *rand.Rand) []string {188 ret := make([]string, rand.Int31()&15)189 for i := range ret {190 s := make([]byte, 1+(rand.Int31()&15))191 for j := range s {192 s[j] = 'a' + uint8(rand.Int31()&15)193 }194 ret[i] = string(s)195 }196 return ret197}198func randomInt(rand *rand.Rand) *big.Int {199 return new(big.Int).SetInt64(int64(int32(rand.Uint32())))200}201func (*kexInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {202 ki := &kexInitMsg{}203 randomBytes(ki.Cookie[:], rand)204 ki.KexAlgos = randomNameList(rand)205 ki.ServerHostKeyAlgos = randomNameList(rand)206 ki.CiphersClientServer = randomNameList(rand)207 ki.CiphersServerClient = randomNameList(rand)208 ki.MACsClientServer = randomNameList(rand)209 ki.MACsServerClient = randomNameList(rand)210 ki.CompressionClientServer = randomNameList(rand)211 ki.CompressionServerClient = randomNameList(rand)212 ki.LanguagesClientServer = randomNameList(rand)213 ki.LanguagesServerClient = randomNameList(rand)214 if rand.Int31()&1 == 1 {215 ki.FirstKexFollows = true216 }217 return reflect.ValueOf(ki)218}219func (*kexDHInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {220 dhi := &kexDHInitMsg{}221 dhi.X = randomInt(rand)222 return reflect.ValueOf(dhi)223}224var (225 _kexInitMsg = new(kexInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface()226 _kexDHInitMsg = new(kexDHInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface()227 _kexInit = Marshal(_kexInitMsg)228 _kexDHInit = Marshal(_kexDHInitMsg)229)230func BenchmarkMarshalKexInitMsg(b *testing.B) {231 for i := 0; i < b.N; i++ {232 Marshal(_kexInitMsg)233 }234}235func BenchmarkUnmarshalKexInitMsg(b *testing.B) {236 m := new(kexInitMsg)237 for i := 0; i < b.N; i++ {238 Unmarshal(_kexInit, m)239 }240}241func BenchmarkMarshalKexDHInitMsg(b *testing.B) {242 for i := 0; i < b.N; i++ {243 Marshal(_kexDHInitMsg)244 }245}246func BenchmarkUnmarshalKexDHInitMsg(b *testing.B) {247 m := new(kexDHInitMsg)248 for i := 0; i < b.N; i++ {249 Unmarshal(_kexDHInit, m)250 }251}...

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("My favorite number is", rand.Intn(10))4}5import (6func main() {7 fmt.Println("Welcome to the playground!")8 fmt.Println("The time is", time.Now())9}10import (11func main() {12 fmt.Println("Welcome to the playground!")13 fmt.Println("The time is", time.Now())14}15import (16func main() {17 fmt.Println("Welcome to the playground!")18}19import (20func main() {21 fmt.Println("Welcome to the playground!")22}23import (24func main() {25 fmt.Println("Welcome to the playground!")26}27import (28func main() {29 fmt.Println("Welcome to the playground!")30}31import (32func main() {33 fmt.Println("Welcome to the playground!")34}35import (36func main() {37 fmt.Println("Welcome to the playground!")38}39import (40func main() {41 fmt.Println("Welcome to the playground!")42}43import (44func main() {45 fmt.Println("Welcome to the playground!")46}47import (

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(rand.Intn(100))4}5import (6func main() {7 fmt.Println(rand.Intn(100))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.Intn(100))44}45import (46func main() {47 fmt.Println(rand.Intn(100))

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(rand.Intn(100))4}5import (6func main() {7 fmt.Println(rand.Intn(100))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.Intn(100))44}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(rand.Intn(100))4}5import (6func main() {7 fmt.Println(time.Now())8}9import (10func main() {11 fmt.Println("Hello World")12}13import (14func main() {15 fmt.Println(strings.ToUpper("hello"))16}17import (18func main() {19 fmt.Println(strconv.Atoi("123"))20}21import (22func main() {23 fmt.Println(os.Getwd())24}25import (26func main() {27 fmt.Println(io.EOF)28}29import (30func main() {31 fmt.Println(bufio.ErrInvalidUnreadByte)32}33import (34func main() {35 fmt.Println(sync.RWMutex{})36}37import (38func main() {39 fmt.Println(flag.ErrHelp)40}41import (42func main() {43 fmt.Println(log.Ldate)44}45import (46func main() {47 fmt.Println(regexp.MatchString("a(b", "abc"))48}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("A number from 1-100", rand.Intn(100))4}5import (6func main() {7 rand.Seed(time.Now().UnixNano())8 fmt.Println("A number from 1-100", rand.Intn(100))9}10import (11func main() {12 fmt.Println("The time is", time.Now())13}14import (15func main() {16 fmt.Println("The value of Pi is", math.Pi)17}18import (19func main() {20 fmt.Println("Contains: ", strings.Contains("test", "es"))21 fmt.Println("Count: ", strings.Count("test", "t"))22 fmt.Println("HasPrefix: ", strings.HasPrefix("test", "te"))23 fmt.Println("HasSuffix: ", strings.HasSuffix("test", "st"))24 fmt.Println("Index: ", strings.Index("test", "e"))25 fmt.Println("Join: ", strings.Join([]string{"a", "b"}, "-"))

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