Skip to content

Commit

Permalink
Fixed lots of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 committed Nov 28, 2023
1 parent 9e02887 commit dd9d743
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ jobs:
- name: Download ROM & CC:T
run: |
sudo git clone https://github.com/MCJack123/craftos2-rom /usr/local/share/craftos
git clone --branch v1.19.4-1.108.0 https://github.com/SquidDev-CC/CC-Tweaked ../CC-Tweaked
git clone --branch v1.20.1-1.109.0 https://github.com/cc-tweaked/CC-Tweaked ../CC-Tweaked
patch -p1 -d ../CC-Tweaked < resources/CCT-Tests.patch
- name: Install dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion resources/CCT-Test-Bootstrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ end
for _,v in ipairs(fs.list("/")) do if not fs.isReadOnly(v) then fs.delete(v) end end
_G._CCPC_FIRST_RUN = nil
_G._CCPC_UPDATED_VERSION = nil
local logfile = io.open("test-log.txt", "w")
local logfile = assert(io.open("test-log.txt", "w"))
io.output(logfile)
shell.run("/test-rom/mcfly /test-rom/spec")
logfile:close()
Expand Down
4 changes: 2 additions & 2 deletions src/apis/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ static int fs_open(lua_State *L) {
if (d.isDir) {
lua_remove(L, fpid);
lua_pushnil(L);
if (strcmp(mode, "r") == 0 || strcmp(mode, "rb") == 0) lua_pushfstring(L, "/%s: No such file", fixpath(computer, str, false, false).string().c_str());
if (strchr(mode, 'r') != NULL) lua_pushfstring(L, "/%s: No such file", fixpath(computer, str, false, false).string().c_str());
else lua_pushfstring(L, "/%s: Cannot write to directory", fixpath(computer, str, false, false).string().c_str());
return 2;
}
Expand All @@ -491,7 +491,7 @@ static int fs_open(lua_State *L) {
std::error_code e;
if (fs::is_directory(path, e)) {
lua_pushnil(L);
if (strcmp(mode, "r") == 0 || strcmp(mode, "rb") == 0) lua_pushfstring(L, "/%s: No such file", fixpath(computer, str, false, false).string().c_str());
if (strchr(mode, 'r') != NULL) lua_pushfstring(L, "/%s: No such file", fixpath(computer, str, false, false).string().c_str());
else lua_pushfstring(L, "/%s: Cannot write to directory", fixpath(computer, str, false, false).string().c_str());
return 2;
}
Expand Down
9 changes: 7 additions & 2 deletions src/apis/handles/fs_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ int fs_handle_readLine(lua_State *L) {
std::getline(*fp, retval);
if (retval.empty() && fp->eof()) return 0;
if (lua_toboolean(L, 1) && fp->good()) retval += '\n';
else if (!retval.empty() && retval[retval.size()-1] == '\r') retval.resize(retval.size() - 1);
lua_pushlstring(L, retval.c_str(), retval.length());
return 1;
}
Expand All @@ -112,6 +113,7 @@ int fs_handle_readChar(lua_State *L) {
if (fp->eof()) return 0;
if (!fp->good()) luaL_error(L, "Could not read file");
if (lua_isnumber(L, 1)) {
if (lua_tointeger(L, 1) < 0) luaL_error(L, "Cannot read a negative number of characters");
const size_t s = lua_tointeger(L, 1);
if (s == 0) {
if (fp->peek() == EOF || fp->eof()) return 0;
Expand Down Expand Up @@ -139,6 +141,7 @@ int fs_handle_readByte(lua_State *L) {
if (fp->eof()) return 0;
if (!fp->good()) luaL_error(L, "Could not read file");
if (lua_isnumber(L, 1)) {
if (lua_tointeger(L, 1) < 0) luaL_error(L, "Cannot read a negative number of bytes");
const size_t s = lua_tointeger(L, 1);
if (s == 0) {
if (fp->peek() == EOF || fp->eof()) return 0;
Expand Down Expand Up @@ -186,7 +189,8 @@ int fs_handle_writeString(lua_State *L) {
else if (!lua_isstring(L, 1) && !lua_isnumber(L, 1)) luaL_error(L, "bad argument #1 (string expected, got %s)", lua_typename(L, lua_type(L, 1)));
if (fp->fail()) luaL_error(L, "Could not write file");
size_t sz = 0;
fp->write(lua_tolstring(L, 1, &sz), sz);
const char * str = lua_tolstring(L, 1, &sz);
fp->write(str, sz);
return 0;
}

Expand All @@ -198,7 +202,8 @@ int fs_handle_writeLine(lua_State *L) {
else if (!lua_isstring(L, 1) && !lua_isnumber(L, 1)) luaL_error(L, "bad argument #1 (string expected, got %s)", lua_typename(L, lua_type(L, 1)));
if (fp->fail()) luaL_error(L, "Could not write file");
size_t sz = 0;
fp->write(lua_tolstring(L, 1, &sz), sz);
const char * str = lua_tolstring(L, 1, &sz);
fp->write(str, sz);
fp->put('\n');
return 0;
}
Expand Down
31 changes: 18 additions & 13 deletions src/apis/redstone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ static std::vector<std::string> sides = {
"left"
};

static const char * _typename(lua_State *L, int num) {
if (luaL_getmetafield(L, num, "__name")) return lua_tostring(L, -1);
else return luaL_typename(L, num);
}

static int rs_getSides(lua_State *L) {
lua_createtable(L, 6, 0);
for (int i = 0; i < 6; i++) {
Expand All @@ -35,7 +40,7 @@ static int rs_getSides(lua_State *L) {

static int rs_getInput(lua_State *L) {
Computer * comp = get_comp(L);
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", luaL_typename(L, 1));
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", _typename(L, 1));
std::string sidestr = lua_tostring(L, 1);
std::transform(sidestr.begin(), sidestr.end(), sidestr.begin(), [](unsigned char c) {return std::tolower(c); });
int side = -1;
Expand All @@ -52,7 +57,7 @@ static int rs_getInput(lua_State *L) {

static int rs_getOutput(lua_State *L) {
Computer * comp = get_comp(L);
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", luaL_typename(L, 1));
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", _typename(L, 1));
std::string sidestr = lua_tostring(L, 1);
std::transform(sidestr.begin(), sidestr.end(), sidestr.begin(), [](unsigned char c) {return std::tolower(c); });
int side = -1;
Expand All @@ -69,7 +74,7 @@ static int rs_getOutput(lua_State *L) {

static int rs_setOutput(lua_State *L) {
Computer * comp = get_comp(L);
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", luaL_typename(L, 1));
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", _typename(L, 1));
std::string sidestr = lua_tostring(L, 1);
std::transform(sidestr.begin(), sidestr.end(), sidestr.begin(), [](unsigned char c) {return std::tolower(c); });
int side = -1;
Expand All @@ -86,7 +91,7 @@ static int rs_setOutput(lua_State *L) {

static int rs_getAnalogInput(lua_State *L) {
Computer * comp = get_comp(L);
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", luaL_typename(L, 1));
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", _typename(L, 1));
std::string sidestr = lua_tostring(L, 1);
std::transform(sidestr.begin(), sidestr.end(), sidestr.begin(), [](unsigned char c) {return std::tolower(c); });
int side = -1;
Expand All @@ -103,7 +108,7 @@ static int rs_getAnalogInput(lua_State *L) {

static int rs_getAnalogOutput(lua_State *L) {
Computer * comp = get_comp(L);
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", luaL_typename(L, 1));
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", _typename(L, 1));
std::string sidestr = lua_tostring(L, 1);
std::transform(sidestr.begin(), sidestr.end(), sidestr.begin(), [](unsigned char c) {return std::tolower(c); });
int side = -1;
Expand All @@ -120,7 +125,7 @@ static int rs_getAnalogOutput(lua_State *L) {

static int rs_setAnalogOutput(lua_State *L) {
Computer * comp = get_comp(L);
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", luaL_typename(L, 1));
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", _typename(L, 1));
std::string sidestr = lua_tostring(L, 1);
std::transform(sidestr.begin(), sidestr.end(), sidestr.begin(), [](unsigned char c) {return std::tolower(c); });
int side = -1;
Expand All @@ -131,7 +136,7 @@ static int rs_setAnalogOutput(lua_State *L) {
}
}
if (side == -1) return luaL_error(L, "bad argument #1 (unknown option %s)", sidestr.c_str());
if (!lua_isnumber(L, 2)) return luaL_error(L, "bad argument #2 (number expected, got %s)", luaL_typename(L, 2));
if (!lua_isnumber(L, 2)) return luaL_error(L, "bad argument #2 (number expected, got %s)", _typename(L, 2));
const lua_Number strength = lua_tonumber(L, 2);
if (isnan(strength)) return luaL_argerror(L, 2, "number expected, got nan");
if (isinf(strength)) return luaL_argerror(L, 2, "number expected, got inf");
Expand All @@ -142,7 +147,7 @@ static int rs_setAnalogOutput(lua_State *L) {

static int rs_getBundledInput(lua_State *L) {
Computer * comp = get_comp(L);
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", luaL_typename(L, 1));
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", _typename(L, 1));
std::string sidestr = lua_tostring(L, 1);
std::transform(sidestr.begin(), sidestr.end(), sidestr.begin(), [](unsigned char c) {return std::tolower(c); });
int side = -1;
Expand All @@ -159,7 +164,7 @@ static int rs_getBundledInput(lua_State *L) {

static int rs_getBundledOutput(lua_State *L) {
Computer * comp = get_comp(L);
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", luaL_typename(L, 1));
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", _typename(L, 1));
std::string sidestr = lua_tostring(L, 1);
std::transform(sidestr.begin(), sidestr.end(), sidestr.begin(), [](unsigned char c) {return std::tolower(c); });
int side = -1;
Expand All @@ -176,7 +181,7 @@ static int rs_getBundledOutput(lua_State *L) {

static int rs_setBundledOutput(lua_State *L) {
Computer * comp = get_comp(L);
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", luaL_typename(L, 1));
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", _typename(L, 1));
std::string sidestr = lua_tostring(L, 1);
std::transform(sidestr.begin(), sidestr.end(), sidestr.begin(), [](unsigned char c) {return std::tolower(c); });
int side = -1;
Expand All @@ -187,7 +192,7 @@ static int rs_setBundledOutput(lua_State *L) {
}
}
if (side == -1) return luaL_error(L, "bad argument #1 (unknown option %s)", sidestr.c_str());
if (!lua_isnumber(L, 2)) return luaL_error(L, "bad argument #2 (number expected, got %s)", luaL_typename(L, 2));
if (!lua_isnumber(L, 2)) return luaL_error(L, "bad argument #2 (number expected, got %s)", _typename(L, 2));
const lua_Number strength = lua_tonumber(L, 2);
if (isnan(strength)) return luaL_argerror(L, 2, "number expected, got nan");
if (isinf(strength)) return luaL_argerror(L, 2, "number expected, got inf");
Expand All @@ -198,7 +203,7 @@ static int rs_setBundledOutput(lua_State *L) {

static int rs_testBundledInput(lua_State *L) {
Computer * comp = get_comp(L);
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", luaL_typename(L, 1));
if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad argument #1 (string expected, got %s)", _typename(L, 1));
std::string sidestr = lua_tostring(L, 1);
std::transform(sidestr.begin(), sidestr.end(), sidestr.begin(), [](unsigned char c) {return std::tolower(c); });
int side = -1;
Expand All @@ -209,7 +214,7 @@ static int rs_testBundledInput(lua_State *L) {
}
}
if (side == -1) return luaL_error(L, "bad argument #1 (unknown option %s)", sidestr.c_str());
if (!lua_isnumber(L, 2)) return luaL_error(L, "bad argument #2 (number expected, got %s)", luaL_typename(L, 2));
if (!lua_isnumber(L, 2)) return luaL_error(L, "bad argument #2 (number expected, got %s)", _typename(L, 2));
const lua_Number mask = lua_tonumber(L, 2);
if (isnan(mask)) return luaL_argerror(L, 2, "number expected, got nan");
if (isinf(mask)) return luaL_argerror(L, 2, "number expected, got inf");
Expand Down

0 comments on commit dd9d743

Please sign in to comment.