summaryrefslogtreecommitdiffstats
path: root/src/testdir
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2019-11-25 15:40:55 +0100
committerBram Moolenaar <Bram@vim.org>2019-11-25 15:40:55 +0100
commit06b0b4bc27077013e9b4b48fd1d9b33e543ccf99 (patch)
tree91ae8240070decf4e90b64d3bd0dd1d2a75433ae /src/testdir
parent67a2deb9cb4ac2224cb1e4d240a5d0659f036264 (diff)
patch 8.1.2342: random number generator in Vim script is slowv8.1.2342
Problem: Random number generator in Vim script is slow. Solution: Add rand() and srand(). (Yasuhiro Matsumoto, closes #1277)
Diffstat (limited to 'src/testdir')
-rw-r--r--src/testdir/Make_all.mak2
-rw-r--r--src/testdir/test_random.vim28
2 files changed, 30 insertions, 0 deletions
diff --git a/src/testdir/Make_all.mak b/src/testdir/Make_all.mak
index ac254202a8..f6d6c17176 100644
--- a/src/testdir/Make_all.mak
+++ b/src/testdir/Make_all.mak
@@ -211,6 +211,7 @@ NEW_TESTS = \
test_pyx3 \
test_quickfix \
test_quotestar \
+ test_random \
test_recover \
test_regex_char_classes \
test_regexp_latin \
@@ -403,6 +404,7 @@ NEW_TESTS_RES = \
test_pyx3.res \
test_quickfix.res \
test_quotestar.res \
+ test_random.res \
test_regex_char_classes.res \
test_registers.res \
test_restricted.res \
diff --git a/src/testdir/test_random.vim b/src/testdir/test_random.vim
new file mode 100644
index 0000000000..381475a43f
--- /dev/null
+++ b/src/testdir/test_random.vim
@@ -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