1+
12// Copyright 2024 Google LLC.
23//
34// Licensed under the Apache License, Version 2.0 (the "License");
1415
1516#include " litert/core/filesystem.h"
1617
18+ #include < fstream>
19+ #include < ios>
20+ #include < string>
21+
22+ #include < gmock/gmock.h>
1723#include < gtest/gtest.h>
1824#include " absl/strings/str_format.h" // from @com_google_absl
1925#include " absl/strings/string_view.h" // from @com_google_absl
2026
2127namespace litert ::internal {
2228namespace {
2329
30+ using ::testing::UnorderedElementsAre;
31+
2432static constexpr absl::string_view kPrefix = " a/prefix" ;
2533static constexpr absl::string_view kInfix = " an/infix" ;
2634static constexpr absl::string_view kSuffix = " suffix.ext" ;
2735static constexpr absl::string_view kPath = " a/prefix.ext" ;
2836static constexpr absl::string_view kStem = " prefix" ;
2937
38+
3039TEST (FilesystemTest, JoinTwo) {
3140 const auto path = Join ({kPrefix , kSuffix });
3241 EXPECT_EQ (path, absl::StrFormat (" %s/%s" , kPrefix , kSuffix ));
@@ -37,10 +46,99 @@ TEST(FilesystemTest, JoinMany) {
3746 EXPECT_EQ (path, absl::StrFormat (" %s/%s/%s" , kPrefix , kInfix , kSuffix ));
3847}
3948
40- TEST (FilesystemTest, Stem){
49+ TEST (FilesystemTest, Stem) {
4150 const auto stem = Stem (kPath );
4251 EXPECT_EQ (stem, kStem );
4352}
4453
54+ void WriteFile (absl::string_view path, absl::string_view content) {
55+ std::ofstream ofs ((std::string (path)), std::ios::binary);
56+ ofs << content;
57+ }
58+
59+ TEST (FilesystemTest, MkDirExistsIsDir) {
60+ const std::string dir = Join ({::testing::TempDir (), " test_dir" });
61+ EXPECT_FALSE (Exists (dir));
62+ auto status = MkDir (dir);
63+ ASSERT_TRUE (status);
64+ EXPECT_TRUE (Exists (dir));
65+ EXPECT_TRUE (IsDir (dir));
66+ EXPECT_FALSE (IsDir (Join ({dir, " foo" })));
67+ }
68+
69+ TEST (FilesystemTest, TouchExists) {
70+ const std::string file = Join ({::testing::TempDir (), " test_file" });
71+ EXPECT_FALSE (Exists (file));
72+ Touch (file);
73+ EXPECT_TRUE (Exists (file));
74+ EXPECT_FALSE (IsDir (file));
75+ }
76+
77+ TEST (FilesystemTest, Size) {
78+ const std::string file = Join ({::testing::TempDir (), " test_file_size" });
79+ WriteFile (file, " 1234" );
80+ auto size = Size (file);
81+ ASSERT_TRUE (size);
82+ EXPECT_EQ (*size, 4 );
83+ }
84+
85+ TEST (FilesystemTest, LoadBinaryFile) {
86+ const std::string file = Join ({::testing::TempDir (), " test_file_load" });
87+ const std::string content = " 12345" ;
88+ WriteFile (file, content);
89+ auto buffer = LoadBinaryFile (file);
90+ ASSERT_TRUE (buffer);
91+ EXPECT_EQ (buffer->Size (), 5 );
92+ EXPECT_EQ (absl::string_view (buffer->StrData (), buffer->Size ()), content);
93+ }
94+
95+ TEST (FilesystemTest, ListDir) {
96+ const std::string dir = Join ({::testing::TempDir (), " list_dir_test" });
97+ auto status = MkDir (dir);
98+ ASSERT_TRUE (status);
99+ const std::string file1 = Join ({dir, " file1.txt" });
100+ const std::string file2 = Join ({dir, " file2.txt" });
101+ Touch (file1);
102+ Touch (file2);
103+ auto list = ListDir (dir);
104+ ASSERT_TRUE (list);
105+ EXPECT_THAT (*list, UnorderedElementsAre (file1, file2));
106+ }
107+
108+ TEST (FilesystemTest, Filename) {
109+ const std::string dir = Join ({::testing::TempDir (), " filename_test" });
110+ auto status = MkDir (dir);
111+ ASSERT_TRUE (status);
112+ const std::string file = Join ({dir, " file1.txt" });
113+ Touch (file);
114+ auto filename = Filename (file);
115+ ASSERT_TRUE (filename);
116+ EXPECT_EQ (*filename, " file1.txt" );
117+ }
118+
119+ TEST (FilesystemTest, Parent) {
120+ const std::string dir = Join ({::testing::TempDir (), " parent_test" });
121+ auto status = MkDir (dir);
122+ ASSERT_TRUE (status);
123+ const std::string file = Join ({dir, " file1.txt" });
124+ Touch (file);
125+ auto parent = Parent (file);
126+ ASSERT_TRUE (parent);
127+ EXPECT_EQ (*parent, dir);
128+ }
129+
130+ TEST (FilesystemTest, RmDir) {
131+ const std::string dir = Join ({::testing::TempDir (), " rm_dir_test" });
132+ auto status = MkDir (dir);
133+ ASSERT_TRUE (status);
134+ const std::string file = Join ({dir, " file1.txt" });
135+ Touch (file);
136+ EXPECT_TRUE (Exists (dir));
137+ auto rm_status = RmDir (dir);
138+ ASSERT_TRUE (rm_status);
139+ EXPECT_FALSE (Exists (dir));
140+ }
141+
45142} // namespace
46143} // namespace litert::internal
144+
0 commit comments