Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Google Test and printing QVector3D
Forum Updated to NodeBB v4.3 + New Features

Google Test and printing QVector3D

Scheduled Pinned Locked Moved 3rd Party Software
3 Posts 2 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    klox
    wrote on last edited by
    #1

    Hey everyone,

    I have had a lot of success using google test's unit testing framework with Qt4/5. I have it set up with cmake and I am using QtCreator. One issue I am having is writing custom print functions. The documentation for it is "here":https://code.google.com/p/googletest/wiki/V1_6_AdvancedGuide#Teaching_Google_Test_How_to_Print_Your_Values.

    I have tried every which way I can think of, but no matter what gtest will not use my custom print functions for QVector3D. I define these at the top of my unit_tests.cpp file:

    @
    #include "gmock/gmock.h"
    #include "gtest/gtest.h"

    #include <QtGui/QVector3D>

    ::std::ostream& operator<<(::std::ostream& os, const QVector3D& vector)
    {
    return os << "(" << vector.x() <<", " << vector.y() << ", " << vector.z() << ")";
    }

    void PrintTo(const QVector3D& vector, ::std::ostream* os)
    {
    *os << "(" << vector.x() <<", " << vector.y() << ", " << vector.z() << ")";
    }
    @

    and then have a test case such as:

    @TEST(Test, WhyTheHellWontQVector3DPrint)
    {
    std::cout << "Testing 1 " << QVector3D(1,1,1) << std::endl;
    std::cout << "Testing 2 ";
    PrintTo(QVector3D(2, 2, 2), &std::cout);
    std::cout << std::endl;
    EXPECT_EQ(QVector3D(1,1,1), QVector3D(2,2,2));
    }
    @

    Which tosses this:
    @
    [ RUN ] Test.WhyTheHellWontQVector3DPrint
    Testing 1 (1, 1, 1)
    Testing 2 (2, 2, 2)
    unit_tests.cpp:144: Failure
    Value of: QVector3D(2,2,2)
    Actual: 12-byte object <00-00 00-40 00-00 00-40 00-00 00-40>
    Expected: QVector3D(1,1,1)
    Which is: 12-byte object <00-00 80-3F 00-00 80-3F 00-00 80-3F>
    @

    So it looks like my functions are correct since they successfully print the strings, but gtest isn't finding them. It just prints the byte-string of the object. I step through the code and it eventually gets to an internal fall-back printer

    template <typename T> void PrintTo(const T& value, ::std::ostream* os) as part of the namespace ::testing::internal.

    I have tried wrapping the functions in testing::internal and a ton of other mixes of namespaces. It just won't print it. Anyone have any ideas?

    Thanks for your time and help!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,
      Do you use a namespace Qt ? If that's the case you could try to surround the function definitions with:
      QT_BEGIN_NAMESPACE
      QT_END_NAMESPACE

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        klox
        wrote on last edited by
        #3

        Thanks for the suggestion. I didn't specify any namespace when compiling Qt 5.0.1 (I think that is where that would be done?), but I also gave that a shot since I saw those macros in qvector3d.h. No luck =/

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved