diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2023-11-15 19:28:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-15 19:28:06 +0100 |
| commit | 90997893cc568c86805afa95db28439c28e32980 (patch) | |
| tree | 4a692fcc91a811e18023ba1896aeee23fe2ee2c5 /spec/02-random_spec.lua | |
| parent | f868c16514be69b20a4e771cc347a80c9c439db6 (diff) | |
| parent | 048f3cec7a18e7a28146f03c3c9e5d89d9613028 (diff) | |
| download | luasystem-90997893cc568c86805afa95db28439c28e32980.tar.gz luasystem-90997893cc568c86805afa95db28439c28e32980.tar.bz2 luasystem-90997893cc568c86805afa95db28439c28e32980.zip | |
Merge pull request #6 from lunarmodules/time
Diffstat (limited to 'spec/02-random_spec.lua')
| -rw-r--r-- | spec/02-random_spec.lua | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/spec/02-random_spec.lua b/spec/02-random_spec.lua new file mode 100644 index 0000000..23b6d95 --- /dev/null +++ b/spec/02-random_spec.lua | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | local system = require("system") | ||
| 2 | |||
| 3 | describe("Random:", function() | ||
| 4 | |||
| 5 | describe("random()", function() | ||
| 6 | |||
| 7 | it("should return random bytes for a valid number of bytes", function() | ||
| 8 | local num_bytes = 1 | ||
| 9 | local result, err_msg = system.random(num_bytes) | ||
| 10 | assert.is_nil(err_msg) | ||
| 11 | assert.is.string(result) | ||
| 12 | assert.is_equal(num_bytes, #result) | ||
| 13 | end) | ||
| 14 | |||
| 15 | |||
| 16 | it("should return an empty string for 0 bytes", function() | ||
| 17 | local num_bytes = 0 | ||
| 18 | local result, err_msg = system.random(num_bytes) | ||
| 19 | assert.is_nil(err_msg) | ||
| 20 | assert.are.equal("", result) | ||
| 21 | end) | ||
| 22 | |||
| 23 | |||
| 24 | it("should return an error message for an invalid number of bytes", function() | ||
| 25 | local num_bytes = -1 | ||
| 26 | local result, err_msg = system.random(num_bytes) | ||
| 27 | assert.is.falsy(result) | ||
| 28 | assert.are.equal("invalid number of bytes, must not be less than 0", err_msg) | ||
| 29 | end) | ||
| 30 | |||
| 31 | |||
| 32 | it("should not return duplicate results", function() | ||
| 33 | local num_bytes = 1025 | ||
| 34 | local result1, err_msg = system.random(num_bytes) | ||
| 35 | assert.is_nil(err_msg) | ||
| 36 | assert.is.string(result1) | ||
| 37 | |||
| 38 | local result2, err_msg = system.random(num_bytes) | ||
| 39 | assert.is_nil(err_msg) | ||
| 40 | assert.is.string(result2) | ||
| 41 | |||
| 42 | assert.is_not.equal(result1, result2) | ||
| 43 | end) | ||
| 44 | |||
| 45 | end) | ||
| 46 | |||
| 47 | end) | ||
