File: rand_test.go

package info (click to toggle)
golang-github-d5-tengo 2.17.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,240 kB
  • sloc: makefile: 12
file content (45 lines) | stat: -rw-r--r-- 1,397 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package stdlib_test

import (
	"math/rand"
	"testing"

	"github.com/d5/tengo/v2"
	"github.com/d5/tengo/v2/require"
)

func TestRand(t *testing.T) {
	var seed int64 = 1234
	r := rand.New(rand.NewSource(seed))

	module(t, "rand").call("seed", seed).expect(tengo.UndefinedValue)
	module(t, "rand").call("int").expect(r.Int63())
	module(t, "rand").call("float").expect(r.Float64())
	module(t, "rand").call("intn", 111).expect(r.Int63n(111))
	module(t, "rand").call("exp_float").expect(r.ExpFloat64())
	module(t, "rand").call("norm_float").expect(r.NormFloat64())
	module(t, "rand").call("perm", 10).expect(r.Perm(10))

	buf1 := make([]byte, 10)
	buf2 := &tengo.Bytes{Value: make([]byte, 10)}
	n, _ := r.Read(buf1)
	module(t, "rand").call("read", buf2).expect(n)
	require.Equal(t, buf1, buf2.Value)

	seed = 9191
	r = rand.New(rand.NewSource(seed))
	randObj := module(t, "rand").call("rand", seed)
	randObj.call("seed", seed).expect(tengo.UndefinedValue)
	randObj.call("int").expect(r.Int63())
	randObj.call("float").expect(r.Float64())
	randObj.call("intn", 111).expect(r.Int63n(111))
	randObj.call("exp_float").expect(r.ExpFloat64())
	randObj.call("norm_float").expect(r.NormFloat64())
	randObj.call("perm", 10).expect(r.Perm(10))

	buf1 = make([]byte, 12)
	buf2 = &tengo.Bytes{Value: make([]byte, 12)}
	n, _ = r.Read(buf1)
	randObj.call("read", buf2).expect(n)
	require.Equal(t, buf1, buf2.Value)
}