Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTest and std::unique_ptr is a bit confusing
Forum Updated to NodeBB v4.3 + New Features

QTest and std::unique_ptr is a bit confusing

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 203 Views
  • 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
    Krulle
    wrote on 22 Jan 2023, 18:00 last edited by
    #1

    Hello together,

    i'll try to implement a QTest on an function, which expects two std::unique_ptr

        bool find(FindSpecifier specifier,
                  const QString &stream,
                  std::unique_ptr<int> &startAt,
                  std::unique_ptr<int> &endAt);
    

    This function search in a cpp file for ctor/dtor or a function and returns the position of the start and end.
    So i have implement a testcase for this function

    void TestClassparser::testFind()
    {
        ClassParser cp;
        std::unique_ptr<int> start(new int(0)), end(new int(0));
        bool state = cp.find(ClassParser::FindSpecifier::ctor, m_stream, start, end);
        QVERIFY(state);
        QCOMPARE(*start, 3);
        QCOMPARE(*end, 8);
    
        state = cp.find(ClassParser::FindSpecifier::dtor, m_stream, start, end);
        QVERIFY(state);
        QCOMPARE(*start, 22);
        QCOMPARE(*end, 25);
    }
    

    Now i'm a bit confuced because the test fails. Qt says, that the start value for "dtor" is still 3.
    But in the debugger everything works perfectly and the expected values come back.
    Can you tell me why this is not the case in the specific test case?
    Even if I split the two passages into two different functions, the test still fails. I do not understand this.

    void TestClassparser::testCtor()
    {
        ClassParser cp;
        std::unique_ptr<int> start(new int(0)), end(new int(0));
        bool state = cp.find(ClassParser::FindSpecifier::ctor, m_stream, start, end);
        QVERIFY(state);
        QCOMPARE(*start, 3);
        QCOMPARE(*end, 8);
    }
    void TestClassparser::testDtor()
    {
        ClassParser cp;
        std::unique_ptr<int> start(new int(0)), end(new int(0));
        bool state = cp.find(ClassParser::FindSpecifier::dtor, m_stream, start, end);
        QVERIFY(state);
        QCOMPARE(*start, 22); // test failed with "compare values not the same" -> *start = 3
        QCOMPARE(*end, 25);
    }
    

    It is also strange that in this case the value of StartAt is still 3.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 22 Jan 2023, 18:10 last edited by
      #2

      I don't see what QtTest can do here - I would guess your ClassParser function does not work and the unittest revealed it.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on 22 Jan 2023, 20:16 last edited by
        #3

        Perhaps your find() function makes the assumption that start and end are zero at invocation and behaves oddly if that is not the case.

        As the for the split test case still returning 3, this is quite possibly because you are still running the original test executable. Did you ensure you have a clean build of the test cases?

        1 Reply Last reply
        0

        1/3

        22 Jan 2023, 18:00

        • Login

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