blob: 2c42445f785d87d6e570f6ad87d852b6dad59489 [file] [log] [blame]
David Ghandehari9e5b5872016-07-28 09:50:04 -07001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/template_util.h"
6
Andrew Top0d1858f2019-05-15 22:01:47 -07007#include <string>
8
9#include "base/containers/flat_tree.h"
10#include "base/test/move_only_int.h"
David Ghandehari9e5b5872016-07-28 09:50:04 -070011#include "testing/gtest/include/gtest/gtest.h"
12
13namespace base {
14namespace {
15
Andrew Top0d1858f2019-05-15 22:01:47 -070016enum SimpleEnum { SIMPLE_ENUM };
17enum EnumWithExplicitType : uint64_t { ENUM_WITH_EXPLICIT_TYPE };
18enum class ScopedEnum { SCOPED_ENUM };
19enum class ScopedEnumWithOperator { SCOPED_ENUM_WITH_OPERATOR };
20std::ostream& operator<<(std::ostream& os, ScopedEnumWithOperator v) {
21 return os;
22}
23struct SimpleStruct {};
24struct StructWithOperator {};
25std::ostream& operator<<(std::ostream& os, const StructWithOperator& v) {
26 return os;
27}
David Ghandehari9e5b5872016-07-28 09:50:04 -070028
29// is_non_const_reference<Type>
Andrew Top0d1858f2019-05-15 22:01:47 -070030static_assert(!is_non_const_reference<int>::value, "IsNonConstReference");
31static_assert(!is_non_const_reference<const int&>::value,
32 "IsNonConstReference");
33static_assert(is_non_const_reference<int&>::value, "IsNonConstReference");
David Ghandehari9e5b5872016-07-28 09:50:04 -070034
Andrew Top0d1858f2019-05-15 22:01:47 -070035// A few standard types that definitely support printing.
36static_assert(internal::SupportsOstreamOperator<int>::value,
37 "ints should be printable");
38static_assert(internal::SupportsOstreamOperator<const char*>::value,
39 "C strings should be printable");
40static_assert(internal::SupportsOstreamOperator<std::string>::value,
41 "std::string should be printable");
David Ghandehari9e5b5872016-07-28 09:50:04 -070042
Andrew Top0d1858f2019-05-15 22:01:47 -070043// Various kinds of enums operator<< support.
44static_assert(internal::SupportsOstreamOperator<SimpleEnum>::value,
45 "simple enum should be printable by value");
46static_assert(internal::SupportsOstreamOperator<const SimpleEnum&>::value,
47 "simple enum should be printable by const ref");
48static_assert(internal::SupportsOstreamOperator<EnumWithExplicitType>::value,
49 "enum with explicit type should be printable by value");
50static_assert(
51 internal::SupportsOstreamOperator<const EnumWithExplicitType&>::value,
52 "enum with explicit type should be printable by const ref");
53static_assert(!internal::SupportsOstreamOperator<ScopedEnum>::value,
54 "scoped enum should not be printable by value");
55static_assert(!internal::SupportsOstreamOperator<const ScopedEnum&>::value,
56 "simple enum should not be printable by const ref");
57static_assert(internal::SupportsOstreamOperator<ScopedEnumWithOperator>::value,
58 "scoped enum with operator<< should be printable by value");
59static_assert(
60 internal::SupportsOstreamOperator<const ScopedEnumWithOperator&>::value,
61 "scoped enum with operator<< should be printable by const ref");
David Ghandehari9e5b5872016-07-28 09:50:04 -070062
Andrew Top0d1858f2019-05-15 22:01:47 -070063// operator<< support on structs.
64static_assert(!internal::SupportsOstreamOperator<SimpleStruct>::value,
65 "simple struct should not be printable by value");
66static_assert(!internal::SupportsOstreamOperator<const SimpleStruct&>::value,
67 "simple struct should not be printable by const ref");
68static_assert(internal::SupportsOstreamOperator<StructWithOperator>::value,
69 "struct with operator<< should be printable by value");
70static_assert(
71 internal::SupportsOstreamOperator<const StructWithOperator&>::value,
72 "struct with operator<< should be printable by const ref");
David Ghandehari9e5b5872016-07-28 09:50:04 -070073
Andrew Top0d1858f2019-05-15 22:01:47 -070074// base::is_trivially_copyable
75class TrivialCopy {
76 public:
77 TrivialCopy(int d) : data_(d) {}
David Ghandehari9e5b5872016-07-28 09:50:04 -070078
Andrew Top0d1858f2019-05-15 22:01:47 -070079 protected:
80 int data_;
81};
David Ghandehari9e5b5872016-07-28 09:50:04 -070082
Andrew Top0d1858f2019-05-15 22:01:47 -070083class TrivialCopyButWithDestructor : public TrivialCopy {
84 public:
85 TrivialCopyButWithDestructor(int d) : TrivialCopy(d) {}
86 ~TrivialCopyButWithDestructor() { data_ = 0; }
87};
88
89static_assert(base::is_trivially_copyable<TrivialCopy>::value,
90 "TrivialCopy should be detected as trivially copyable");
91static_assert(!base::is_trivially_copyable<TrivialCopyButWithDestructor>::value,
92 "TrivialCopyButWithDestructor should not be detected as "
93 "trivially copyable");
94
95class NoCopy {
96 public:
97 NoCopy(const NoCopy&) = delete;
98};
99
100static_assert(
101 !base::is_trivially_copy_constructible<std::vector<NoCopy>>::value,
102 "is_trivially_copy_constructible<std::vector<T>> must be compiled.");
David Ghandehari9e5b5872016-07-28 09:50:04 -0700103
104} // namespace
Andrew Top0d1858f2019-05-15 22:01:47 -0700105
David Ghandehari9e5b5872016-07-28 09:50:04 -0700106} // namespace base