mardi 31 janvier 2017

Validating test code against actual string content _M_dataplus._M_p

When copying strings, I noticed that the actual string content is preserved/reused but a new std::string constructor wraps around it.

I'm writing a lengthy unit test code on C++ std::string handling (also applies to std::wstring) and ensuring that it didn't get overwritten inside nested function.

My problem stems from my own poorly-written C++ test case where I am UNABLE to create a unique string content without being subjected to compiler-optimized away the duplicate constant strings.

std::wstring unique_test_str1 = L"counter";
std::wstring unique_test_str2 = uniqueteststr1;

GNU debugger (gdb) reports:

gdb> p unique_test_str1._M_dataplus._M_p
unique_test_str1._M_dataplus._M_p = 0x6173a8 L"counter"
gdb>
gdb> p unique_test_str2._M_dataplus._M_p
unique_test_str2._M_dataplus._M_p = 0x6173a8 L"counter"

Test code fails because I am unable to test for actual address of string content (via ._M_dataplus._M_p). Yet it passes as having a unique pointer to each string constructor.

assert( unique_test_str != unique_test_str2 );

And I cannot write a unit test against a private _M_p variable of a basic_string class.

I'm sure it is something stupid, but how does one make sure that each and all L"counter" constant string is unique?

Aucun commentaire:

Enregistrer un commentaire