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

Side by Side Diff: ios/chrome/common/block_unittest.mm

Issue 2480233002: [ios] Adds unittests for blocks and NSRegularExpression. (Closed)
Patch Set: Fix typos. Created 4 years, 1 month 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
OLDNEW
(Empty)
1 // Copyright (c) 2012 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
5 #import <Foundation/Foundation.h>
6 #include <vector>
7
8 #include "base/memory/ref_counted.h"
9 #include "testing/platform_test.h"
10
11 // This test verifies assumptions about the murky world of interaction between
12 // C++ objects and blocks. Just to make sure.
13
14 namespace {
15
16 class RefCountedObject : public base::RefCounted<RefCountedObject> {
17 public:
18 RefCountedObject(){};
sdefresne 2016/11/07 13:54:57 Remove trailing semi-colon ...
rohitrao (ping after 24h) 2016/11/07 14:04:05 Done.
19
20 // Refcount is private in the superclass, fake it by counting how many times
21 // release can be called until there is one count left, then retain the count
22 // back.
23 int refcount() {
24 int count = 1;
25 while (!HasOneRef()) {
26 bool check = base::subtle::RefCountedBase::Release();
27 EXPECT_FALSE(check);
28 ++count;
29 }
30 for (int ii = 1; ii < count; ii++)
sdefresne 2016/11/07 13:54:57 nit: why "ii" and not "i"?
rohitrao (ping after 24h) 2016/11/07 14:04:05 I tend to use "ii" because it doesn't commonly occ
31 base::subtle::RefCountedBase::AddRef();
32 return count;
33 }
34
35 protected:
36 friend base::RefCounted<RefCountedObject>;
37 virtual ~RefCountedObject(){};
sdefresne 2016/11/07 13:54:57 Remove trailing semi-colon ...
rohitrao (ping after 24h) 2016/11/07 14:04:05 Done.
38 };
39
40 TEST_F(PlatformTest, BlockAndCPlusPlus) {
41 RefCountedObject* object = new RefCountedObject();
42 object->AddRef();
43 EXPECT_TRUE(object->HasOneRef());
44 EXPECT_EQ(1, object->refcount());
45
46 {
47 scoped_refptr<RefCountedObject> object_test_ptr(object);
48 EXPECT_EQ(2, object->refcount());
49 }
50 EXPECT_TRUE(object->HasOneRef());
51
52 void (^heap_block)(int) = 0;
53 {
54 scoped_refptr<RefCountedObject> object_ptr(object);
55 EXPECT_EQ(2, object->refcount());
56 void* object_void_ptr = (void*)object;
57
58 void (^stack_block)(int) = ^(int expected) {
59 EXPECT_EQ(object_void_ptr, object_ptr.get());
60 EXPECT_EQ(expected, object_ptr.get()->refcount());
61 };
62 stack_block(3);
63 heap_block = [stack_block copy];
64 stack_block(4);
65 }
66 heap_block(2);
67 [heap_block release];
68 EXPECT_TRUE(object->HasOneRef());
69 {
70 scoped_refptr<RefCountedObject> object_test2_ptr(object);
71 EXPECT_EQ(2, object->refcount());
72 }
73 EXPECT_TRUE(object->HasOneRef());
74 object->Release();
75 }
76
77 TEST_F(PlatformTest, BlockAndVectors) {
78 void (^heap_block)(void) = 0;
79 {
80 std::vector<int> vector;
81 vector.push_back(0);
82 vector.push_back(1);
83 vector.push_back(2);
84
85 void (^stack_block)(void) = ^{
86 EXPECT_EQ(3ul, vector.size());
87 EXPECT_EQ(2, vector[2]);
88 };
89 stack_block();
90 vector[2] = 42;
91 vector.push_back(22);
92 stack_block();
93 heap_block = [stack_block copy];
94 stack_block();
95 }
96 heap_block();
97 [heap_block release];
98 }
99
100 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698