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

Side by Side Diff: tests/LazyPtrTest.cpp

Issue 1322933005: Port uses of SkLazyPtr to SkOncePtr. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: name Created 5 years, 3 months 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
« no previous file with comments | « src/utils/SkEventTracer.cpp ('k') | tests/OncePtrTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "Test.h"
9 #include "SkLazyPtr.h"
10 #include "SkRunnable.h"
11 #include "SkTaskGroup.h"
12
13 namespace {
14
15 struct CreateIntFromFloat {
16 CreateIntFromFloat(float val) : fVal(val) {}
17 int* operator()() const { return new int((int)fVal); }
18 float fVal;
19 };
20
21 // As a template argument this must have external linkage.
22 void custom_destroy(int* ptr) { *ptr = 99; }
23
24 } // namespace
25
26 DEF_TEST(LazyPtr, r) {
27 // Basic usage: calls new int.
28 SkLazyPtr<int> lazy;
29 int* ptr = lazy.get();
30 REPORTER_ASSERT(r, ptr);
31 REPORTER_ASSERT(r, lazy.get() == ptr);
32
33 // Advanced usage: calls a functor.
34 SkLazyPtr<int> lazyFunctor;
35 int* six = lazyFunctor.get(CreateIntFromFloat(6.4f));
36 REPORTER_ASSERT(r, six);
37 REPORTER_ASSERT(r, 6 == *six);
38
39 // Just makes sure this is safe.
40 SkLazyPtr<double> neverRead;
41
42 // SkLazyPtr supports custom destroy methods.
43 {
44 SkLazyPtr<int, custom_destroy> customDestroy;
45 ptr = customDestroy.get();
46 // custom_destroy called here.
47 }
48 REPORTER_ASSERT(r, ptr);
49 REPORTER_ASSERT(r, 99 == *ptr);
50 // Since custom_destroy didn't actually delete ptr, we do now.
51 delete ptr;
52 }
53
54 DEF_TEST(LazyPtr_Threaded, r) {
55 static const int kRacers = 321;
56
57 // Race to intialize the pointer by calling .get().
58 SkLazyPtr<int> lazy;
59 int* seen[kRacers];
60
61 sk_parallel_for(kRacers, [&](int i) {
62 seen[i] = lazy.get();
63 });
64
65 // lazy.get() should return the same pointer to all threads.
66 for (int i = 1; i < kRacers; i++) {
67 REPORTER_ASSERT(r, seen[i] != nullptr);
68 REPORTER_ASSERT(r, seen[i] == seen[0]);
69 }
70 }
OLDNEW
« no previous file with comments | « src/utils/SkEventTracer.cpp ('k') | tests/OncePtrTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698