| 1 |
#include <gtest/gtest.h> |
| 2 |
#include <gtest/gtest-matchers.h> |
| 3 |
#include <gmock/gmock.h> |
| 4 |
#include <gmock/gmock-matchers.h> |
| 5 |
|
| 6 |
#include "utils/utils.hpp" |
| 7 |
|
| 8 |
using cpptrace::detail::byteswap; |
| 9 |
using cpptrace::detail::n_digits; |
| 10 |
using cpptrace::detail::to; |
| 11 |
using cpptrace::detail::raii_wrapper; |
| 12 |
using cpptrace::detail::raii_wrap; |
| 13 |
using cpptrace::detail::maybe_owned; |
| 14 |
|
| 15 |
namespace { |
| 16 |
|
| 17 |
TEST(ByteSwapTest, ByteSwapUint8) { |
| 18 |
uint8_t input = 0x12; |
| 19 |
uint8_t expected = 0x12; |
| 20 |
uint8_t result = byteswap(input); |
| 21 |
EXPECT_EQ(result, expected); |
| 22 |
} |
| 23 |
|
| 24 |
TEST(ByteSwapTest, ByteSwapInt8) { |
| 25 |
int8_t input = 0x7F; |
| 26 |
int8_t expected = 0x7F; |
| 27 |
int8_t result = byteswap(input); |
| 28 |
EXPECT_EQ(result, expected); |
| 29 |
|
| 30 |
int8_t neg_input = to<int8_t>(0x80); |
| 31 |
int8_t neg_expected = to<int8_t>(0x80); |
| 32 |
int8_t neg_result = byteswap(neg_input); |
| 33 |
EXPECT_EQ(neg_result, neg_expected); |
| 34 |
} |
| 35 |
|
| 36 |
TEST(ByteSwapTest, ByteSwapUint16) |
| 37 |
{ |
| 38 |
uint16_t input = 0x1234; |
| 39 |
uint16_t expected = 0x3412; |
| 40 |
uint16_t result = byteswap(input); |
| 41 |
EXPECT_EQ(result, expected); |
| 42 |
EXPECT_EQ(byteswap(to<uint16_t>(0x0000)), to<uint16_t>(0x0000)); |
| 43 |
EXPECT_EQ(byteswap(to<uint16_t>(0xFFFF)), to<uint16_t>(0xFFFF)); |
| 44 |
} |
| 45 |
|
| 46 |
TEST(ByteSwapTest, ByteSwapInt16) { |
| 47 |
int16_t input = 0x1234; |
| 48 |
int16_t expected = to<int16_t>(0x3412); |
| 49 |
int16_t result = byteswap(input); |
| 50 |
EXPECT_EQ(result, expected); |
| 51 |
|
| 52 |
int16_t neg_input = to<uint16_t>(0xFEFF); |
| 53 |
int16_t neg_expected = to<int16_t>(0xFFFE); |
| 54 |
int16_t neg_result = byteswap(neg_input); |
| 55 |
EXPECT_EQ(neg_result, neg_expected); |
| 56 |
} |
| 57 |
|
| 58 |
TEST(ByteSwapTest, ByteSwapUint32) |
| 59 |
{ |
| 60 |
uint32_t input = 0x12345678; |
| 61 |
uint32_t expected = 0x78563412; |
| 62 |
uint32_t result = byteswap(input); |
| 63 |
EXPECT_EQ(result, expected); |
| 64 |
EXPECT_EQ(byteswap(to<uint32_t>(0x00000000)), to<uint32_t>(0x00000000)); |
| 65 |
EXPECT_EQ(byteswap(to<uint32_t>(0xFFFFFFFF)), to<uint32_t>(0xFFFFFFFF)); |
| 66 |
} |
| 67 |
|
| 68 |
TEST(ByteSwapTest, ByteSwapInt32) |
| 69 |
{ |
| 70 |
int32_t input = 0x12345678; |
| 71 |
int32_t expected = to<int32_t>(0x78563412); |
| 72 |
int32_t result = byteswap(input); |
| 73 |
EXPECT_EQ(result, expected); |
| 74 |
|
| 75 |
int32_t neg_input = to<uint32_t>(0xFF000000); |
| 76 |
int32_t neg_expected = to<int32_t>(0x000000FF); |
| 77 |
int32_t neg_result = byteswap(neg_input); |
| 78 |
EXPECT_EQ(neg_result, neg_expected); |
| 79 |
} |
| 80 |
|
| 81 |
TEST(ByteSwapTest, ByteSwapUint64) |
| 82 |
{ |
| 83 |
uint64_t input = 0x1122334455667788ULL; |
| 84 |
uint64_t expected = 0x8877665544332211ULL; |
| 85 |
uint64_t result = byteswap(input); |
| 86 |
EXPECT_EQ(result, expected); |
| 87 |
EXPECT_EQ(byteswap(to<uint64_t>(0x0000000000000000ULL)), to<uint64_t>(0x0000000000000000ULL)); |
| 88 |
EXPECT_EQ(byteswap(to<uint64_t>(0xFFFFFFFFFFFFFFFFULL)), to<uint64_t>(0xFFFFFFFFFFFFFFFFULL)); |
| 89 |
} |
| 90 |
|
| 91 |
TEST(ByteSwapTest, ByteSwapInt64) |
| 92 |
{ |
| 93 |
int64_t input = 0x1122334455667788LL; |
| 94 |
int64_t expected = to<int64_t>(0x8877665544332211ULL); |
| 95 |
int64_t result = byteswap(input); |
| 96 |
EXPECT_EQ(result, expected); |
| 97 |
|
| 98 |
int64_t neg_input = to<uint64_t>(0xFF00000000000000ULL); |
| 99 |
int64_t neg_expected = to<int64_t>(0x00000000000000FFULL); |
| 100 |
int64_t neg_result = byteswap(neg_input); |
| 101 |
EXPECT_EQ(neg_result, neg_expected); |
| 102 |
} |
| 103 |
|
| 104 |
TEST(NDigitsTest, Basic) |
| 105 |
{ |
| 106 |
static_assert(n_digits(1) == 1, "n_digits utility producing the wrong result"); |
| 107 |
static_assert(n_digits(9) == 1, "n_digits utility producing the wrong result"); |
| 108 |
static_assert(n_digits(10) == 2, "n_digits utility producing the wrong result"); |
| 109 |
static_assert(n_digits(11) == 2, "n_digits utility producing the wrong result"); |
| 110 |
static_assert(n_digits(1024) == 4, "n_digits utility producing the wrong result"); |
| 111 |
|
| 112 |
EXPECT_EQ(n_digits(1), 1); |
| 113 |
EXPECT_EQ(n_digits(9), 1); |
| 114 |
EXPECT_EQ(n_digits(10), 2); |
| 115 |
EXPECT_EQ(n_digits(11), 2); |
| 116 |
EXPECT_EQ(n_digits(1024), 4); |
| 117 |
} |
| 118 |
|
| 119 |
struct test_deleter { |
| 120 |
static int call_count; |
| 121 |
static int last_value; |
| 122 |
|
| 123 |
void operator()(int value) { |
| 124 |
call_count++; |
| 125 |
last_value = value; |
| 126 |
} |
| 127 |
}; |
| 128 |
int test_deleter::call_count = 0; |
| 129 |
int test_deleter::last_value = 0; |
| 130 |
|
| 131 |
class RaiiWrapperTest : public ::testing::Test { |
| 132 |
protected: |
| 133 |
RaiiWrapperTest() { |
| 134 |
test_deleter::call_count = 0; |
| 135 |
test_deleter::last_value = 0; |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
TEST_F(RaiiWrapperTest, construct_and_destroy_calls_deleter) { |
| 140 |
{ |
| 141 |
auto w = raii_wrap(42, test_deleter{}); |
| 142 |
EXPECT_EQ(test_deleter::call_count, 0); |
| 143 |
} |
| 144 |
EXPECT_EQ(test_deleter::call_count, 1); |
| 145 |
EXPECT_EQ(test_deleter::last_value, 42); |
| 146 |
} |
| 147 |
|
| 148 |
TEST_F(RaiiWrapperTest, move_constructor_transfers_ownership) { |
| 149 |
{ |
| 150 |
auto w1 = raii_wrap(123, test_deleter{}); |
| 151 |
auto w2 = std::move(w1); |
| 152 |
EXPECT_EQ(test_deleter::call_count, 0); |
| 153 |
EXPECT_EQ(static_cast<int>(w2), 123); |
| 154 |
} |
| 155 |
EXPECT_EQ(test_deleter::call_count, 1); |
| 156 |
EXPECT_EQ(test_deleter::last_value, 123); |
| 157 |
} |
| 158 |
|
| 159 |
TEST_F(RaiiWrapperTest, operator_t_and_get) { |
| 160 |
{ |
| 161 |
auto w = raii_wrap(999, test_deleter{}); |
| 162 |
int i = w; |
| 163 |
EXPECT_EQ(i, 999); |
| 164 |
w.get() = 1000; |
| 165 |
EXPECT_EQ(static_cast<int>(w), 1000); |
| 166 |
} |
| 167 |
EXPECT_EQ(test_deleter::call_count, 1); |
| 168 |
EXPECT_EQ(test_deleter::last_value, 1000); |
| 169 |
} |
| 170 |
|
| 171 |
class counting_helper { |
| 172 |
public: |
| 173 |
static int active; |
| 174 |
int value; |
| 175 |
counting_helper(int value) : value(value) { |
| 176 |
++active; |
| 177 |
} |
| 178 |
~counting_helper() { |
| 179 |
--active; |
| 180 |
} |
| 181 |
counting_helper(const counting_helper&) = delete; |
| 182 |
counting_helper(counting_helper&&) = delete; |
| 183 |
counting_helper& operator=(const counting_helper&) = delete; |
| 184 |
counting_helper& operator=(counting_helper&&) = delete; |
| 185 |
int foo() const { |
| 186 |
return value; |
| 187 |
} |
| 188 |
}; |
| 189 |
int counting_helper::active = 0; |
| 190 |
|
| 191 |
TEST(MaybeOwnedTest, NonOwningPointer) { |
| 192 |
ASSERT_EQ(counting_helper::active, 0); |
| 193 |
auto instance = cpptrace::detail::make_unique<counting_helper>(42); |
| 194 |
EXPECT_EQ(counting_helper::active, 1); |
| 195 |
{ |
| 196 |
maybe_owned<counting_helper> non_owning(instance.get()); |
| 197 |
EXPECT_EQ(counting_helper::active, 1); |
| 198 |
EXPECT_EQ(non_owning->foo(), 42); |
| 199 |
} |
| 200 |
EXPECT_EQ(counting_helper::active, 1); |
| 201 |
instance.reset(); |
| 202 |
EXPECT_EQ(counting_helper::active, 0); |
| 203 |
} |
| 204 |
|
| 205 |
TEST(MaybeOwnedTest, OwningPointer) { |
| 206 |
ASSERT_EQ(counting_helper::active, 0); |
| 207 |
auto instance = cpptrace::detail::make_unique<counting_helper>(42); |
| 208 |
EXPECT_EQ(counting_helper::active, 1); |
| 209 |
{ |
| 210 |
maybe_owned<counting_helper> non_owning(std::move(instance)); |
| 211 |
EXPECT_EQ(counting_helper::active, 1); |
| 212 |
EXPECT_EQ(non_owning->foo(), 42); |
| 213 |
} |
| 214 |
EXPECT_EQ(counting_helper::active, 0); |
| 215 |
instance.reset(); |
| 216 |
EXPECT_EQ(counting_helper::active, 0); |
| 217 |
} |
| 218 |
|
| 219 |
} |