ASSERT_NE clarifications

116 views
Skip to first unread message

josephnicho...@gmail.com

unread,
May 7, 2019, 9:34:25 AM5/7/19
to Google C++ Testing Framework

Hello,

 

I have a question about ASSERT_NE, so I am thinking the assertion returns true if the 2 parameters are not equal right? But it seems to be failing on my unit test. Please see below, even though value 1 and value 2 are not equal. This should pass right?

 

Expected: (account.get_keys().m_spend_secret_key) != (keys.m_spend_secret_key),

actual: <821f77909fbd52db7596324128e63425e4e4b7d253e73ca95c12a968dd4b3f60e4ed086b9944aee0> vs <8d04533ad362f4273322b6091c7aff7e32ffa33b2a875a3fb100eace840db50d2f83b41d09>


Thanks.

Josh Kelley

unread,
May 7, 2019, 9:58:19 AM5/7/19
to josephnicho...@gmail.com, Google C++ Testing Framework
What data type is m_spend_secret_key? If it overrides operator!=, that might explain the behavior you're seeing.

-- 
Josh Kelley

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googletestframework/b2d73626-00ac-4a8a-aafb-1933c4b10012%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joseph Nicholas Alcantara

unread,
May 12, 2019, 2:01:06 PM5/12/19
to Google C++ Testing Framework
Hi Josh Kelley.

Thanks for the reply.

m_spend_secret_key is of type unsigned char[3504].

I think the != was not overloaded on this scope, what noticed is, when I do ASSERT_NE(1,2) it outputs correctly, but when I do the ASSERT_NE(<some 3504 byte char>, <some 3504 byte char>) it does not behave correctly even though I have debugged and found out params are different, one crazy thing though is when I do ASSERT_EQ(<some 3504 byte char>, <some 3504 byte char>) even with same params, it somewhat asserts that they are equal.

Is there limitation or something that I don't know of?

Thanks.


On Tuesday, May 7, 2019 at 9:58:19 PM UTC+8, Josh Kelley wrote:
What data type is m_spend_secret_key? If it overrides operator!=, that might explain the behavior you're seeing.

-- 
Josh Kelley

On Tue, May 7, 2019 at 9:34 AM <josephnicho...@gmail.com> wrote:

Hello,

 

I have a question about ASSERT_NE, so I am thinking the assertion returns true if the 2 parameters are not equal right? But it seems to be failing on my unit test. Please see below, even though value 1 and value 2 are not equal. This should pass right?

 

Expected: (account.get_keys().m_spend_secret_key) != (keys.m_spend_secret_key),

actual: <821f77909fbd52db7596324128e63425e4e4b7d253e73ca95c12a968dd4b3f60e4ed086b9944aee0> vs <8d04533ad362f4273322b6091c7aff7e32ffa33b2a875a3fb100eace840db50d2f83b41d09>


Thanks.

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframework+unsub...@googlegroups.com.

Samuel Benzaquen

unread,
May 13, 2019, 10:41:28 AM5/13/19
to Joseph Nicholas Alcantara, Google C++ Testing Framework
From: Joseph Nicholas Alcantara <josephnicho...@gmail.com>
Date: Sun, May 12, 2019 at 2:01 PM
To: Google C++ Testing Framework

Hi Josh Kelley.

Thanks for the reply.

m_spend_secret_key is of type unsigned char[3504].

Arrays do _not_ have relational operators.
When you do `some_array == some_other_array` what is happening is that both arrays are decaying to pointers and then only the pointers are being compared.
This means that any two arrays will always be different because their pointers are different.
This will never look into the elements of the arrays.

You can avoid this problem by using a higher level class, like std::array<> or absl::Span<>, that will provide deep relational semantics.
 

I think the != was not overloaded on this scope, what noticed is, when I do ASSERT_NE(1,2) it outputs correctly, but when I do the ASSERT_NE(<some 3504 byte char>, <some 3504 byte char>) it does not behave correctly even though I have debugged and found out params are different, one crazy thing though is when I do ASSERT_EQ(<some 3504 byte char>, <some 3504 byte char>) even with same params, it somewhat asserts that they are equal.

Is there limitation or something that I don't know of?

Thanks.

On Tuesday, May 7, 2019 at 9:58:19 PM UTC+8, Josh Kelley wrote:
What data type is m_spend_secret_key? If it overrides operator!=, that might explain the behavior you're seeing.

-- 
Josh Kelley

On Tue, May 7, 2019 at 9:34 AM <josephnicho...@gmail.com> wrote:

Hello,

 

I have a question about ASSERT_NE, so I am thinking the assertion returns true if the 2 parameters are not equal right? But it seems to be failing on my unit test. Please see below, even though value 1 and value 2 are not equal. This should pass right?

 

Expected: (account.get_keys().m_spend_secret_key) != (keys.m_spend_secret_key),

actual: <821f77909fbd52db7596324128e63425e4e4b7d253e73ca95c12a968dd4b3f60e4ed086b9944aee0> vs <8d04533ad362f4273322b6091c7aff7e32ffa33b2a875a3fb100eace840db50d2f83b41d09>


Thanks.

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googletestframework/0aa1d82a-4999-440d-b679-9b3cf9a6f821%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to the Google Groups "opensource-gtest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to opensource-gte...@google.com.
To view this discussion on the web visit https://groups.google.com/a/google.com/d/msgid/opensource-gtest/0aa1d82a-4999-440d-b679-9b3cf9a6f821%40googlegroups.com.

Josh Kelley

unread,
May 13, 2019, 5:30:03 PM5/13/19
to Joseph Nicholas Alcantara, Google C++ Testing Framework
An "unsigned char[3504]" is an array, so it decays to a pointer, and so Google Test compares the pointer values rather than comparing the string contents. The primer explains this in a bit more detail. (See the section on "pointer equality on pointers.") Try using ASSERT_STREQ instead, or (as Samuel Benzaquen suggested), try using a higher-level class.

-- 
Josh Kelley

Joseph Nicholas Alcantara

unread,
May 15, 2019, 8:22:20 AM5/15/19
to Google C++ Testing Framework
@Samuel and @Josh,

I am very thankful for both of you, sometimes I am just to ashamed to say that I am a C++ developer and not knowing all those pointer degradation. Thank you very much, I will look on your suggestions both,
and Samuel's approach is really a great long term improvement.

Best,
Joseph

Keith Ray

unread,
May 15, 2019, 10:30:53 AM5/15/19
to Joseph Nicholas Alcantara, Google C++ Testing Framework
--

---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googletestframework/765d5fac-c62e-4fc3-a2fc-06e659390aee%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages