blob: efcea3d6d9adbdca69445f72bec3f111cda0d155 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]b38d3572011-02-15 01:27:382// 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"
[email protected]a6d6d4682012-06-19 18:34:086
Peter Kasting61152c22023-04-25 03:27:567#include <stdint.h>
8
jbroman6bcfec422016-05-26 00:28:469#include <string>
Jan Wilken Dörriea33bc992020-03-24 17:45:3110#include <type_traits>
jbroman6bcfec422016-05-26 00:28:4611
dyaroshev99a394d2017-07-22 19:10:1812#include "base/containers/flat_tree.h"
13#include "base/test/move_only_int.h"
[email protected]b38d3572011-02-15 01:27:3814#include "testing/gtest/include/gtest/gtest.h"
15
16namespace base {
17namespace {
18
jbroman6bcfec422016-05-26 00:28:4619enum SimpleEnum { SIMPLE_ENUM };
20enum EnumWithExplicitType : uint64_t { ENUM_WITH_EXPLICIT_TYPE };
21enum class ScopedEnum { SCOPED_ENUM };
jbroman6bcfec422016-05-26 00:28:4622struct SimpleStruct {};
Collin Baker89e9e072019-06-10 22:39:0523struct StructWithToString {
24 std::string ToString() const { return ""; }
25};
jbroman6bcfec422016-05-26 00:28:4626
Collin Baker89e9e072019-06-10 22:39:0527// .ToString() support on structs.
28static_assert(!internal::SupportsToString<SimpleStruct>::value,
29 "simple struct value doesn't support .ToString()");
30static_assert(!internal::SupportsToString<const SimpleStruct&>::value,
31 "simple struct const ref doesn't support .ToString()");
32static_assert(internal::SupportsToString<StructWithToString>::value,
33 "struct with .ToString() should be printable by value");
34static_assert(internal::SupportsToString<const StructWithToString&>::value,
35 "struct with .ToString() should be printable by const ref");
36
Jan Wilken Dörrief89fc2ac2021-03-05 06:51:2237// is_scoped_enum
38TEST(TemplateUtil, IsScopedEnum) {
39 static_assert(!is_scoped_enum<int>::value, "");
40 static_assert(!is_scoped_enum<SimpleEnum>::value, "");
41 static_assert(!is_scoped_enum<EnumWithExplicitType>::value, "");
42 static_assert(is_scoped_enum<ScopedEnum>::value, "");
43}
44
Jan Wilken Dörrie94bb5bc2021-03-22 18:37:4045TEST(TemplateUtil, RemoveCvRefT) {
46 static_assert(std::is_same<int, remove_cvref_t<const int>>::value, "");
47 static_assert(std::is_same<int, remove_cvref_t<const volatile int>>::value,
48 "");
49 static_assert(std::is_same<int, remove_cvref_t<int&>>::value, "");
50 static_assert(std::is_same<int, remove_cvref_t<const int&>>::value, "");
51 static_assert(std::is_same<int, remove_cvref_t<const volatile int&>>::value,
52 "");
53 static_assert(std::is_same<int, remove_cvref_t<int&&>>::value, "");
54 static_assert(
55 std::is_same<SimpleStruct, remove_cvref_t<const SimpleStruct&>>::value,
56 "");
57 static_assert(std::is_same<int*, remove_cvref_t<int*>>::value, "");
58
59 // Test references and pointers to arrays.
60 static_assert(std::is_same<int[3], remove_cvref_t<int[3]>>::value, "");
61 static_assert(std::is_same<int[3], remove_cvref_t<int(&)[3]>>::value, "");
62 static_assert(std::is_same<int(*)[3], remove_cvref_t<int(*)[3]>>::value, "");
63
64 // Test references and pointers to functions.
65 static_assert(std::is_same<void(int), remove_cvref_t<void(int)>>::value, "");
66 static_assert(std::is_same<void(int), remove_cvref_t<void (&)(int)>>::value,
67 "");
68 static_assert(
69 std::is_same<void (*)(int), remove_cvref_t<void (*)(int)>>::value, "");
70}
71
[email protected]b38d3572011-02-15 01:27:3872} // namespace
dyaroshev99a394d2017-07-22 19:10:1873
[email protected]b38d3572011-02-15 01:27:3874} // namespace base