the Compartmented Robust Posix C++ Unit Test system

Specific crpcut predicates

predicate class

A specific crpcut predicate inherits from crpcut::predicate, has an operator()(T) accepting one parameter and returning a boolean value, and has a stream insertion operator for error reporting. The stream insertion operator os only called if operator()(T) returns false.

pseudo operator =~()

crpcut implements a pseudo operator =~()[3] to create match expressions with predicates, for use in ASSERT_TRUE(expr), VERIFY_TRUE(expr), ASSERT_FALSE(expr) and VERIFY_FALSE(expr).

Example program

The following program implements a specific crpcut predicate used to match the end of a string.

     
     #include <crpcut.hpp>
     #include <string>
     class ends_with : public crpcut::predicate
     {
     public:
       ends_with(std::string s) : s_(s) {}
       bool operator()(const std::string &p)
       {
         return p.length() >= s_.length()
             && p.compare(p.length() - s_.length(), s_.length(), s_) == 0;
       }
       friend std::ostream &operator<<(std::ostream &os, const ends_with& b)
       {
         return os << "ends_with(\"" << b.s_ << "\")";
       }
     private:
       std::string s_;
     };
     
     TEST(not_an_awk_file)
     {
       ASSERT_FALSE(__FILE__ =~ ends_with(".awk"));
     }
     
     TEST(is_a_c_file)
     {
       ASSERT_TRUE(__FILE__ =~ ends_with(".c"));
     }
     
     int main(int argc, char *argv[])
     {
       return crpcut::run(argc, argv);
     }

      

When run, the output stream insertion operator is used to provide information about the predicate if the matching fails:

      
     FAILED!: is_a_c_file
     phase="running"  --------------------------------------------------------------
     samples/match-operator-example.cpp:54
     ASSERT_TRUE("samples/match-operator-example.cpp" =~ ends_with(".c"))
       is evaluated as:
         samples/match-operator-example.cpp =~ ends_with(".c")
     -------------------------------------------------------------------------------
     ===============================================================================
     2 test cases selected
     
                    Sum   Critical   Non-critical
     PASSED   :       1          1              0
     FAILED   :       1          1              0

      



[3] No, there is no such operator; this is an ugly trick using operator~() for things inheriting from crpcut::predicate and assignment to an expression.