How to test CopyConstructible, MoveConstructible, Assignable requirements?

409 views
Skip to first unread message

Niels Dekker

unread,
Dec 1, 2019, 12:09:27 PM12/1/19
to Google C++ Testing Framework
For ITK, an open source C++ library that I'm involved with, I would like to add a few GoogleTest unit tests, testing at if certain classes satisfy CopyConstructible, MoveConstructible, CopyAssignable, and MoveAssignable requirements. So I was writing some helper functions, that "expect" the required behavior. For example:

    template <typename T>
   
void Expect_CopyConstructible(const T& value)
   
{
      EXPECT_EQ
(T(value), value);
   
}

   
template <typename T>
   
void Expect_MoveConstructible(const T& value)
   
{
     
const auto movedValue = T(value);
      EXPECT_EQ
(movedValue, value);
   
}

   
template <typename T>
   
void Expect_CopyAssignable(const T& initialValue, const T& newValue)
   
{
      T lhs
(initialValue);
      lhs
= newValue;
      EXPECT_EQ
(lhs, newValue);
   
}

   
// Etc., etc...

Now I just guess that all of this has been done before. Is there a "standard way" to do it, using GoogleTest?

hyuk myeong

unread,
Dec 3, 2019, 9:34:37 AM12/3/19
to Niels Dekker, Google C++ Testing Framework
Hello?
I'm not 100% sure but I think there isn't any "standard way" to do it

If you see below code they test constructors in the same way to you (call a specific constructor -> check the result)
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/googletestframework/aab1a70f-79b3-4a84-aa1e-419fe769246d%40googlegroups.com.

Josh Kelley

unread,
Dec 3, 2019, 10:26:13 AM12/3/19
to Niels Dekker, Google C++ Testing Framework
These are aspects of your classes that are determined at compile time, rather than something you need to check at runtime, so you should be able to use static_assert and C++'s type traits to do this. For example, for std::string:

static_assert(std::is_copy_constructible<std::string>::value, "std::string must be copy constructible");
static_assert(std::is_move_constructible<std::string>::value, "std::string must be move constructible");
static_assert(std::is_copy_assignable<std::string>::value, "std::string must be copy assignable");


Josh Kelley

--

Niels Dekker

unread,
Dec 3, 2019, 5:50:51 PM12/3/19
to Google C++ Testing Framework
Thanks for your reply, Josh. Indeed, the compile-time aspects of those requirements can be tested by using `static_assert` and type traits from the Standard Library.

However, I would like to also test that the run-time behavior of the tested class satisfies the requirements. I think it would be useful if GoogleTest would have some helper functions (or macro's) to test those very common class requirements. What do you think?

Kind regards, Niels

Josh Kelley

unread,
Dec 3, 2019, 8:53:27 PM12/3/19
to Niels Dekker, Google C++ Testing Framework
Thanks for the explanation; I apologize for misunderstanding.

My initial thought is that I'm not sure how useful generic helper functions would be. They'd have to rely on a class's operator== to test their results, so if I'm concerned that my core constructor functionality isn't working, then I may also be concerned that my core operator functionality isn't working. If a class's functionality is complex enough to need a custom constructor or assignment operator, then it may also be complex enough to need additional tests beyond checking EXPECT_EQ. (To pick a pre-C++11 example, a class that manages a raw pointer to a buffer would need to make sure that any copies allocate their own buffers instead of copying the pointer.)

So, because the generic versions are simple enough to implement, and because it may be easy to encounter situations where a generic version wouldn't work unchanged, I expect it's something where it works better for each project to do something to meet their own needs than to add a standard approach to Google Test.

Of course, I could be wrong. Thanks.

Josh Kelley

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages