Skip to content

Commit bde0072

Browse files
committed
Add a flag and FileReader in a test
1 parent 39938c4 commit bde0072

File tree

4 files changed

+69
-30
lines changed

4 files changed

+69
-30
lines changed

src_test/TestHelper.cpp

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,48 @@
99
#include <regex>
1010
#include <string>
1111

12+
namespace
13+
{
14+
class DefaultTestFileReader : public TestFileReader
15+
{
16+
public:
17+
std::vector<uint8_t> Read(const char* path) override
18+
{
19+
std::vector<uint8_t> ret;
20+
21+
#ifdef _WIN32
22+
FILE* fp = nullptr;
23+
fopen_s(&fp, path, "rb");
24+
25+
#else
26+
FILE* fp = fopen(path, "rb");
27+
#endif
28+
29+
if (fp == nullptr)
30+
{
31+
std::cout << "Not found : " << path << std::endl;
32+
return ret;
33+
}
34+
35+
fseek(fp, 0, SEEK_END);
36+
auto size = ftell(fp);
37+
fseek(fp, 0, SEEK_SET);
38+
39+
ret.resize(size);
40+
fread(ret.data(), 1, size, fp);
41+
fclose(fp);
42+
43+
return ret;
44+
}
45+
};
46+
} // namespace
47+
1248
struct InternalTestHelper
1349
{
1450
std::string Root;
1551
bool IsCaptureRequired = false;
1652
std::map<std::string, std::function<void(LLGI::DeviceType)>> tests;
53+
std::shared_ptr<TestFileReader> FileReader;
1754
};
1855

1956
std::shared_ptr<InternalTestHelper> TestHelper::Get()
@@ -66,6 +103,18 @@ ParsedArgs TestHelper::ParseArg(int argc, char* argv[])
66103
return args;
67104
}
68105

106+
std::shared_ptr<TestFileReader> TestHelper::GetFileReader()
107+
{
108+
auto helper = Get();
109+
if (helper->FileReader == nullptr)
110+
{
111+
helper->FileReader = std::make_shared<DefaultTestFileReader>();
112+
}
113+
return helper->FileReader;
114+
}
115+
116+
void TestHelper::SetFileReader(std::shared_ptr<TestFileReader> fileReader) { Get()->FileReader = fileReader; }
117+
69118
std::vector<uint8_t> TestHelper::CreateDummyTextureData(LLGI::Vec2I size, LLGI::TextureFormatType format)
70119
{
71120
std::vector<uint8_t> ret;
@@ -149,34 +198,7 @@ std::vector<uint8_t> TestHelper::LoadData(const char* path)
149198
return LoadDataWithoutRoot(path_.c_str());
150199
}
151200

152-
std::vector<uint8_t> TestHelper::LoadDataWithoutRoot(const char* path)
153-
{
154-
std::vector<uint8_t> ret;
155-
156-
#ifdef _WIN32
157-
FILE* fp = nullptr;
158-
fopen_s(&fp, path, "rb");
159-
160-
#else
161-
FILE* fp = fopen(path, "rb");
162-
#endif
163-
164-
if (fp == nullptr)
165-
{
166-
std::cout << "Not found : " << path << std::endl;
167-
return ret;
168-
}
169-
170-
fseek(fp, 0, SEEK_END);
171-
auto size = ftell(fp);
172-
fseek(fp, 0, SEEK_SET);
173-
174-
ret.resize(size);
175-
fread(ret.data(), 1, size, fp);
176-
fclose(fp);
177-
178-
return ret;
179-
}
201+
std::vector<uint8_t> TestHelper::LoadDataWithoutRoot(const char* path) { return GetFileReader()->Read(path); }
180202

181203
void TestHelper::SetRoot(const char* root)
182204
{

src_test/TestHelper.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@ struct ParsedArgs
1616
std::string Filter;
1717
};
1818

19+
class TestFileReader
20+
{
21+
public:
22+
virtual ~TestFileReader() = default;
23+
virtual std::vector<uint8_t> Read(const char* path) = 0;
24+
};
25+
1926
class TestHelper
2027
{
2128
private:
2229
static std::shared_ptr<InternalTestHelper> Get();
30+
static std::shared_ptr<TestFileReader> GetFileReader();
2331

2432
public:
33+
static void SetFileReader(std::shared_ptr<TestFileReader> fileReader);
34+
2535
static ParsedArgs ParseArg(int argc, char* argv[]);
2636

2737
static std::vector<uint8_t> CreateDummyTextureData(LLGI::Vec2I size, LLGI::TextureFormatType format);

src_test/capture.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ void test_capture(LLGI::DeviceType deviceType, LLGI::Vec2I windowSize)
188188
pips.clear();
189189
}
190190

191-
#if defined(__linux__) || defined(__APPLE__) || defined(WIN32)
191+
#ifndef _DISABLE_CAPTURE_TEST
192+
193+
#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
192194

193195
TestRegister Capture_Size1279("Capture.Size1279", [](LLGI::DeviceType device) -> void { test_capture(device, LLGI::Vec2I(1279, 719)); });
194196

@@ -199,3 +201,5 @@ TestRegister Capture_Size1280("Capture.Size1280", [](LLGI::DeviceType device) ->
199201
TestRegister Capture_Texture("Capture.Texture", [](LLGI::DeviceType device) -> void { test_capture_texture(device); });
200202

201203
#endif
204+
205+
#endif

src_test/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifndef _DISABLE_MAIN
12

23
#include "TestHelper.h"
34
#include "test.h"
@@ -13,7 +14,7 @@
1314

1415
#endif
1516

16-
#if defined(__linux__) || defined(__APPLE__) || defined(WIN32)
17+
#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
1718

1819
int main(int argc, char* argv[])
1920
{
@@ -67,3 +68,5 @@ int main(int argc, char* argv[])
6768
return 0;
6869
}
6970
#endif
71+
72+
#endif

0 commit comments

Comments
 (0)