6

I voted up @TomalakGeretkal for a good note about by-contract; I'm haven't accepted an answer as my question is how to programatically check the equals function.


I have a POD struct & an equality operator, a (very) small part of a system with >100 engineers.

Over time I expect the struct to be modified (members added/removed/reordered) and I want to write a test to verify that the equality op is testing every member of the struct (eg is kept up to date as the struct changes).

As Tomalak pointed out - comments & "by contract" is often the best/only way to enforce this; however in my situation I expect issues and want to explore whether there are any ways to proactively catch (at least many) of the modifications.

I'm not coming up with a satisfactory answer - this is the best I've thought of:

-new up two instances struct (x, y), fill each with identical non-zero data.
-check x==y
-modify x "byte by byte"
    -take ptr to be (unsigned char*)&x
    -iterator over ptr (for sizeof(x))
        -increment the current byte
        -check !(x==y)
        -decrement the current byte
        -check x==y

The test passes if the equality operator caught every byte (NOTE: there is a caveat to this - not all bytes are used in the compilers representation of x, therefore the test would have to 'skip' these bytes - eg hard code ignore bytes)

My proposed test has significant problems: (at least) the 'don't care' bytes, and the fact that incrementing one byte of the types in x may not result in a valid value for the variable at that memory location.

Any better solutions?

(This shouldn't matter, but I'm using VS2008, rtti is off, googletest suite)

5 Answers 5

8

Though tempting to make code 'fool-proof' with self-checks like this, it's my experience that keeping the self-checks themselves fool-proof is, well, a fool's errand.

Keep it simple and localise the effect of any changes. Write a comment in the struct definition making it clear that the equality operator must also be updated if the struct is; then, if this fails, it's just the programmer's fault.

I know that this will not seem optimal to you as it leaves the potential for user error in the future, but in reality you can't get around this (at least without making your code horrendously complicated), and often it's most practical just not to bother.

3
  • I agree that 'by contract' is the best you can often do - in my case I have situation where I expect problems and want to explore if there are any ways to be proactive and catch at least most "programmer ignored comments" occurances. Thanks for your help! Commented May 2, 2011 at 0:22
  • @mike: Thanks; that's the phrase I was after. :) I'd go so far as to say that, often, it's the best you should do. This is pretty subjective, mind you. Commented May 2, 2011 at 0:22
  • I'm going to vote up this response, as it is correct - I'm not marking as accepted as my question is how to programatically check the equals function. Commented May 3, 2011 at 17:53
3

I agree with (and upvoted) Tomalak's answer. It's unlikely that you'll find a foolproof solution. Nonetheless, one simple semi-automated approach could be to validate the expected size within the equality operator:

MyStruct::operator==(const MyStruct &rhs) 
{
  assert(sizeof(MyStruct) == 42);  // reminder to update as new members added

  // actual functionality here ...
}

This way, if any new members are added, the assert will fire until someone updates the equality operator. This isn't foolproof, of course. (Member vars might be replaced with something of same size, etc.) Nonetheless, it's a relatively simple (one line assert) that has a good shot of detecting the error case.

2
  • 3
    it could be improved with a static_assert
    – justin
    Commented May 2, 2011 at 3:14
  • Have seen this in my current organisation's codebase (well, justin's version anyway), and to be fair it works pretty well. Literally caught me out ;) Commented Jul 22, 2019 at 15:23
0

I'm sure I'm going to get downvoted for this but...

How about a template equality function that takes a reference to an int parameter, and the two objects being tested. The equality function will return bool, but will increment the size reference (int) by the sizeof(T).

Then have a large test function that calls the template for each object and sums the total size --> compare this sum with the sizeof the object. The existence of virtual functions/inheritance, etc could kill this idea.

3
  • The existence of virtual functions in a POD? Commented May 2, 2011 at 14:51
  • These are excluded by definition? I was just trying to grasp for areas where it could break down -- maybe a virtual destructor?
    – Jess
    Commented May 2, 2011 at 18:37
  • A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-declared copy assignment operator and no user-declared destructor. An aggregate is an array or a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions. Commented May 2, 2011 at 19:37
0

it's actually a difficult problem to solve correctly in a self-test.

the easiest solution i can think of is to take a few template functions which operate on multiple types, perform the necessary conversions, promotions, and comparisons, then verify the result in an external unit test. when a breaking change is introduced, at least you'll know.

some of these challenges are more easily maintained/verified using approaches such as composition, rather than extension/subclassing.

0

Agree with Tomalak and Eric. I have used this for very similar problems.

Assert does not work unless the DEBUG is defined, so potentially you can release code that is wrong. These tests will not always work reliably. If the structure contains bit fields, or items are inserted that take up slack space cause by compiler aligning to word boundaries, the size won't change. For this reason they offer limited value. e.g.

struct MyStruct {   
    char a ;
    ulong l ;
}

changed to

struct MyStruct {   
    char a ;
    char b ;  
    ulong l ;
}

Both structures are 8 bytes (on 32bit Linux x86)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.