Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Adrian Taylor | bbfcdab | 2021-11-17 03:22:02 | [diff] [blame] | 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 "base/strings/string_piece_rust.h" |
Adrian Taylor | 4a1138b | 2023-03-20 15:47:22 | [diff] [blame] | 6 | #include "base/rust_buildflags.h" |
Adrian Taylor | bbfcdab | 2021-11-17 03:22:02 | [diff] [blame] | 7 | |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | |
Adrian Taylor | 4a1138b | 2023-03-20 15:47:22 | [diff] [blame] | 10 | #if BUILDFLAG(BUILD_RUST_BASE_CONVERSIONS) |
danakj | 6f4ab5a | 2022-11-17 18:42:51 | [diff] [blame] | 11 | |
Adrian Taylor | bbfcdab | 2021-11-17 03:22:02 | [diff] [blame] | 12 | namespace base { |
| 13 | namespace { |
| 14 | |
| 15 | TEST(BaseStringPieceRustTest, StrRoundTrip) { |
| 16 | std::string data = "hello"; |
| 17 | StringPiece data_piece(data); |
| 18 | rust::Str rust_str = StringPieceToRustStrUTF8(data_piece); |
| 19 | EXPECT_EQ(5ul, rust_str.length()); |
| 20 | StringPiece data_piece2 = RustStrToStringPiece(rust_str); |
| 21 | EXPECT_EQ(data_piece, data_piece2); |
| 22 | } |
| 23 | |
| 24 | TEST(BaseStringPieceRustTest, StrToSlice) { |
| 25 | std::string data = "hello"; |
| 26 | StringPiece data_piece(data); |
| 27 | rust::Slice<const uint8_t> rust_slice = StringPieceToRustSlice(data_piece); |
| 28 | EXPECT_EQ(5ul, rust_slice.length()); |
| 29 | EXPECT_EQ('e', rust_slice[1]); |
| 30 | } |
| 31 | |
| 32 | } // namespace |
| 33 | } // namespace base |
danakj | 6f4ab5a | 2022-11-17 18:42:51 | [diff] [blame] | 34 | |
Adrian Taylor | 4a1138b | 2023-03-20 15:47:22 | [diff] [blame] | 35 | #endif // BUILDFLAG(BUILD_RUST_BASE_CONVERSIONS) |