Catch2 | test framework for unit-tests TDD and BDD - using C++14 | Unit Testing library
kandi X-RAY | Catch2 Summary
kandi X-RAY | Catch2 Summary
Catch2 is mainly a unit testing framework for C++, but it also provides basic micro-benchmarking features, and simple BDD macros. Catch2's main advantage is that using it is both simple and natural. Tests autoregister themselves and do not have to be named with valid identifiers, assertions look like normal C++ code, and sections provide a nice way to share set-up and tear-down code in tests.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Catch2
Catch2 Key Features
Catch2 Examples and Code Snippets
Community Discussions
Trending Discussions on Catch2
QUESTION
I'm rolling a custom, standard-like container and I'd like to have it compatible with the boost::range
library.
So far it works with all the STL algorithms and it also satisfies the following:
...ANSWER
Answered 2022-Mar-15 at 23:40You need iterator
as well as const_iterator
. Try:
QUESTION
I'm trying to represent NTP timestamps (including the NTP epoch) in C++ using std::chrono
. Therefore, I decided to use a 64-bit unsigned int (unsigned long long
) for the ticks and divide it such that the lowest 28-bit represent the fraction of a second (accepting trunction of 4 bits in comparison to the original standard timestamps), the next 32-bit represent the seconds of an epoch and the highest 4-bit represent the epoch. This means that every tick takes 1 / (2^28 - 1)
seconds.
I now have the following simple implementation:
...ANSWER
Answered 2022-Mar-15 at 17:50Your tick period: 1/268'435'455 is unfortunately both extremely fine and also doesn't lend itself to much of a reduced fraction when your desired conversions are used (i.e. between system_clock::duration
and NTPClock::duration
. This is leading to internal overflow of your unsigned long long
NTPClock::rep
.
For example, on Windows the system_clock
tick period is 1/10,000,000 seconds. The current value of now()
is around 1.6 x 1016. To convert this to NTPClock::duration
you have to compute 1.6 x 1016 times 53,687,091/2,000,000. The first step in that is the value times the numerator of the conversion factor which is about 8 x 1023, which overflows unsigned long long
.
There's a couple of ways to overcome this overflow, and both involve using at least an intermediate representation with a larger range. One could use a 128 bit integral type, but I don't believe that is available on Windows, except perhaps by a 3rd party library. long double
is another option. This might look like:
QUESTION
I'm using Catch2 with TEST_CASE
blocks from within I sometimes declare local temporary struct
for convenience. These struct
sometimes needs to be displayed, and to do so Catch2 suggests to implement the <<
operator with std::ostream
. Unfortunately, this becomes quite complicated to implement with local-only struct
because such operator can't be defined inline nor in the TEST_CASE
block.
I thought of a possible solution which would be to define a template for <<
which would call toString()
instead if that method exists:
ANSWER
Answered 2022-Feb-27 at 13:01You can think of both methods as "operator<<
on all types with some property".
The first property is "has a toString()
" method (and will work in C++11 even. This is still SFINAE, in this case the substitutions are in the return type). You can make it check that toString()
returns a std::string
with a different style of SFINAE:
QUESTION
I am trying to learn/use Catch (https://github.com/catchorg/Catch2) for the first time on a Qt applcation.
I am trying to follow the tutorial presented on Catch's initial page (https://github.com/catchorg/Catch2/blob/devel/docs/tutorial.md#top).
The first line of the above tutorial says that ideally I should be using Catch2 through its "CMake integration" (https://github.com/catchorg/Catch2/blob/devel/docs/cmake-integration.md#top). I faithfully follow the "ideal" path.
On the second paragraph of the "CMake integration" page I start to get lost: If you do not need custom main function, you should...
Do I need a custom main function? Why would anyone need one? How can a person live without one? I have no idea at all and the text neither explains any of this nor provides any kind of sensible default orientation (If you don't know what we are talking about just pretend you... or something similar).
I tried to ignore that and just follow on.
On the third paragraph (reproduced below per request) it is presented a block of code and the reader gets to know that it should be enough to do the block of code. What is to do a block of code? Should I include this code in some pre existing file? Which file? In what part of said file? Or should I create a new file with the proposed content? Which file? Where should I put it?
...This means that if Catch2 has been installed on the system, it should be enough to do
ANSWER
Answered 2022-Jan-15 at 00:37You mean you do not even know whether you need a custom main function?! Just kidding, of course, that was entertaining to read and I agree this could be made a little clearer. However, I am familiar with Catch2 and CMake, so I shall now expel all doubt!
Catch2 tests need a small amount of code in a program's main
function, to pass the command line arguments to its implementation and start running your test cases. So, as a convenience, it offers a default main function that does this for you, which is normally sufficient. Their own documentation gives some examples of how you might supply your own main to alter the parsing of the command line. Another case could be an external library you use that requires some global setup and/or cleanup.
So yes, you do need one or more separate executables for your tests, and the third paragraph shows the basic CMake setup for such an executable. CMake is far too broad of a topic to cover in this answer, but I typically use a fairly standard directory layout like this:
QUESTION
I am trying to get catch2 running for a barebone project just to get familiar with it but so far I failed installing it in whatever sense possible. the catch2-git repository either points you to installing it together with cmake (via vcpkg (I cannot use MSVC and don't want to at this point) or points to some Ubuntu solution.)
I figured I should just download the catch2 files directly and put it somewhere locally, run my code then and include the .hpp directly:
...ANSWER
Answered 2022-Jan-13 at 16:47The GitHub page says:
Catch2 v3 is being developed!You are on the
devel
branch, where the next major version, v3, of Catch2 is being developed. As it is a significant rework, you will find that parts of this documentation are likely still stuck on v2.For stable (and documentation-matching) version of Catch2, go to the v2.x branch.
On the v2.x
page there's a link to a standalone header file:
The latest version of the single header can be downloaded directly using this link
QUESTION
I have the following project structure:
test_main.cc
...ANSWER
Answered 2022-Jan-11 at 09:52I've found in documentation this:
Catch2/command-line.md at devel · catchorg/Catch2 · GitHub
Filenames as tags-#, --filenames-as-tags
When this option is used then every test is given an additional tag which is formed of the unqualified filename it is found in, with any extension stripped, prefixed with the
#
character.So, for example, tests within the file
~\Dev\MyProject\Ferrets.cpp
would be tagged[#Ferrets]
.
Looks like it works, so now just filter test based on that tag.
There is even exact example in documentation
...
QUESTION
I am writing a UnitTest using Catch2.
I want to check if two vectors are equal. They look like the following using gmplib:
ANSWER
Answered 2022-Jan-11 at 00:03First, we need to see some Minimal, Reproducible Example to be sure of what is happening. You can for example cut down some code from your test.cpp
until you are left with just a few lines of code, but the issue still happens. Also, please provide compilation and running instructions. Frequently, a little bit of explanation on what your goals are may also help. As Catch2 is available on GitHub you don't need to provide it.
Without seeing the code, the best I can guess is that your code is trying to comparing mpf_t
types in the mpf_class
using the ==
operator, which I'm afraid has not been overload (see here). You should compare mpf_t
s with the cmp
function, since the C type mpf_t
is actually an struct containing the pointer to the actual significand limbs. Check some usage examples in the tests/cxx/
directory of GMP (like here).
I note you are using GNU MP 4.1 version which is very old, you probably want to move to the 6.2.1 latest version if possible. Also, for using floats it's recommended that you use the GNU MPFR library instead of GMP floats.
EDIT: I did not yet manage to run Catch2, but the issue with your code is the expected_result
is actually not equal to the actual_result
. In GMP mpf_t
variables are created with a 64-bit significand precision (on 64-bit machines), so that the division a / b
actually results in a binary that prints 0.166666666666666666667 (that's 19 sixes after the digit 1). Try printing the result with gmp_printf("%.50Ff\n", actual_result);
, because the standard cout
output will only give you the value rounded to 6 digits: 0.166667.
But the problem is you can't just assign this like expected_result = 0.166666666666666666667
because in C/C++ numeric constants are parsed as double
, thus you have to use the string overload attribution to get more precision.
But you can't also manage to easily (or, in general, justifiably) coin a decimal string that will correctly convert to the exact same binary given by a / b
because decimal to float conversion has subtleties, see for example here and here.
So, it all depends on your application and the kind of numerical validation you aim to do. If you know that your decimal validation values are correct to some known precision, and if you set the mpf_t
variables to withstanding precision (using for example mpf_set_prec
), then you can use tolerance comparison, like so.
in C++ (without Catch2), it works like this:
QUESTION
We have the following c++ code using catch2 framework:
...ANSWER
Answered 2022-Jan-04 at 23:19You simply replace
QUESTION
I want to defer the execution of a packaged task in a loop.
...ANSWER
Answered 2021-Dec-29 at 13:52By default a lambda's call operator is const
-qualified.
Inside the lambda's body the this
pointer to the lambda is therefore also const
-qualified and so is the member wrapper
.
std::packaged_task
does not have a const
-qualified operator()
, so it cannot be called.
You can make the lambda's operator()
non-const
-qualified by adding the mutable
keyword:
QUESTION
I'm trying to test some code that requires a little bit of setup to use, and I'd like to avoid repeating the setup steps. The setup steps have some dependency on the input, but the results could be significantly different depending on what exactly the inputs are. Is there a good way to set up a catch2 test case for this?
By way of example, let's consider a very basic object we might want to test:
...ANSWER
Answered 2021-Dec-23 at 20:06I tend to write small helper functions (or lambdas) even for simple cases. For your example, this could be as short as:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Catch2
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page