summaryrefslogtreecommitdiff
path: root/test/memory_util_test.cc
blob: 43dedb5ce3e415920058b3f8013e9d65820dd3f9 (plain)
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
/*
    Copyright (C) 2026 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "lib/memory_util.h"
#include <boost/test/unit_test.hpp>


void check(void* p, int size, uint64_t value)
{
	uint8_t* up = reinterpret_cast<uint8_t*>(p);
	for (int i = 0; i < size; ++i) {
		BOOST_CHECK_EQUAL(*up++, (value >> (i * 8)) & 0xff);
	}
}


/** Aligned, multiple of 64 size */
BOOST_AUTO_TEST_CASE(fill_memory_test1)
{
	int constexpr size = 256;
	auto memory = wrapped_av_malloc(size);
	BOOST_REQUIRE_EQUAL(reinterpret_cast<uintptr_t>(memory) % 8, 0);

	fill_memory(memory, size, 0x1928374654abdfea);
	check(memory, size, 0x1928374654abdfea);
}


/** Aligned, extra bytes at the end */
BOOST_AUTO_TEST_CASE(fill_memory_test2)
{
	int constexpr size = 259;

	auto memory = wrapped_av_malloc(size);
	BOOST_REQUIRE_EQUAL(reinterpret_cast<uintptr_t>(memory) % 8, 0);

	fill_memory(memory, size, 0x1928374654abdfea);
	check(memory, size, 0x1928374654abdfea);
}


/** Non-aligned, extra bytes at start and end */
BOOST_AUTO_TEST_CASE(fill_memory_test3)
{
	int constexpr size = 265;

	auto memory = wrapped_av_malloc(size + 512);
	BOOST_REQUIRE_EQUAL(reinterpret_cast<uintptr_t>(memory) % 8, 0);
	memory = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(memory) + 3);

	fill_memory(memory, size, 0x1928374654abdfea);
	check(memory, size, 0x1928374654abdfea);
}