1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2018 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "src/core/SkTLazy.h" 9cb93a386Sopenharmony_ci#include "tests/Test.h" 10cb93a386Sopenharmony_ci 11cb93a386Sopenharmony_ciDEF_TEST(TLazy_copy, r) { 12cb93a386Sopenharmony_ci SkTLazy<int> lazy; 13cb93a386Sopenharmony_ci 14cb93a386Sopenharmony_ci REPORTER_ASSERT(r, !lazy.isValid()); 15cb93a386Sopenharmony_ci REPORTER_ASSERT(r, lazy.getMaybeNull() == nullptr); 16cb93a386Sopenharmony_ci 17cb93a386Sopenharmony_ci { 18cb93a386Sopenharmony_ci SkTLazy<int> lazy_copy(lazy); // NOLINT(performance-unnecessary-copy-initialization) 19cb93a386Sopenharmony_ci REPORTER_ASSERT(r, !lazy_copy.isValid()); 20cb93a386Sopenharmony_ci REPORTER_ASSERT(r, lazy_copy.getMaybeNull() == nullptr); 21cb93a386Sopenharmony_ci } 22cb93a386Sopenharmony_ci 23cb93a386Sopenharmony_ci lazy.init(42); 24cb93a386Sopenharmony_ci 25cb93a386Sopenharmony_ci REPORTER_ASSERT(r, lazy.isValid()); 26cb93a386Sopenharmony_ci REPORTER_ASSERT(r, 42 == *lazy.get()); 27cb93a386Sopenharmony_ci 28cb93a386Sopenharmony_ci { 29cb93a386Sopenharmony_ci SkTLazy<int> lazy_copy(lazy); // NOLINT(performance-unnecessary-copy-initialization) 30cb93a386Sopenharmony_ci REPORTER_ASSERT(r, lazy_copy.isValid()); 31cb93a386Sopenharmony_ci REPORTER_ASSERT(r, 42 == *lazy_copy.get()); 32cb93a386Sopenharmony_ci REPORTER_ASSERT(r, lazy.get() != lazy_copy.get()); 33cb93a386Sopenharmony_ci } 34cb93a386Sopenharmony_ci} 35cb93a386Sopenharmony_ci 36cb93a386Sopenharmony_ciDEF_TEST(TCopyOnFirstWrite_copy, r) { 37cb93a386Sopenharmony_ci const int v = 42; 38cb93a386Sopenharmony_ci 39cb93a386Sopenharmony_ci SkTCopyOnFirstWrite<int> cow(v); 40cb93a386Sopenharmony_ci 41cb93a386Sopenharmony_ci REPORTER_ASSERT(r, 42 == *cow); 42cb93a386Sopenharmony_ci REPORTER_ASSERT(r, &v == cow.get()); 43cb93a386Sopenharmony_ci 44cb93a386Sopenharmony_ci { 45cb93a386Sopenharmony_ci SkTCopyOnFirstWrite<int> cow_copy(cow); 46cb93a386Sopenharmony_ci REPORTER_ASSERT(r, 42 == *cow_copy); 47cb93a386Sopenharmony_ci REPORTER_ASSERT(r, &v == cow_copy.get()); 48cb93a386Sopenharmony_ci REPORTER_ASSERT(r, cow.get() == cow_copy.get()); 49cb93a386Sopenharmony_ci 50cb93a386Sopenharmony_ci *cow_copy.writable() = 43; 51cb93a386Sopenharmony_ci REPORTER_ASSERT(r, 42 == *cow); 52cb93a386Sopenharmony_ci REPORTER_ASSERT(r, &v == cow.get()); 53cb93a386Sopenharmony_ci REPORTER_ASSERT(r, 43 == *cow_copy); 54cb93a386Sopenharmony_ci REPORTER_ASSERT(r, &v != cow_copy.get()); 55cb93a386Sopenharmony_ci REPORTER_ASSERT(r, cow.get() != cow_copy.get()); 56cb93a386Sopenharmony_ci } 57cb93a386Sopenharmony_ci 58cb93a386Sopenharmony_ci *cow.writable() = 43; 59cb93a386Sopenharmony_ci 60cb93a386Sopenharmony_ci REPORTER_ASSERT(r, 43 == *cow); 61cb93a386Sopenharmony_ci REPORTER_ASSERT(r, &v != cow.get()); 62cb93a386Sopenharmony_ci 63cb93a386Sopenharmony_ci { 64cb93a386Sopenharmony_ci SkTCopyOnFirstWrite<int> cow_copy(cow); 65cb93a386Sopenharmony_ci REPORTER_ASSERT(r, 43 == *cow_copy); 66cb93a386Sopenharmony_ci REPORTER_ASSERT(r, &v != cow_copy.get()); 67cb93a386Sopenharmony_ci REPORTER_ASSERT(r, cow.get() != cow_copy.get()); 68cb93a386Sopenharmony_ci 69cb93a386Sopenharmony_ci *cow_copy.writable() = 44; 70cb93a386Sopenharmony_ci 71cb93a386Sopenharmony_ci REPORTER_ASSERT(r, 43 == *cow); 72cb93a386Sopenharmony_ci REPORTER_ASSERT(r, &v != cow.get()); 73cb93a386Sopenharmony_ci REPORTER_ASSERT(r, 44 == *cow_copy); 74cb93a386Sopenharmony_ci REPORTER_ASSERT(r, &v != cow_copy.get()); 75cb93a386Sopenharmony_ci REPORTER_ASSERT(r, cow.get() != cow_copy.get()); 76cb93a386Sopenharmony_ci } 77cb93a386Sopenharmony_ci} 78