1 | //===-- sanitizer_type_traits_test.cpp ------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file is a part of ThreadSanitizer/AddressSanitizer runtime. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | #include "sanitizer_common/sanitizer_type_traits.h" |
13 | |
14 | #include <vector> |
15 | |
16 | #include "gtest/gtest.h" |
17 | #include "sanitizer_common/sanitizer_internal_defs.h" |
18 | |
19 | namespace __sanitizer { |
20 | |
21 | TEST(SanitizerCommon, IsSame) { |
22 | ASSERT_TRUE((is_same<unsigned, unsigned>::value)); |
23 | ASSERT_TRUE((is_same<uptr, uptr>::value)); |
24 | ASSERT_TRUE((is_same<sptr, sptr>::value)); |
25 | ASSERT_TRUE((is_same<const uptr, const uptr>::value)); |
26 | |
27 | ASSERT_FALSE((is_same<unsigned, signed>::value)); |
28 | ASSERT_FALSE((is_same<uptr, sptr>::value)); |
29 | ASSERT_FALSE((is_same<uptr, const uptr>::value)); |
30 | } |
31 | |
32 | TEST(SanitizerCommon, Conditional) { |
33 | ASSERT_TRUE((is_same<int, conditional<true, int, double>::type>::value)); |
34 | ASSERT_TRUE((is_same<double, conditional<false, int, double>::type>::value)); |
35 | } |
36 | |
37 | TEST(SanitizerCommon, RemoveReference) { |
38 | ASSERT_TRUE((is_same<int, remove_reference<int>::type>::value)); |
39 | ASSERT_TRUE((is_same<const int, remove_reference<const int>::type>::value)); |
40 | ASSERT_TRUE((is_same<int, remove_reference<int&>::type>::value)); |
41 | ASSERT_TRUE((is_same<const int, remove_reference<const int&>::type>::value)); |
42 | ASSERT_TRUE((is_same<int, remove_reference<int&&>::type>::value)); |
43 | } |
44 | |
45 | TEST(SanitizerCommon, Move) { |
46 | std::vector<int> v = {1, 2, 3}; |
47 | auto v2 = __sanitizer::move(v); |
48 | EXPECT_EQ(3u, v2.size()); |
49 | EXPECT_TRUE(v.empty()); |
50 | } |
51 | |
52 | TEST(SanitizerCommon, Forward) { |
53 | std::vector<int> v = {1, 2, 3}; |
54 | auto v2 = __sanitizer::forward<std::vector<int>>(v); |
55 | EXPECT_EQ(3u, v2.size()); |
56 | EXPECT_TRUE(v.empty()); |
57 | } |
58 | |
59 | TEST(SanitizerCommon, ForwardConst) { |
60 | const std::vector<int> v = {1, 2, 3}; |
61 | auto v2 = __sanitizer::forward<const std::vector<int>&>(v); |
62 | EXPECT_EQ(3u, v2.size()); |
63 | EXPECT_EQ(3u, v.size()); |
64 | } |
65 | |
66 | struct TestStruct { |
67 | int a; |
68 | float b; |
69 | }; |
70 | |
71 | TEST(SanitizerCommon, IsTriviallyDestructible) { |
72 | ASSERT_TRUE((is_trivially_destructible<int>::value)); |
73 | ASSERT_TRUE((is_trivially_destructible<TestStruct>::value)); |
74 | ASSERT_FALSE((is_trivially_destructible<std::vector<int>>::value)); |
75 | } |
76 | |
77 | TEST(SanitizerCommon, IsTriviallyCopyable) { |
78 | ASSERT_TRUE((is_trivially_copyable<int>::value)); |
79 | ASSERT_TRUE((is_trivially_copyable<TestStruct>::value)); |
80 | ASSERT_FALSE((is_trivially_copyable<std::vector<int>>::value)); |
81 | } |
82 | |
83 | } // namespace __sanitizer |