Electroneum
Loading...
Searching...
No Matches
aligned.cpp
Go to the documentation of this file.
1
// Copyright (c) 2018, The Monero Project
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification, are
6
// permitted provided that the following conditions are met:
7
//
8
// 1. Redistributions of source code must retain the above copyright notice, this list of
9
// conditions and the following disclaimer.
10
//
11
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12
// of conditions and the following disclaimer in the documentation and/or other
13
// materials provided with the distribution.
14
//
15
// 3. Neither the name of the copyright holder nor the names of its contributors may be
16
// used to endorse or promote products derived from this software without specific
17
// prior written permission.
18
//
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
#include "gtest/gtest.h"
30
31
#include "
common/aligned.h
"
32
33
TEST
(aligned, large_null) {
ASSERT_TRUE
(
aligned_malloc
((
size_t
)-1, 1) == NULL); }
34
TEST
(aligned, free_null) {
aligned_free
(NULL); }
35
TEST
(aligned, zero) {
void
*ptr =
aligned_malloc
(0, 1);
ASSERT_TRUE
(ptr);
aligned_free
(ptr); }
36
TEST
(aligned, aligned1) {
void
*ptr =
aligned_malloc
(1, 1);
ASSERT_TRUE
(ptr);
aligned_free
(ptr); }
37
TEST
(aligned, aligned4096) {
void
*ptr =
aligned_malloc
(1, 4096);
ASSERT_TRUE
(ptr && ((
uintptr_t
)ptr & 4095) == 0);
aligned_free
(ptr); }
38
TEST
(aligned, aligned8) {
void
*ptr =
aligned_malloc
(1, 8);
ASSERT_TRUE
(ptr && ((
uintptr_t
)ptr & 7) == 0);
aligned_free
(ptr); }
39
TEST
(aligned, realloc_null) {
void
*ptr =
aligned_realloc
(NULL, 1, 4096);
ASSERT_TRUE
(ptr && ((
uintptr_t
)ptr & 4095) == 0);
aligned_free
(ptr); }
40
TEST
(aligned, realloc_diff_align) {
void
*ptr =
aligned_malloc
(1, 4096);
ASSERT_TRUE
(!
aligned_realloc
(ptr, 1, 2048));
aligned_free
(ptr); }
41
TEST
(aligned, realloc_same) {
void
*ptr =
aligned_malloc
(1, 4096), *ptr2 =
aligned_realloc
(ptr, 1, 4096);
ASSERT_TRUE
(ptr == ptr2);
aligned_free
(ptr2); }
42
TEST
(aligned, realloc_larger) {
void
*ptr =
aligned_malloc
(1, 4096), *ptr2 =
aligned_realloc
(ptr, 2, 4096);
ASSERT_TRUE
(ptr != ptr2);
aligned_free
(ptr2); }
43
TEST
(aligned, realloc_zero) {
void
*ptr =
aligned_malloc
(1, 4096), *ptr2 =
aligned_realloc
(ptr, 0, 4096);
ASSERT_TRUE
(ptr && !ptr2); }
44
45
TEST
(aligned, contents_larger)
46
{
47
unsigned
char
*ptr = (
unsigned
char
*)
aligned_malloc
(50, 256);
48
ASSERT_TRUE
(ptr);
49
for
(
int
n = 0; n < 50; ++n)
50
ptr[n] = n;
51
unsigned
char
*ptr2 = (
unsigned
char
*)
aligned_realloc
(ptr, 51, 256);
52
for
(
int
n = 0; n < 50; ++n)
53
{
54
ASSERT_TRUE
(ptr2[n] == n);
55
}
56
aligned_free
(ptr2);
57
}
58
59
TEST
(aligned, contents_same)
60
{
61
unsigned
char
*ptr = (
unsigned
char
*)
aligned_malloc
(50, 256);
62
ASSERT_TRUE
(ptr);
63
for
(
int
n = 0; n < 50; ++n)
64
ptr[n] = n;
65
unsigned
char
*ptr2 = (
unsigned
char
*)
aligned_realloc
(ptr, 50, 256);
66
for
(
int
n = 0; n < 50; ++n)
67
{
68
ASSERT_TRUE
(ptr2[n] == n);
69
}
70
aligned_free
(ptr2);
71
}
72
73
TEST
(aligned, contents_smaller)
74
{
75
unsigned
char
*ptr = (
unsigned
char
*)
aligned_malloc
(50, 256);
76
ASSERT_TRUE
(ptr);
77
for
(
int
n = 0; n < 50; ++n)
78
ptr[n] = n;
79
unsigned
char
*ptr2 = (
unsigned
char
*)
aligned_realloc
(ptr, 49, 256);
80
for
(
int
n = 0; n < 49; ++n)
81
{
82
ASSERT_TRUE
(ptr2[n] == n);
83
}
84
aligned_free
(ptr2);
85
}
86
87
TEST
(aligned, alignment)
88
{
89
static
const
size_t
good_alignments[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192};
90
for
(
size_t
a
= 0;
a
<= 8192; ++
a
)
91
{
92
bool
good =
false
;
93
for
(
const
auto
t: good_alignments)
if
(
a
== t) good =
true
;
94
void
*ptr =
aligned_malloc
(1,
a
);
95
if
(good)
96
{
97
ASSERT_TRUE
(ptr != NULL);
98
aligned_free
(ptr);
99
}
100
else
101
{
102
ASSERT_TRUE
(ptr == NULL);
103
}
104
}
105
106
ASSERT_TRUE
(
aligned_malloc
(1, ~0) == NULL);
107
}
aligned.h
aligned_malloc
void * aligned_malloc(size_t bytes, size_t align)
aligned_free
void aligned_free(void *ptr)
aligned_realloc
void * aligned_realloc(void *ptr, size_t bytes, size_t align)
TEST
#define TEST(test_case_name, test_name)
Definition
gtest.h:2187
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition
gtest.h:1865
a
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition
pointer.h:1124
uintptr_t
_W64 unsigned int uintptr_t
Definition
stdint.h:165
tests
unit_tests
aligned.cpp
Generated on
for Electroneum by
1.16.1