File size: 3,107 Bytes
5f923cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2026 The ODML Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "runtime/util/file_data_stream.h"

#include <cstdint>
#include <filesystem>  // NOLINT(build/c++17)
#include <memory>
#include <string>

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/status/status.h"  // from @com_google_absl
#include "absl/status/statusor.h"  // from @com_google_absl
#include "runtime/util/test_utils.h"  // IWYU pragma: keep

namespace litert::lm {
namespace {

using ::testing::HasSubstr;

std::string GetTestModelPath() {
  const auto model_path =
      std::filesystem::path(::testing::SrcDir()) /
      "litert_lm/runtime/testdata/test_lm.litertlm";
  return model_path.string();
}

TEST(FileDataStreamTest, CreateSuccessfully) {
  auto stream_or = FileDataStream::Create(GetTestModelPath());
  ASSERT_OK(stream_or);
  EXPECT_NE(*stream_or, nullptr);
}

TEST(FileDataStreamTest, CreateFailsForInvalidPath) {
  auto stream_or = FileDataStream::Create("invalid/path/to/file.ext");
  EXPECT_EQ(stream_or.status().code(), absl::StatusCode::kNotFound);
  EXPECT_THAT(stream_or.status().message(), HasSubstr("Failed to open file"));
}

TEST(FileDataStreamTest, ReadAndPreserve) {
  ASSERT_OK_AND_ASSIGN(auto stream, FileDataStream::Create(GetTestModelPath()));

  // The very beginning of the test model is a header, starting with "LITERTLM"
  // magic number
  char buffer[8];
  ASSERT_OK(stream->ReadAndPreserve(buffer, 0, 8));

  std::string magic_number(buffer, 8);
  EXPECT_EQ(magic_number, "LITERTLM");
}

TEST(FileDataStreamTest, ReadAndDiscard) {
  ASSERT_OK_AND_ASSIGN(auto stream, FileDataStream::Create(GetTestModelPath()));

  char buffer[8];
  // Since it's a file stream, ReadAndDiscard just acts like ReadAndPreserve
  ASSERT_OK(stream->ReadAndDiscard(buffer, 0, 8));

  std::string magic_number(buffer, 8);
  EXPECT_EQ(magic_number, "LITERTLM");
}

TEST(FileDataStreamTest, ReadOutOfBounds) {
  ASSERT_OK_AND_ASSIGN(auto stream, FileDataStream::Create(GetTestModelPath()));

  char buffer[8];
  // Attempt to read far past the end of the file.
  // 100 GB is well out of bounds.
  uint64_t huge_offset = 100ULL * 1024 * 1024 * 1024;
  absl::Status status = stream->ReadAndPreserve(buffer, huge_offset, 8);
  EXPECT_EQ(status.code(), absl::StatusCode::kOutOfRange);
  EXPECT_THAT(status.message(), HasSubstr("Read past end of file"));
}

TEST(FileDataStreamTest, DiscardDoesNotFail) {
  ASSERT_OK_AND_ASSIGN(auto stream, FileDataStream::Create(GetTestModelPath()));
  ASSERT_OK(stream->Discard(0, 100));
}

}  // namespace
}  // namespace litert::lm