OLD | NEW |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 TEST(AlignedAllocator, AlignedVector) { | 34 TEST(AlignedAllocator, AlignedVector) { |
35 // Test a structure with natural alignment. | 35 // Test a structure with natural alignment. |
36 struct NaturalAlignedStruct { | 36 struct NaturalAlignedStruct { |
37 int i; | 37 int i; |
38 }; | 38 }; |
39 | 39 |
40 AlignedVector<NaturalAlignedStruct> natural_aligned_vector; | 40 AlignedVector<NaturalAlignedStruct> natural_aligned_vector; |
41 natural_aligned_vector.push_back(NaturalAlignedStruct()); | 41 natural_aligned_vector.push_back(NaturalAlignedStruct()); |
42 EXPECT_TRUE( | 42 EXPECT_TRUE( |
43 IsAligned(&natural_aligned_vector[0], ALIGNOF(NaturalAlignedStruct))); | 43 IsAligned(&natural_aligned_vector[0], alignof(NaturalAlignedStruct))); |
44 | 44 |
45 natural_aligned_vector.resize(3); | 45 natural_aligned_vector.resize(3); |
46 EXPECT_TRUE( | 46 EXPECT_TRUE( |
47 IsAligned(&natural_aligned_vector[0], ALIGNOF(NaturalAlignedStruct))); | 47 IsAligned(&natural_aligned_vector[0], alignof(NaturalAlignedStruct))); |
48 EXPECT_TRUE( | 48 EXPECT_TRUE( |
49 IsAligned(&natural_aligned_vector[1], ALIGNOF(NaturalAlignedStruct))); | 49 IsAligned(&natural_aligned_vector[1], alignof(NaturalAlignedStruct))); |
50 EXPECT_TRUE( | 50 EXPECT_TRUE( |
51 IsAligned(&natural_aligned_vector[2], ALIGNOF(NaturalAlignedStruct))); | 51 IsAligned(&natural_aligned_vector[2], alignof(NaturalAlignedStruct))); |
52 | 52 |
53 // Test a structure that declares its own alignment. | 53 // Test a structure that declares its own alignment. |
54 struct ALIGNAS(32) AlignedStruct { | 54 struct alignas(32) AlignedStruct { |
55 int i; | 55 int i; |
56 }; | 56 }; |
57 ASSERT_EQ(ALIGNOF(AlignedStruct), 32u); | 57 ASSERT_EQ(alignof(AlignedStruct), 32u); |
58 | 58 |
59 AlignedVector<AlignedStruct> aligned_vector; | 59 AlignedVector<AlignedStruct> aligned_vector; |
60 aligned_vector.push_back(AlignedStruct()); | 60 aligned_vector.push_back(AlignedStruct()); |
61 EXPECT_TRUE(IsAligned(&aligned_vector[0], ALIGNOF(AlignedStruct))); | 61 EXPECT_TRUE(IsAligned(&aligned_vector[0], alignof(AlignedStruct))); |
62 | 62 |
63 aligned_vector.resize(3); | 63 aligned_vector.resize(3); |
64 EXPECT_TRUE(IsAligned(&aligned_vector[0], ALIGNOF(AlignedStruct))); | 64 EXPECT_TRUE(IsAligned(&aligned_vector[0], alignof(AlignedStruct))); |
65 EXPECT_TRUE(IsAligned(&aligned_vector[1], ALIGNOF(AlignedStruct))); | 65 EXPECT_TRUE(IsAligned(&aligned_vector[1], alignof(AlignedStruct))); |
66 EXPECT_TRUE(IsAligned(&aligned_vector[2], ALIGNOF(AlignedStruct))); | 66 EXPECT_TRUE(IsAligned(&aligned_vector[2], alignof(AlignedStruct))); |
67 | 67 |
68 // Try a custom alignment. Since the structure itself doesn’t specify an | 68 // Try a custom alignment. Since the structure itself doesn’t specify an |
69 // alignment constraint, only the base address will be aligned to the | 69 // alignment constraint, only the base address will be aligned to the |
70 // requested boundary. | 70 // requested boundary. |
71 AlignedVector<NaturalAlignedStruct, 64> custom_aligned_vector; | 71 AlignedVector<NaturalAlignedStruct, 64> custom_aligned_vector; |
72 custom_aligned_vector.push_back(NaturalAlignedStruct()); | 72 custom_aligned_vector.push_back(NaturalAlignedStruct()); |
73 EXPECT_TRUE(IsAligned(&custom_aligned_vector[0], 64)); | 73 EXPECT_TRUE(IsAligned(&custom_aligned_vector[0], 64)); |
74 | 74 |
75 // Try a structure with a pretty big alignment request. | 75 // Try a structure with a pretty big alignment request. |
76 struct ALIGNAS(1024) BigAlignedStruct { | 76 struct alignas(1024) BigAlignedStruct { |
77 int i; | 77 int i; |
78 }; | 78 }; |
79 ASSERT_EQ(ALIGNOF(BigAlignedStruct), 1024u); | 79 ASSERT_EQ(alignof(BigAlignedStruct), 1024u); |
80 | 80 |
81 AlignedVector<BigAlignedStruct> big_aligned_vector; | 81 AlignedVector<BigAlignedStruct> big_aligned_vector; |
82 big_aligned_vector.push_back(BigAlignedStruct()); | 82 big_aligned_vector.push_back(BigAlignedStruct()); |
83 EXPECT_TRUE(IsAligned(&big_aligned_vector[0], ALIGNOF(BigAlignedStruct))); | 83 EXPECT_TRUE(IsAligned(&big_aligned_vector[0], alignof(BigAlignedStruct))); |
84 | 84 |
85 big_aligned_vector.resize(3); | 85 big_aligned_vector.resize(3); |
86 EXPECT_TRUE(IsAligned(&big_aligned_vector[0], ALIGNOF(BigAlignedStruct))); | 86 EXPECT_TRUE(IsAligned(&big_aligned_vector[0], alignof(BigAlignedStruct))); |
87 EXPECT_TRUE(IsAligned(&big_aligned_vector[1], ALIGNOF(BigAlignedStruct))); | 87 EXPECT_TRUE(IsAligned(&big_aligned_vector[1], alignof(BigAlignedStruct))); |
88 EXPECT_TRUE(IsAligned(&big_aligned_vector[2], ALIGNOF(BigAlignedStruct))); | 88 EXPECT_TRUE(IsAligned(&big_aligned_vector[2], alignof(BigAlignedStruct))); |
89 } | 89 } |
90 | 90 |
91 void BadAlignmentTest() { | 91 void BadAlignmentTest() { |
92 #if defined(OS_WIN) | 92 #if defined(OS_WIN) |
93 // Suppress the assertion MessageBox() normally displayed by the CRT in debug | 93 // Suppress the assertion MessageBox() normally displayed by the CRT in debug |
94 // mode. | 94 // mode. |
95 int previous = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); | 95 int previous = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); |
96 | 96 |
97 // In release mode, _CrtSetReportMode() is #defined to ((int)0), so |previous| | 97 // In release mode, _CrtSetReportMode() is #defined to ((int)0), so |previous| |
98 // would appear unused. | 98 // would appear unused. |
99 ALLOW_UNUSED_LOCAL(previous); | 99 ALLOW_UNUSED_LOCAL(previous); |
100 #endif | 100 #endif |
101 | 101 |
102 // Alignment constraints must be powers of 2. 7 is not valid. | 102 // Alignment constraints must be powers of 2. 7 is not valid. |
103 AlignedVector<int, 7> bad_aligned_vector; | 103 AlignedVector<int, 7> bad_aligned_vector; |
104 bad_aligned_vector.push_back(0); | 104 bad_aligned_vector.push_back(0); |
105 | 105 |
106 #if defined(OS_WIN) | 106 #if defined(OS_WIN) |
107 _CrtSetReportMode(_CRT_ASSERT, previous); | 107 _CrtSetReportMode(_CRT_ASSERT, previous); |
108 #endif | 108 #endif |
109 } | 109 } |
110 | 110 |
111 TEST(AlignedAllocatorDeathTest, BadAlignment) { | 111 TEST(AlignedAllocatorDeathTest, BadAlignment) { |
112 ASSERT_DEATH(BadAlignmentTest(), ""); | 112 ASSERT_DEATH(BadAlignmentTest(), ""); |
113 } | 113 } |
114 | 114 |
115 } // namespace | 115 } // namespace |
116 } // namespace test | 116 } // namespace test |
117 } // namespace crashpad | 117 } // namespace crashpad |
OLD | NEW |