Skip to content

Commit

Permalink
Add new Lua testing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Jun 6, 2023
1 parent 5527c1e commit db3ffd4
Show file tree
Hide file tree
Showing 13 changed files with 2,276 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Updated Lua version to 5.4
- Use Lua path environment variables specific for version 5.4
- Refactored and added new Lua tests using a Lua testing framework

## [0.14.0] - 2023-05-03
### Added
Expand Down
1 change: 1 addition & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ test-jsonrpc: luacartesi jsonrpc
./tests/test-jsonrpc-server.sh ./jsonrpc-remote-cartesi-machine "$(LUA) ./cartesi-machine.lua" "$(LUA) ./cartesi-machine-tests.lua" "$(TEST_PATH)" "$(LUA)"

test-scripts: luacartesi
$(LUA) spec/all-tests.lua
for x in `find tests -maxdepth 1 -type f -name "*.lua"` ; do \
echo -n 'CTSICTSI' | $(LUA) $$x local || exit 1; \
done
Expand Down
4 changes: 4 additions & 0 deletions src/clua-cartesi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "clua.h"
#include "machine-c-api.h"
#include "riscv-constants.h"
#include "rtc.h"

/// \file
/// \brief Scripting interface for the Cartesi SDK.
Expand Down Expand Up @@ -122,6 +123,9 @@ CM_API int luaopen_cartesi(lua_State *L) {
clua_setintegerfield(L, CM_UARCH_BREAK_REASON_REACHED_TARGET_CYCLE, "UARCH_BREAK_REASON_REACHED_TARGET_CYCLE", -1);
clua_setintegerfield(L, CM_UARCH_BREAK_REASON_HALTED, "UARCH_BREAK_REASON_HALTED", -1);

clua_setintegerfield(L, UINT64_MAX, "MAX_MCYCLE", -1);
clua_setintegerfield(L, RTC_FREQ_DIV, "RTC_FREQ_DIV", -1);
clua_setintegerfield(L, RTC_CLOCK_FREQ, "RTC_CLOCK_FREQ", -1);
clua_setintegerfield(L, MVENDORID_INIT, "MVENDORID", -1);
clua_setintegerfield(L, MARCHID_INIT, "MARCHID", -1);
clua_setintegerfield(L, MIMPID_INIT, "MIMPID", -1);
Expand Down
1 change: 0 additions & 1 deletion src/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,6 @@ void machine::dump_pmas(void) const {
}
std::array<char, 256> filename{};
(void) sprintf(filename.data(), "%016" PRIx64 "--%016" PRIx64 ".bin", pma->get_start(), pma->get_length());
std::cerr << "writing to " << filename.data() << '\n';
auto fp = unique_fopen(filename.data(), "wb");
for (uint64_t page_start_in_range = 0; page_start_in_range < pma->get_length();
page_start_in_range += PMA_PAGE_SIZE) {
Expand Down
32 changes: 32 additions & 0 deletions src/spec/all-tests.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env lua5.4

-- Copyright 2023 Cartesi Pte. Ltd.
--
-- This file is part of the machine-emulator. The machine-emulator is free
-- software: you can redistribute it and/or modify it under the terms of the GNU
-- Lesser General Public License as published by the Free Software Foundation,
-- either version 3 of the License, or (at your option) any later version.
--
-- The machine-emulator is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
-- for more details.
--
-- You should have received a copy of the GNU Lesser General Public License
-- along with the machine-emulator. If not, see http://www.gnu.org/licenses/.
--

local lester = require("spec.util.lester")

-- Parse arguments from command line.
lester.parse_args()

require("spec.keccak-tests")
require("spec.config-tests")
require("spec.htif-tests")
require("spec.machine-tests")
require("spec.clua-tests")
require("spec.step-tests")

lester.report() -- Print overall statistic of the tests run.
lester.exit() -- Exit with success if all tests passed.
44 changes: 44 additions & 0 deletions src/spec/clua-tests.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env lua5.4

-- Copyright 2023 Cartesi Pte. Ltd.
--
-- This file is part of the machine-emulator. The machine-emulator is free
-- software: you can redistribute it and/or modify it under the terms of the GNU
-- Lesser General Public License as published by the Free Software Foundation,
-- either version 3 of the License, or (at your option) any later version.
--
-- The machine-emulator is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
-- for more details.
--
-- You should have received a copy of the GNU Lesser General Public License
-- along with the machine-emulator. If not, see http://www.gnu.org/licenses/.
--

local lester = require("spec.util.lester")
local fs = require("spec.util.fs")
local cartesi = require("cartesi")
local describe, it, expect = lester.describe, lester.it, lester.expect

describe("machine clua", function()
local dummy_machine <close> = cartesi.machine({
ram = { length = 0x4000 },
rom = { image_filename = fs.rom_image },
})

it("should fail when trying to pass non machine to a a machine API", function()
local read_mcycle = dummy_machine.read_mcycle
expect.fail(function() read_mcycle(1) end, "expected cartesi machine object")
expect.fail(function() read_mcycle(nil) end, "expected cartesi machine object")
expect.fail(function() read_mcycle() end, "expected cartesi machine object")
expect.fail(function() read_mcycle({}) end, "expected cartesi machine object")
expect.fail(function() read_mcycle(setmetatable({}, {})) end, "expected cartesi machine object")
end)

it("should be able to convert a machine to a string", function()
local s = tostring(dummy_machine)
expect.truthy(s)
expect.equal(s:match("[a-z ]+"), "cartesi machine object")
end)
end)
Loading

0 comments on commit db3ffd4

Please sign in to comment.