aboutsummaryrefslogtreecommitdiff
path: root/spec/02-random_spec.lua
blob: 6a8ea5b1db74ed6013bd832016ea9ae0969e1250 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
local system = require("system")

describe("Random:", function()

  describe("random()", function()

    it("should return random bytes for a valid number of bytes", function()
        local num_bytes = 1
        local result, err_msg = system.random(num_bytes)
        assert.is_nil(err_msg)
        assert.is.string(result)
        assert.is_equal(num_bytes, #result)
    end)


    it("should return an empty string for 0 bytes", function()
        local num_bytes = 0
        local result, err_msg = system.random(num_bytes)
        assert.is_nil(err_msg)
        assert.are.equal("", result)
    end)


    it("should return an error message for an invalid number of bytes", function()
        local num_bytes = -1
        local result, err_msg = system.random(num_bytes)
        assert.is.falsy(result)
        assert.are.equal("invalid number of bytes, must not be less than 0", err_msg)
    end)


    it("should not return duplicate results", function()
      local num_bytes = 1025
      local result1, err_msg = system.random(num_bytes)
      assert.is_nil(err_msg)
      assert.is.string(result1)

      local result2, err_msg = system.random(num_bytes)
      assert.is_nil(err_msg)
      assert.is.string(result2)

      assert.is_not.equal(result1, result2)
    end)

  end)



  describe("rnd()", function()

    local has_math_type = type(math.type) == "function"
    local is_luajit = (type(jit) == "table" and jit.version ~= nil)

    it("with no args returns a number in [0, 1)", function()
      local v, err = system.rnd()
      assert.is_nil(err)
      assert.is_number(v)
      assert.is_true(v >= 0 and v < 1)
    end)


    it("with no args returns different values on multiple calls", function()
      local seen = {}
      for _ = 1, 20 do
        local v = system.rnd()
        seen[v] = true
      end
      local count = 0
      for _ in pairs(seen) do count = count + 1 end
      assert.is_true(count >= 2, "expected at least 2 distinct values in 20 calls")
    end)


    it("with one arg m returns integer in [1, m]", function()
      for _ = 1, 50 do
        local v, err = system.rnd(6)
        assert.is_nil(err)
        assert.is_true(v >= 1 and v <= 6 and math.floor(v) == v)
      end
    end)


    it("matches math.random type behaviour for one-arg m when available", function()
      if not has_math_type then
        return
      end
      -- use math.random as ground truth for numeric type
      local mval = math.random(6)
      local rval = system.rnd(6)
      assert.are.equal(math.type(mval), math.type(rval))
    end)


    it("with one arg 1 always returns 1", function()
      for _ = 1, 10 do
        local v, err = system.rnd(1)
        assert.is_nil(err)
        assert.are.equal(1, v)
      end
    end)


    it("with one arg 0 returns a full-range integer", function()
      local v, err = system.rnd(0)
      assert.is_nil(err)
      assert.is_true(type(v) == "number" or (math.type and math.type(v) == "integer"))
      assert.is_true(v >= -0x8000000000000000 and v <= 0x7fffffffffffffff)
    end)


    it("matches math.random type behaviour for arg 0 when math.random(0) is supported", function()
      if not has_math_type then
        return
      end
      local ok, mval = pcall(math.random, 0)
      if not ok then
        return -- older Lua where math.random(0) is not supported
      end
      local rval = system.rnd(0)
      assert.are.equal(math.type(mval), math.type(rval))
    end)


    it("with two args returns integer in [m, n]", function()
      for _ = 1, 30 do
        local v, err = system.rnd(10, 20)
        assert.is_nil(err)
        assert.is_true(v >= 10 and v <= 20 and math.floor(v) == v)
      end
    end)


    it("matches math.random type behaviour for two-arg range when available", function()
      if not has_math_type then
        return
      end
      local mval = math.random(10, 20)
      local rval = system.rnd(10, 20)
      assert.are.equal(math.type(mval), math.type(rval))
    end)


    it("with two args supports negative range", function()
      for _ = 1, 30 do
        local v, err = system.rnd(-5, 5)
        assert.is_nil(err)
        assert.is_true(v >= -5 and v <= 5 and math.floor(v) == v)
      end
    end)


    it("with two equal args returns that value", function()
      local v, err = system.rnd(7, 7)
      assert.is_nil(err)
      assert.are.equal(7, v)
    end)


    if not is_luajit then
      -- LuaJIT's math.random does not raise errors for these invalid-argument
      -- cases (it accepts empty intervals, negatives, extra args, etc.),
      -- while stock Lua does. We only run these strict "throws like math.random"
      -- checks on non-LuaJIT runtimes.

      it("throws for empty interval (m > n), like math.random", function()
        local ok_math, _ = pcall(math.random, 10, 5)
        assert.is_falsy(ok_math, "math.random(10, 5) should error")
        local ok_rnd, _ = pcall(system.rnd, 10, 5)
        assert.is_falsy(ok_rnd, "rnd(10, 5) should throw like math.random")
      end)


      it("throws for invalid one-arg (m < 1, m ~= 0), like math.random", function()
        local ok_math, _ = pcall(math.random, -1)
        assert.is_falsy(ok_math, "math.random(-1) should error")
        local ok_rnd, _ = pcall(system.rnd, -1)
        assert.is_falsy(ok_rnd, "rnd(-1) should throw like math.random")
      end)


      it("throws for wrong number of arguments, like math.random", function()
        local ok_math, _ = pcall(math.random, 1, 2, 3)
        assert.is_falsy(ok_math, "math.random(1, 2, 3) should error")
        local ok_rnd, _ = pcall(system.rnd, 1, 2, 3)
        assert.is_falsy(ok_rnd, "rnd(1, 2, 3) should throw like math.random")
      end)

    end

  end)

end)