Worldstone
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
SystemUtilsTests.cpp
Go to the documentation of this file.
1 
4 #include <SystemUtils.h>
5 #include <doctest.h>
6 
7 using WorldStone::Utils::signExtend;
11 TEST_CASE("SignExtend")
12 {
13  // clang-format off
14  SUBCASE("Positive input")
15  {
16  CHECK(signExtend<int32_t,13>( 0) == 0);
17  CHECK(signExtend<int32_t,32>(0x16641337) == 0x16641337);
18  CHECK(signExtend<int32_t,32>(0x7FFFFFFF) == 0x7FFFFFFF);
19  CHECK(signExtend<int32_t, 2>( 0b01) == 1);
20  CHECK(signExtend<int32_t, 3>( 0b011) == 3);
21  }
22  SUBCASE("Negative input")
23  {
24  CHECK(signExtend<int32_t, 5>( 0b10110) == -10);
25  CHECK(signExtend<int32_t, 8>( 0xFF) == -1);
26  CHECK(signExtend<int32_t,32>(0xFFFFFFFF) == -1);
27  CHECK(signExtend<int32_t,32>(0xDEADBEEF) == 0xDEADBEEF);
28  CHECK(signExtend<int32_t,16>(0xDEADBEEF) == 0xFFFFBEEF);
29  CHECK(signExtend<int32_t,16>(0x16641337) == 0x1337);
30  CHECK(signExtend<int32_t, 8>( 0x80) == -128);
31  CHECK(signExtend<int32_t, 3>( 0x07) == -1);
32  CHECK(signExtend<int32_t, 9>( 0x1CE) == -50);
33  }
34  SUBCASE("1-bit special case")
35  {
36  CHECK(signExtend<int32_t, 1>(0b0) == 0);
37  CHECK(signExtend<int32_t, 1>(0b1) == -1); // Sign extension => 0xFFFFFFFF => -1
38  }
39  // clang-format on
40 }
41 
42 using WorldStone::Utils::reverseBits;
44 TEST_CASE("ReverseBits")
45 {
46  CHECK(reverseBits<uint8_t>(0xF0) == 0x0F);
47  CHECK(reverseBits<uint16_t>(0xF0) == 0x0F00);
48  CHECK(reverseBits<uint32_t>(0xF0) == 0x0F000000);
49  CHECK(reverseBits<uint64_t>(0xF0) == 0x0F00000000000000);
50  CHECK(reverseBits<uint32_t>(0x01234567) == 0xE6A2C480);
51 }
52 
53 using WorldStone::Utils::popCount;
57 TEST_CASE("PopCount 16bits")
58 {
59  CHECK(popCount(uint16_t(0b0000000000000000)) == 0);
60  CHECK(popCount(uint16_t(0b0000000000011111)) == 5);
61  CHECK(popCount(uint16_t(0b1110011101011001)) == 10);
62  CHECK(popCount(uint16_t(0b1111111111111111)) == 16);
63 
64  CHECK(popCount(uint32_t(0x00000000)) == 0);
65  CHECK(popCount(uint32_t(0x000F000F)) == 8);
66  CHECK(popCount(uint32_t(0x0F000F00)) == 8);
67  CHECK(popCount(uint32_t(0xFFFFFFFF)) == 32);
68 
69  CHECK(popCount(uint64_t(0x0000000000000000)) == 0);
70  CHECK(popCount(uint64_t(0x00000000000000FF)) == 8);
71  CHECK(popCount(uint64_t(0x000000000000FF00)) == 8);
72  CHECK(popCount(uint64_t(0xFFFFFFFFFFFFFFFF)) == 64);
73 }
TEST_CASE("SignExtend")
Test that SignExtend is giving the right values.