OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkRefCnt.h" | 8 #include "SkRefCnt.h" |
9 #include "SkTRefArray.h" | 9 #include "SkTRefArray.h" |
10 #include "SkThreadUtils.h" | 10 #include "SkThreadUtils.h" |
11 #include "SkTypes.h" | 11 #include "SkTypes.h" |
12 #include "SkWeakRefCnt.h" | 12 #include "SkWeakRefCnt.h" |
13 #include "RefCntIs.h" | |
14 #include "Test.h" | 13 #include "Test.h" |
15 | 14 |
16 class InstCounterClass { | 15 class InstCounterClass { |
17 public: | 16 public: |
18 InstCounterClass() { fCount = gInstCounter++; } | 17 InstCounterClass() { fCount = gInstCounter++; } |
19 InstCounterClass(const InstCounterClass& src) { | 18 InstCounterClass(const InstCounterClass& src) { |
20 fCount = src.fCount; | 19 fCount = src.fCount; |
21 gInstCounter += 1; | 20 gInstCounter += 1; |
22 } | 21 } |
23 virtual ~InstCounterClass() { gInstCounter -= 1; } | 22 virtual ~InstCounterClass() { gInstCounter -= 1; } |
24 | 23 |
25 static int gInstCounter; | 24 static int gInstCounter; |
26 int fCount; | 25 int fCount; |
27 }; | 26 }; |
28 | 27 |
29 int InstCounterClass::gInstCounter; | 28 int InstCounterClass::gInstCounter; |
30 | 29 |
31 static void test_refarray(skiatest::Reporter* reporter) { | 30 static void test_refarray(skiatest::Reporter* reporter) { |
32 REPORTER_ASSERT(reporter, 0 == InstCounterClass::gInstCounter); | 31 REPORTER_ASSERT(reporter, 0 == InstCounterClass::gInstCounter); |
33 | 32 |
34 const int N = 10; | 33 const int N = 10; |
35 SkTRefArray<InstCounterClass>* array = SkTRefArray<InstCounterClass>::Create
(N); | 34 SkTRefArray<InstCounterClass>* array = SkTRefArray<InstCounterClass>::Create
(N); |
36 | 35 |
37 REPORTER_ASSERT(reporter, RefCntIs(*array, 1)); | 36 REPORTER_ASSERT(reporter, 1 == array->getRefCnt()); |
38 REPORTER_ASSERT(reporter, N == array->count()); | 37 REPORTER_ASSERT(reporter, N == array->count()); |
39 | 38 |
40 REPORTER_ASSERT(reporter, N == InstCounterClass::gInstCounter); | 39 REPORTER_ASSERT(reporter, N == InstCounterClass::gInstCounter); |
41 array->unref(); | 40 array->unref(); |
42 REPORTER_ASSERT(reporter, 0 == InstCounterClass::gInstCounter); | 41 REPORTER_ASSERT(reporter, 0 == InstCounterClass::gInstCounter); |
43 | 42 |
44 // Now test the copy factory | 43 // Now test the copy factory |
45 | 44 |
46 int i; | 45 int i; |
47 InstCounterClass* src = new InstCounterClass[N]; | 46 InstCounterClass* src = new InstCounterClass[N]; |
48 REPORTER_ASSERT(reporter, N == InstCounterClass::gInstCounter); | 47 REPORTER_ASSERT(reporter, N == InstCounterClass::gInstCounter); |
49 for (i = 0; i < N; ++i) { | 48 for (i = 0; i < N; ++i) { |
50 REPORTER_ASSERT(reporter, i == src[i].fCount); | 49 REPORTER_ASSERT(reporter, i == src[i].fCount); |
51 } | 50 } |
52 | 51 |
53 array = SkTRefArray<InstCounterClass>::Create(src, N); | 52 array = SkTRefArray<InstCounterClass>::Create(src, N); |
54 REPORTER_ASSERT(reporter, RefCntIs(*array, 1)); | 53 REPORTER_ASSERT(reporter, 1 == array->getRefCnt()); |
55 REPORTER_ASSERT(reporter, N == array->count()); | 54 REPORTER_ASSERT(reporter, N == array->count()); |
56 | 55 |
57 REPORTER_ASSERT(reporter, 2*N == InstCounterClass::gInstCounter); | 56 REPORTER_ASSERT(reporter, 2*N == InstCounterClass::gInstCounter); |
58 for (i = 0; i < N; ++i) { | 57 for (i = 0; i < N; ++i) { |
59 REPORTER_ASSERT(reporter, i == (*array)[i].fCount); | 58 REPORTER_ASSERT(reporter, i == (*array)[i].fCount); |
60 } | 59 } |
61 | 60 |
62 delete[] src; | 61 delete[] src; |
63 REPORTER_ASSERT(reporter, N == InstCounterClass::gInstCounter); | 62 REPORTER_ASSERT(reporter, N == InstCounterClass::gInstCounter); |
64 | 63 |
(...skipping 20 matching lines...) Expand all Loading... |
85 | 84 |
86 thing1.setProcessorAffinity(0); | 85 thing1.setProcessorAffinity(0); |
87 thing2.setProcessorAffinity(23); | 86 thing2.setProcessorAffinity(23); |
88 | 87 |
89 SkASSERT(thing1.start()); | 88 SkASSERT(thing1.start()); |
90 SkASSERT(thing2.start()); | 89 SkASSERT(thing2.start()); |
91 | 90 |
92 thing1.join(); | 91 thing1.join(); |
93 thing2.join(); | 92 thing2.join(); |
94 | 93 |
95 REPORTER_ASSERT(reporter, RefCntIs(*ref, 1)); | 94 REPORTER_ASSERT(reporter, ref->getRefCnt() == 1); |
96 ref->unref(); | 95 ref->unref(); |
97 } | 96 } |
98 | 97 |
99 static void bounce_weak_ref(void* data) { | 98 static void bounce_weak_ref(void* data) { |
100 SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data); | 99 SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data); |
101 for (int i = 0; i < 100000; ++i) { | 100 for (int i = 0; i < 100000; ++i) { |
102 if (ref->try_ref()) { | 101 if (ref->try_ref()) { |
103 ref->unref(); | 102 ref->unref(); |
104 } | 103 } |
105 } | 104 } |
(...skipping 23 matching lines...) Expand all Loading... |
129 SkASSERT(thing1.start()); | 128 SkASSERT(thing1.start()); |
130 SkASSERT(thing2.start()); | 129 SkASSERT(thing2.start()); |
131 SkASSERT(thing3.start()); | 130 SkASSERT(thing3.start()); |
132 SkASSERT(thing4.start()); | 131 SkASSERT(thing4.start()); |
133 | 132 |
134 thing1.join(); | 133 thing1.join(); |
135 thing2.join(); | 134 thing2.join(); |
136 thing3.join(); | 135 thing3.join(); |
137 thing4.join(); | 136 thing4.join(); |
138 | 137 |
139 REPORTER_ASSERT(reporter, RefCntIs(*ref, 1)); | 138 REPORTER_ASSERT(reporter, ref->getRefCnt() == 1); |
140 REPORTER_ASSERT(reporter, WeakRefCntIs(*ref, 1)); | 139 REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1); |
141 ref->unref(); | 140 ref->unref(); |
142 } | 141 } |
143 | 142 |
144 DEF_TEST(RefCnt, reporter) { | 143 DEF_TEST(RefCnt, reporter) { |
145 test_refCnt(reporter); | 144 test_refCnt(reporter); |
146 test_weakRefCnt(reporter); | 145 test_weakRefCnt(reporter); |
147 test_refarray(reporter); | 146 test_refarray(reporter); |
148 } | 147 } |
OLD | NEW |