1
0
forked from aniani/vim

patch 8.1.2342: random number generator in Vim script is slow

Problem:    Random number generator in Vim script is slow.
Solution:   Add rand() and srand(). (Yasuhiro Matsumoto, closes #1277)
This commit is contained in:
Bram Moolenaar
2019-11-25 15:40:55 +01:00
parent 67a2deb9cb
commit 06b0b4bc27
5 changed files with 163 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
" Tests for srand() and rand()
func Test_Rand()
let r = srand(123456789)
call assert_equal([123456789, 362436069, 521288629, 88675123], r)
call assert_equal(3701687786, rand(r))
call assert_equal(458299110, rand(r))
call assert_equal(2500872618, rand(r))
call assert_equal(3633119408, rand(r))
call assert_equal(516391518, rand(r))
call test_settime(12341234)
let s = srand()
call assert_equal(s, srand())
call test_settime(12341235)
call assert_notequal(s, srand())
call srand()
let v = rand()
call assert_notequal(v, rand())
call assert_fails('echo srand([1])', 'E745:')
call assert_fails('echo rand([1, 2, 3])', 'E475:')
call assert_fails('echo rand([[1], 2, 3, 4])', 'E475:')
call assert_fails('echo rand([1, [2], 3, 4])', 'E475:')
call assert_fails('echo rand([1, 2, [3], 4])', 'E475:')
call assert_fails('echo rand([1, 2, 3, [4]])', 'E475:')
endfunc