Add unit tests for aligned alloc
authorSergey Sharybin <sergey.vfx@gmail.com>
Thu, 19 Jun 2014 06:45:00 +0000 (12:45 +0600)
committerSergey Sharybin <sergey.vfx@gmail.com>
Thu, 19 Jun 2014 06:45:00 +0000 (12:45 +0600)
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.

tests/gtests/CMakeLists.txt
tests/gtests/guardedalloc/CMakeLists.txt [new file with mode: 0644]
tests/gtests/guardedalloc/guardedalloc_alignment_test.cc [new file with mode: 0644]

index 2ab62fa05b077c63abd3820a5901bbea5a968c06..317f030a658e581d0fff88623a9ade20d421f029 100644 (file)
@@ -9,5 +9,6 @@ if(WITH_GTESTS)
 
        add_subdirectory(testing)
        add_subdirectory(blenlib)
+       add_subdirectory(guardedalloc)
 endif()
 
diff --git a/tests/gtests/guardedalloc/CMakeLists.txt b/tests/gtests/guardedalloc/CMakeLists.txt
new file mode 100644 (file)
index 0000000..c09f872
--- /dev/null
@@ -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 "")
diff --git a/tests/gtests/guardedalloc/guardedalloc_alignment_test.cc b/tests/gtests/guardedalloc/guardedalloc_alignment_test.cc
new file mode 100644 (file)
index 0000000..34c4773
--- /dev/null
@@ -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