Add test to demonstrate byte aliasing problem on g++ and clang++

This commit is contained in:
MikeGitb 2016-09-18 22:19:04 +02:00
parent e4a34128a8
commit 1869ff56b3
2 changed files with 17 additions and 3 deletions

View File

@ -24,9 +24,9 @@ else()
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
if(COMPILER_SUPPORTS_CXX14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wno-missing-braces")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -O3 -Wall -Wno-missing-braces")
elseif(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wno-missing-braces")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3 -Wall -Wno-missing-braces")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

View File

@ -43,7 +43,7 @@ SUITE(byte_tests)
byte b = byte(12);
CHECK(static_cast<unsigned char>(b) == 12);
}
{
byte b = to_byte<12>();
CHECK(static_cast<unsigned char>(b) == 12);
@ -61,6 +61,20 @@ SUITE(byte_tests)
//}
}
int modify_both(gsl::byte * b, int* i)
{
*i = 10;
*b = to_byte<5>();
return *i;
}
TEST(aliasing)
{
int i{0};
int res = modify_both(reinterpret_cast<byte*>(&i), &i);
CHECK(res == i);
}
TEST(bitwise_operations)
{
byte b = to_byte<0xFF>();