title: CMake and GoogleTest weird behaviour with comparison when changing build type from Debug to Release
body: <h2>Context</h2>
  <p>I am writing a function which calculates some exponential value for a timer application. It simply takes 2^x until some threshold maxVal, in which case the threshold should be returned. Also, in all cases, edge cases should be accepted.</p>
  <p>util.cpp:</p>
  <pre><code>#include &quot;util.h&quot;
  #include &lt;iostream&gt;
  
  int calculateExponentialBackoffDelay(int x, int maxVal)
  {
      int y;
      
      if(x &lt;= 0 || maxVal &lt;= 0) return maxVal;
      else if (x &gt; maxVal) return maxVal;
  
      y = std::pow(2, x);
      std::cout &lt;&lt; &quot;y = &quot; &lt;&lt; y &lt;&lt; std::endl;
      
      if(y &gt; maxVal) return maxVal;
      else if(y &lt; 0) return maxVal;
      else return y;
  }
  </code></pre>
  <p>Now I make a CMake configuration with a GoogleTest dependency fetch.</p>
  <p>CMakeLists.txt:</p>
  <pre><code>cmake_minimum_required(VERSION 3.14)
  project(my_project)
  
  # GoogleTest requires at least C++14
  set(CMAKE_CXX_STANDARD 14)
  set(CMAKE_BUILD_TYPE Release)
  
  include(FetchContent)
  FetchContent_Declare(
    googletest
    GIT_REPOSITORY https://github.com/google/googletest.git
    GIT_TAG release-1.12.1
  )
  # For Windows: Prevent overriding the parent project's compiler/linker settings
  set(gtest_force_shared_crt ON CACHE BOOL &quot;&quot; FORCE)
  FetchContent_MakeAvailable(googletest)
  
  enable_testing()
  include_directories(
      ${CMAKE_CURRENT_LIST_DIR}/
      )
  # Add the main source compilation units
  add_executable(
    test_calc
    test_calc.cpp
    util.cpp
  )
  target_link_libraries(
    test_calc
    GTest::gtest_main
  )
  
  include(GoogleTest)
  gtest_discover_tests(test_calc)
  </code></pre>
  <p>I run a test on the function that I have written which tests some boundary conditions. One of which is testing that if 2^x &gt; maxVal, then maxVal should just be returned (because the result of the 2^x is above the maximum value). This is the threshold.</p>
  <p>test_calc.cpp:</p>
  <pre><code>#include &quot;util.h&quot;
  #include &lt;climits&gt;
  #include &lt;gtest/gtest.h&gt;
  
  TEST(util_tests, ExponentialBackoff)
  {
      int x, maxVal, res;
  
      // Test x = maxVal and maxVal = 1000
      // Expected output: 1000
      maxVal = 1000;
      x = maxVal;
      EXPECT_EQ(maxVal, calculateExponentialBackoffDelay(x, maxVal));
  }
  </code></pre>
  <p>When I set x and maxVal to 1000, 2^(1000) is calculated and because it's such a big number, there is an overflow / wraparound to a really negative number (-2<code>147</code>483`648). That is expected, and therefore my test will expect the result of x=1000, maxVal=1000 to be &lt; 0.</p>
  <h1>Problem</h1>
  <p>This is where things go unexpectedly. I run ctest inside my build directory and all test cases pass. Then I change one line in CMakeLists.txt from ... :</p>
  <pre><code>set(CMAKE_BUILD_TYPE Debug)
  </code></pre>
  <p>... to ...</p>
  <pre><code>set(CMAKE_BUILD_TYPE Release)
  </code></pre>
  <p>... and that test case fails:</p>
  <pre><code>1: Expected equality of these values:
  1:   maxVal
  1:     Which is: 1000
  1:   calculateExponentialBackoffDelay(x, maxVal)
  1:     Which is: -2147483648
  </code></pre>
  <p>So for some reason, the case where y &lt; 0 inside the function body is not being reached and instead, we are returning the wraparound result.</p>
  <p>Why is this? What am I doing wrong? I tried using the Linux <code>strip -s test_calc</code> to check if it was a symbols thing while keeping the debug configuration in CMake, only to find that test cases still pass. What else does CMake do to change the comparison behaviour of the resulting binary?</p>