OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "Test.h" | 8 #include "Test.h" |
9 #include "SkLazyPtr.h" | 9 #include "SkLazyPtr.h" |
10 #include "SkRunnable.h" | 10 #include "SkRunnable.h" |
11 #include "SkTaskGroup.h" | 11 #include "SkTaskGroup.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 struct CreateIntFromFloat { | 15 struct CreateIntFromFloat { |
16 CreateIntFromFloat(float val) : fVal(val) {} | 16 CreateIntFromFloat(float val) : fVal(val) {} |
17 int* operator()() const { return SkNEW_ARGS(int, ((int)fVal)); } | 17 int* operator()() const { return new int((int)fVal); } |
18 float fVal; | 18 float fVal; |
19 }; | 19 }; |
20 | 20 |
21 // As a template argument this must have external linkage. | 21 // As a template argument this must have external linkage. |
22 void custom_destroy(int* ptr) { *ptr = 99; } | 22 void custom_destroy(int* ptr) { *ptr = 99; } |
23 | 23 |
24 } // namespace | 24 } // namespace |
25 | 25 |
26 DEF_TEST(LazyPtr, r) { | 26 DEF_TEST(LazyPtr, r) { |
27 // Basic usage: calls SkNEW(int). | 27 // Basic usage: calls new int. |
28 SkLazyPtr<int> lazy; | 28 SkLazyPtr<int> lazy; |
29 int* ptr = lazy.get(); | 29 int* ptr = lazy.get(); |
30 REPORTER_ASSERT(r, ptr); | 30 REPORTER_ASSERT(r, ptr); |
31 REPORTER_ASSERT(r, lazy.get() == ptr); | 31 REPORTER_ASSERT(r, lazy.get() == ptr); |
32 | 32 |
33 // Advanced usage: calls a functor. | 33 // Advanced usage: calls a functor. |
34 SkLazyPtr<int> lazyFunctor; | 34 SkLazyPtr<int> lazyFunctor; |
35 int* six = lazyFunctor.get(CreateIntFromFloat(6.4f)); | 35 int* six = lazyFunctor.get(CreateIntFromFloat(6.4f)); |
36 REPORTER_ASSERT(r, six); | 36 REPORTER_ASSERT(r, six); |
37 REPORTER_ASSERT(r, 6 == *six); | 37 REPORTER_ASSERT(r, 6 == *six); |
38 | 38 |
39 // Just makes sure this is safe. | 39 // Just makes sure this is safe. |
40 SkLazyPtr<double> neverRead; | 40 SkLazyPtr<double> neverRead; |
41 | 41 |
42 // SkLazyPtr supports custom destroy methods. | 42 // SkLazyPtr supports custom destroy methods. |
43 { | 43 { |
44 SkLazyPtr<int, custom_destroy> customDestroy; | 44 SkLazyPtr<int, custom_destroy> customDestroy; |
45 ptr = customDestroy.get(); | 45 ptr = customDestroy.get(); |
46 // custom_destroy called here. | 46 // custom_destroy called here. |
47 } | 47 } |
48 REPORTER_ASSERT(r, ptr); | 48 REPORTER_ASSERT(r, ptr); |
49 REPORTER_ASSERT(r, 99 == *ptr); | 49 REPORTER_ASSERT(r, 99 == *ptr); |
50 // Since custom_destroy didn't actually delete ptr, we do now. | 50 // Since custom_destroy didn't actually delete ptr, we do now. |
51 SkDELETE(ptr); | 51 delete ptr; |
52 } | 52 } |
53 | 53 |
54 DEF_TEST(LazyPtr_Threaded, r) { | 54 DEF_TEST(LazyPtr_Threaded, r) { |
55 static const int kRacers = 321; | 55 static const int kRacers = 321; |
56 | 56 |
57 // Race to intialize the pointer by calling .get(). | 57 // Race to intialize the pointer by calling .get(). |
58 SkLazyPtr<int> lazy; | 58 SkLazyPtr<int> lazy; |
59 int* seen[kRacers]; | 59 int* seen[kRacers]; |
60 | 60 |
61 sk_parallel_for(kRacers, [&](int i) { | 61 sk_parallel_for(kRacers, [&](int i) { |
62 seen[i] = lazy.get(); | 62 seen[i] = lazy.get(); |
63 }); | 63 }); |
64 | 64 |
65 // lazy.get() should return the same pointer to all threads. | 65 // lazy.get() should return the same pointer to all threads. |
66 for (int i = 1; i < kRacers; i++) { | 66 for (int i = 1; i < kRacers; i++) { |
67 REPORTER_ASSERT(r, seen[i] != nullptr); | 67 REPORTER_ASSERT(r, seen[i] != nullptr); |
68 REPORTER_ASSERT(r, seen[i] == seen[0]); | 68 REPORTER_ASSERT(r, seen[i] == seen[0]); |
69 } | 69 } |
70 } | 70 } |
OLD | NEW |