byte_tests.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
  4. //
  5. // This code is licensed under the MIT License (MIT).
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  8. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  12. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  13. // THE SOFTWARE.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include <gtest/gtest.h>
  17. #include <gsl/byte> // for to_byte, to_integer, byte, operator&, ope...
  18. using namespace std;
  19. using namespace gsl;
  20. namespace
  21. {
  22. int modify_both(gsl::byte& b, int& i)
  23. {
  24. i = 10;
  25. b = to_byte<5>();
  26. return i;
  27. }
  28. TEST(byte_tests, construction)
  29. {
  30. {
  31. const byte b = static_cast<byte>(4);
  32. EXPECT_TRUE(static_cast<unsigned char>(b) == 4);
  33. }
  34. {
  35. const byte b = byte(12);
  36. EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
  37. }
  38. {
  39. const byte b = to_byte<12>();
  40. EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
  41. }
  42. {
  43. const unsigned char uc = 12;
  44. const byte b = to_byte(uc);
  45. EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
  46. }
  47. #if defined(__cplusplus) && (__cplusplus >= 201703L)
  48. {
  49. const byte b{14};
  50. EXPECT_TRUE(static_cast<unsigned char>(b) == 14);
  51. }
  52. #endif
  53. #ifdef CONFIRM_COMPILATION_ERRORS
  54. to_byte(char{});
  55. to_byte(3);
  56. to_byte(3u);
  57. #endif
  58. }
  59. TEST(byte_tests, bitwise_operations)
  60. {
  61. const byte b = to_byte<0xFF>();
  62. byte a = to_byte<0x00>();
  63. EXPECT_TRUE((b | a) == to_byte<0xFF>());
  64. EXPECT_TRUE(a == to_byte<0x00>());
  65. a |= b;
  66. EXPECT_TRUE(a == to_byte<0xFF>());
  67. a = to_byte<0x01>();
  68. EXPECT_TRUE((b & a) == to_byte<0x01>());
  69. a &= b;
  70. EXPECT_TRUE(a == to_byte<0x01>());
  71. EXPECT_TRUE((b ^ a) == to_byte<0xFE>());
  72. EXPECT_TRUE(a == to_byte<0x01>());
  73. a ^= b;
  74. EXPECT_TRUE(a == to_byte<0xFE>());
  75. a = to_byte<0x01>();
  76. EXPECT_TRUE(~a == to_byte<0xFE>());
  77. a = to_byte<0xFF>();
  78. EXPECT_TRUE((a << 4) == to_byte<0xF0>());
  79. EXPECT_TRUE((a >> 4) == to_byte<0x0F>());
  80. a <<= 4;
  81. EXPECT_TRUE(a == to_byte<0xF0>());
  82. a >>= 4;
  83. EXPECT_TRUE(a == to_byte<0x0F>());
  84. }
  85. TEST(byte_tests, to_integer)
  86. {
  87. const byte b = to_byte<0x12>();
  88. EXPECT_TRUE(0x12 == gsl::to_integer<char>(b));
  89. EXPECT_TRUE(0x12 == gsl::to_integer<short>(b));
  90. EXPECT_TRUE(0x12 == gsl::to_integer<long>(b));
  91. EXPECT_TRUE(0x12 == gsl::to_integer<long long>(b));
  92. EXPECT_TRUE(0x12 == gsl::to_integer<unsigned char>(b));
  93. EXPECT_TRUE(0x12 == gsl::to_integer<unsigned short>(b));
  94. EXPECT_TRUE(0x12 == gsl::to_integer<unsigned long>(b));
  95. EXPECT_TRUE(0x12 == gsl::to_integer<unsigned long long>(b));
  96. // EXPECT_TRUE(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
  97. // EXPECT_TRUE(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
  98. }
  99. TEST(byte_tests, aliasing)
  100. {
  101. int i{0};
  102. const int res = modify_both(reinterpret_cast<byte&>(i), i);
  103. EXPECT_TRUE(res == i);
  104. }
  105. } // namespace
  106. #ifdef CONFIRM_COMPILATION_ERRORS
  107. copy(src_span_static, dst_span_static);
  108. #endif