Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Tests fail with exception code c0000005

I am trying to create Unit Tests in Visual Studios 2012 using a Native Unit Test Project.

This is the test I have:

TEST_METHOD(CalculationsRoundTests)
{
    int result = Calculations::Round(1.0);
    Assert::AreEqual(1, result);
}

Exporting the Class:

#ifdef EXPORT_TEST_FUNCTIONS
#define MY_CALCULATIONS_EXPORT __declspec(dllexport)
#else
#define MY_CALCULATIONS_EXPORT
#endif
...
class CALCULATIONS_EXPORT Calculations {
...
public:
static int Round(const double& x);

The function itself:

int Calculations::Round(const double& x)
{
    int temp;
    if (floor(x) + 0.5 > x)
        temp = floor(x);
    else
        temp = ceil(x);

    return int(temp);
}

However, the test nearly always fails with error code c0000005 (Access Violation). The test will fail the first time that x, or any other variable that may be declared in the function, is used.

I followed the instructions at Unresolved externals when compiling unit tests for Visual C++ 2012

like image 261
Morgan Redshaw Avatar asked Aug 11 '13 03:08

Morgan Redshaw


People also ask

How do I fix exception code C0000005?

On the 'Advanced Configuration' dialog, set the 'Maximum Userdump Limit' to 25, then click the 'Exceptions' button. Click 'Add Exception' Select C0000005 in the list, then set the 'Action Type' to 'Full Userdump' and the 'Action Limit' to 25. Click 'OK', then 'Save and Close' and finally click 'Next'

What is C0000005?

A C0000005 error is memory error. Specifically, a C0000005 error is an access violation error caused by a buffer overrun.

How do I run Mstest in Visual Studio?

Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.


1 Answers

This is a known bug. Unfortunately, Microsoft considers this as "Won't Fix".

In short, there are two workarounds:

  1. Compile the actual project in release mode and the test project in debug mode.
  2. Outsource all testable functions to a static library project.
like image 199
manuel Avatar answered Sep 16 '22 11:09

manuel