Blender V5.3
stl_exporter_tests.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
7#include "BKE_appdir.hh"
8
9#include "BLI_fileops.hh"
10#include "BLI_string.hh"
11
12#include "DEG_depsgraph.hh"
13
14#include "MEM_guardedalloc.h"
15
16#include "IO_stl.hh"
17#include "stl_export.hh"
18
19namespace blender::io::stl {
20
21/* Set this true to keep comparison-failing test output in temp file directory. */
22constexpr bool save_failing_test_output = false;
23
24static std::string read_temp_file_in_string(const std::string &file_path)
25{
26 std::string res;
27 size_t buffer_len;
28 char *buffer = BLI_file_read_text_as_mem(file_path.c_str(), 0, &buffer_len);
29 if (buffer != nullptr) {
30 res.assign(buffer, buffer_len);
31 MEM_delete(buffer);
32 }
33 return res;
34}
35
37 public:
38 bool load_file_and_depsgraph(const std::string &filepath)
39 {
40 if (!blendfile_load(filepath.c_str())) {
41 return false;
42 }
44 return true;
45 }
46
47 protected:
49 {
50 _params.ascii_format = true;
51 }
52 void SetUp() override
53 {
54 BlendfileLoadingBaseTest::SetUp();
55 BKE_tempdir_init(nullptr);
56 }
57
63
64 static std::string get_temp_filename(const std::string &filename)
65 {
66 return std::string(BKE_tempdir_base()) + SEP_STR + filename;
67 }
68
75 void compare_to_golden(const std::string &blendfile, const std::string &golden_stl)
76 {
78 return;
79 }
80
81 std::string out_file_path = get_temp_filename(BLI_path_basename(golden_stl.c_str()));
82 STRNCPY(_params.filepath, out_file_path.c_str());
83 std::string golden_file_path = tests::flags_test_asset_dir() + SEP_STR + golden_stl;
85 std::string output_str = read_temp_file_in_string(out_file_path);
86
87 std::string golden_str = read_temp_file_in_string(golden_file_path);
88 bool are_equal = output_str == golden_str;
89 if (save_failing_test_output && !are_equal) {
90 printf("failing test output in %s\n", out_file_path.c_str());
91 }
92 ASSERT_TRUE(are_equal);
93 if (!save_failing_test_output || are_equal) {
94 BLI_delete(out_file_path.c_str(), false, false);
95 }
96 }
97
99};
100
102{
103 compare_to_golden("io_tests" SEP_STR "blend_geometry" SEP_STR "all_tris.blend",
104 "io_tests" SEP_STR "stl" SEP_STR "all_tris.stl");
105}
106
108{
109 compare_to_golden("io_tests" SEP_STR "blend_geometry" SEP_STR "all_quads.blend",
110 "io_tests" SEP_STR "stl" SEP_STR "all_quads.stl");
111}
112
113TEST_F(STLExportTest, non_uniform_scale)
114{
115 compare_to_golden("io_tests" SEP_STR "blend_geometry" SEP_STR "non_uniform_scale.blend",
116 "io_tests" SEP_STR "stl" SEP_STR "non_uniform_scale.stl");
117}
118
119TEST_F(STLExportTest, cubes_positioned)
120{
121 compare_to_golden("io_tests" SEP_STR "blend_geometry" SEP_STR "cubes_positioned.blend",
122 "io_tests" SEP_STR "stl" SEP_STR "cubes_positioned.stl");
123}
124
125} // namespace blender::io::stl
File and directory operations.
Read Blender memory allocation and freeing.
virtual void depsgraph_create(eEvaluationMode depsgraph_evaluation_mode)
void compare_to_golden(const std::string &blendfile, const std::string &golden_stl)
bool load_file_and_depsgraph(const std::string &filepath)
static std::string get_temp_filename(const std::string &filename)
std::string filename(const std::string_view &str, size_t pos)
void export_frame(Depsgraph *depsgraph, float scene_unit_scale, const STLExportParams &export_params)
Definition stl_export.cc:49
static std::string read_temp_file_in_string(const std::string &file_path)
constexpr bool save_failing_test_output
TEST_F(STLExportTest, all_tris)
void BKE_tempdir_session_purge()
Definition appdir.cc:1293
char * STRNCPY(char(&dst)[N], const char *src)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
char * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:539
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
@ DAG_EVAL_VIEWPORT
const char * BKE_tempdir_base() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1288
void BKE_tempdir_init(const char *userdir)
Definition appdir.cc:1245
char filepath[FILE_MAX]
Definition IO_stl.hh:40
#define SEP_STR
Definition unit.cc:41