skia2/tests/TLazyTest.cpp
John Stiles 31954bf23f Enable ClangTidy check performance-unnecessary-copy-initialization.
https://clang.llvm.org/extra/clang-tidy/checks/performance-unnecessary-copy-initialization.html

Finds local variable declarations that are initialized using the copy
constructor of a non-trivially-copyable type but it would suffice to
obtain a const reference.

The check is only applied if it is safe to replace the copy by a const
reference. This is the case when the variable is const qualified or when
it is only used as a const, i.e. only const methods or operators are
invoked on it, or it is used as const reference or value argument in
constructors or function calls.

Change-Id: I1261410deccd8ea64e85edec53fbd5360940e587
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/308759
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-08-07 22:20:36 +00:00

78 lines
2.2 KiB
C++

/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "src/core/SkTLazy.h"
#include "tests/Test.h"
DEF_TEST(TLazy_copy, r) {
SkTLazy<int> lazy;
REPORTER_ASSERT(r, !lazy.isValid());
REPORTER_ASSERT(r, lazy.getMaybeNull() == nullptr);
{
SkTLazy<int> lazy_copy(lazy); // NOLINT(performance-unnecessary-copy-initialization)
REPORTER_ASSERT(r, !lazy_copy.isValid());
REPORTER_ASSERT(r, lazy_copy.getMaybeNull() == nullptr);
}
lazy.init(42);
REPORTER_ASSERT(r, lazy.isValid());
REPORTER_ASSERT(r, 42 == *lazy.get());
{
SkTLazy<int> lazy_copy(lazy); // NOLINT(performance-unnecessary-copy-initialization)
REPORTER_ASSERT(r, lazy_copy.isValid());
REPORTER_ASSERT(r, 42 == *lazy_copy.get());
REPORTER_ASSERT(r, lazy.get() != lazy_copy.get());
}
}
DEF_TEST(TCopyOnFirstWrite_copy, r) {
const int v = 42;
SkTCopyOnFirstWrite<int> cow(v);
REPORTER_ASSERT(r, 42 == *cow);
REPORTER_ASSERT(r, &v == cow.get());
{
SkTCopyOnFirstWrite<int> cow_copy(cow);
REPORTER_ASSERT(r, 42 == *cow_copy);
REPORTER_ASSERT(r, &v == cow_copy.get());
REPORTER_ASSERT(r, cow.get() == cow_copy.get());
*cow_copy.writable() = 43;
REPORTER_ASSERT(r, 42 == *cow);
REPORTER_ASSERT(r, &v == cow.get());
REPORTER_ASSERT(r, 43 == *cow_copy);
REPORTER_ASSERT(r, &v != cow_copy.get());
REPORTER_ASSERT(r, cow.get() != cow_copy.get());
}
*cow.writable() = 43;
REPORTER_ASSERT(r, 43 == *cow);
REPORTER_ASSERT(r, &v != cow.get());
{
SkTCopyOnFirstWrite<int> cow_copy(cow);
REPORTER_ASSERT(r, 43 == *cow_copy);
REPORTER_ASSERT(r, &v != cow_copy.get());
REPORTER_ASSERT(r, cow.get() != cow_copy.get());
*cow_copy.writable() = 44;
REPORTER_ASSERT(r, 43 == *cow);
REPORTER_ASSERT(r, &v != cow.get());
REPORTER_ASSERT(r, 44 == *cow_copy);
REPORTER_ASSERT(r, &v != cow_copy.get());
REPORTER_ASSERT(r, cow.get() != cow_copy.get());
}
}