Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(643)

Unified Diff: mojo/system/memory_unittest.cc

Issue 304303006: Mojo: Specify/check alignment of pointers more carefully. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix msvs Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/system/memory.cc ('k') | mojo/system/message_pipe_dispatcher.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/system/memory_unittest.cc
diff --git a/mojo/system/memory_unittest.cc b/mojo/system/memory_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b23d50ae7be5ceeed08b3a8ac3cdbfa373bd4c34
--- /dev/null
+++ b/mojo/system/memory_unittest.cc
@@ -0,0 +1,136 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/system/memory.h"
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include <limits>
+
+#include "mojo/public/c/system/macros.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace mojo {
+namespace system {
+namespace {
+
+TEST(MemoryTest, Valid) {
+ char my_char;
+ int32_t my_int32;
+ int64_t my_int64;
+ char my_char_array[5];
+ int32_t my_int32_array[5];
+ int64_t my_int64_array[5];
+
+ // |VerifyUserPointer|:
+
+ EXPECT_TRUE(VerifyUserPointer<char>(&my_char));
+ EXPECT_TRUE(VerifyUserPointer<int32_t>(&my_int32));
+ EXPECT_TRUE(VerifyUserPointer<int64_t>(&my_int64));
+
+ // |VerifyUserPointerWithCount|:
+
+ EXPECT_TRUE(VerifyUserPointerWithCount<char>(my_char_array, 5));
+ EXPECT_TRUE(VerifyUserPointerWithCount<int32_t>(my_int32_array, 5));
+ EXPECT_TRUE(VerifyUserPointerWithCount<int64_t>(my_int64_array, 5));
+
+ // It shouldn't care what the pointer is if the count is zero.
+ EXPECT_TRUE(VerifyUserPointerWithCount<char>(NULL, 0));
+ EXPECT_TRUE(VerifyUserPointerWithCount<int32_t>(NULL, 0));
+ EXPECT_TRUE(VerifyUserPointerWithCount<int32_t>(
+ reinterpret_cast<const int32_t*>(1), 0));
+ EXPECT_TRUE(VerifyUserPointerWithCount<int64_t>(NULL, 0));
+ EXPECT_TRUE(VerifyUserPointerWithCount<int64_t>(
+ reinterpret_cast<const int64_t*>(1), 0));
+
+ // |VerifyUserPointerWithSize|:
+
+ EXPECT_TRUE(VerifyUserPointerWithSize<1>(&my_char, sizeof(my_char)));
+ EXPECT_TRUE(VerifyUserPointerWithSize<1>(&my_int32, sizeof(my_int32)));
+ EXPECT_TRUE(VerifyUserPointerWithSize<MOJO_ALIGNOF(int32_t)>(
+ &my_int32, sizeof(my_int32)));
+ EXPECT_TRUE(VerifyUserPointerWithSize<1>(&my_int64, sizeof(my_int64)));
+ EXPECT_TRUE(VerifyUserPointerWithSize<MOJO_ALIGNOF(int64_t)>(
+ &my_int64, sizeof(my_int64)));
+
+ EXPECT_TRUE(VerifyUserPointerWithSize<1>(my_char_array,
+ sizeof(my_char_array)));
+ EXPECT_TRUE(VerifyUserPointerWithSize<1>(my_int32_array,
+ sizeof(my_int32_array)));
+ EXPECT_TRUE(VerifyUserPointerWithSize<MOJO_ALIGNOF(int32_t)>(
+ my_int32_array, sizeof(my_int32_array)));
+ EXPECT_TRUE(VerifyUserPointerWithSize<1>(my_int64_array,
+ sizeof(my_int64_array)));
+ EXPECT_TRUE(VerifyUserPointerWithSize<MOJO_ALIGNOF(int64_t)>(
+ my_int64_array, sizeof(my_int64_array)));
+}
+
+TEST(MemoryTest, Invalid) {
+ // Note: |VerifyUserPointer...()| are defined to be "best effort" checks (and
+ // may always return true). Thus these tests of invalid cases only reflect the
+ // current implementation.
+
+ // These tests depend on |int32_t| and |int64_t| having nontrivial alignment.
+ MOJO_COMPILE_ASSERT(MOJO_ALIGNOF(int32_t) != 1,
+ int32_t_does_not_have_to_be_aligned);
+ MOJO_COMPILE_ASSERT(MOJO_ALIGNOF(int64_t) != 1,
+ int64_t_does_not_have_to_be_aligned);
+
+ int32_t my_int32;
+ int64_t my_int64;
+
+ // |VerifyUserPointer|:
+
+ EXPECT_FALSE(VerifyUserPointer<char>(NULL));
+ EXPECT_FALSE(VerifyUserPointer<int32_t>(NULL));
+ EXPECT_FALSE(VerifyUserPointer<int64_t>(NULL));
+
+ // Unaligned:
+ EXPECT_FALSE(VerifyUserPointer<int32_t>(reinterpret_cast<const int32_t*>(1)));
+ EXPECT_FALSE(VerifyUserPointer<int64_t>(reinterpret_cast<const int64_t*>(1)));
+
+ // |VerifyUserPointerWithCount|:
+
+ EXPECT_FALSE(VerifyUserPointerWithCount<char>(NULL, 1));
+ EXPECT_FALSE(VerifyUserPointerWithCount<int32_t>(NULL, 1));
+ EXPECT_FALSE(VerifyUserPointerWithCount<int64_t>(NULL, 1));
+
+ // Unaligned:
+ EXPECT_FALSE(VerifyUserPointerWithCount<int32_t>(
+ reinterpret_cast<const int32_t*>(1), 1));
+ EXPECT_FALSE(VerifyUserPointerWithCount<int64_t>(
+ reinterpret_cast<const int64_t*>(1), 1));
+
+ // Count too big:
+ EXPECT_FALSE(VerifyUserPointerWithCount<int32_t>(
+ &my_int32, std::numeric_limits<size_t>::max()));
+ EXPECT_FALSE(VerifyUserPointerWithCount<int64_t>(
+ &my_int64, std::numeric_limits<size_t>::max()));
+
+ // |VerifyUserPointerWithSize|:
+
+ EXPECT_FALSE(VerifyUserPointerWithSize<1>(NULL, 1));
+ EXPECT_FALSE(VerifyUserPointerWithSize<4>(NULL, 1));
+ EXPECT_FALSE(VerifyUserPointerWithSize<4>(NULL, 4));
+ EXPECT_FALSE(VerifyUserPointerWithSize<8>(NULL, 1));
+ EXPECT_FALSE(VerifyUserPointerWithSize<8>(NULL, 4));
+ EXPECT_FALSE(VerifyUserPointerWithSize<8>(NULL, 8));
+
+ // Unaligned:
+ EXPECT_FALSE(VerifyUserPointerWithSize<4>(reinterpret_cast<const int32_t*>(1),
+ 1));
+ EXPECT_FALSE(VerifyUserPointerWithSize<4>(reinterpret_cast<const int32_t*>(1),
+ 4));
+ EXPECT_FALSE(VerifyUserPointerWithSize<8>(reinterpret_cast<const int32_t*>(1),
+ 1));
+ EXPECT_FALSE(VerifyUserPointerWithSize<8>(reinterpret_cast<const int32_t*>(1),
+ 4));
+ EXPECT_FALSE(VerifyUserPointerWithSize<8>(reinterpret_cast<const int32_t*>(1),
+ 8));
+}
+
+} // namespace
+} // namespace system
+} // namespace mojo
« no previous file with comments | « mojo/system/memory.cc ('k') | mojo/system/message_pipe_dispatcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698