From f4c9039af1ec8aa936d7b374a2d827779257c724 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Mon, 4 Aug 2025 08:33:48 +0100 Subject: [PATCH] fix failing unit test in search-list.sml/build-search-list.sml: perform bounds checking into array --- fcore/build-search-list.sml | 2 +- fcore/search-list.sml | 11 +++++++---- test/regression.sml | 17 ++++++++++++++--- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/fcore/build-search-list.sml b/fcore/build-search-list.sml index 63d5332..6bf30ce 100644 --- a/fcore/build-search-list.sml +++ b/fcore/build-search-list.sml @@ -27,7 +27,7 @@ struct case rightStrings of hd :: tl => let - val strIdx = absIdx - bufferIdx + val strIdx = Int.max (0, absIdx - bufferIdx) in if strIdx < String.size hd then helpNextMatch (strIdx, hd, tl, absIdx, searchString, 0) diff --git a/fcore/search-list.sml b/fcore/search-list.sml index 8111ccb..85b5312 100644 --- a/fcore/search-list.sml +++ b/fcore/search-list.sml @@ -226,11 +226,14 @@ struct fun goToNum (num, {left, right}: t) = case right of hd :: tl => - if num >= Vector.sub (hd, 0) then - (* num is greater or equal to first el on right so go right *) - helpGoToNumRight (num, left, right) + if Vector.length hd > 0 then + if Vector.sub (hd, 0) >= Vector.sub (hd, 0) then + (* num is greater or equal to first el on right so go right *) + helpGoToNumRight (num, left, right) + else + (* num is less than first el on right so go left *) + helpGoToNumLeft (num, left, right) else - (* num is less than first el on right so go left *) helpGoToNumLeft (num, left, right) | [] => helpGoToNumLeft (num, left, right) diff --git a/test/regression.sml b/test/regression.sml index e6f9412..11e85cd 100644 --- a/test/regression.sml +++ b/test/regression.sml @@ -10,12 +10,12 @@ struct let val chr = String.sub (str, pos) val app = AppUpdate.update (app, InputMsg.CHAR_EVENT chr) + handle _ => raise Fail (Int.toString pos) in updateLoop (pos + 1, str, app) end - fun updateAppWithChars (historyString, app) = - updateLoop (0, historyString, app) + fun applyChars (historyString, app) = updateLoop (0, historyString, app) fun appFromText text = let val buffer = LineGap.fromString text @@ -36,8 +36,19 @@ struct str end + val initialApp = appFromText initialText + val charEventTests = describe "CHAR_EVENT regressions" - [test "placeholder" (fn _ => Expect.isTrue true)] + [test "SearchList.goToNum vector bounds regression (1)" (fn _ => + let + val app = appFromText initialText + val history = "G12dk" + val history = "100G55dkz33dk" + val newApp = applyChars (history, app) + in + (* just expect that we do not fail or throw an exception *) + Expect.isTrue true + end)] val tests = [charEventTests] end