Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/shaderc
Path: blob/main/libshaderc_util/src/io_shaderc_test.cc
1560 views
1
// Copyright 2015 The Shaderc Authors. All rights reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "libshaderc_util/io_shaderc.h"
16
17
#include <gmock/gmock.h>
18
19
#include <fstream>
20
21
namespace {
22
23
using shaderc_util::GetBaseFileName;
24
using shaderc_util::GetOutputStream;
25
using shaderc_util::IsAbsolutePath;
26
using shaderc_util::ReadFile;
27
using shaderc_util::WriteFile;
28
using testing::Eq;
29
using testing::HasSubstr;
30
31
std::string ToString(const std::vector<char>& v) {
32
return std::string(v.data(), v.size());
33
}
34
35
class ReadFileTest : public testing::Test {
36
protected:
37
// A vector to pass to ReadFile.
38
std::vector<char> read_data;
39
};
40
41
TEST(IsAbsolutePathTest, Linux) {
42
EXPECT_FALSE(IsAbsolutePath(""));
43
EXPECT_TRUE(IsAbsolutePath("/"));
44
EXPECT_FALSE(IsAbsolutePath("."));
45
EXPECT_FALSE(IsAbsolutePath(".."));
46
EXPECT_TRUE(IsAbsolutePath("/bin/echo"));
47
EXPECT_TRUE(IsAbsolutePath("//etc/shadow"));
48
EXPECT_TRUE(IsAbsolutePath("/../../../lib"));
49
EXPECT_FALSE(IsAbsolutePath("./something"));
50
EXPECT_FALSE(IsAbsolutePath("input"));
51
EXPECT_FALSE(IsAbsolutePath("../test"));
52
EXPECT_FALSE(IsAbsolutePath(" /abc"));
53
EXPECT_TRUE(IsAbsolutePath("/abc def/ttt"));
54
}
55
56
TEST(IsAbsolutePathTest, Windows) {
57
EXPECT_TRUE(IsAbsolutePath(R"(\\Server1000\superuser\file)"));
58
EXPECT_TRUE(IsAbsolutePath(R"(\\zzzz 1000\user with space\file with space)"));
59
EXPECT_TRUE(
60
IsAbsolutePath(R"(C:\Program Files (x86)\Windows Folder\shader.glsl)"));
61
EXPECT_FALSE(IsAbsolutePath(R"(third_party\gmock)"));
62
EXPECT_FALSE(IsAbsolutePath(R"(C:..\File.txt)"));
63
}
64
65
TEST(GetBaseFileName, Linux) {
66
EXPECT_EQ("", GetBaseFileName(""));
67
EXPECT_EQ("", GetBaseFileName("/"));
68
EXPECT_EQ("", GetBaseFileName("."));
69
EXPECT_EQ("", GetBaseFileName(".."));
70
EXPECT_EQ("echo", GetBaseFileName("/bin/echo"));
71
EXPECT_EQ("shadow", GetBaseFileName("//etc/shadow"));
72
EXPECT_EQ("lib", GetBaseFileName("/../../../lib"));
73
EXPECT_EQ("something", GetBaseFileName("./something"));
74
EXPECT_EQ("input", GetBaseFileName("input"));
75
EXPECT_EQ("test", GetBaseFileName("../test"));
76
EXPECT_EQ("abc", GetBaseFileName(" /abc"));
77
EXPECT_EQ("ttt", GetBaseFileName("/abc def/ttt"));
78
}
79
80
TEST(GetBaseFileName, Windows) {
81
EXPECT_EQ("file", GetBaseFileName(R"(\\Server1000\superuser\file)"));
82
EXPECT_EQ("file with space",
83
GetBaseFileName(R"(\\zzzz 1000\user with space\file with space)"));
84
EXPECT_EQ(
85
"shader.glsl",
86
GetBaseFileName(R"(C:\Program Files (x86)\Windows Folder\shader.glsl)"));
87
EXPECT_EQ("gmock", GetBaseFileName(R"(third_party\gmock)"));
88
EXPECT_EQ("File.txt", GetBaseFileName(R"(C:..\File.txt)"));
89
}
90
91
TEST_F(ReadFileTest, CorrectContent) {
92
ASSERT_TRUE(ReadFile("include_file.1", &read_data));
93
EXPECT_EQ("The quick brown fox jumps over a lazy dog.", ToString(read_data));
94
}
95
96
TEST_F(ReadFileTest, EmptyContent) {
97
ASSERT_TRUE(ReadFile("dir/subdir/include_file.2", &read_data));
98
EXPECT_TRUE(read_data.empty());
99
}
100
101
TEST_F(ReadFileTest, FileNotFound) {
102
EXPECT_FALSE(ReadFile("garbage garbage vjoiarhiupo hrfewi", &read_data));
103
}
104
105
TEST_F(ReadFileTest, EmptyFilename) { EXPECT_FALSE(ReadFile("", &read_data)); }
106
107
TEST(WriteFiletest, BadStream) {
108
std::ofstream fstream;
109
std::ostringstream err;
110
std::ostream* output_stream = GetOutputStream(
111
"/this/should/not/be/writable/asdfasdfasdfasdf", &fstream, &err);
112
EXPECT_EQ(nullptr, output_stream);
113
EXPECT_TRUE(fstream.fail());
114
EXPECT_EQ(nullptr, output_stream);
115
EXPECT_THAT(err.str(), HasSubstr("cannot open output file"));
116
}
117
118
TEST(WriteFileTest, Roundtrip) {
119
const std::string content = "random content 12345";
120
const std::string filename = "WriteFileTestOutput.tmp";
121
std::ofstream fstream;
122
std::ostringstream err;
123
std::ostream* output_stream = GetOutputStream(filename, &fstream, &err);
124
ASSERT_EQ(output_stream, &fstream);
125
EXPECT_THAT(err.str(), Eq(""));
126
ASSERT_TRUE(WriteFile(output_stream, content));
127
std::vector<char> read_data;
128
ASSERT_TRUE(ReadFile(filename, &read_data));
129
EXPECT_EQ(content, ToString(read_data));
130
}
131
132
TEST(OutputStreamTest, Stdout) {
133
std::ofstream fstream;
134
std::ostringstream err;
135
std::ostream* output_stream = GetOutputStream("-", &fstream, &err);
136
EXPECT_EQ(&std::cout, output_stream);
137
EXPECT_THAT(err.str(), Eq(""));
138
}
139
} // anonymous namespace
140
141