xref: /aosp_15_r20/external/libchrome/base/debug/alias_unittest.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1 // Copyright 2018 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 <memory>
6 #include <string>
7 
8 #include "base/debug/alias.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
TEST(DebugAlias,Test)11 TEST(DebugAlias, Test) {
12   std::unique_ptr<std::string> input =
13       std::make_unique<std::string>("string contents");
14 
15   // Verify the contents get copied + the new local variable has the right type.
16   DEBUG_ALIAS_FOR_CSTR(copy1, input->c_str(), 100 /* > input->size() */);
17   static_assert(sizeof(copy1) == 100,
18                 "Verification that copy1 has expected size");
19   EXPECT_STREQ("string contents", copy1);
20 
21   // Verify that the copy is properly null-terminated even when it is smaller
22   // than the input string.
23   DEBUG_ALIAS_FOR_CSTR(copy2, input->c_str(), 3 /* < input->size() */);
24   static_assert(sizeof(copy2) == 3,
25                 "Verification that copy2 has expected size");
26   EXPECT_STREQ("st", copy2);
27   EXPECT_EQ('\0', copy2[2]);
28 }
29