blob: ed8ebc0e00096432a66962c60a62f40c06bd3571 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2021 The Chromium Authors
Adrian Taylorbbfcdab2021-11-17 03:22:022// 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 Taylor4a1138b2023-03-20 15:47:226#include "base/rust_buildflags.h"
Adrian Taylorbbfcdab2021-11-17 03:22:027
8#include "testing/gtest/include/gtest/gtest.h"
9
Adrian Taylor4a1138b2023-03-20 15:47:2210#if BUILDFLAG(BUILD_RUST_BASE_CONVERSIONS)
danakj6f4ab5a2022-11-17 18:42:5111
Adrian Taylorbbfcdab2021-11-17 03:22:0212namespace base {
13namespace {
14
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
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
danakj6f4ab5a2022-11-17 18:42:5134
Adrian Taylor4a1138b2023-03-20 15:47:2235#endif // BUILDFLAG(BUILD_RUST_BASE_CONVERSIONS)