Search code examples
c++mutation-testing

Mutation Survives When Comparing to Empty String in C++?


I am mutating my code and it's complaining that the following mutation survived:

--- /home/user/src/file.cc 2019-12-03 13:48:12.510311
+++ /home/user/src/file.cc 2019-12-03 14:00:14.164006
@@ -283,7 +283,7 @@
 }

 bool SomeClass::isValid(proto::SomeProto const& task_pb) const {
-    if (task_pb.task_id() == "") {
+    if (task_pb.task_id() < "") {
         log(LOG_WARNING, "no id given");
         return false;
     } else if (task_pb.task_type() == proto::TaskType::UNKNOWN) {

The declaration for task_id() is:

const ::std::string& task_id() const;

Is there a string that can be less than ""? Would this even be considered a valid mutation for strings?


Solution

  • std::string defines a lexicographic "less-than" relation, that can be accessed using operator<.

    The specific expression you've shown will always evaluate to false, because no string sorts (lexicographically) less-than the empty string. But that doesn't make the code invalid, merely pointless. I don't know what you mean by a "valid mutation" exactly, but hopefully that will point you to the answer.