1 // Copyright 2021 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 "base/strings/string_piece_rust.h" 6 #include "base/rust_buildflags.h" 7 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 #if BUILDFLAG(BUILD_RUST_BASE_CONVERSIONS) 11 12 namespace base { 13 namespace { 14 TEST(BaseStringPieceRustTest,StrRoundTrip)15TEST(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 TEST(BaseStringPieceRustTest,StrToSlice)24TEST(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 34 35 #endif // BUILDFLAG(BUILD_RUST_BASE_CONVERSIONS) 36