Test a function: Difference between revisions

C++ entry
(C++ entry)
Line 282:
}
}</lang>
 
=={{header|C++}}==
<lang cpp>#include <algorithm>
#include <string>
 
constexpr bool is_palindrome(std::string_view s)
{
return std::equal(s.begin(), s.begin()+s.length()/2, s.rbegin());
}
</lang>
 
 
C++ has several popular frameworks for testing. However, simple things like a palidrome
test can be tested at compile time - failing the test will cause a compilation failure.
 
<lang cpp>void CompileTimeTests()
{
static_assert(is_palindrome("ada"));
static_assert(!is_palindrome("C++"));
 
static_assert(is_palindrome("C++")); // fails at compile time
static_assert(!is_palindrome("ada")); // fails at compile time
}
 
int main()
{
}</lang>
{{out}}
<pre>
/home/garbanzo/play/rosettascratch/test-a-function_constexpr.cpp: In function 'void CompileTimeTests()':
/home/garbanzo/play/rosettascratch/test-a-function_constexpr.cpp:18:30: error: static assertion failed
18 | static_assert(is_palindrome("C++")); // fails at compile time
| ~~~~~~~~~~~~~^~~~~~~
/home/garbanzo/play/rosettascratch/test-a-function_constexpr.cpp:19:17: error: static assertion failed
19 | static_assert(!is_palindrome("ada")); // fails at compile time
| ^~~~~~~~~~~~~~~~~~~~~
gmake[2]: *** [CMakeFiles/scratch.dir/build.make:286: CMakeFiles/scratch.dir/test-a-function_constexpr.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/scratch.dir/all] Error 2
gmake: *** [Makefile:136: all] Error 2
</pre>
 
 
A popular testing framework is gtest.
 
<lang cpp>#include <gtest/gtest.h>
 
TEST(PalindromeSuite, Test1)
{
EXPECT_TRUE(is_palindrome("ada"));
EXPECT_FALSE(is_palindrome("C++"));
 
EXPECT_FALSE(is_palindrome("ada")); // will fail
EXPECT_TRUE(is_palindrome("C++")); // will fail
}</lang>
{{out}}
<pre>
Running main() from /var/tmp/portage/dev-cpp/gtest-1.11.0/work/googletest-release-1.11.0/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from PalindromeSuite
[ RUN ] PalindromeSuite.Test1
/home/garbanzo/play/rosettascratch/test-a-function_gtest.cpp:16: Failure
Value of: is_palindrome("ada")
Actual: true
Expected: false
/home/garbanzo/play/rosettascratch/test-a-function_gtest.cpp:17: Failure
Value of: is_palindrome("C++")
Actual: false
Expected: true
[ FAILED ] PalindromeSuite.Test1 (0 ms)
[----------] 1 test from PalindromeSuite (0 ms total)
 
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] PalindromeSuite.Test1
 
1 FAILED TEST
</pre>
 
 
Boost also has a testing framework.
 
<lang cpp>#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
 
BOOST_AUTO_TEST_CASE( Test1 )
{
BOOST_CHECK( is_palindrome("ada") == true );
BOOST_CHECK( is_palindrome("C++") == false );
 
BOOST_CHECK( is_palindrome("ada") == false); // will fail
BOOST_CHECK( is_palindrome("C++") == true); // will fail
}</lang>
{{out}}
<pre>
Running 1 test case...
[path]/test-a-function_boost.cpp(20): error: in "Test1": check is_palindrome("ada") == false has failed
[path]/test-a-function_boost.cpp(21): error: in "Test1": check is_palindrome("C++") == true has failed
 
*** 2 failures are detected in the test module "Master Test Suite"
</pre>
 
=={{header|Clojure}}==
125

edits