blob: b9c674c00d9f4e147ddfda99944ec0930728e5e8 [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"
danakj6f4ab5a2022-11-17 18:42:516#include "build/rust/rust_buildflags.h"
Adrian Taylorbbfcdab2021-11-17 03:22:027
8#include "testing/gtest/include/gtest/gtest.h"
9
danakj6f4ab5a2022-11-17 18:42:5110#if BUILDFLAG(TOOLCHAIN_HAS_RUST)
11
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
35#endif // BUILDFLAG(TOOLCHAIN_HAS_RUST)