xref: /aosp_15_r20/external/federated-compute/fcp/base/platform_test.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2017 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "fcp/base/platform.h"
18 
19 #include "gtest/gtest.h"
20 #include "absl/strings/cord.h"
21 #include "fcp/base/base_name.h"
22 #include "fcp/base/monitoring.h"
23 #include "fcp/testing/testing.h"
24 
25 namespace fcp {
26 
27 namespace {
28 
TEST(PlatformTest,ConcatPath)29 TEST(PlatformTest, ConcatPath) {
30   auto combined = ConcatPath("first", "second");
31 #if _WIN32
32   ASSERT_EQ(combined, "first\\second");
33 #else
34   ASSERT_EQ(combined, "first/second");
35 #endif
36 }
37 
TEST(PlatformTest,StripTrailingPathSeparator)38 TEST(PlatformTest, StripTrailingPathSeparator) {
39 #if _WIN32
40   ASSERT_EQ(StripTrailingPathSeparator("path\\"), "path");
41   ASSERT_EQ(StripTrailingPathSeparator("dir/path"), "dir/path");
42 #else
43   ASSERT_EQ(StripTrailingPathSeparator("path/"), "path");
44   ASSERT_EQ(StripTrailingPathSeparator("dir/path"), "dir/path");
45 #endif
46 }
47 
TEST(PlatformTest,ReadWriteString)48 TEST(PlatformTest, ReadWriteString) {
49   auto file = TemporaryTestFile(".dat");
50   ASSERT_EQ(WriteStringToFile(file, "Ein Text").code(), OK);
51   auto status_or_string = ReadFileToString(file);
52   ASSERT_TRUE(status_or_string.ok()) << status_or_string.status();
53   ASSERT_EQ(status_or_string.value(), "Ein Text");
54 }
55 
TEST(PlatformTest,ReadWriteCord)56 TEST(PlatformTest, ReadWriteCord) {
57   auto file = TemporaryTestFile(".dat");
58   // Make cord with two chunks.
59   absl::Cord content("Ein");
60   content.Append(" Text");
61   ASSERT_EQ(WriteCordToFile(file, content).code(), OK);
62   auto status_or_cord = ReadFileToCord(file);
63   ASSERT_TRUE(status_or_cord.ok()) << status_or_cord.status();
64   ASSERT_EQ(status_or_cord.value(), "Ein Text");
65 }
66 
TEST(PlatformTest,ReadStringFails)67 TEST(PlatformTest, ReadStringFails) {
68   ASSERT_FALSE(ReadFileToString("foobarbaz").ok());
69 }
70 
TEST(PlatformTest,ReadCordFails)71 TEST(PlatformTest, ReadCordFails) {
72   ASSERT_FALSE(ReadFileToCord("foobarbaz").ok());
73 }
74 
TEST(PlatformTest,BaseName)75 TEST(PlatformTest, BaseName) {
76   ASSERT_EQ(BaseName(ConcatPath("foo", "bar.x")), "bar.x");
77 }
78 
TEST(PlatformTest,FileExists)79 TEST(PlatformTest, FileExists) {
80   auto file = TemporaryTestFile(".dat");
81   ASSERT_EQ(WriteStringToFile(file, "Ein Text").code(), OK);
82   ASSERT_TRUE(FileExists(file));
83 }
84 
TEST(PlatformTest,FileExistsNot)85 TEST(PlatformTest, FileExistsNot) { ASSERT_FALSE(FileExists("foobarbaz")); }
86 
87 }  // namespace
88 
89 }  // namespace fcp
90