1 /*
2  * Copyright 2018 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 "src/core/SkTLazy.h"
9 #include "tests/Test.h"
10 
DEF_TEST(TLazy_copy,r)11 DEF_TEST(TLazy_copy, r) {
12     SkTLazy<int> lazy;
13 
14     REPORTER_ASSERT(r, !lazy.isValid());
15     REPORTER_ASSERT(r, lazy.getMaybeNull() == nullptr);
16 
17     {
18         SkTLazy<int> lazy_copy(lazy);  // NOLINT(performance-unnecessary-copy-initialization)
19         REPORTER_ASSERT(r, !lazy_copy.isValid());
20         REPORTER_ASSERT(r, lazy_copy.getMaybeNull() == nullptr);
21     }
22 
23     lazy.init(42);
24 
25     REPORTER_ASSERT(r, lazy.isValid());
26     REPORTER_ASSERT(r, 42 == *lazy.get());
27 
28     {
29         SkTLazy<int> lazy_copy(lazy);  // NOLINT(performance-unnecessary-copy-initialization)
30         REPORTER_ASSERT(r, lazy_copy.isValid());
31         REPORTER_ASSERT(r, 42 == *lazy_copy.get());
32         REPORTER_ASSERT(r, lazy.get() != lazy_copy.get());
33     }
34 }
35 
DEF_TEST(TCopyOnFirstWrite_copy,r)36 DEF_TEST(TCopyOnFirstWrite_copy, r) {
37     const int v = 42;
38 
39     SkTCopyOnFirstWrite<int> cow(v);
40 
41     REPORTER_ASSERT(r, 42 == *cow);
42     REPORTER_ASSERT(r, &v ==  cow.get());
43 
44     {
45         SkTCopyOnFirstWrite<int> cow_copy(cow);
46         REPORTER_ASSERT(r, 42 == *cow_copy);
47         REPORTER_ASSERT(r, &v ==  cow_copy.get());
48         REPORTER_ASSERT(r, cow.get() ==  cow_copy.get());
49 
50         *cow_copy.writable() = 43;
51         REPORTER_ASSERT(r, 42 == *cow);
52         REPORTER_ASSERT(r, &v ==  cow.get());
53         REPORTER_ASSERT(r, 43 == *cow_copy);
54         REPORTER_ASSERT(r, &v !=  cow_copy.get());
55         REPORTER_ASSERT(r, cow.get() !=  cow_copy.get());
56     }
57 
58     *cow.writable() = 43;
59 
60     REPORTER_ASSERT(r, 43 == *cow);
61     REPORTER_ASSERT(r, &v !=  cow.get());
62 
63     {
64         SkTCopyOnFirstWrite<int> cow_copy(cow);
65         REPORTER_ASSERT(r, 43 == *cow_copy);
66         REPORTER_ASSERT(r, &v !=  cow_copy.get());
67         REPORTER_ASSERT(r, cow.get() !=  cow_copy.get());
68 
69         *cow_copy.writable() = 44;
70 
71         REPORTER_ASSERT(r, 43 == *cow);
72         REPORTER_ASSERT(r, &v !=  cow.get());
73         REPORTER_ASSERT(r, 44 == *cow_copy);
74         REPORTER_ASSERT(r, &v !=  cow_copy.get());
75         REPORTER_ASSERT(r, cow.get() !=  cow_copy.get());
76     }
77 }
78