From: Pasi Kallinen Date: Sat, 22 Feb 2020 11:31:22 +0000 (+0200) Subject: Make lua selection percent filter create a new selection X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=00a32c6039a34c0b6053dc234180f5fed3352150;p=nethack Make lua selection percent filter create a new selection ... instead of modifying the one given as a parameter. Also add some tests for it. --- diff --git a/src/nhlsel.c b/src/nhlsel.c index 5d780967f..6a13d72e4 100644 --- a/src/nhlsel.c +++ b/src/nhlsel.c @@ -299,9 +299,14 @@ l_selection_filter_percent(L) lua_State *L; { struct selectionvar *sel = l_selection_check(L, 1); + struct selectionvar *ret; int p = (int) luaL_checkinteger(L, 2); - selection_filter_percent(sel, p); - lua_settop(L, 1); + + lua_pop(L, 1); + (void) l_selection_clone(L); + ret = l_selection_check(L, 1); + selection_filter_percent(ret, p); + return 1; } diff --git a/test/test_sel.lua b/test/test_sel.lua index f7cc2b303..43518b54a 100644 --- a/test/test_sel.lua +++ b/test/test_sel.lua @@ -29,6 +29,16 @@ function sel_has_n_points(sel, pts, msg) end end +function sel_are_equal(sela, selb, msg) + for x = 0,nhc.COLNO - 2 do + for y = 0,nhc.ROWNO - 1 do + if (sela:get(x,y) ~= selb:get(x,y)) then + error("selections differ at (" .. x .. "," .. y .. "),sela=" .. sela:get(x,y) .. ",selb=" .. selb:get(x,y) .. ": " .. msg); + end + end + end +end + -- test selection parameters function test_selection_params() local sel = selection.new(); @@ -201,9 +211,25 @@ function test_sel_logical_xor() sel_has_n_points(selr, 2, __func__); end -- test_sel_logical_xor +function test_sel_filter_percent() + local __func__ = "test_sel_filter_percent"; + local sela = selection.negate(); + local sela_clone = sela:clone(); + + local selb = sela:percentage(100); + sel_are_equal(sela, selb, __func__); + sel_are_equal(sela, sela_clone, __func__); + + local selc = sela:percentage(0); + sel_are_equal(sela, sela_clone, __func__); + sel_has_n_points(selc, 0, __func__); + + -- TODO: Need a predictable rn2 to test for percentage(50) +end -- test_sel_filter_percent test_selection_params(); test_sel_negate(); test_sel_logical_and(); test_sel_logical_or(); test_sel_logical_xor(); +test_sel_filter_percent();