Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
scoped_bstr_unittest.cc
Go to the documentation of this file.
1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <cstddef>
8
9#include "gtest/gtest.h"
10
11namespace base {
12namespace win {
13
14namespace {
15
16constexpr wchar_t kTestString1[] = L"123";
17constexpr wchar_t kTestString2[] = L"456789";
18constexpr size_t test1_len = std::size(kTestString1) - 1;
19constexpr size_t test2_len = std::size(kTestString2) - 1;
20
21} // namespace
22
23TEST(ScopedBstrTest, Empty) {
25 EXPECT_EQ(nullptr, b.Get());
26 EXPECT_EQ(0u, b.Length());
27 EXPECT_EQ(0u, b.ByteLength());
28 b.Reset(nullptr);
29 EXPECT_EQ(nullptr, b.Get());
30 EXPECT_EQ(nullptr, b.Release());
31 ScopedBstr b2;
32 b.Swap(b2);
33 EXPECT_EQ(nullptr, b.Get());
34}
35
36TEST(ScopedBstrTest, Basic) {
37 ScopedBstr b(kTestString1);
38 EXPECT_EQ(test1_len, b.Length());
39 EXPECT_EQ(test1_len * sizeof(kTestString1[0]), b.ByteLength());
40}
41
42namespace {
43
44void CreateTestString1(BSTR* ret) {
45 *ret = SysAllocString(kTestString1);
46}
47
48} // namespace
49
50TEST(ScopedBstrTest, Swap) {
51 ScopedBstr b1(kTestString1);
52 ScopedBstr b2;
53 b1.Swap(b2);
54 EXPECT_EQ(test1_len, b2.Length());
55 EXPECT_EQ(0u, b1.Length());
56 EXPECT_STREQ(kTestString1, b2.Get());
57
58 BSTR tmp = b2.Release();
59 EXPECT_NE(nullptr, tmp);
60 EXPECT_STREQ(kTestString1, tmp);
61 EXPECT_EQ(nullptr, b2.Get());
62 SysFreeString(tmp);
63}
64
65TEST(ScopedBstrTest, OutParam) {
67 CreateTestString1(b.Receive());
68 EXPECT_STREQ(kTestString1, b.Get());
69}
70
71TEST(ScopedBstrTest, AllocateBytesAndSetByteLen) {
72 constexpr size_t num_bytes = 100;
74 EXPECT_NE(nullptr, b.AllocateBytes(num_bytes));
75 EXPECT_EQ(num_bytes, b.ByteLength());
76 EXPECT_EQ(num_bytes / sizeof(kTestString1[0]), b.Length());
77
78 lstrcpy(b.Get(), kTestString1);
79 EXPECT_EQ(test1_len, static_cast<size_t>(lstrlen(b.Get())));
80 EXPECT_EQ(num_bytes / sizeof(kTestString1[0]), b.Length());
81
82 b.SetByteLen(lstrlen(b.Get()) * sizeof(kTestString2[0]));
83 EXPECT_EQ(b.Length(), static_cast<size_t>(lstrlen(b.Get())));
84}
85
86TEST(ScopedBstrTest, AllocateAndSetByteLen) {
88 EXPECT_NE(nullptr, b.Allocate(kTestString2));
89 EXPECT_EQ(test2_len, b.Length());
90
91 b.SetByteLen((test2_len - 1) * sizeof(kTestString2[0]));
92 EXPECT_EQ(test2_len - 1, b.Length());
93}
94
95} // namespace win
96} // namespace base
#define TEST(S, s, D, expected)
void Swap(ScopedBstr &bstr2)
size_t Length() const
static bool b