xgo/stdlib/rand_test.go
Daniel 3500c686b3
Improvements on compiler/VM error reporting (filename:line:col)
- add type infos to VM error messages
- add 'Name' to UserFunction objects 
- add 'expectErrorString' to VM tests
- replace vm.expectError() with vm.expectErrorString() to make it more explicit
- add source map info to VM error messages
- optimization in function calls
- add file/line/col info to compiler errors
- change stdlib module to be loaded from VM (instead of compiler) so they can be properly loaded after the source is compiled into binary
- VM can take builtin modules optionally
2019-02-20 16:26:11 -08:00

45 lines
1.4 KiB
Go

package stdlib_test
import (
"math/rand"
"testing"
"github.com/d5/tengo/assert"
"github.com/d5/tengo/objects"
)
func TestRand(t *testing.T) {
var seed int64 = 1234
r := rand.New(rand.NewSource(seed))
module(t, "rand").call("seed", seed).expect(objects.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 := &objects.Bytes{Value: make([]byte, 10)}
n, _ := r.Read(buf1)
module(t, "rand").call("read", buf2).expect(n)
assert.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(objects.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 = &objects.Bytes{Value: make([]byte, 12)}
n, _ = r.Read(buf1)
randObj.call("read", buf2).expect(n)
assert.Equal(t, buf1, buf2.Value)
}