[Bf-blender-cvs] [1eed0031ecf] soc-2020-io-performance: Add framework to fast obj i/o to regression test whole files.

Howard Trickey noreply at git.blender.org
Sun Aug 29 19:56:48 CEST 2021


Commit: 1eed0031ecf0b6931606153c05183fa5990d273e
Author: Howard Trickey
Date:   Sun Aug 15 16:23:28 2021 -0400
Branches: soc-2020-io-performance
https://developer.blender.org/rB1eed0031ecf0b6931606153c05183fa5990d273e

Add framework to fast obj i/o to regression test whole files.

One tricky aspect is that we want to allow for the version string
part of the header to mismatch, so only check for file equality
after the first lines.

===================================================================

M	source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
M	source/blender/io/wavefront_obj/exporter/obj_exporter.cc
M	source/blender/io/wavefront_obj/exporter/obj_exporter.hh
M	source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc

===================================================================

diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
index b1cacfb2026..d2faccfee0e 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
@@ -484,7 +484,6 @@ MTLWriter::MTLWriter(const char *obj_filepath) noexcept(false)
     throw std::system_error(ENAMETOOLONG, std::system_category(), "");
   }
   file_handler_ = std::make_unique<FileHandler<eFileType::MTL>>(mtl_filepath_);
-  std::cout << "Material Library created at: " << mtl_filepath_ << std::endl;
 }
 
 void MTLWriter::write_header(const char *blen_filepath) const
diff --git a/source/blender/io/wavefront_obj/exporter/obj_exporter.cc b/source/blender/io/wavefront_obj/exporter/obj_exporter.cc
index 0929ba2b9d0..9bf75105d78 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_exporter.cc
+++ b/source/blender/io/wavefront_obj/exporter/obj_exporter.cc
@@ -211,9 +211,7 @@ static void write_nurbs_curve_objects(const Vector<std::unique_ptr<OBJCurve>> &e
  *
  * Conditionally write a .MTL file also.
  */
-static void export_frame(Depsgraph *depsgraph,
-                         const OBJExportParams &export_params,
-                         const char *filepath)
+void export_frame(Depsgraph *depsgraph, const OBJExportParams &export_params, const char *filepath)
 {
   std::unique_ptr<OBJWriter> frame_writer = nullptr;
   try {
diff --git a/source/blender/io/wavefront_obj/exporter/obj_exporter.hh b/source/blender/io/wavefront_obj/exporter/obj_exporter.hh
index 48a54f8f5a3..dff9eb2681c 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_exporter.hh
+++ b/source/blender/io/wavefront_obj/exporter/obj_exporter.hh
@@ -71,6 +71,10 @@ void exporter_main(bContext *C, const OBJExportParams &export_params);
 class OBJMesh;
 class OBJCurve;
 
+void export_frame(Depsgraph *depsgraph,
+                  const OBJExportParams &export_params,
+                  const char *filepath);
+
 std::pair<Vector<std::unique_ptr<OBJMesh>>, Vector<std::unique_ptr<OBJCurve>>>
 filter_supported_objects(Depsgraph *depsgraph, const OBJExportParams &export_params);
 
diff --git a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
index f3603a741f1..a0be2a31e69 100644
--- a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
+++ b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
@@ -13,6 +13,7 @@
 
 #include "BKE_blender_version.h"
 
+#include "BLI_fileops.h"
 #include "BLI_index_range.hh"
 #include "BLI_string_utf8.h"
 #include "BLI_vector.hh"
@@ -50,8 +51,6 @@ const std::string all_objects_file = "io_tests/blend_scene/all_objects_2_92.blen
 // https://developer.blender.org/F9278970
 const std::string all_curve_objects_file = "io_tests/blend_scene/all_curves_2_92.blend";
 
-const std::string some_meshes_file = "io_tests/blend_scene/all_meshes.blend";
-
 TEST_F(obj_exporter_test, filter_objects_curves_as_mesh)
 {
   OBJExportParamsDefault _export;
@@ -188,6 +187,7 @@ static std::unique_ptr<OBJWriter> init_writer(const OBJExportParams &params,
 
 /* The following is relative to the flags_test_release_dir(). */
 const char *const temp_file_path = "../../Testing/Temporary/output.OBJ";
+const char *const temp_file_dir = "../../Testing/Temporary/";
 
 static std::string read_temp_file_in_string(const std::string &file_path)
 {
@@ -231,4 +231,67 @@ TEST(obj_exporter_writer, mtllib)
   ASSERT_EQ(result, "mtllib blah.mtl\nmtllib blah.mtl\n");
 }
 
+/* Return true if string #a and string #b are equal after their first newline. */
+static bool strings_equal_after_first_lines(const std::string &a, const std::string &b)
+{
+  size_t a_len = a.size();
+  size_t b_len = b.size();
+  size_t a_next = a.find_first_of('\n');
+  size_t b_next = b.find_first_of('\n');
+  if (a_next == std::string::npos || b_next == std::string::npos) {
+    return false;
+  }
+  return a.compare(a_next, a_len - a_next, b, b_next, b_len - b_next) == 0;
+}
+
+/* From here on, tests are whole file tests, testing for golden output. */
+class obj_exporter_regression_test : public obj_exporter_test {
+ public:
+  /**
+   * Export the given blend file with the given parameters and
+   * test to see if it matches a golden file (ignoring any difference in Blender version number).
+   * \param blendfile: input, relative to "tests" directory.
+   * \param golden_obj: expected output, relative to "tests" directory.
+   * \param params: the parameters to be used for export.
+   */
+  void compare_obj_export_to_golden(const std::string &blendfile,
+                                    const std::string &golden_obj,
+                                    const std::string &golden_mtl,
+                                    OBJExportParams &params)
+  {
+    if (!load_file_and_depsgraph(blendfile)) {
+      return;
+    }
+    std::string out_file_path = blender::tests::flags_test_release_dir() + "/" + temp_file_dir +
+                                BLI_path_basename(golden_obj.c_str());
+    strncpy(params.filepath, out_file_path.c_str(), FILE_MAX);
+    params.blen_filepath = blendfile.c_str();
+    export_frame(depsgraph, params, out_file_path.c_str());
+    std::string output_str = read_temp_file_in_string(out_file_path);
+
+    std::string golden_file_path = blender::tests::flags_test_asset_dir() + "/" + golden_obj;
+    std::string golden_str = read_temp_file_in_string(golden_file_path);
+    ASSERT_TRUE(strings_equal_after_first_lines(output_str, golden_str));
+    BLI_delete(out_file_path.c_str(), false, false);
+    if (!golden_mtl.empty()) {
+      std::string out_mtl_file_path = blender::tests::flags_test_release_dir() + "/" +
+                                      temp_file_dir + BLI_path_basename(golden_mtl.c_str());
+      std::string output_mtl_str = read_temp_file_in_string(out_mtl_file_path);
+      std::string golden_mtl_file_path = blender::tests::flags_test_asset_dir() + "/" + golden_mtl;
+      std::string golden_mtl_str = read_temp_file_in_string(golden_mtl_file_path);
+      ASSERT_TRUE(strings_equal_after_first_lines(output_mtl_str, golden_mtl_str));
+      BLI_delete(out_mtl_file_path.c_str(), false, false);
+    }
+  }
+};
+
+TEST_F(obj_exporter_regression_test, all_tris)
+{
+  OBJExportParamsDefault _export;
+  compare_obj_export_to_golden("io_tests/blend_geometry/all_tris.blend",
+                               "io_tests/obj/all_tris.obj",
+                               "io_tests/obj/all_tris.mtl",
+                               _export.params);
+}
+
 }  // namespace blender::io::obj



More information about the Bf-blender-cvs mailing list