Skip to content

Commit a131997

Browse files
committed
move Cache into io folder
1 parent 5016a32 commit a131997

File tree

7 files changed

+113
-122
lines changed

7 files changed

+113
-122
lines changed

src/wmtk/io/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

22
set(SRC_FILES
3+
Cache.cpp
4+
Cache.hpp
35
HDF5Reader.hpp
46
HDF5Reader.cpp
57
HDF5Writer.hpp
File renamed without changes.
File renamed without changes.

src/wmtk/utils/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11

22
set(SRC_FILES
3-
Cache.cpp
4-
Cache.hpp
53
Logger.cpp
64
Logger.hpp
75
edgemesh_topology_initialization.h

tests/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ include(wmtk_data)
88

99
# Sources
1010
set(TEST_SOURCES
11-
test_cache.cpp
1211
test_topology.cpp
1312
test_mesh.cpp
1413
test_autogen.cpp

tests/test_cache.cpp

Lines changed: 0 additions & 119 deletions
This file was deleted.

tests/test_io.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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>
@@ -18,6 +19,8 @@
1819
using namespace wmtk;
1920
using namespace wmtk::tests;
2021

22+
namespace fs = std::filesystem;
23+
2124

2225
constexpr PrimitiveType PV = PrimitiveType::Vertex;
2326
constexpr 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

Comments
 (0)