test_random.vim 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. " Tests for srand() and rand()
  2. source check.vim
  3. source shared.vim
  4. func Test_Rand()
  5. let r = srand(123456789)
  6. call assert_equal([1573771921, 319883699, 2742014374, 1324369493], r)
  7. call assert_equal(4284103975, rand(r))
  8. call assert_equal(1001954530, rand(r))
  9. call assert_equal(2701803082, rand(r))
  10. call assert_equal(2658065534, rand(r))
  11. call assert_equal(3104308804, rand(r))
  12. let s = srand()
  13. " using /dev/urandom or used time, result is different each time
  14. call assert_notequal(s, srand())
  15. " Nvim does not support test_srand_seed
  16. " call test_srand_seed(123456789)
  17. " call assert_equal(4284103975, rand())
  18. " call assert_equal(1001954530, rand())
  19. " call test_srand_seed()
  20. if has('float')
  21. call assert_fails('echo srand(1.2)', 'E805:')
  22. endif
  23. call assert_fails('echo srand([1])', 'E745:')
  24. call assert_fails('echo rand("burp")', 'E475:')
  25. call assert_fails('echo rand([1, 2, 3])', 'E730:')
  26. call assert_fails('echo rand([[1], 2, 3, 4])', 'E730:')
  27. call assert_fails('echo rand([1, [2], 3, 4])', 'E730:')
  28. call assert_fails('echo rand([1, 2, [3], 4])', 'E730:')
  29. call assert_fails('echo rand([1, 2, 3, [4]])', 'E730:')
  30. endfunc
  31. func Test_issue_5587()
  32. call rand()
  33. call garbagecollect()
  34. call rand()
  35. endfunc
  36. func Test_srand()
  37. CheckNotGui
  38. let cmd = GetVimCommand() .. ' -V -es -c "echo rand()" -c qa!'
  39. let bad = 0
  40. for _ in range(10)
  41. echo cmd
  42. let result1 = system(cmd)
  43. let result2 = system(cmd)
  44. if result1 ==# result2
  45. let bad += 1
  46. endif
  47. endfor
  48. call assert_inrange(0, 4, bad)
  49. endfunc
  50. " vim: shiftwidth=2 sts=2 expandtab