0

I'm writing a classic binary search function in Clojure and testing it with the mutant mutation testing library.

My tests work fine in terms of correctness, but mutation testing reports one surviving mutant that I can't get rid of — even after writing what I thought were thorough test cases.

My code:

(defn binary-search
  ([coll target]
   (binary-search coll target 0 (dec (count coll))))
  ([coll target left right]
   (if (<= left right)
     (let [mid (quot (+ left right) 2)
           mid-val (nth coll mid)]
       (cond
         (= mid-val target) mid
         (<= mid-val target) (binary-search coll target (inc mid) right)
         :else              (binary-search coll target left (dec mid))))
     -1)))

Test cases:

(deftest binary-search-test
  (is (= (search/binary-search [1 2 3 4 5 6 7 8 9] 5) 4))
  (is (= (search/binary-search [2 2 2 2 2 2 2] 1) -1))
  (is (= (search/binary-search [42] 42) 0)) 
  (is (= (search/binary-search [1 2] 2) 1)) 
  (is (= (search/binary-search [10 20 30 40 50] 30) 2))
  (is (= (search/binary-search [10 20 30 40 50] 60) -1))
  (is (= (search/binary-search [10 20 30 40 50] 50) 4)) 
  (is (= (search/binary-search [10 20 30 40 50] 40) 3))
  (is (= (search/binary-search [1 2 3 4 5 6 7 8 9] 9) 8))
  (is (= (search/binary-search [7 4 3 0 1 8 2] 2) -1)))

Mutant survivor:

(defn binary-search
  ([coll target]
   (binary-search coll target 0 (dec (count coll))))
  ([coll target left right]
   (if (<= left right)
     (let [mid (quot (+ left right) 2)
           mid-val (nth coll mid)]
       (cond
         (= mid-val target) mid
         (<[-=-] mid-val target) (binary-search coll target (inc mid) right)
         :else              (binary-search coll target left (dec mid))))
     -1)))

I’ve verified that the < comparison does get executed by multiple test cases. But the mutated operator (<[-=-]) should cause an error — and yet the mutant survives.

My questions:

  • Has anyone seen this kind of "survivor" mutant that seems syntactically broken?
  • Could this be a limitation or bug in the mutant tool?
  • Is there a way to configure it to treat malformed mutations as killed?
  • Or should I be writing different kinds of tests to kill this?
1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Apr 24 at 11:23

1 Answer 1

1

There's nothing syntactically broken here. <[-=-] is mutant's notation for "You wrote <=, but even if I remove the =, leaving <, your tests still pass". I don't know what mutant expects you to do about that: you've already handled the = case, so < and <= are equivalent functions at this point, and no test case could possibly distinguish which one you're using.

Is it a limitation of mutant? Probably. That's sorta the kind of thing I would expect from a tool that was written 8 years ago by one person, who marked it as "Wildly experimental 🔥", and hasn't touched it since.

If you are enamored of mutant and want to work around this limitation, I imagine you could reorder your clauses such that there's no mutation that a test case couldn't detect. Test < first (replacing with <= would then actually break tests), then > (again, replacing with >= would break tests), and finally fall into the = case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.