xref: /aosp_15_r20/external/cronet/url/url_test_utils.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 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 #ifndef URL_URL_TEST_UTILS_H_
6 #define URL_URL_TEST_UTILS_H_
7 
8 // Convenience functions for string conversions.
9 // These are mostly intended for use in unit tests.
10 
11 #include <string>
12 
13 #include "base/strings/utf_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "url/url_canon_internal.h"
16 
17 namespace url {
18 
19 namespace test_utils {
20 
21 // Converts a UTF-16 string from native wchar_t format to char16 by
22 // truncating the high 32 bits. This is different than the conversion function
23 // in base bacause it passes invalid UTF-16 characters which is important for
24 // test purposes. As a result, this is not meant to handle true UTF-32 encoded
25 // strings.
TruncateWStringToUTF16(const wchar_t * src)26 inline std::u16string TruncateWStringToUTF16(const wchar_t* src) {
27   std::u16string str;
28   int length = static_cast<int>(wcslen(src));
29   for (int i = 0; i < length; ++i) {
30     str.push_back(static_cast<char16_t>(src[i]));
31   }
32   return str;
33 }
34 
35 }  // namespace test_utils
36 
37 }  // namespace url
38 
39 #endif  // URL_URL_TEST_UTILS_H_
40