title: C++ test to verify equality operator is kept consistent with struct over time
body: <p>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. </p>
  
  <hr>
  
  <p>I have a POD struct &amp; an equality operator, a (very) small part of a system with >100 engineers.  </p>
  
  <p>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).</p>
  
  <p>As Tomalak pointed out - comments &amp; "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.</p>
  
  <p>I'm not coming up with a satisfactory answer - this is the best I've thought of:</p>
  
  <pre><code>-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*)&amp;x
      -iterator over ptr (for sizeof(x))
          -increment the current byte
          -check !(x==y)
          -decrement the current byte
          -check x==y
  </code></pre>
  
  <p>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)</p>
  
  <p>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.</p>
  
  <p>Any better solutions?</p>
  
  <p>(This shouldn't matter, but I'm using VS2008, rtti is off, googletest suite)</p>