From 9c11cfeea09d9130c79162428e20e35ab72d1e3b Mon Sep 17 00:00:00 2001
From: mkirsz <s1351949@sms.ed.ac.uk>
Date: Tue, 26 Nov 2024 13:10:09 +0000
Subject: [PATCH] replaced std::tmpnam

---
 tests/test_structure.cpp | 115 +++++++++++++++++++++++----------------
 1 file changed, 68 insertions(+), 47 deletions(-)

diff --git a/tests/test_structure.cpp b/tests/test_structure.cpp
index 055f39a..0b890d5 100644
--- a/tests/test_structure.cpp
+++ b/tests/test_structure.cpp
@@ -56,63 +56,84 @@ TEST_CASE( "Testing Structure virial pressure calculations", "[structure_virial_
   REQUIRE(fac*st.get_virial_pressure() == Approx(499.93));
 
 }
-TEST_CASE( "Testing Structure read and write", "[structure_read_write]" ) {
-  Structure st;
-  st.read("tests_data/structure_1.dat");
+TEST_CASE("Testing Structure read and write", "[structure_read_write]") {
+    Structure st;
+    st.read("tests_data/structure_1.dat");
+
+    REQUIRE(st.get_volume() == Approx(989.521812));
 
-  REQUIRE(st.get_volume() == Approx(989.521812));
+    REQUIRE(fac * st.get_virial_pressure() == Approx(26.705578));
+    REQUIRE(fac * st.get_pressure(300) == Approx(28.965914));
+    REQUIRE(fac * st.get_pressure(0) == Approx(26.705578));
 
-  REQUIRE(fac*st.get_virial_pressure() == Approx(26.705578));
-  REQUIRE(fac*st.get_pressure(300) == Approx(28.965914));
-  REQUIRE(fac*st.get_pressure(0) == Approx(26.705578));
+    // Create a temporary file
+    std::FILE* tmpFile = std::tmpfile();
+    if (!tmpFile) {
+        FAIL("Unable to create temporary file");
+    }
 
-  std::string tempfile = std::tmpnam(nullptr);
-  st.save(tempfile);
+    const int fd = fileno(tmpFile);
+    REQUIRE(fd != -1);
 
-  Structure st_temp;
-  st_temp.read(tempfile);
+    char tempfile[256];
+    snprintf(tempfile, sizeof(tempfile), "/proc/self/fd/%d", fd);
 
-  REQUIRE(st_temp.get_volume() == Approx(989.521812));
+    st.save(tempfile);
 
-  REQUIRE(fac*st_temp.get_virial_pressure() == Approx(26.705578));
-  REQUIRE(fac*st_temp.get_pressure(300) == Approx(28.965914));
-  REQUIRE(fac*st_temp.get_pressure(0) == Approx(26.705578));
+    Structure st_temp;
+    st_temp.read(tempfile);
 
-  REQUIRE(st==st_temp);
+    REQUIRE(st_temp.get_volume() == Approx(989.521812));
+    REQUIRE(fac * st_temp.get_virial_pressure() == Approx(26.705578));
+    REQUIRE(fac * st_temp.get_pressure(300) == Approx(28.965914));
+    REQUIRE(fac * st_temp.get_pressure(0) == Approx(26.705578));
 
-  std::remove(tempfile.c_str());
+    REQUIRE(st == st_temp);
 
+    // Temporary file will be closed automatically
 }
-TEST_CASE( "Testing Structure compare", "[structure_compare]" ) {
-  Structure st;
-  st.read("tests_data/structure_1.dat");
-  std::string tempfile = std::tmpnam(nullptr);
-  st.save(tempfile);
-
-  Structure st_temp;
-  st_temp.read(tempfile);
-
-  SECTION("Compare unchanged") {
-    REQUIRE(st==st_temp);
-  }
-  SECTION("Compare symbols") {
-    REQUIRE(st==st_temp);
-    st_temp.atoms[0].symbol[0]='X';
-    st_temp.atoms[0].symbol[1]='X';
-    REQUIRE(!(st==st_temp));
-  }
-  SECTION("Compare position") {
-    REQUIRE(st==st_temp);
-    st_temp.atoms[0].position(0.12,0.13,10.14);
-    REQUIRE(!(st==st_temp));
-  }
-  SECTION("Compare force") {
-    REQUIRE(st==st_temp);
-    st_temp.atoms[0].force(1.12, 0.13, 0.134);
-    REQUIRE(!(st==st_temp));
-  }
-
-  std::remove(tempfile.c_str());
+TEST_CASE("Testing Structure compare", "[structure_compare]") {
+    Structure st;
+    st.read("tests_data/structure_1.dat");
+
+    // Create a temporary file
+    std::FILE* tmpFile = std::tmpfile();
+    if (!tmpFile) {
+        FAIL("Unable to create temporary file");
+    }
+
+    const int fd = fileno(tmpFile);
+    REQUIRE(fd != -1);
+
+    char tempfile[256];
+    snprintf(tempfile, sizeof(tempfile), "/proc/self/fd/%d", fd);
+
+    st.save(tempfile);
+
+    Structure st_temp;
+    st_temp.read(tempfile);
+
+    SECTION("Compare unchanged") {
+        REQUIRE(st == st_temp);
+    }
+    SECTION("Compare symbols") {
+        REQUIRE(st == st_temp);
+        st_temp.atoms[0].symbol[0] = 'X';
+        st_temp.atoms[0].symbol[1] = 'X';
+        REQUIRE(!(st == st_temp));
+    }
+    SECTION("Compare position") {
+        REQUIRE(st == st_temp);
+        st_temp.atoms[0].position(0.12, 0.13, 10.14);
+        REQUIRE(!(st == st_temp));
+    }
+    SECTION("Compare force") {
+        REQUIRE(st == st_temp);
+        st_temp.atoms[0].force(1.12, 0.13, 0.134);
+        REQUIRE(!(st == st_temp));
+    }
+
+    // Temporary file will be closed automatically
 }
 TEST_CASE( "Testing Structure copy", "[structure_copy]" ) {
   Structure st;
-- 
GitLab