-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(*): add environment variable and random functions
- Loading branch information
Showing
11 changed files
with
445 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
local system = require 'system.core' | ||
|
||
describe('Test time functions', function() | ||
|
||
-- returns the new second, on the new second | ||
local function wait_for_second_rollover() | ||
local start_time = math.floor(os.time()) | ||
local end_time = math.floor(os.time()) | ||
while end_time == start_time do | ||
end_time = math.floor(os.time()) | ||
end | ||
return end_time | ||
end | ||
|
||
|
||
describe("time()", function() | ||
|
||
it('returns current time', function() | ||
wait_for_second_rollover() | ||
local lua_time = wait_for_second_rollover() | ||
local aaa_time = system.time() | ||
assert.is.near(lua_time, aaa_time, 0.001) | ||
|
||
wait_for_second_rollover() | ||
assert.is.near(1, system.time() - aaa_time, 0.001) | ||
end) | ||
|
||
end) | ||
|
||
|
||
|
||
describe("monotime()", function() | ||
|
||
it('returns monotonically increasing time', function() | ||
local starttime = system.monotime() | ||
local endtime = system.monotime() | ||
local delta = endtime - starttime | ||
assert.is_true(starttime > 0) | ||
assert.is_true(delta >= 0) | ||
assert.is_true(system.monotime() - endtime >= 0) | ||
end) | ||
|
||
end) | ||
|
||
|
||
|
||
describe("sleep()", function() | ||
|
||
it("should sleep for the specified time", function() | ||
local start_time = wait_for_second_rollover() | ||
system.sleep(1) | ||
local end_time = os.time() | ||
local elapsed_time = end_time - start_time | ||
assert.is.near(elapsed_time, 1, 0.001) | ||
end) | ||
|
||
|
||
it("should sleep for the specified time; fractional", function() | ||
local start_time = system.time() | ||
system.sleep(0.5) | ||
local end_time = system.time() | ||
local elapsed_time = end_time - start_time | ||
assert.is.near(0.5, elapsed_time, 0.01) | ||
end) | ||
|
||
|
||
it("should return immediately for a non-positive sleep time", function() | ||
local start_time = wait_for_second_rollover() | ||
system.sleep(-1) | ||
local end_time = os.time() | ||
local elapsed_time = end_time - start_time | ||
assert.is.near(elapsed_time, 0, 0.001) | ||
end) | ||
|
||
end) | ||
|
||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 = system.MAX_RANDOM_BUFFER_SIZE | ||
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 error message for an invalid number of bytes", function() | ||
local num_bytes = 0 | ||
local result, err_msg = system.random(num_bytes) | ||
assert.is.falsy(result) | ||
assert.are.equal("invalid number of bytes, must be between 1 and 1024", err_msg) | ||
end) | ||
|
||
|
||
it("should return an error message for exceeding the maximum buffer size", function() | ||
local num_bytes = system.MAX_RANDOM_BUFFER_SIZE + 1 | ||
local result, err_msg = system.random(num_bytes) | ||
assert.is.falsy(result) | ||
assert.are.equal("invalid number of bytes, must be between 1 and 1024", err_msg) | ||
end) | ||
|
||
|
||
it("should not return duplicate results", function() | ||
local num_bytes = 10 | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
-- Import the library that contains the environment-related functions | ||
local aaa = require("aaa") | ||
|
||
describe("Environment Variables:", function() | ||
|
||
describe("setenv()", function() | ||
|
||
it("should set an environment variable", function() | ||
assert.is_true(aaa.setenv("TEST_VAR", "test_value")) | ||
assert.is_equal("test_value", os.getenv("TEST_VAR")) | ||
end) | ||
|
||
|
||
it("should set an empty environment variable value", function() | ||
assert.is_true(aaa.setenv("TEST_VAR", "")) | ||
assert.is_equal("", os.getenv("TEST_VAR")) | ||
end) | ||
|
||
|
||
it("should set an empty environment variable", function() | ||
assert.is_true(aaa.setenv("TEST_VAR", "")) | ||
assert.is_equal("", os.getenv("TEST_VAR")) | ||
end) | ||
|
||
|
||
it("should error on input bad type", function() | ||
assert.has_error(function() | ||
aaa.setenv("TEST_VAR", {}) | ||
end) | ||
assert.has_error(function() | ||
aaa.setenv({}, "test_value") | ||
end) | ||
end) | ||
|
||
|
||
it("should return success on deleting a variable that doesn't exist", function() | ||
if os.getenv("TEST_VAR") ~= nil then | ||
-- clear if it was already set | ||
assert.is_true(aaa.setenv("TEST_VAR", nil)) | ||
end | ||
|
||
assert.is_true(aaa.setenv("TEST_VAR", nil)) -- clear again shouldn't fail | ||
end) | ||
|
||
end) | ||
|
||
|
||
|
||
describe("getenvs()", function() | ||
|
||
it("should list environment variables", function() | ||
assert.is_true(aaa.setenv("TEST_VAR1", nil)) | ||
assert.is_true(aaa.setenv("TEST_VAR2", nil)) | ||
assert.is_true(aaa.setenv("TEST_VAR3", nil)) | ||
local envVars1 = aaa.getenvs() | ||
assert.is_true(aaa.setenv("TEST_VAR1", "test_value1")) | ||
assert.is_true(aaa.setenv("TEST_VAR2", "test_value2")) | ||
assert.is_true(aaa.setenv("TEST_VAR3", "test_value3")) | ||
local envVars2 = aaa.getenvs() | ||
assert.is_true(aaa.setenv("TEST_VAR1", nil)) | ||
assert.is_true(aaa.setenv("TEST_VAR2", nil)) | ||
assert.is_true(aaa.setenv("TEST_VAR3", nil)) | ||
|
||
for k,v in pairs(envVars1) do | ||
envVars2[k] = nil | ||
end | ||
assert.are.same({ | ||
TEST_VAR1 = "test_value1", | ||
TEST_VAR2 = "test_value2", | ||
TEST_VAR3 = "test_value3", | ||
}, envVars2) | ||
end) | ||
|
||
end) | ||
|
||
end) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.