12#include "gtest/gtest.h"
19static constexpr std::array<int, 5> kInputValues = {0, 1, 2, 1, 0};
21static void PopulateScopedSafearrayOfInts(ScopedSafearray& scoped_safe_array) {
23 scoped_safe_array.Reset(SafeArrayCreateVector(
25 kInputValues.size()));
26 ASSERT_NE(scoped_safe_array.Get(),
nullptr);
27 ASSERT_EQ(SafeArrayGetDim(scoped_safe_array.Get()), 1U);
28 ASSERT_EQ(scoped_safe_array.GetCount(), kInputValues.size());
31 ASSERT_HRESULT_SUCCEEDED(SafeArrayAccessData(
32 scoped_safe_array.Get(),
reinterpret_cast<void**
>(&int_array)));
33 for (
size_t i = 0;
i < kInputValues.size(); ++
i)
34 int_array[
i] = kInputValues[
i];
35 ASSERT_HRESULT_SUCCEEDED(SafeArrayUnaccessData(scoped_safe_array.Get()));
40TEST(ScopedSafearrayTest, ScopedSafearrayMethods) {
42 EXPECT_EQ(empty_safe_array.
Get(),
nullptr);
43 EXPECT_EQ(empty_safe_array.
Release(),
nullptr);
44 EXPECT_NE(empty_safe_array.
Receive(),
nullptr);
46 SAFEARRAY* safe_array = SafeArrayCreateVector(
49 EXPECT_EQ(scoped_safe_array.
Get(), safe_array);
50 EXPECT_EQ(scoped_safe_array.
Release(), safe_array);
51 EXPECT_NE(scoped_safe_array.
Receive(),
nullptr);
54 EXPECT_EQ(scoped_safe_array.
Get(),
nullptr);
56 scoped_safe_array.
Reset(safe_array);
57 EXPECT_EQ(scoped_safe_array.
Get(), safe_array);
60 EXPECT_EQ(moved_safe_array.
Get(), safe_array);
61 EXPECT_EQ(moved_safe_array.
Release(), safe_array);
62 EXPECT_NE(moved_safe_array.
Receive(),
nullptr);
65 EXPECT_EQ(scoped_safe_array.
Get(),
nullptr);
66 EXPECT_EQ(scoped_safe_array.
Release(),
nullptr);
67 EXPECT_NE(scoped_safe_array.
Receive(),
nullptr);
69 scoped_safe_array.
Reset(safe_array);
70 EXPECT_EQ(scoped_safe_array.
Get(), safe_array);
72 ScopedSafearray assigment_moved_safe_array = std::move(scoped_safe_array);
73 EXPECT_EQ(assigment_moved_safe_array.
Get(), safe_array);
74 EXPECT_EQ(assigment_moved_safe_array.
Release(), safe_array);
75 EXPECT_NE(assigment_moved_safe_array.
Receive(),
nullptr);
79 EXPECT_EQ(scoped_safe_array.
Get(),
nullptr);
80 EXPECT_EQ(scoped_safe_array.
Release(),
nullptr);
81 EXPECT_NE(scoped_safe_array.
Receive(),
nullptr);
86 EXPECT_NE(safe_array_received.
Receive(),
nullptr);
87 EXPECT_EQ(safe_array_received.
Get(),
nullptr);
90TEST(ScopedSafearrayTest, ScopedSafearrayMoveConstructor) {
92 PopulateScopedSafearrayOfInts(first);
93 EXPECT_NE(first.
Get(),
nullptr);
94 EXPECT_EQ(first.
GetCount(), kInputValues.size());
96 SAFEARRAY* safearray = first.
Get();
98 EXPECT_EQ(first.
Get(),
nullptr);
99 EXPECT_EQ(second.
Get(), safearray);
102TEST(ScopedSafearrayTest, ScopedSafearrayMoveAssignOperator) {
104 PopulateScopedSafearrayOfInts(first);
105 EXPECT_NE(first.
Get(),
nullptr);
106 EXPECT_EQ(first.
GetCount(), kInputValues.size());
108 SAFEARRAY* safearray = first.
Get();
109 second = std::move(first);
110 EXPECT_EQ(first.
Get(),
nullptr);
111 EXPECT_EQ(second.
Get(), safearray);
115 second = std::move(reference_to_second);
116 EXPECT_EQ(second.
GetCount(), kInputValues.size());
117 EXPECT_EQ(second.
Get(), safearray);
120TEST(ScopedSafearrayTest, ScopedSafearrayCast) {
121 SAFEARRAY* safe_array = SafeArrayCreateVector(
124 EXPECT_EQ(SafeArrayGetDim(scoped_safe_array.
Get()), 1U);
127 EXPECT_HRESULT_SUCCEEDED(
128 SafeArrayGetLBound(scoped_safe_array.
Get(), 1, &lower_bound));
129 EXPECT_EQ(lower_bound, 1);
132 EXPECT_HRESULT_SUCCEEDED(
133 SafeArrayGetUBound(scoped_safe_array.
Get(), 1, &upper_bound));
134 EXPECT_EQ(upper_bound, 5);
136 VARTYPE variable_type;
137 EXPECT_HRESULT_SUCCEEDED(
138 SafeArrayGetVartype(scoped_safe_array.
Get(), &variable_type));
139 EXPECT_EQ(variable_type, VT_R8);
142TEST(ScopedSafearrayTest, InitiallyEmpty) {
144 EXPECT_EQ(empty_safe_array.
Get(),
nullptr);
148TEST(ScopedSafearrayTest, ScopedSafearrayGetCount) {
152 ASSERT_NE(scoped_safe_array.
Get(),
nullptr);
153 EXPECT_EQ(SafeArrayGetDim(scoped_safe_array.
Get()), 1U);
156 EXPECT_HRESULT_SUCCEEDED(
157 SafeArrayGetLBound(scoped_safe_array.
Get(), 1, &lower_bound));
158 EXPECT_EQ(lower_bound, 2);
161 EXPECT_HRESULT_SUCCEEDED(
162 SafeArrayGetUBound(scoped_safe_array.
Get(), 1, &upper_bound));
163 EXPECT_EQ(upper_bound, 6);
165 EXPECT_EQ(scoped_safe_array.
GetCount(), 5U);
168TEST(ScopedSafearrayTest, ScopedSafearrayInitialLockScope) {
170 std::optional<ScopedSafearray::LockScope<VT_I4>> lock_scope =
172 EXPECT_FALSE(lock_scope.has_value());
175TEST(ScopedSafearrayTest, ScopedSafearrayLockScopeMoveConstructor) {
177 PopulateScopedSafearrayOfInts(scoped_safe_array);
179 std::optional<ScopedSafearray::LockScope<VT_I4>> first =
181 ASSERT_TRUE(first.has_value());
182 EXPECT_EQ(first->Type(), VT_I4);
183 EXPECT_EQ(first->size(), kInputValues.size());
186 EXPECT_EQ(first->Type(), VT_EMPTY);
187 EXPECT_EQ(first->size(), 0
U);
188 EXPECT_EQ(second.
Type(), VT_I4);
189 EXPECT_EQ(second.
size(), kInputValues.size());
192TEST(ScopedSafearrayTest, ScopedSafearrayLockScopeMoveAssignOperator) {
194 PopulateScopedSafearrayOfInts(scoped_safe_array);
196 std::optional<ScopedSafearray::LockScope<VT_I4>> first =
198 ASSERT_TRUE(first.has_value());
199 EXPECT_EQ(first->Type(), VT_I4);
200 EXPECT_EQ(first->size(), kInputValues.size());
203 second = std::move(*first);
204 EXPECT_EQ(first->Type(), VT_EMPTY);
205 EXPECT_EQ(first->size(), 0
U);
206 EXPECT_EQ(second.
Type(), VT_I4);
207 EXPECT_EQ(second.
size(), kInputValues.size());
214TEST(ScopedSafearrayTest, ScopedSafearrayLockScopeTypeMismatch) {
216 PopulateScopedSafearrayOfInts(scoped_safe_array);
219 std::optional<ScopedSafearray::LockScope<VT_BSTR>> invalid_lock_scope =
221 EXPECT_FALSE(invalid_lock_scope.has_value());
225 std::optional<ScopedSafearray::LockScope<VT_UI4>> invalid_lock_scope =
227 EXPECT_FALSE(invalid_lock_scope.has_value());
231TEST(ScopedSafearrayTest, ScopedSafearrayLockScopeRandomAccess) {
233 PopulateScopedSafearrayOfInts(scoped_safe_array);
235 std::optional<ScopedSafearray::LockScope<VT_I4>> lock_scope =
237 ASSERT_TRUE(lock_scope.has_value());
238 EXPECT_EQ(lock_scope->Type(), VT_I4);
239 EXPECT_EQ(lock_scope->size(), kInputValues.size());
240 for (
size_t i = 0;
i < kInputValues.size(); ++
i) {
241 EXPECT_EQ(lock_scope->at(
i), kInputValues[
i]);
242 EXPECT_EQ((*lock_scope)[
i], kInputValues[
i]);
246TEST(ScopedSafearrayTest, ScopedSafearrayLockScopeIterator) {
248 PopulateScopedSafearrayOfInts(scoped_safe_array);
250 std::optional<ScopedSafearray::LockScope<VT_I4>> lock_scope =
253 std::vector<int> unpacked_vector(lock_scope->begin(), lock_scope->end());
254 ASSERT_EQ(unpacked_vector.size(), kInputValues.size());
255 for (
size_t i = 0;
i < kInputValues.size(); ++
i)
256 EXPECT_EQ(unpacked_vector[
i], kInputValues[
i]);
void Reset(SAFEARRAY *safearray=nullptr)
size_t GetCount(UINT dimension=0) const
std::optional< LockScope< ElementVartype > > CreateLockScope() const
#define EXPECT_DCHECK_DEATH(statement)
TEST(Display, ScaleFactorToFloat)