Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 329979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:38:19+00:00 2026-05-12T09:38:19+00:00

I’ve got a unit test I’m writing which seems to have some kind of

  • 0

I’ve got a unit test I’m writing which seems to have some kind of a pointer problem. Basically, it’s testing a class that, once constructed, returns information about a file. If all the files expected are detected, then the test operates correctly. If there are more files expected than detected, then the routine correctly reports error. But if there are more files detected than expected, the executable crashes. This has been difficult to follow because when I try to step through with the debugger, the current code point jumps all over the method — it doesn’t follow line by line like you’d expect.

Any ideas as to what I’m doing incorrectly?

Here’s my code:

#include "stdafx.h"
#include "boostUnitTest.h"

#include "../pevFind/fileData.h" //Header file for tested code.
#include "../pevFind/stringUtil.h" //convertUnicode()

struct fileDataLoadingFixture
{
    std::vector<fileData> testSuiteCandidates;
    fileDataLoadingFixture()
    {
        WIN32_FIND_DATA curFileData;
        HANDLE fData = FindFirstFile(L"testFiles\\*", &curFileData);
        if (fData == INVALID_HANDLE_VALUE)
            throw std::runtime_error("Failed to load file list!");
        do {
            if(boost::algorithm::equals(curFileData.cFileName, L".")) continue;
            if(boost::algorithm::equals(curFileData.cFileName, L"..")) continue;
            fileData theFile(curFileData, L".\\testFiles\\");
            testSuiteCandidates.push_back(theFile);
        } while (FindNextFile(fData, &curFileData));
        FindClose(fData);
    };
};

BOOST_FIXTURE_TEST_SUITE( fileDataTests, fileDataLoadingFixture )

BOOST_AUTO_TEST_CASE( testPEData )
{
    std::vector<std::wstring> expectedResults;
    expectedResults.push_back(L"a.cfexe");
    expectedResults.push_back(L"b.cfexe");
    //More files.... 
    expectedResults.push_back(L"c.cfexe");
    std::sort(expectedResults.begin(), expectedResults.end());
    for (std::vector<fileData>::const_iterator it = testSuiteCandidates.begin(); it != testSuiteCandidates.end(); it++)
    {
        if (it->isPE())
        {
            std::wstring theFileString(it->getFileName().substr(it->getFileName().find_last_of(L'\\') + 1 ));
            std::vector<std::wstring>::const_iterator target = std::lower_bound(expectedResults.begin(), expectedResults.end(), theFileString);
            BOOST_REQUIRE_MESSAGE(*target == theFileString, std::string("The file ") + convertUnicode(theFileString) + " was unexpected." );
            if (*target == theFileString)
            {
                expectedResults.erase(target);
            }
        }
    }
    BOOST_CHECK_MESSAGE(expectedResults.size() == 0, "Some expected results were not found." );
}

BOOST_AUTO_TEST_SUITE_END()

Thanks!

Billy3

I solved the problem using the following code instead:

BOOST_AUTO_TEST_CASE( testPEData )
{
    std::vector<std::wstring> expectedResults;
    expectedResults.push_back(L"blah.cfexe");
    //files
    expectedResults.push_back(L"tail.cfexe");
    expectedResults.push_back(L"zip.cfexe");
    std::vector<std::wstring> actualResults;
    for(std::vector<fileData>::const_iterator it = testSuiteCandidates.begin(); it != testSuiteCandidates.end(); it++)
    {
        if (it->isPE()) actualResults.push_back(it->getFileName().substr(it->getFileName().find_last_of(L'\\') + 1 ));
    }

    std::sort(expectedResults.begin(), expectedResults.end());
    std::sort(actualResults.begin(), actualResults.end());

    std::vector<std::wstring> missed;
    std::set_difference(expectedResults.begin(), expectedResults.end(), actualResults.begin(), actualResults.end(), std::back_inserter(missed));

    std::vector<std::wstring> incorrect;
    std::set_difference(actualResults.begin(), actualResults.end(), expectedResults.begin(), expectedResults.end(), std::back_inserter(incorrect));

    for(std::vector<std::wstring>::const_iterator it = missed.begin(); it != missed.end(); it++)
    {
        BOOST_ERROR(std::string("The file ") + convertUnicode(*it) + " was expected but not returned.");
    }

    for(std::vector<std::wstring>::const_iterator it = incorrect.begin(); it != incorrect.end(); it++)
    {
        BOOST_ERROR(std::string("The file ") + convertUnicode(*it) + " was returned but not expected.");
    }

    BOOST_CHECK(true); //Suppress commandline "No assertions" warning

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:38:20+00:00Added an answer on May 12, 2026 at 9:38 am

    What do you think this will return, in the event that the file was unexpected? It looks like you expect it to be a valid file name.

    std::vector<std::wstring>::const_iterator target = std::lower_bound(expectedResults.begin(), expectedResults.end(), theFileString);
    

    It will in fact be an iterator at one past the end of the array – you cannot treat it as a valid pointer:

    BOOST_REQUIRE_MESSAGE(*target == theFileString, std::string("The file ") + convertUnicode(theFileString) + " was unexpected." );
    

    Normall, you compare the result with the value of end() (fixed):

    BOOST_REQUIRE_MESSAGE(target != expectedResults.end() || *target == theFileString, std::string("The file ") + convertUnicode(theFileString) + " was unexpected." );
    if (target != expectedResults.end() && *target == theFileString)
    {
      expectedResults.erase(target);
    }
    

    See in this example here, lower_bound will return off-then-end values:

    int main()
    {
      int A[] = { 1, 2, 3, 3, 3, 5, 8 };
      const int N = sizeof(A) / sizeof(int);
    
      for (int i = 1; i <= 10; ++i) {
        int* p = lower_bound(A, A + N, i);
        cout << "Searching for " << i << ".  ";
        cout << "Result: index = " << p - A << ", ";
        if (p != A + N)
          cout << "A[" << p - A << "] == " << *p << endl;
        else
          cout << "which is off-the-end." << endl;
      }
    }
    
    The output is:
    
    Searching for 1.  Result: index = 0, A[0] == 1
    Searching for 2.  Result: index = 1, A[1] == 2
    Searching for 3.  Result: index = 2, A[2] == 3
    Searching for 4.  Result: index = 5, A[5] == 5
    Searching for 5.  Result: index = 5, A[5] == 5
    Searching for 6.  Result: index = 6, A[6] == 8
    Searching for 7.  Result: index = 6, A[6] == 8
    Searching for 8.  Result: index = 6, A[6] == 8
    Searching for 9.  Result: index = 7, which is off-the-end.
    Searching for 10.  Result: index = 7, which is off-the-end.
    

    You cannot safely dereference the off-the-end value, but you can use it for the purpose of comparison, or as an insertion point.

    Why are you using lower_bound anyway? For the purposes of search, surely you should use find? If you are using lower_bound, it will return a position where the file name could be inserted, but not necessarily equal to the file name you are looking for. In other words, you not only need to compare with the off-the-end value, but also with the file name, in case it returned something valid.

    Here is a version that uses find. As you can see it is simpler than the fixed version above.

    std::vector<std::wstring>::const_iterator target = std::find(expectedResults.begin(), expectedResults.end(), theFileString);
    BOOST_REQUIRE_MESSAGE(target != expectedResults.end(), std::string("The file ") + convertUnicode(theFileString) + " was unexpected." );
    if (target != expectedResults.end())
    {
      expectedResults.erase(target);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.