aboutsummaryrefslogtreecommitdiff
path: root/spec/02-random_spec.lua
blob: 23b6d959bb10acc6cad77dc2aa4e616a109cb285 (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
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)

end)