Add unit tests for aligned alloc

This was really handy on initial work of aligned alloc
and would be handy as well when we'll need to support
arbitrary alignment on Apple platforms.
This commit is contained in:
Sergey Sharybin 2014-06-19 12:45:00 +06:00
parent 89ee6e0808
commit 16d64a99b4
3 changed files with 84 additions and 0 deletions

View File

@ -9,5 +9,6 @@ if(WITH_GTESTS)
add_subdirectory(testing)
add_subdirectory(blenlib)
add_subdirectory(guardedalloc)
endif()

View File

@ -0,0 +1,36 @@
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# The Original Code is Copyright (C) 2014, Blender Foundation
# All rights reserved.
#
# Contributor(s): Sergey Sharybin
#
# ***** END GPL LICENSE BLOCK *****
set(INC
.
../
../../../intern/guardedalloc
)
include_directories(${INC})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}")
BLENDER_TEST(guardedalloc_alignment "")

View File

@ -0,0 +1,47 @@
#include "testing/testing.h"
#include "MEM_guardedalloc.h"
#define CHECK_ALIGNMENT(ptr, align) EXPECT_EQ(0, (size_t)ptr % align)
namespace {
void DoBasicAlignmentChecks(const int alignment) {
int *foo, *bar;
foo = (int *) MEM_mallocN_aligned(sizeof(int) * 10, alignment, "test");
CHECK_ALIGNMENT(foo, alignment);
bar = (int *) MEM_dupallocN(foo);
CHECK_ALIGNMENT(bar, alignment);
MEM_freeN(bar);
foo = (int *) MEM_reallocN(foo, sizeof(int) * 5);
CHECK_ALIGNMENT(foo, alignment);
foo = (int *) MEM_recallocN(foo, sizeof(int) * 5);
CHECK_ALIGNMENT(foo, alignment);
MEM_freeN(foo);
}
} // namespace
TEST(guardedalloc, LockfreeAlignedAlloc16) {
DoBasicAlignmentChecks(16);
}
TEST(guardedalloc, GuardedAlignedAlloc16) {
MEM_use_guarded_allocator();
DoBasicAlignmentChecks(16);
}
// On Apple we currently support 16 bit alignment only.
// Harmless for Blender, but would be nice to support
// eventually.
#ifndef __APPLE__
TEST(guardedalloc, GuardedAlignedAlloc32) {
MEM_use_guarded_allocator();
DoBasicAlignmentChecks(16);
}
#endif