1 // Copyright 2018 The Chromium Authors
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 <algorithm>
6 #include <memory>
7 #include <string>
8
9 #include "base/debug/alias.h"
10 #include "base/ranges/algorithm.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
TEST(DebugAlias,Test)13 TEST(DebugAlias, Test) {
14 constexpr char kTestString[] = "string contents";
15 constexpr auto kTestStringLength = std::string_view(kTestString).size();
16 std::unique_ptr<std::string> input =
17 std::make_unique<std::string>(kTestString);
18
19 // Verify the contents get copied + the new local variable has the right type
20 // when the array size given is exactly `input->size() + 1`.
21 DEBUG_ALIAS_FOR_CSTR(copy1, input->c_str(),
22 kTestStringLength + 1 /* == input->size() + 1 */);
23 static_assert(std::is_same_v<decltype(copy1), char[kTestStringLength + 1]>);
24 EXPECT_TRUE(
25 std::equal(std::begin(kTestString), std::end(kTestString), copy1));
26
27 // Verify that the copy is properly null-terminated even when it is smaller
28 // than the input string.
29 DEBUG_ALIAS_FOR_CSTR(copy2, input->c_str(), 3 /* < input->size() */);
30 static_assert(std::is_same_v<decltype(copy2), char[3]>);
31 EXPECT_TRUE(std::equal(std::begin(copy2), std::end(copy2), "st"));
32
33 // Verify that the copy is properly null-terminated even when it is larger
34 // than the input string.
35 DEBUG_ALIAS_FOR_CSTR(copy3, input->c_str(), 100 /* > input->size() + 1 */);
36 static_assert(std::is_same_v<decltype(copy3), char[100]>);
37 EXPECT_TRUE(
38 std::equal(std::begin(kTestString), std::end(kTestString), copy3));
39 }
40
TEST(DebugAlias,U16String)41 TEST(DebugAlias, U16String) {
42 constexpr char16_t kTestString[] = u"H͟e͟l͟l͟o͟ ͟w͟o͟r͟l͟d͟!͟";
43 constexpr auto kTestStringLength = std::u16string_view(kTestString).size();
44 std::u16string input = kTestString;
45
46 // Verify the contents get copied + the new local variable has the right type
47 // when the array size given is exactly `input->size() + 1`.
48 DEBUG_ALIAS_FOR_U16CSTR(aliased_copy, input.c_str(), kTestStringLength + 1);
49 static_assert(
50 std::is_same_v<decltype(aliased_copy), char16_t[kTestStringLength + 1]>);
51 EXPECT_TRUE(
52 std::equal(std::begin(kTestString), std::end(kTestString), aliased_copy));
53
54 // Verify that the copy is properly null-terminated even when it is larger
55 // than the input string.
56 DEBUG_ALIAS_FOR_U16CSTR(aliased_copy2, input.c_str(), kTestStringLength + 1);
57 static_assert(
58 std::is_same_v<decltype(aliased_copy2), char16_t[kTestStringLength + 1]>);
59 EXPECT_TRUE(std::equal(std::begin(kTestString), std::end(kTestString),
60 aliased_copy2));
61 }
62
TEST(DebugAlias,U16StringPartialCopy)63 TEST(DebugAlias, U16StringPartialCopy) {
64 std::u16string input = u"Hello world!";
65 DEBUG_ALIAS_FOR_U16CSTR(aliased_copy, input.c_str(), 5);
66 // Make sure we don't write past the specified number of characters. We
67 // subtract 1 to account for the null terminator.
68 EXPECT_EQ(input.substr(0, 4), aliased_copy);
69 }
70