File: test_random.rb

package info (click to toggle)
sonic-pi 3.2.2~repack-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 71,872 kB
  • sloc: ruby: 30,548; cpp: 8,490; sh: 957; ansic: 461; erlang: 360; lisp: 141; makefile: 44
file content (117 lines) | stat: -rw-r--r-- 2,593 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
require_relative "../../setup_test"
require_relative "../../../lib/sonicpi/lang/core"

module SonicPi
  class RandomTester < Minitest::Test
    include SonicPi::Lang::Core

    def test_rand
      rand_reset
      assert_equal(rand, 0.75006103515625)
      assert_equal(rand, 0.733917236328125)
      assert_equal(rand, 0.464202880859375)
      assert_equal(rand, 0.24249267578125)
    end

    def test_rand_reset
      rand_reset
      assert_equal(rand, 0.75006103515625)
      assert_equal(rand, 0.733917236328125)
      rand_reset
      assert_equal(rand, 0.75006103515625)
      assert_equal(rand, 0.733917236328125)
    end

    def test_rand_skip
      rand_reset
      assert_equal(rand, 0.75006103515625)
      rand_skip
      assert_equal(rand, 0.464202880859375)
    end

    def test_rand_skip_with_args
      rand_reset
      assert_equal(rand, 0.75006103515625)
      rand_skip(1)
      assert_equal(rand, 0.464202880859375)
      rand_skip(0)
      assert_equal(rand, 0.24249267578125)
    end

    def test_rand_multi_skip
      rand_reset
      assert_equal(rand, 0.75006103515625)
      rand_skip(2)
      assert_equal(rand, 0.24249267578125)
    end

    def test_rand_multi_skip2
      rand_reset
      99.times{rand}
      a = rand
      rand_reset
      rand_skip(99)
      assert_equal(rand, a)
    end

    def test_rand_back
      rand_reset
      a = rand
      b = rand
      c = rand
      rand_back
      assert_equal(rand, c)
      rand_back(2)
      assert_equal(rand, b)
      assert_equal(rand, c)
      rand_back(3)
      assert_equal(rand, a)
      assert_equal(rand, b)
      assert_equal(rand, c)
    end

    def test_rrand_handles_0_range
      assert_equal(1, rrand(1,1))
    end

    def test_rrand_i_handles_0_range
      assert_equal(1, rrand_i(1,1))
    end

    def test_rand_handles_0
      number = rand(0)
      assert(number >= 0 && number < 1)
    end

    def test_rand_i_handles_0
      number = rand_i(0)
      assert(number == 0)
    end

    def test_rand_only_returns_floats
      assert_equal(Float, rand(0..10).class)
      assert_equal(Float, rand(1).class)
    end

    def test_rand_i_only_returns_ints
      assert_equal(Integer, rand_i(0..10).class)
      assert_equal(Integer, rand_i(1.5).class)
    end

    def test_rand_look
      rand_reset
      assert_equal(rand_look, 0.75006103515625)

      rand_reset
      assert_equal(rand_look(0.5), 0.375030517578125)
    end

    def test_rand_i_look
      rand_reset
      assert_equal(rand_i_look, 1)

      rand_reset
      assert_equal(rand_i_look(100), 75)
    end
  end
end