// What: Unit testing for lightweight, regular-expression pattern matcher // Where: Published in Application Development Advisor, "Put to the Test" // When: November 2002 (updated February 2006) // Who: Kevlin Henney #include "regex-lite.hpp" #include #include #include int main() { // test regex_match with pattern defined by a string literal const std::string text = "the cat sat on the mat"; assert(regex_match(text.begin(), text.end(), "the")); assert(!regex_match(text.begin(), text.end(), "foo")); assert(regex_match(text.begin(), text.end(), "c.t")); assert(!regex_match(text.begin(), text.end(), "t.t")); assert(regex_match(text.begin(), text.end(), "^t")); assert(!regex_match(text.begin(), text.end(), "^c")); assert(regex_match(text.begin(), text.end(), "^t.*t")); assert(!regex_match(text.begin(), text.end(), "^t.*f")); assert(regex_match(text.begin(), text.end(), "t$")); assert(!regex_match(text.begin(), text.end(), "a$")); assert(regex_match(text.begin(), text.end(), "^t.*t$")); assert(!regex_match(text.begin(), text.end(), "^t.*f$")); assert(regex_match(text.begin(), text.end(), "t.*cat.*o")); assert(!regex_match(text.begin(), text.end(), "t.*rat.*o")); // test regex_match with pattern defined by an iterator range const std::string pattern = "s.*o"; assert(regex_match(text.begin(), text.end(), pattern.begin(), pattern.end())); // test regex_match across embedded null character std::string next = "abcd"; next += '\0'; next += "efgh"; assert(regex_match(next.begin(), next.end(), "fg")); assert(regex_match(next.begin(), next.end(), "d.e")); // test regex_match on empty ranges const std::string empty, non_empty = "cheshire cat"; assert(regex_match(non_empty.begin(), non_empty.end(), "")); assert(regex_match(non_empty.begin(), non_empty.end(), empty.begin(), empty.end())); assert(!regex_match(empty.begin(), empty.end(), non_empty.begin(), non_empty.end())); // test regex_match with wide-character strings (optional) #ifndef INCOMPLETE_WCHAR_SUPPORT const std::wstring wtext = L"the cat sat on the mat"; assert(regex_match(wtext.begin(), wtext.end(), L"^t.*t$")); assert(!regex_match(wtext.begin(), wtext.end(), L"^t.*f$")); assert(regex_match(wtext.begin(), wtext.end(), "t.*cat.*o")); assert(!regex_match(wtext.begin(), wtext.end(), "t.*rat.*o")); #endif std::cout << "Tests completed OK" << std::endl; return 0; }