Skip to content

Commit e4326fb

Browse files
committed
Add new Lua testing framework
1 parent ad24e6b commit e4326fb

13 files changed

+2276
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

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

2324
## [0.14.0] - 2023-05-03
2425
### Added

src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ test-jsonrpc: luacartesi jsonrpc
410410
./tests/test-jsonrpc-server.sh ./jsonrpc-remote-cartesi-machine "$(LUA) ./cartesi-machine.lua" "$(LUA) ./cartesi-machine-tests.lua" "$(TEST_PATH)" "$(LUA)"
411411

412412
test-scripts: luacartesi
413+
$(LUA) spec/all-tests.lua
413414
for x in `find tests -maxdepth 1 -type f -name "*.lua"` ; do \
414415
echo -n 'CTSICTSI' | $(LUA) $$x local || exit 1; \
415416
done

src/clua-cartesi.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "clua.h"
2323
#include "machine-c-api.h"
2424
#include "riscv-constants.h"
25+
#include "rtc.h"
2526

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

126+
clua_setintegerfield(L, UINT64_MAX, "MAX_MCYCLE", -1);
127+
clua_setintegerfield(L, RTC_FREQ_DIV, "RTC_FREQ_DIV", -1);
128+
clua_setintegerfield(L, RTC_CLOCK_FREQ, "RTC_CLOCK_FREQ", -1);
125129
clua_setintegerfield(L, MVENDORID_INIT, "MVENDORID", -1);
126130
clua_setintegerfield(L, MARCHID_INIT, "MARCHID", -1);
127131
clua_setintegerfield(L, MIMPID_INIT, "MIMPID", -1);

src/machine.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,6 @@ void machine::dump_pmas(void) const {
15691569
}
15701570
std::array<char, 256> filename{};
15711571
(void) sprintf(filename.data(), "%016" PRIx64 "--%016" PRIx64 ".bin", pma->get_start(), pma->get_length());
1572-
std::cerr << "writing to " << filename.data() << '\n';
15731572
auto fp = unique_fopen(filename.data(), "wb");
15741573
for (uint64_t page_start_in_range = 0; page_start_in_range < pma->get_length();
15751574
page_start_in_range += PMA_PAGE_SIZE) {

src/spec/all-tests.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env lua5.4
2+
3+
-- Copyright 2023 Cartesi Pte. Ltd.
4+
--
5+
-- This file is part of the machine-emulator. The machine-emulator is free
6+
-- software: you can redistribute it and/or modify it under the terms of the GNU
7+
-- Lesser General Public License as published by the Free Software Foundation,
8+
-- either version 3 of the License, or (at your option) any later version.
9+
--
10+
-- The machine-emulator is distributed in the hope that it will be useful, but
11+
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13+
-- for more details.
14+
--
15+
-- You should have received a copy of the GNU Lesser General Public License
16+
-- along with the machine-emulator. If not, see http://www.gnu.org/licenses/.
17+
--
18+
19+
local lester = require("spec.util.lester")
20+
21+
-- Parse arguments from command line.
22+
lester.parse_args()
23+
24+
require("spec.keccak-tests")
25+
require("spec.config-tests")
26+
require("spec.htif-tests")
27+
require("spec.machine-tests")
28+
require("spec.clua-tests")
29+
require("spec.step-tests")
30+
31+
lester.report() -- Print overall statistic of the tests run.
32+
lester.exit() -- Exit with success if all tests passed.

src/spec/clua-tests.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env lua5.4
2+
3+
-- Copyright 2023 Cartesi Pte. Ltd.
4+
--
5+
-- This file is part of the machine-emulator. The machine-emulator is free
6+
-- software: you can redistribute it and/or modify it under the terms of the GNU
7+
-- Lesser General Public License as published by the Free Software Foundation,
8+
-- either version 3 of the License, or (at your option) any later version.
9+
--
10+
-- The machine-emulator is distributed in the hope that it will be useful, but
11+
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13+
-- for more details.
14+
--
15+
-- You should have received a copy of the GNU Lesser General Public License
16+
-- along with the machine-emulator. If not, see http://www.gnu.org/licenses/.
17+
--
18+
19+
local lester = require("spec.util.lester")
20+
local fs = require("spec.util.fs")
21+
local cartesi = require("cartesi")
22+
local describe, it, expect = lester.describe, lester.it, lester.expect
23+
24+
describe("machine clua", function()
25+
local dummy_machine <close> = cartesi.machine({
26+
ram = { length = 0x4000 },
27+
rom = { image_filename = fs.rom_image },
28+
})
29+
30+
it("should fail when trying to pass non machine to a a machine API", function()
31+
local read_mcycle = dummy_machine.read_mcycle
32+
expect.fail(function() read_mcycle(1) end, "expected cartesi machine object")
33+
expect.fail(function() read_mcycle(nil) end, "expected cartesi machine object")
34+
expect.fail(function() read_mcycle() end, "expected cartesi machine object")
35+
expect.fail(function() read_mcycle({}) end, "expected cartesi machine object")
36+
expect.fail(function() read_mcycle(setmetatable({}, {})) end, "expected cartesi machine object")
37+
end)
38+
39+
it("should be able to convert a machine to a string", function()
40+
local s = tostring(dummy_machine)
41+
expect.truthy(s)
42+
expect.equal(s:match("[a-z ]+"), "cartesi machine object")
43+
end)
44+
end)

0 commit comments

Comments
 (0)