diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/01-time_spec.lua | 76 | ||||
-rw-r--r-- | spec/02-random_spec.lua | 47 | ||||
-rw-r--r-- | spec/03-environment_spec.lua | 81 | ||||
-rw-r--r-- | spec/time_spec.lua | 31 |
4 files changed, 204 insertions, 31 deletions
diff --git a/spec/01-time_spec.lua b/spec/01-time_spec.lua new file mode 100644 index 0000000..1607cca --- /dev/null +++ b/spec/01-time_spec.lua | |||
@@ -0,0 +1,76 @@ | |||
1 | local system = require 'system.core' | ||
2 | |||
3 | describe('Test time functions', function() | ||
4 | |||
5 | -- returns the new second, on the new second | ||
6 | local function wait_for_second_rollover() | ||
7 | local start_time = math.floor(os.time()) | ||
8 | local end_time = math.floor(os.time()) | ||
9 | while end_time == start_time do | ||
10 | end_time = math.floor(os.time()) | ||
11 | end | ||
12 | return end_time | ||
13 | end | ||
14 | |||
15 | |||
16 | describe("time()", function() | ||
17 | |||
18 | it('returns current time', function() | ||
19 | local expected_time = wait_for_second_rollover() | ||
20 | local received_time = system.gettime() | ||
21 | assert.is.near(expected_time, received_time, 0.02) | ||
22 | |||
23 | wait_for_second_rollover() | ||
24 | assert.is.near(1, system.gettime() - received_time, 0.02) | ||
25 | end) | ||
26 | |||
27 | end) | ||
28 | |||
29 | |||
30 | |||
31 | describe("monotime()", function() | ||
32 | |||
33 | it('returns monotonically increasing time', function() | ||
34 | local starttime = system.monotime() | ||
35 | local endtime = system.monotime() | ||
36 | local delta = endtime - starttime | ||
37 | assert.is_true(starttime > 0) | ||
38 | assert.is_true(delta >= 0) | ||
39 | assert.is_true(system.monotime() - endtime >= 0) | ||
40 | end) | ||
41 | |||
42 | end) | ||
43 | |||
44 | |||
45 | |||
46 | describe("sleep()", function() | ||
47 | |||
48 | it("should sleep for the specified time", function() | ||
49 | local start_time = system.gettime() | ||
50 | system.sleep(1, 1) | ||
51 | local end_time = system.gettime() | ||
52 | local elapsed_time = end_time - start_time | ||
53 | assert.is.near(elapsed_time, 1, 0.2) -- large marging of error due to CI priorities | ||
54 | end) | ||
55 | |||
56 | |||
57 | it("should sleep for the specified time; fractional", function() | ||
58 | local start_time = system.gettime() | ||
59 | system.sleep(0.5, 1) | ||
60 | local end_time = system.gettime() | ||
61 | local elapsed_time = end_time - start_time | ||
62 | assert.is.near(0.5, elapsed_time, 0.2) -- large marging of error due to CI priorities | ||
63 | end) | ||
64 | |||
65 | |||
66 | it("should return immediately for a non-positive sleep time", function() | ||
67 | local start_time = system.gettime() | ||
68 | system.sleep(-1) | ||
69 | local end_time = system.gettime() | ||
70 | local elapsed_time = end_time - start_time | ||
71 | assert.is.near(elapsed_time, 0, 0.01) | ||
72 | end) | ||
73 | |||
74 | end) | ||
75 | |||
76 | end) | ||
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) | ||
diff --git a/spec/03-environment_spec.lua b/spec/03-environment_spec.lua new file mode 100644 index 0000000..842ed6f --- /dev/null +++ b/spec/03-environment_spec.lua | |||
@@ -0,0 +1,81 @@ | |||
1 | -- Import the library that contains the environment-related functions | ||
2 | local system = require("system") | ||
3 | |||
4 | describe("Environment Variables:", function() | ||
5 | |||
6 | describe("setenv()", function() | ||
7 | |||
8 | it("should set an environment variable", function() | ||
9 | assert.is_true(system.setenv("TEST_VAR", "test_value")) | ||
10 | assert.is_equal("test_value", system.getenv("TEST_VAR")) | ||
11 | end) | ||
12 | |||
13 | |||
14 | local func = system.windows and pending or it --pending on Windows | ||
15 | -- Windows will unset a variable if set as an empty string | ||
16 | func("should set an empty environment variable value", function() | ||
17 | assert.is_true(system.setenv("TEST_VAR", "")) | ||
18 | assert.is_equal("", system.getenv("TEST_VAR")) | ||
19 | end) | ||
20 | |||
21 | |||
22 | it("should unset an environment variable on nil", function() | ||
23 | assert.is_true(system.setenv("TEST_VAR", "test_value")) | ||
24 | assert.is_equal("test_value", system.getenv("TEST_VAR")) | ||
25 | |||
26 | assert.is_true(system.setenv("TEST_VAR", nil)) | ||
27 | assert.is_nil(system.getenv("TEST_VAR")) | ||
28 | end) | ||
29 | |||
30 | |||
31 | it("should error on input bad type", function() | ||
32 | assert.has_error(function() | ||
33 | system.setenv("TEST_VAR", {}) | ||
34 | end) | ||
35 | assert.has_error(function() | ||
36 | system.setenv({}, "test_value") | ||
37 | end) | ||
38 | end) | ||
39 | |||
40 | |||
41 | it("should return success on deleting a variable that doesn't exist", function() | ||
42 | if system.getenv("TEST_VAR") ~= nil then | ||
43 | -- clear if it was already set | ||
44 | assert.is_true(system.setenv("TEST_VAR", nil)) | ||
45 | end | ||
46 | |||
47 | assert.is_true(system.setenv("TEST_VAR", nil)) -- clear again shouldn't fail | ||
48 | end) | ||
49 | |||
50 | end) | ||
51 | |||
52 | |||
53 | |||
54 | describe("getenvs()", function() | ||
55 | |||
56 | it("should list environment variables", function() | ||
57 | assert.is_true(system.setenv("TEST_VAR1", nil)) | ||
58 | assert.is_true(system.setenv("TEST_VAR2", nil)) | ||
59 | assert.is_true(system.setenv("TEST_VAR3", nil)) | ||
60 | local envVars1 = system.getenvs() | ||
61 | assert.is_true(system.setenv("TEST_VAR1", "test_value1")) | ||
62 | assert.is_true(system.setenv("TEST_VAR2", "test_value2")) | ||
63 | assert.is_true(system.setenv("TEST_VAR3", "test_value3")) | ||
64 | local envVars2 = system.getenvs() | ||
65 | assert.is_true(system.setenv("TEST_VAR1", nil)) | ||
66 | assert.is_true(system.setenv("TEST_VAR2", nil)) | ||
67 | assert.is_true(system.setenv("TEST_VAR3", nil)) | ||
68 | |||
69 | for k,v in pairs(envVars1) do | ||
70 | envVars2[k] = nil | ||
71 | end | ||
72 | assert.are.same({ | ||
73 | TEST_VAR1 = "test_value1", | ||
74 | TEST_VAR2 = "test_value2", | ||
75 | TEST_VAR3 = "test_value3", | ||
76 | }, envVars2) | ||
77 | end) | ||
78 | |||
79 | end) | ||
80 | |||
81 | end) | ||
diff --git a/spec/time_spec.lua b/spec/time_spec.lua deleted file mode 100644 index a017cfe..0000000 --- a/spec/time_spec.lua +++ /dev/null | |||
@@ -1,31 +0,0 @@ | |||
1 | local system = require 'system.core' | ||
2 | |||
3 | describe('Test time functions', function() | ||
4 | it('gettime returns current time', function() | ||
5 | local starttime = system.gettime() | ||
6 | local expected = os.time() | ||
7 | local endtime = system.gettime() | ||
8 | local delta = endtime - starttime | ||
9 | local avg = starttime + delta/2 | ||
10 | assert.is_true(expected >= math.floor(starttime)) | ||
11 | assert.is_true(expected <= math.ceil(endtime)) | ||
12 | assert.is_near(expected, avg, 1 + delta) | ||
13 | end) | ||
14 | |||
15 | it('monottime returns monotonically increasing time', function() | ||
16 | local starttime = system.monotime() | ||
17 | local endtime = system.monotime() | ||
18 | local delta = endtime - starttime | ||
19 | assert.is_true(starttime > 0) | ||
20 | assert.is_true(delta >= 0) | ||
21 | assert.is_true(system.monotime() - endtime >= 0) | ||
22 | end) | ||
23 | |||
24 | it('sleep will wait for specified amount of time', function() | ||
25 | local starttime = system.gettime() | ||
26 | local starttick = system.monotime() | ||
27 | system.sleep(0.5) | ||
28 | assert.is_near(0.5, system.gettime() - starttime, 0.15) | ||
29 | assert.is_near(0.5, system.monotime() - starttick, 0.15) | ||
30 | end) | ||
31 | end) | ||