Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit 1832c18

Browse files
committed
[Test] Refactor test code
1 parent 7c3ed56 commit 1832c18

File tree

4 files changed

+138
-57
lines changed

4 files changed

+138
-57
lines changed

Source/Tests/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33

44
set(EXE_NAME ShaderConductorTest)
55

6+
set(HEADER_FILES
7+
Common.hpp
8+
)
9+
610
set(SOURCE_FILES
7-
ShaderConductorTest.cpp
11+
Common.cpp
12+
CrossCompileTest.cpp
813
)
914

1015
set(DATA_INC_FILES
@@ -38,11 +43,12 @@ set_source_files_properties(${DATA_FILES}
3843
PROPERTIES VS_TOOL_OVERRIDE "None"
3944
)
4045

46+
source_group("Header Files" FILES ${HEADER_FILES})
4147
source_group("Source Files" FILES ${SOURCE_FILES})
4248
source_group("Data Files\\Input" FILES ${DATA_FILES})
4349
source_group("Data Files\\Input\\Inc" FILES ${DATA_INC_FILES})
4450

45-
add_executable(${EXE_NAME} ${SOURCE_FILES} ${DATA_FILES} ${DATA_INC_FILES})
51+
add_executable(${EXE_NAME} ${HEADER_FILES} ${SOURCE_FILES} ${DATA_FILES} ${DATA_INC_FILES})
4652

4753
target_compile_definitions(${EXE_NAME}
4854
PRIVATE

Source/Tests/Common.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* ShaderConductor
3+
*
4+
* Copyright (c) Microsoft Corporation. All rights reserved.
5+
* Licensed under the MIT License.
6+
*
7+
* MIT License
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
10+
* software and associated documentation files (the "Software"), to deal in the Software
11+
* without restriction, including without limitation the rights to use, copy, modify, merge,
12+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
13+
* to whom the Software is furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all copies or
16+
* substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20+
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
21+
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23+
* DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
#include "Common.hpp"
27+
28+
#include <gtest/gtest.h>
29+
30+
#include <fstream>
31+
#include <string>
32+
#include <vector>
33+
34+
namespace ShaderConductor
35+
{
36+
std::vector<uint8_t> LoadFile(const std::string& name, bool isText)
37+
{
38+
std::vector<uint8_t> ret;
39+
std::ios_base::openmode mode = std::ios_base::in;
40+
if (!isText)
41+
{
42+
mode |= std::ios_base::binary;
43+
}
44+
std::ifstream file(name, mode);
45+
if (file)
46+
{
47+
file.seekg(0, std::ios::end);
48+
ret.resize(static_cast<size_t>(file.tellg()));
49+
file.seekg(0, std::ios::beg);
50+
file.read(reinterpret_cast<char*>(ret.data()), ret.size());
51+
ret.resize(static_cast<size_t>(file.gcount()));
52+
}
53+
return ret;
54+
}
55+
56+
void CompareWithExpected(const std::vector<uint8_t>& actual, bool isText, const std::string& compareName)
57+
{
58+
std::vector<uint8_t> expected = LoadFile(TEST_DATA_DIR "Expected/" + compareName, isText);
59+
if (expected != actual)
60+
{
61+
if (!actual.empty())
62+
{
63+
std::ios_base::openmode mode = std::ios_base::out;
64+
if (!isText)
65+
{
66+
mode |= std::ios_base::binary;
67+
}
68+
std::ofstream actualFile(TEST_DATA_DIR "Result/" + compareName, mode);
69+
actualFile.write(reinterpret_cast<const char*>(actual.data()), actual.size());
70+
}
71+
}
72+
73+
EXPECT_EQ(std::string(expected.begin(), expected.end()), std::string(actual.begin(), actual.end()));
74+
}
75+
} // namespace ShaderConductor
76+
77+
int main(int argc, char** argv)
78+
{
79+
testing::InitGoogleTest(&argc, argv);
80+
81+
int retVal = RUN_ALL_TESTS();
82+
if (retVal != 0)
83+
{
84+
getchar();
85+
}
86+
87+
return retVal;
88+
}

Source/Tests/Common.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* ShaderConductor
3+
*
4+
* Copyright (c) Microsoft Corporation. All rights reserved.
5+
* Licensed under the MIT License.
6+
*
7+
* MIT License
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
10+
* software and associated documentation files (the "Software"), to deal in the Software
11+
* without restriction, including without limitation the rights to use, copy, modify, merge,
12+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
13+
* to whom the Software is furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all copies or
16+
* substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20+
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
21+
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23+
* DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
#ifndef SHADER_CONDUCTOR_TEST_COMMON_HPP
27+
#define SHADER_CONDUCTOR_TEST_COMMON_HPP
28+
29+
#pragma once
30+
31+
#include <string>
32+
#include <vector>
33+
34+
namespace ShaderConductor
35+
{
36+
std::vector<uint8_t> LoadFile(const std::string& name, bool isText);
37+
void CompareWithExpected(const std::vector<uint8_t>& actual, bool isText, const std::string& compareName);
38+
} // namespace ShaderConductor
39+
40+
#endif

Source/Tests/ShaderConductorTest.cpp renamed to Source/Tests/CrossCompileTest.cpp

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626
#include <ShaderConductor/ShaderConductor.hpp>
2727

28+
#include "Common.hpp"
29+
2830
#include <gtest/gtest.h>
2931

30-
#include <cassert>
31-
#include <fstream>
3232
#include <string>
3333
#include <tuple>
3434
#include <vector>
@@ -37,46 +37,6 @@ using namespace ShaderConductor;
3737

3838
namespace
3939
{
40-
std::vector<uint8_t> LoadFile(const std::string& name, bool isText)
41-
{
42-
std::vector<uint8_t> ret;
43-
std::ios_base::openmode mode = std::ios_base::in;
44-
if (!isText)
45-
{
46-
mode |= std::ios_base::binary;
47-
}
48-
std::ifstream file(name, mode);
49-
if (file)
50-
{
51-
file.seekg(0, std::ios::end);
52-
ret.resize(static_cast<size_t>(file.tellg()));
53-
file.seekg(0, std::ios::beg);
54-
file.read(reinterpret_cast<char*>(ret.data()), ret.size());
55-
ret.resize(static_cast<size_t>(file.gcount()));
56-
}
57-
return ret;
58-
}
59-
60-
void CompareWithExpected(const std::vector<uint8_t>& actual, bool isText, const std::string& compareName)
61-
{
62-
std::vector<uint8_t> expected = LoadFile(TEST_DATA_DIR "Expected/" + compareName, isText);
63-
if (expected != actual)
64-
{
65-
if (!actual.empty())
66-
{
67-
std::ios_base::openmode mode = std::ios_base::out;
68-
if (!isText)
69-
{
70-
mode |= std::ios_base::binary;
71-
}
72-
std::ofstream actualFile(TEST_DATA_DIR "Result/" + compareName, mode);
73-
actualFile.write(reinterpret_cast<const char*>(actual.data()), actual.size());
74-
}
75-
}
76-
77-
EXPECT_EQ(std::string(expected.begin(), expected.end()), std::string(actual.begin(), actual.end()));
78-
}
79-
8040
void HlslToAnyTest(const std::string& name, const Compiler::SourceDesc& source, const Compiler::Options& options,
8141
const std::vector<Compiler::TargetDesc>& targets, const std::vector<bool>& expectSuccessFlags)
8242
{
@@ -642,16 +602,3 @@ namespace
642602
}
643603
}
644604
} // namespace
645-
646-
int main(int argc, char** argv)
647-
{
648-
testing::InitGoogleTest(&argc, argv);
649-
650-
int retVal = RUN_ALL_TESTS();
651-
if (retVal != 0)
652-
{
653-
getchar();
654-
}
655-
656-
return retVal;
657-
}

0 commit comments

Comments
 (0)