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

Side by Side Diff: mojo/system/memory_unittest.cc

Issue 425523002: Mojo: Convert MemoryTest.Valid to the new user pointer handling (see r285350). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 6 years, 4 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/system/memory.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/system/memory.h" 5 #include "mojo/system/memory.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
11 11
12 #include "mojo/public/c/system/macros.h" 12 #include "mojo/public/c/system/macros.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace mojo { 15 namespace mojo {
16 namespace system { 16 namespace system {
17 namespace { 17 namespace {
18 18
19 TEST(MemoryTest, Valid) { 19 TEST(MemoryTest, Valid) {
20 char my_char; 20 char my_char;
21 int32_t my_int32; 21 int32_t my_int32;
22 int64_t my_int64; 22 int64_t my_int64_array[5] = {}; // Zero initialize.
23 char my_char_array[5];
24 int32_t my_int32_array[5];
25 int64_t my_int64_array[5];
26 23
27 // |VerifyUserPointer|: 24 UserPointer<char> my_char_ptr(&my_char);
25 UserPointer<int32_t> my_int32_ptr(&my_int32);
26 UserPointer<int64_t> my_int64_array_ptr(my_int64_array);
28 27
29 EXPECT_TRUE(VerifyUserPointer<char>(&my_char)); 28 // |UserPointer<>::IsNull()|:
30 EXPECT_TRUE(VerifyUserPointer<int32_t>(&my_int32)); 29 EXPECT_FALSE(my_char_ptr.IsNull());
31 EXPECT_TRUE(VerifyUserPointer<int64_t>(&my_int64)); 30 EXPECT_FALSE(my_int32_ptr.IsNull());
31 EXPECT_FALSE(my_int64_array_ptr.IsNull());
32 32
33 // |VerifyUserPointerWithCount|: 33 // |UserPointer<>::Put()| and |UserPointer<>::Get()|:
34 my_char_ptr.Put('x');
35 EXPECT_EQ('x', my_char);
36 EXPECT_EQ('x', my_char_ptr.Get());
37 my_int32_ptr.Put(123);
38 EXPECT_EQ(123, my_int32);
39 EXPECT_EQ(123, my_int32_ptr.Get());
40 my_int64_array_ptr.Put(456);
41 EXPECT_EQ(456, my_int64_array[0]);
42 EXPECT_EQ(456, my_int64_array_ptr.Get());
34 43
35 EXPECT_TRUE(VerifyUserPointerWithCount<char>(my_char_array, 5)); 44 // |UserPointer<>::At()|, etc.:
36 EXPECT_TRUE(VerifyUserPointerWithCount<int32_t>(my_int32_array, 5)); 45 my_int64_array_ptr.At(3).Put(789);
37 EXPECT_TRUE(VerifyUserPointerWithCount<int64_t>(my_int64_array, 5)); 46 EXPECT_EQ(789, my_int64_array[3]);
47 {
48 // Copy construction:
49 UserPointer<int64_t> other(my_int64_array_ptr.At(3));
50 EXPECT_FALSE(other.IsNull());
51 EXPECT_EQ(789, other.Get());
38 52
39 // It shouldn't care what the pointer is if the count is zero. 53 // Assignment:
40 EXPECT_TRUE(VerifyUserPointerWithCount<char>(NULL, 0)); 54 other = my_int64_array_ptr;
41 EXPECT_TRUE(VerifyUserPointerWithCount<int32_t>(NULL, 0)); 55 EXPECT_FALSE(other.IsNull());
42 EXPECT_TRUE(VerifyUserPointerWithCount<int32_t>( 56 EXPECT_EQ(456, other.Get());
43 reinterpret_cast<const int32_t*>(1), 0));
44 EXPECT_TRUE(VerifyUserPointerWithCount<int64_t>(NULL, 0));
45 EXPECT_TRUE(VerifyUserPointerWithCount<int64_t>(
46 reinterpret_cast<const int64_t*>(1), 0));
47 57
48 // |VerifyUserPointerWithSize|: 58 // Assignment to |NullUserPointer()|:
59 other = NullUserPointer();
60 EXPECT_TRUE(other.IsNull());
49 61
50 EXPECT_TRUE(VerifyUserPointerWithSize<1>(&my_char, sizeof(my_char))); 62 // |MakeUserPointer()|:
51 EXPECT_TRUE(VerifyUserPointerWithSize<1>(&my_int32, sizeof(my_int32))); 63 other = MakeUserPointer(&my_int64_array[1]);
52 EXPECT_TRUE(VerifyUserPointerWithSize<MOJO_ALIGNOF(int32_t)>( 64 other.Put(-123);
53 &my_int32, sizeof(my_int32))); 65 EXPECT_EQ(-123, my_int64_array_ptr.At(1).Get());
54 EXPECT_TRUE(VerifyUserPointerWithSize<1>(&my_int64, sizeof(my_int64))); 66 }
55 EXPECT_TRUE(VerifyUserPointerWithSize<MOJO_ALIGNOF(int64_t)>(
56 &my_int64, sizeof(my_int64)));
57 67
58 EXPECT_TRUE(VerifyUserPointerWithSize<1>(my_char_array, 68 // "const" |UserPointer<>|:
59 sizeof(my_char_array))); 69 {
60 EXPECT_TRUE(VerifyUserPointerWithSize<1>(my_int32_array, 70 // Explicit constructor from |NullUserPointer()|:
61 sizeof(my_int32_array))); 71 UserPointer<const char> other((NullUserPointer()));
62 EXPECT_TRUE(VerifyUserPointerWithSize<MOJO_ALIGNOF(int32_t)>( 72 EXPECT_TRUE(other.IsNull());
63 my_int32_array, sizeof(my_int32_array))); 73
64 EXPECT_TRUE(VerifyUserPointerWithSize<1>(my_int64_array, 74 // Conversion to "const":
65 sizeof(my_int64_array))); 75 other = my_char_ptr;
66 EXPECT_TRUE(VerifyUserPointerWithSize<MOJO_ALIGNOF(int64_t)>( 76 EXPECT_EQ('x', other.Get());
67 my_int64_array, sizeof(my_int64_array))); 77 }
78
79 // Default constructor:
80 {
81 UserPointer<int32_t> other;
82 EXPECT_TRUE(other.IsNull());
83
84 other = my_int32_ptr;
85 other.Put(-456);
86 EXPECT_EQ(-456, my_int32_ptr.Get());
87 }
88
89 // |UserPointer<>::CheckArray()|:
90 my_int64_array_ptr.CheckArray(5);
91
92 // |UserPointer<>::GetArray()|:
93 {
94 // From a "const" |UserPointer<>| (why not?):
95 UserPointer<const int64_t> other(my_int64_array_ptr);
96 int64_t array[3] = {1, 2, 3};
97 other.At(1).GetArray(array, 3);
98 EXPECT_EQ(-123, array[0]);
99 EXPECT_EQ(0, array[1]);
100 EXPECT_EQ(789, array[2]);
101 }
102
103 // |UserPointer<>::PutArray()|:
104 {
105 const int64_t array[2] = {654, 321};
106 my_int64_array_ptr.At(3).PutArray(array, 2);
107 EXPECT_EQ(0, my_int64_array[2]);
108 EXPECT_EQ(654, my_int64_array[3]);
109 EXPECT_EQ(321, my_int64_array[4]);
110 }
111
112 // |UserPointer<>::Reader|:
113 {
114 UserPointer<int64_t>::Reader reader(my_int64_array_ptr, 5);
115 EXPECT_EQ(456, reader.GetPointer()[0]);
116 EXPECT_EQ(321, reader.GetPointer()[4]);
117 }
118
119 // Non-const to const:
120 {
121 UserPointer<const int64_t>::Reader reader(my_int64_array_ptr.At(3), 1);
122 const int64_t* ptr = reader.GetPointer();
123 EXPECT_EQ(654, *ptr);
124 }
125
126 // |UserPointer<>::Writer|:
127 {
128 UserPointer<int64_t>::Writer writer(my_int64_array_ptr.At(2), 1);
129 int64_t* ptr = writer.GetPointer();
130 *ptr = 1234567890123LL;
131 writer.Commit();
132 EXPECT_EQ(1234567890123LL, my_int64_array[2]);
133 }
134
135 // |UserPointer<>::ReaderWriter|:
136 {
137 UserPointer<int32_t>::ReaderWriter reader_writer(my_int32_ptr, 1);
138 int32_t* ptr = reader_writer.GetPointer();
139 EXPECT_EQ(-456, *ptr);
140 *ptr = 42;
141 reader_writer.Commit();
142 EXPECT_EQ(42, my_int32);
143 }
144
145 // |UserPointer<>::ReinterpretCast<>|:
146 // (This assumes little-endian, etc.)
147 {
148 UserPointer<const char> other(my_int32_ptr.ReinterpretCast<char>());
149 EXPECT_EQ(42, other.Get());
150 EXPECT_EQ(0, other.At(1).Get());
151 EXPECT_EQ(0, other.At(2).Get());
152 EXPECT_EQ(0, other.At(3).Get());
153 }
154
155 // |UserPointer<>::GetPointerValue()|:
156 {
157 UserPointer<int32_t> other;
158 EXPECT_EQ(0u, other.GetPointerValue());
159 other = my_int32_ptr;
160 EXPECT_EQ(reinterpret_cast<uintptr_t>(&my_int32), other.GetPointerValue());
161 }
68 } 162 }
69 163
164 // TODO(vtl): Convert this test.
165 #if 0
70 TEST(MemoryTest, Invalid) { 166 TEST(MemoryTest, Invalid) {
71 // Note: |VerifyUserPointer...()| are defined to be "best effort" checks (and 167 // Note: |VerifyUserPointer...()| are defined to be "best effort" checks (and
72 // may always return true). Thus these tests of invalid cases only reflect the 168 // may always return true). Thus these tests of invalid cases only reflect the
73 // current implementation. 169 // current implementation.
74 170
75 // These tests depend on |int32_t| and |int64_t| having nontrivial alignment. 171 // These tests depend on |int32_t| and |int64_t| having nontrivial alignment.
76 MOJO_COMPILE_ASSERT(MOJO_ALIGNOF(int32_t) != 1, 172 MOJO_COMPILE_ASSERT(MOJO_ALIGNOF(int32_t) != 1,
77 int32_t_does_not_have_to_be_aligned); 173 int32_t_does_not_have_to_be_aligned);
78 MOJO_COMPILE_ASSERT(MOJO_ALIGNOF(int64_t) != 1, 174 MOJO_COMPILE_ASSERT(MOJO_ALIGNOF(int64_t) != 1,
79 int64_t_does_not_have_to_be_aligned); 175 int64_t_does_not_have_to_be_aligned);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 1)); 219 1));
124 EXPECT_FALSE(VerifyUserPointerWithSize<4>(reinterpret_cast<const int32_t*>(1), 220 EXPECT_FALSE(VerifyUserPointerWithSize<4>(reinterpret_cast<const int32_t*>(1),
125 4)); 221 4));
126 EXPECT_FALSE(VerifyUserPointerWithSize<8>(reinterpret_cast<const int32_t*>(1), 222 EXPECT_FALSE(VerifyUserPointerWithSize<8>(reinterpret_cast<const int32_t*>(1),
127 1)); 223 1));
128 EXPECT_FALSE(VerifyUserPointerWithSize<8>(reinterpret_cast<const int32_t*>(1), 224 EXPECT_FALSE(VerifyUserPointerWithSize<8>(reinterpret_cast<const int32_t*>(1),
129 4)); 225 4));
130 EXPECT_FALSE(VerifyUserPointerWithSize<8>(reinterpret_cast<const int32_t*>(1), 226 EXPECT_FALSE(VerifyUserPointerWithSize<8>(reinterpret_cast<const int32_t*>(1),
131 8)); 227 8));
132 } 228 }
229 #endif
133 230
134 } // namespace 231 } // namespace
135 } // namespace system 232 } // namespace system
136 } // namespace mojo 233 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/system/memory.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698