File: general.cpp

package info (click to toggle)
cpptrace 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,996 kB
  • sloc: cpp: 15,646; python: 962; ansic: 155; sh: 103; makefile: 86
file content (219 lines) | stat: -rw-r--r-- 6,491 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <gtest/gtest.h>
#include <gtest/gtest-matchers.h>
#include <gmock/gmock.h>
#include <gmock/gmock-matchers.h>

#include "utils/utils.hpp"

using cpptrace::detail::byteswap;
using cpptrace::detail::n_digits;
using cpptrace::detail::to;
using cpptrace::detail::raii_wrapper;
using cpptrace::detail::raii_wrap;
using cpptrace::detail::maybe_owned;

namespace {

TEST(ByteSwapTest, ByteSwapUint8) {
    uint8_t input = 0x12;
    uint8_t expected = 0x12;
    uint8_t result = byteswap(input);
    EXPECT_EQ(result, expected);
}

TEST(ByteSwapTest, ByteSwapInt8) {
    int8_t input = 0x7F;
    int8_t expected = 0x7F;
    int8_t result = byteswap(input);
    EXPECT_EQ(result, expected);

    int8_t neg_input = to<int8_t>(0x80);
    int8_t neg_expected = to<int8_t>(0x80);
    int8_t neg_result = byteswap(neg_input);
    EXPECT_EQ(neg_result, neg_expected);
}

TEST(ByteSwapTest, ByteSwapUint16)
{
    uint16_t input = 0x1234;
    uint16_t expected = 0x3412;
    uint16_t result = byteswap(input);
    EXPECT_EQ(result, expected);
    EXPECT_EQ(byteswap(to<uint16_t>(0x0000)), to<uint16_t>(0x0000));
    EXPECT_EQ(byteswap(to<uint16_t>(0xFFFF)), to<uint16_t>(0xFFFF));
}

TEST(ByteSwapTest, ByteSwapInt16) {
    int16_t input = 0x1234;
    int16_t expected = to<int16_t>(0x3412);
    int16_t result = byteswap(input);
    EXPECT_EQ(result, expected);

    int16_t neg_input = to<uint16_t>(0xFEFF);
    int16_t neg_expected = to<int16_t>(0xFFFE);
    int16_t neg_result = byteswap(neg_input);
    EXPECT_EQ(neg_result, neg_expected);
}

TEST(ByteSwapTest, ByteSwapUint32)
{
    uint32_t input = 0x12345678;
    uint32_t expected = 0x78563412;
    uint32_t result = byteswap(input);
    EXPECT_EQ(result, expected);
    EXPECT_EQ(byteswap(to<uint32_t>(0x00000000)), to<uint32_t>(0x00000000));
    EXPECT_EQ(byteswap(to<uint32_t>(0xFFFFFFFF)), to<uint32_t>(0xFFFFFFFF));
}

TEST(ByteSwapTest, ByteSwapInt32)
{
    int32_t input = 0x12345678;
    int32_t expected = to<int32_t>(0x78563412);
    int32_t result = byteswap(input);
    EXPECT_EQ(result, expected);

    int32_t neg_input = to<uint32_t>(0xFF000000);
    int32_t neg_expected = to<int32_t>(0x000000FF);
    int32_t neg_result = byteswap(neg_input);
    EXPECT_EQ(neg_result, neg_expected);
}

TEST(ByteSwapTest, ByteSwapUint64)
{
    uint64_t input = 0x1122334455667788ULL;
    uint64_t expected = 0x8877665544332211ULL;
    uint64_t result = byteswap(input);
    EXPECT_EQ(result, expected);
    EXPECT_EQ(byteswap(to<uint64_t>(0x0000000000000000ULL)), to<uint64_t>(0x0000000000000000ULL));
    EXPECT_EQ(byteswap(to<uint64_t>(0xFFFFFFFFFFFFFFFFULL)), to<uint64_t>(0xFFFFFFFFFFFFFFFFULL));
}

TEST(ByteSwapTest, ByteSwapInt64)
{
    int64_t input = 0x1122334455667788LL;
    int64_t expected = to<int64_t>(0x8877665544332211ULL);
    int64_t result = byteswap(input);
    EXPECT_EQ(result, expected);

    int64_t neg_input = to<uint64_t>(0xFF00000000000000ULL);
    int64_t neg_expected = to<int64_t>(0x00000000000000FFULL);
    int64_t neg_result = byteswap(neg_input);
    EXPECT_EQ(neg_result, neg_expected);
}

TEST(NDigitsTest, Basic)
{
    static_assert(n_digits(1) == 1, "n_digits utility producing the wrong result");
    static_assert(n_digits(9) == 1, "n_digits utility producing the wrong result");
    static_assert(n_digits(10) == 2, "n_digits utility producing the wrong result");
    static_assert(n_digits(11) == 2, "n_digits utility producing the wrong result");
    static_assert(n_digits(1024) == 4, "n_digits utility producing the wrong result");

    EXPECT_EQ(n_digits(1), 1);
    EXPECT_EQ(n_digits(9), 1);
    EXPECT_EQ(n_digits(10), 2);
    EXPECT_EQ(n_digits(11), 2);
    EXPECT_EQ(n_digits(1024), 4);
}

struct test_deleter {
    static int call_count;
    static int last_value;

    void operator()(int value) {
        call_count++;
        last_value = value;
    }
};
int test_deleter::call_count = 0;
int test_deleter::last_value = 0;

class RaiiWrapperTest : public ::testing::Test {
protected:
    RaiiWrapperTest() {
        test_deleter::call_count = 0;
        test_deleter::last_value = 0;
    }
};

TEST_F(RaiiWrapperTest, construct_and_destroy_calls_deleter) {
    {
        auto w = raii_wrap(42, test_deleter{});
        EXPECT_EQ(test_deleter::call_count, 0);
    }
    EXPECT_EQ(test_deleter::call_count, 1);
    EXPECT_EQ(test_deleter::last_value, 42);
}

TEST_F(RaiiWrapperTest, move_constructor_transfers_ownership) {
    {
        auto w1 = raii_wrap(123, test_deleter{});
        auto w2 = std::move(w1);
        EXPECT_EQ(test_deleter::call_count, 0);
        EXPECT_EQ(static_cast<int>(w2), 123);
    }
    EXPECT_EQ(test_deleter::call_count, 1);
    EXPECT_EQ(test_deleter::last_value, 123);
}

TEST_F(RaiiWrapperTest, operator_t_and_get) {
    {
        auto w = raii_wrap(999, test_deleter{});
        int i = w;
        EXPECT_EQ(i, 999);
        w.get() = 1000;
        EXPECT_EQ(static_cast<int>(w), 1000);
    }
    EXPECT_EQ(test_deleter::call_count, 1);
    EXPECT_EQ(test_deleter::last_value, 1000);
}

class counting_helper {
public:
    static int active;
    int value;
    counting_helper(int value) : value(value) {
        ++active;
    }
    ~counting_helper() {
        --active;
    }
    counting_helper(const counting_helper&) = delete;
    counting_helper(counting_helper&&) = delete;
    counting_helper& operator=(const counting_helper&) = delete;
    counting_helper& operator=(counting_helper&&) = delete;
    int foo() const {
        return value;
    }
};
int counting_helper::active = 0;

TEST(MaybeOwnedTest, NonOwningPointer) {
    ASSERT_EQ(counting_helper::active, 0);
    auto instance = cpptrace::detail::make_unique<counting_helper>(42);
    EXPECT_EQ(counting_helper::active, 1);
    {
        maybe_owned<counting_helper> non_owning(instance.get());
        EXPECT_EQ(counting_helper::active, 1);
        EXPECT_EQ(non_owning->foo(), 42);
    }
    EXPECT_EQ(counting_helper::active, 1);
    instance.reset();
    EXPECT_EQ(counting_helper::active, 0);
}

TEST(MaybeOwnedTest, OwningPointer) {
    ASSERT_EQ(counting_helper::active, 0);
    auto instance = cpptrace::detail::make_unique<counting_helper>(42);
    EXPECT_EQ(counting_helper::active, 1);
    {
        maybe_owned<counting_helper> non_owning(std::move(instance));
        EXPECT_EQ(counting_helper::active, 1);
        EXPECT_EQ(non_owning->foo(), 42);
    }
    EXPECT_EQ(counting_helper::active, 0);
    instance.reset();
    EXPECT_EQ(counting_helper::active, 0);
}

}