From 22f0725aac300ed9b249f995df7889f6c203d1e0 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sun, 9 Jan 2022 22:48:29 +0000 Subject: 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 vim/vim#1277) https://github.com/vim/vim/commit/06b0b4bc27077013e9b4b48fd1d9b33e543ccf99 Add missing method call usage to builtin.txt. vim_time and test_settime is N/A. Add a modeline to test_random.vim. Use typval_T* over listitem_T* vars so we don't need to use TV_LIST_ITEM_TV all over the place... Remove NULL list checks (tv_list_len covers this). --- src/nvim/testdir/test_random.vim | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/nvim/testdir/test_random.vim (limited to 'src/nvim/testdir') diff --git a/src/nvim/testdir/test_random.vim b/src/nvim/testdir/test_random.vim new file mode 100644 index 0000000000..bde034a96c --- /dev/null +++ b/src/nvim/testdir/test_random.vim @@ -0,0 +1,31 @@ +" 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)) + + " Nvim does not support test_settime + " 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 + +" vim: shiftwidth=2 sts=2 expandtab -- cgit From f6a0d5498b5f0d62e10f7ba891bc6ea5e20dad69 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sun, 9 Jan 2022 23:53:55 +0000 Subject: vim-patch:8.1.2343: using time() for srand() is not very random Problem: Using time() for srand() is not very random. Solution: use /dev/urandom if available https://github.com/vim/vim/commit/07e4a197953d12902fb97beb48830a5323a52280 Use os_open and os_close. time_settime is N/A, so some parts of the test are disabled. There's maybe a very, very, very, very small chance the /dev/urandom test fails, but it shouldn't matter. :P --- src/nvim/testdir/test_random.vim | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/nvim/testdir') diff --git a/src/nvim/testdir/test_random.vim b/src/nvim/testdir/test_random.vim index bde034a96c..46091836d4 100644 --- a/src/nvim/testdir/test_random.vim +++ b/src/nvim/testdir/test_random.vim @@ -11,10 +11,16 @@ func Test_Rand() " Nvim does not support test_settime " call test_settime(12341234) - " let s = srand() - " call assert_equal(s, srand()) - " call test_settime(12341235) - " call assert_notequal(s, srand()) + let s = srand() + if filereadable('/dev/urandom') + " using /dev/urandom + call assert_notequal(s, srand()) + " else + " " using time() + " call assert_equal(s, srand()) + " call test_settime(12341235) + " call assert_notequal(s, srand()) + endif call srand() let v = rand() @@ -26,6 +32,8 @@ func Test_Rand() 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 test_settime(0) endfunc " vim: shiftwidth=2 sts=2 expandtab -- cgit From c97614d98fc7ab040851b7fe1bc4cb575ce8c627 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sun, 9 Jan 2022 23:26:03 +0000 Subject: vim-patch:8.1.2356: rand() does not use the best algorithm Problem: rand() does not use the best algorithm. Solution: use xoshiro128** instead of xorshift. (Kaito Udagawa, closes vim/vim#5279) https://github.com/vim/vim/commit/f8c1f9200c4b50969a8191a4fe0b0d09edb38979 --- src/nvim/testdir/test_random.vim | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/nvim/testdir') diff --git a/src/nvim/testdir/test_random.vim b/src/nvim/testdir/test_random.vim index 46091836d4..ed98433b94 100644 --- a/src/nvim/testdir/test_random.vim +++ b/src/nvim/testdir/test_random.vim @@ -2,12 +2,12 @@ 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 assert_equal([1573771921, 319883699, 2742014374, 1324369493], r) + call assert_equal(4284103975, rand(r)) + call assert_equal(1001954530, rand(r)) + call assert_equal(2701803082, rand(r)) + call assert_equal(2658065534, rand(r)) + call assert_equal(3104308804, rand(r)) " Nvim does not support test_settime " call test_settime(12341234) -- cgit From cc7ccf6d31d14457070055be07884edaf2eb165f Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 10 Jan 2022 00:49:07 +0000 Subject: vim-patch:8.1.2357: no test with wrong argument for rand() Problem: No test with wrong argument for rand(). Solution: Add a test case. https://github.com/vim/vim/commit/68e9e5f7fccb8038cf0ca5b5d95c85a923152f46 --- src/nvim/testdir/test_random.vim | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/nvim/testdir') diff --git a/src/nvim/testdir/test_random.vim b/src/nvim/testdir/test_random.vim index ed98433b94..f953b93d51 100644 --- a/src/nvim/testdir/test_random.vim +++ b/src/nvim/testdir/test_random.vim @@ -26,7 +26,11 @@ func Test_Rand() let v = rand() call assert_notequal(v, rand()) + if has('float') + call assert_fails('echo srand(1.2)', 'E805:') + endif call assert_fails('echo srand([1])', 'E745:') + call assert_fails('echo rand("burp")', 'E475:') 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:') -- cgit From 4f7a8991a93ddb1b6ab7cd8a8f21b5197c4612bb Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 10 Jan 2022 10:31:16 +0000 Subject: vim-patch:8.2.0233: crash when using garbagecollect() in between rand() Problem: Crash when using garbagecollect() in between rand(). Solution: Redesign the rand() and srand() implementation. (Yasuhiro Matsumoto, closes vim/vim#5587, closes vim/vim#5588) https://github.com/vim/vim/commit/4f645c54efe33d7a11e314676e503118761f08a7 Omit test_srand_seed. Unmacroify SHUFFLE_XOSHIRO128STARSTAR and SPLITMIX32 while we're at it (leave ROTL alone as it's fairly innocent). --- src/nvim/testdir/test_random.vim | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/nvim/testdir') diff --git a/src/nvim/testdir/test_random.vim b/src/nvim/testdir/test_random.vim index f953b93d51..6d3f7dcfd9 100644 --- a/src/nvim/testdir/test_random.vim +++ b/src/nvim/testdir/test_random.vim @@ -12,7 +12,7 @@ func Test_Rand() " Nvim does not support test_settime " call test_settime(12341234) let s = srand() - if filereadable('/dev/urandom') + if !has('win32') && filereadable('/dev/urandom') " using /dev/urandom call assert_notequal(s, srand()) " else @@ -22,9 +22,11 @@ func Test_Rand() " call assert_notequal(s, srand()) endif - call srand() - let v = rand() - call assert_notequal(v, rand()) + " Nvim does not support test_srand_seed + " call test_srand_seed(123456789) + " call assert_equal(4284103975, rand()) + " call assert_equal(1001954530, rand()) + " call test_srand_seed() if has('float') call assert_fails('echo srand(1.2)', 'E805:') @@ -40,4 +42,10 @@ func Test_Rand() " call test_settime(0) endfunc +func Test_issue_5587() + call rand() + call garbagecollect() + call rand() +endfunc + " vim: shiftwidth=2 sts=2 expandtab -- cgit