File: debug_helpers.cpp

package info (click to toggle)
foonathan-memory 0.7.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,644 kB
  • sloc: cpp: 12,425; xml: 139; sh: 48; makefile: 25
file content (79 lines) | stat: -rw-r--r-- 2,297 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
// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib

#include <foonathan/memory/detail/debug_helpers.hpp>

#include <doctest/doctest.h>

#include <foonathan/memory/debugging.hpp>

using namespace foonathan::memory;
using namespace detail;

TEST_CASE("detail::debug_fill")
{
    debug_magic array[10];
    for (auto& el : array)
        el = debug_magic::freed_memory;

    debug_fill(array, sizeof(array), debug_magic::new_memory);
#if FOONATHAN_MEMORY_DEBUG_FILL
    for (auto el : array)
        REQUIRE(el == debug_magic::new_memory);
#else
    for (auto el : array)
        REQUIRE(el == debug_magic::freed_memory);
#endif
}

TEST_CASE("detail::debug_is_filled")
{
    debug_magic array[10];
    for (auto& el : array)
        el = debug_magic::freed_memory;

    REQUIRE(debug_is_filled(array, sizeof(array), debug_magic::freed_memory) == nullptr);

    array[5] = debug_magic::new_memory;
    auto ptr =
        static_cast<debug_magic*>(debug_is_filled(array, sizeof(array), debug_magic::freed_memory));
#if FOONATHAN_MEMORY_DEBUG_FILL
    REQUIRE(ptr == array + 5);
#else
    REQUIRE(ptr == nullptr);
#endif
}

TEST_CASE("detail::debug_fill_new/free")
{
    debug_magic array[10];

    auto result          = debug_fill_new(array, 8 * sizeof(debug_magic), sizeof(debug_magic));
    auto offset          = static_cast<debug_magic*>(result) - array;
    auto expected_offset = debug_fence_size ? sizeof(debug_magic) : 0u;
    REQUIRE(offset == expected_offset);

#if FOONATHAN_MEMORY_DEBUG_FILL
#if FOONATHAN_MEMORY_DEBUG_FENCE
    REQUIRE(array[0] == debug_magic::fence_memory);
    REQUIRE(array[9] == debug_magic::fence_memory);
    const auto start = 1;
#else
    const auto start = 0;
#endif
    for (auto i = start; i < start + 8; ++i)
        REQUIRE(array[i] == debug_magic::new_memory);
#endif

    result = debug_fill_free(result, 8 * sizeof(debug_magic), sizeof(debug_magic));
    REQUIRE(static_cast<debug_magic*>(result) == array);

#if FOONATHAN_MEMORY_DEBUG_FILL
#if FOONATHAN_MEMORY_DEBUG_FENCE
    REQUIRE(array[0] == debug_magic::fence_memory);
    REQUIRE(array[9] == debug_magic::fence_memory);
#endif
    for (auto i = start; i < start + 8; ++i)
        REQUIRE(array[i] == debug_magic::freed_memory);
#endif
}