| 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" | |
| 10 #include "SkThreadUtils.h" | 9 #include "SkThreadUtils.h" |
| 11 #include "SkTypes.h" | 10 #include "SkTypes.h" |
| 12 #include "SkWeakRefCnt.h" | 11 #include "SkWeakRefCnt.h" |
| 13 #include "Test.h" | 12 #include "Test.h" |
| 14 | 13 |
| 15 class InstCounterClass { | 14 class InstCounterClass { |
| 16 public: | 15 public: |
| 17 InstCounterClass() { fCount = gInstCounter++; } | 16 InstCounterClass() { fCount = gInstCounter++; } |
| 18 InstCounterClass(const InstCounterClass& src) { | 17 InstCounterClass(const InstCounterClass& src) { |
| 19 fCount = src.fCount; | 18 fCount = src.fCount; |
| 20 gInstCounter += 1; | 19 gInstCounter += 1; |
| 21 } | 20 } |
| 22 virtual ~InstCounterClass() { gInstCounter -= 1; } | 21 virtual ~InstCounterClass() { gInstCounter -= 1; } |
| 23 | 22 |
| 24 static int gInstCounter; | 23 static int gInstCounter; |
| 25 int fCount; | 24 int fCount; |
| 26 }; | 25 }; |
| 27 | 26 |
| 28 int InstCounterClass::gInstCounter; | 27 int InstCounterClass::gInstCounter; |
| 29 | 28 |
| 30 static void test_refarray(skiatest::Reporter* reporter) { | |
| 31 REPORTER_ASSERT(reporter, 0 == InstCounterClass::gInstCounter); | |
| 32 | |
| 33 const int N = 10; | |
| 34 SkTRefArray<InstCounterClass>* array = SkTRefArray<InstCounterClass>::Create
(N); | |
| 35 | |
| 36 REPORTER_ASSERT(reporter, 1 == array->getRefCnt()); | |
| 37 REPORTER_ASSERT(reporter, N == array->count()); | |
| 38 | |
| 39 REPORTER_ASSERT(reporter, N == InstCounterClass::gInstCounter); | |
| 40 array->unref(); | |
| 41 REPORTER_ASSERT(reporter, 0 == InstCounterClass::gInstCounter); | |
| 42 | |
| 43 // Now test the copy factory | |
| 44 | |
| 45 int i; | |
| 46 InstCounterClass* src = new InstCounterClass[N]; | |
| 47 REPORTER_ASSERT(reporter, N == InstCounterClass::gInstCounter); | |
| 48 for (i = 0; i < N; ++i) { | |
| 49 REPORTER_ASSERT(reporter, i == src[i].fCount); | |
| 50 } | |
| 51 | |
| 52 array = SkTRefArray<InstCounterClass>::Create(src, N); | |
| 53 REPORTER_ASSERT(reporter, 1 == array->getRefCnt()); | |
| 54 REPORTER_ASSERT(reporter, N == array->count()); | |
| 55 | |
| 56 REPORTER_ASSERT(reporter, 2*N == InstCounterClass::gInstCounter); | |
| 57 for (i = 0; i < N; ++i) { | |
| 58 REPORTER_ASSERT(reporter, i == (*array)[i].fCount); | |
| 59 } | |
| 60 | |
| 61 delete[] src; | |
| 62 REPORTER_ASSERT(reporter, N == InstCounterClass::gInstCounter); | |
| 63 | |
| 64 for (i = 0; i < N; ++i) { | |
| 65 REPORTER_ASSERT(reporter, i == (*array)[i].fCount); | |
| 66 } | |
| 67 array->unref(); | |
| 68 REPORTER_ASSERT(reporter, 0 == InstCounterClass::gInstCounter); | |
| 69 } | |
| 70 | |
| 71 static void bounce_ref(void* data) { | 29 static void bounce_ref(void* data) { |
| 72 SkRefCnt* ref = static_cast<SkRefCnt*>(data); | 30 SkRefCnt* ref = static_cast<SkRefCnt*>(data); |
| 73 for (int i = 0; i < 100000; ++i) { | 31 for (int i = 0; i < 100000; ++i) { |
| 74 ref->ref(); | 32 ref->ref(); |
| 75 ref->unref(); | 33 ref->unref(); |
| 76 } | 34 } |
| 77 } | 35 } |
| 78 | 36 |
| 79 static void test_refCnt(skiatest::Reporter* reporter) { | 37 static void test_refCnt(skiatest::Reporter* reporter) { |
| 80 SkRefCnt* ref = new SkRefCnt(); | 38 SkRefCnt* ref = new SkRefCnt(); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 thing4.join(); | 94 thing4.join(); |
| 137 | 95 |
| 138 REPORTER_ASSERT(reporter, ref->getRefCnt() == 1); | 96 REPORTER_ASSERT(reporter, ref->getRefCnt() == 1); |
| 139 REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1); | 97 REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1); |
| 140 ref->unref(); | 98 ref->unref(); |
| 141 } | 99 } |
| 142 | 100 |
| 143 DEF_TEST(RefCnt, reporter) { | 101 DEF_TEST(RefCnt, reporter) { |
| 144 test_refCnt(reporter); | 102 test_refCnt(reporter); |
| 145 test_weakRefCnt(reporter); | 103 test_weakRefCnt(reporter); |
| 146 test_refarray(reporter); | |
| 147 } | 104 } |
| OLD | NEW |