Skip to content

Commit

Permalink
Use a temporary file in /tmp in the serialization test
Browse files Browse the repository at this point in the history
The current working directory may not be writable. Also, we should
clean up tepmorary files created by the test, if it passes.
  • Loading branch information
rocallahan committed Jul 2, 2024
1 parent c6b66cf commit 95460d5
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions test/io/serialize.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
#include <catch.hpp>

#include <filesystem>
#include <random>

#include <mockturtle/io/serialize.hpp>

#include <fmt/format.h>

using namespace mockturtle;

// Insecure but portable temporary file name generation
static std::filesystem::path temp_file_name()
{
std::random_device rd;
std::mt19937_64 generator(rd());
std::uniform_int_distribution<uint64_t> distribution(0, std::numeric_limits<uint64_t>::max());
uint64_t random_number = distribution(generator);

std::filesystem::path path = std::filesystem::temp_directory_path();
path /= fmt::format( "test{:x}.aig", random_number );
return path;
}

TEST_CASE( "serialize aig_network into a file", "[serialize]" )
{
aig_network aig;
Expand All @@ -18,11 +36,15 @@ TEST_CASE( "serialize aig_network into a file", "[serialize]" )
const auto f5 = aig.create_nand( f4, f3 );
aig.create_po( f5 );

auto file_name = temp_file_name();

/* serialize */
serialize_network( aig, "aig.dmp" );
serialize_network( aig, file_name.c_str() );

/* deserialize */
aig_network aig2 = deserialize_network( "aig.dmp" );
aig_network aig2 = deserialize_network( file_name.c_str() );

std::filesystem::remove( file_name );

CHECK( aig.size() == aig2.size() );
CHECK( aig.num_cis() == aig2.num_cis() );
Expand Down

0 comments on commit 95460d5

Please sign in to comment.