44#include < wmtk/io/HDF5Writer.hpp>
55#include < wmtk/io/MeshReader.hpp>
66#include < wmtk/io/ParaviewWriter.hpp>
7+ #include < wmtk/io/Cache.hpp>
78#include < wmtk/utils/mesh_utils.hpp>
89
910#include < wmtk/operations/OperationFactory.hpp>
1819using namespace wmtk ;
1920using namespace wmtk ::tests;
2021
22+ namespace fs = std::filesystem;
23+
2124
2225constexpr PrimitiveType PV = PrimitiveType::Vertex;
2326constexpr PrimitiveType PE = PrimitiveType::Edge;
@@ -152,3 +155,111 @@ TEST_CASE("attribute_after_split", "[io]")
152155 ParaviewWriter writer (" attribute_after_split" , " position" , m, true , true , true , false );
153156 m.serialize (writer);
154157}
158+
159+ TEST_CASE (" cache_init" , " [cache]" )
160+ {
161+ const fs::path dir = std::filesystem::current_path ();
162+ const std::string prefix = " wmtk_cache" ;
163+
164+ fs::path cache_dir;
165+ {
166+ utils::Cache cache (prefix, dir);
167+ cache_dir = cache.get_cache_path ();
168+
169+ CHECK (fs::exists (cache_dir));
170+
171+ CHECK (dir == cache_dir.parent_path ());
172+ CHECK (cache_dir.stem ().string ().rfind (prefix, 0 ) == 0 ); // cache dir starts with prefix
173+ }
174+ CHECK_FALSE (fs::exists (cache_dir));
175+ }
176+
177+ TEST_CASE (" cache_files" , " [cache]" )
178+ {
179+ fs::path filepath;
180+ std::string file_name = " my_new_file" ;
181+ {
182+ utils::Cache cache (" wmtk_cache" , fs::current_path ());
183+
184+ filepath = cache.create_unique_file (file_name, " .txt" );
185+
186+ CHECK (fs::exists (filepath));
187+ CHECK (filepath.stem ().string ().rfind (file_name, 0 ) == 0 );
188+ CHECK (filepath.extension ().string () == " .txt" );
189+
190+ const fs::path filepath_from_cache = cache.get_file_path (file_name);
191+
192+ CHECK (filepath_from_cache == filepath);
193+ }
194+ CHECK_FALSE (fs::exists (filepath));
195+ }
196+
197+ TEST_CASE (" cache_read_write_mesh" , " [cache]" )
198+ {
199+ utils::Cache cache (" wmtk_cache" , fs::current_path ());
200+ TriMesh mesh = tests::single_triangle ();
201+
202+ const std::string name = " cached_mesh" ;
203+ cache.write_mesh (mesh, name);
204+
205+ auto mesh_from_cache = cache.read_mesh (name);
206+
207+ CHECK (*mesh_from_cache == mesh);
208+ CHECK_THROWS (cache.read_mesh (" some_file_that_does_not_exist" ));
209+ }
210+
211+ TEST_CASE (" cache_export_import" , " [cache]" )
212+ {
213+ const fs::path export_location =
214+ utils::Cache::create_unique_directory (" wmtk_cache_export" , fs::current_path ());
215+
216+ const std::vector<std::string> file_names = {" a" , " b" , " c" };
217+
218+ // create cache
219+ fs::path first_cache_path;
220+ {
221+ utils::Cache cache (" wmtk_cache" , fs::current_path ());
222+ // generate some files
223+ for (const std::string& name : file_names) {
224+ const fs::path p = cache.create_unique_file (name, " .txt" );
225+ CHECK (fs::exists (p));
226+ CHECK (p.stem ().string ().rfind (name, 0 ) == 0 );
227+ CHECK (p.extension ().string () == " .txt" );
228+ }
229+
230+ first_cache_path = cache.get_cache_path ();
231+
232+ // delete dummy directory
233+ fs::remove_all (export_location);
234+ REQUIRE_FALSE (fs::exists (export_location));
235+ // export cache to dummy directory
236+ REQUIRE (cache.export_cache (export_location));
237+ }
238+ CHECK_FALSE (fs::exists (first_cache_path));
239+
240+ // create new cache
241+ {
242+ utils::Cache cache (" wmtk_cache" , fs::current_path ());
243+ // import the previously exported
244+ CHECK (cache.import_cache (export_location));
245+
246+ // check if files are there
247+ for (const std::string& name : file_names) {
248+ const fs::path p = cache.get_file_path (name);
249+ CHECK (fs::exists (p));
250+ CHECK (p.stem ().string ().rfind (name, 0 ) == 0 );
251+ CHECK (p.extension ().string () == " .txt" );
252+ }
253+ }
254+
255+ // try to import even though the cache contains a file
256+ {
257+ utils::Cache cache (" wmtk_cache" , fs::current_path ());
258+ cache.create_unique_file (" some_file" , " " );
259+ // import should not work if the cache already contains files
260+ CHECK_FALSE (cache.import_cache (export_location));
261+ }
262+
263+ // clean up export
264+ fs::remove_all (export_location);
265+ }
0 commit comments