chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hello, I am getting the following exception when trying to get a static const member.

```
 Exception Code: C0000005
```

Here's an example:

```
class Person {
public:
    Person() {};

private:
    static const int m_static_const_member = 2;
};

TEST_METHOD(GetStaticConstMember)
{
    Person* person = new Person();

    // now let's get that private member
    int static_const_member;
    ISOLATOR_GET_VARIABLE(person, m_static_const_member, static_const_member);

    Assert::AreEqual(2, static_const_member);
}
```
, I can access a simple `const int` with this code but not a `static const int`
Did I miss something or is it not possible ?
asked by rea (600 points)

2 Answers

0 votes

Hi Rea

Thanks for reaching out! We were able to reproduce the issue.
We will investigate it and keep you posted on our progress.

Cheers,

Lilach Derfel

Support Specialist.

answered by lilachd (300 points)
0 votes
Hello Rea,

After checking it out with our development team, I am sorry to say that we currently do not support the access to a hidden static constant variable.

But we added it to our Roadmap and we will work on it ASAP.

Could you please tell us why you want to access a private static constant variable? Are you writing a specific test that needs it? If so, we will be more than happy to help you with the test.

Regards,

Lilach Derfel

Support Specialist
answered by lilachd (300 points)

Hello thank you for your answer.

Currently the test looks like this. Simplified for the context but should be enough I think.

class MyClass {

public: 

friend class MyClassTest;

MyClass() = default;

private:

static const int max_value_ = 50;

static const int measured_value_ = 20;

}

enum class MyEnum {

ONE,

TWO,

THREE

};

int MyClassTest::MyTest(MyEnum e) {

int max_value = MyClass::max_value_;

int measured_value_ = MyClass::measured_value_; 

switch(e) {

case MyEnum::ONE: return max_value_ - measured_value_;

case MyEnum::TWO: return max_value_ + measured_value_;

default:

assert(e == MyEnum::THREE);

return max_value_ * measured_value_;

}

}

For this example, i can't modify the class MyClass except that we should remove this `friend` which makes no sense.

Which is why i want to modify MyTest to get the static const value.

Hi Rea,

Thanks for your reply and so sorry for the delay!

As I said before, we do not support faking static const member yet.

If it was necessary for you to set the variables as constant, could you please tell us why they must be constant?

Thank you!

Regards,

Lilach Derfel

Support Specialist
Hello,

I am not sure how to respond to this. The member variable should be read-only which is why the keyword const is used. However, if it's only declared const, it could be modified by a pointer-to-const. The most common example would be changing the value with the constructor. To avoid that, the keyword static is used.
Hope it answers question.

Regards,
Hi Rea,

Thanks for your reply! I sent you an email last week and hopefully you received it.

I understand that sometimes the complier's optimization for a constant might make it less meaningful to fake a static constant in C++. We are investigating this.

In the meantime,  we are sure that there is another way to perform the TEST you want. Could you please give an example of what you are trying to TEST and we can help you write this test specifically.

Looking forward to your reply!

Regards,

Lilach
Hi,

I already put the test we want in a previous answer (at least the beginning since we can't go further). For now, we can just directly write the value instead of trying to get the static const variable. It's not expected to change. However if it does, we would have to modify it both in source and in the test which is something we should not have to do ideally.

If something is missing in my example, please point out the issue. I'll try to fix it.

 
Also as a side node, the keyword "default" is not recognize. I had to use "{}" instead. It's not a big issue but do you know if it will be supported in the future ?
 

class MyClass {

public:

friend class MyClassTest;

MyClass() {};

private:

static const int max_value_ = 50;

static const int measured_value_ = 20;

}

enum class MyEnum {

ONE,

TWO,

THREE

};

int MyClassTest::MyTest(MyEnum e) {

int max_value = MyClass::max_value_;

int measured_value_ = MyClass::measured_value_;   

switch(e) {

case MyEnum::ONE: return max_value_ - measured_value_;

case MyEnum::TWO: return max_value_ + measured_value_;

default:

assert(e == MyEnum::THREE);

return max_value_ * measured_value_;

}}
Hi Rea,

So sorry for the late reply!

After checking it out with our development team, I am sorry to tell you that we do not support it at this moment as it sometimes does not even exist as code and the constant variable is copied by the compiler.

Regarding the default issue, I tired to reproduce the same issue but the keyword default is recognized from my side -- maybe I did not do it in a correct way in order to reproduce the issue.

Is that possible by your convenience to send us a sample code of the default keyword issue? We will be more than happy to help!

Regards,

Lilach

Hello, thanks for the answer. I am actually not sure if it's the default keyword or something else as the behavior is really strange in my opinion. 


Here's a sample:

// HPP

Class FakeTest

{

public:

    FakeTest() = default;

    FakeTest(int id);

private:

    int m_id;

};

// CPP 

FakeTest::FakeTest(int id) : m_id(id) {}

// TEST

TEST_METHOD(CallConstructorTest)

{

    FakeTest* fake = new FakeTest();        // Unrelated but needed to make it compile.

    auto fake_test = FAKE<FakeTest>();

    ISOLATOR_INVOKE_CONSTRUCTOR(fake_test, 2);

    ISOLATOR_SET_VARIABLE(fake_test, m_id, 3);

    int id;

    ISOLATOR_GET_VARIABLE(fake_test, m_id, id);

    Assert::AreEqual(3, id);

}

ERROR: Assert failed. The function 'FakeTest::FakeTest' could not be found. If the method is not called in the code under test it will be optimized away. Remove any inline keywords, and Add ISOLATOR_TESTABLE to the method.

Now in the HPP part, modify the default to `FakeTest() {}` instead and it will work.

Now another thing that i don't understand. Remove ` FakeTest* fake = new FakeTest();` from the test and it will fail again.
Note that you can add this line in another test above that one and for some reason, it will affect this test and make it works. Also might need to clear cache as sometimes it keeps working even after removing the line.
 

Hi Rea,

Thanks for your reply and favor for our product!

In order to help you and explain better, I sent you an email.

Regards,

Lilach
...