]> granicus.if.org Git - nethack/commitdiff
Make lua selection percent filter create a new selection
authorPasi Kallinen <paxed@alt.org>
Sat, 22 Feb 2020 11:31:22 +0000 (13:31 +0200)
committerPasi Kallinen <paxed@alt.org>
Sat, 22 Feb 2020 11:31:47 +0000 (13:31 +0200)
... instead of modifying the one given as a parameter.

Also add some tests for it.

src/nhlsel.c
test/test_sel.lua

index 5d780967fb5b01e258613025ec6ead994f862250..6a13d72e4d4355b8b424e7f07582495d4cd388d0 100644 (file)
@@ -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;
 }
 
index f7cc2b303cd353b1fad583809d4b7fcff6e61b67..43518b54a8e3e08e93e3c699084f3b0833385caf 100644 (file)
@@ -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();