Commits

Anonymous committed f8ec46e

Fixed an off_by_one error.

Comments (0)

Files changed (2)

nurikabe-solver/t/verdict-matrix-tests.py.t

 from TAP.Simple import *
 from ctypes import *
 
-plan(11)
+plan(15)
 
 NK_SOLVE_ERROR__SUCCESS = 0
 NK_SOLVE_ERROR__ALLOC_FAILED = 1
             )
 
 test_set_and_get()
+
+def test_off_by_one():
+    m = Mat()
+    # TEST
+    eq_ok (
+            m.create(1, 3), 
+            NK_SOLVE_ERROR__SUCCESS, 
+            "off_by_one create"
+            )
+
+    # TEST
+    ok (m.matrix, "off_by_one matrix is allocated")
+
+    # TEST
+    eq_ok (
+            m.set(0, 2, NK_SOLVE_VERDICT__BLACK),
+            NK_SOLVE_ERROR__SUCCESS,
+            "off_by_one value set.",
+        )
+
+    # TEST
+    eq_ok (
+            m.free(),
+            NK_SOLVE_ERROR__SUCCESS,
+            "off_by_one free was successful"
+            )
+
+
+test_off_by_one()
+

nurikabe-solver/verdict_mat.c

     ret->width = width;
     ret->height = height;
 
-    ret->buf = g_malloc0((width*height)>>2);
+    /*
+     * If width*height == 3, then we need one octet to hold
+     * the three quarters.
+     * So it's (width*height)/4+1
+     * */
+    ret->buf = g_malloc0( ((width*height)>>2) + 1 );
     if (! ret->buf)
     {
         g_free(ret);