Skip to content

Commit

Permalink
Merge pull request #5 from arminms:issue_#4
Browse files Browse the repository at this point in the history
Fix issue #4 (tmpnam() warning)
  • Loading branch information
arminms committed Feb 2, 2024
2 parents 1cf1760 + f3e5361 commit 0bca8fd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ option(G3P_ENABLE_TESTS "Enable the unit tests ?" ON)
#
project(
g3p
VERSION 1.0.0
VERSION 1.0.1
LANGUAGES CXX
DESCRIPTION "gnuplot for Modern C++ with support for Jupyter"
)
Expand Down
32 changes: 23 additions & 9 deletions include/g3p/gnuplot
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <algorithm>
#include <filesystem>
#include <stdexcept>
#include <random>

#ifdef __CLING__
# include <xcpp/xdisplay.hpp>
Expand All @@ -46,15 +47,25 @@ namespace g3p
// -- isolated functions in a namespace ----------------------------------------

namespace detail
{ std::string inline random_name(const int len)
{ static const char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
{ inline std::string random_name(const int len)
{ static const char alphanum[] =
"1234567890"
"abcdefghijklmnopqrstuvwxyz";
std::string tmp_s;
tmp_s.reserve(len);
std::default_random_engine rng(std::random_device{}());
std::uniform_int_distribution<> dist(0, sizeof(alphanum) - 2);
for (int i = 0; i < len; ++i)
tmp_s += alpha[rand() % (sizeof(alpha) - 1)];
tmp_s += alphanum[dist(rng)];
return tmp_s;
}

inline std::string tmpnam()
{ auto filename = std::filesystem::temp_directory_path().string();
filename += "/g3p" + random_name(16);
return filename;
}

void wait4signal(std::uintmax_t size, const std::filesystem::path& file)
{ auto start = std::chrono::system_clock::now();
while
Expand Down Expand Up @@ -145,13 +156,12 @@ namespace g3p
gnuplot(bool persist = true, std::string logfile = {})
: _gp(nullptr)
, _logfile(logfile)
{
const char* gnuplot_path = std::getenv("G3P_GNUPLOT_PATH");
{ const char* gnuplot_path = std::getenv("G3P_GNUPLOT_PATH");
std::string gnuplot_cmd = gnuplot_path ? gnuplot_path : "gnuplot";
if (nullptr != gnuplot_path)
gnuplot_cmd.insert(0, 1, '\"').append(1, '\"');
if (_logfile.empty())
_logfile = tmpnam(NULL);
_logfile = detail::tmpnam();
if (persist)
gnuplot_cmd += " -persist";
gnuplot_cmd += " > " + _logfile + " 2>&1";
Expand All @@ -163,9 +173,13 @@ namespace g3p
(gnuplot_cmd.c_str(), "w");
if (nullptr == _gp)
throw std::domain_error("gnuplot -- failed");
fprintf(_gp, "print '>>>>> g3p -- gnuplot log <<<<<'; print ' '\n");
fprintf(_gp, "print '>> gnuplot log generated by g3p <<'\n");
fprintf(_gp, "print 'version: ',GPVAL_VERSION\n");
fprintf(_gp, "print 'patchlevel: ',GPVAL_PATCHLEVEL\n");
fprintf(_gp, "print 'system: ',GPVAL_SYSNAME\n");
fprintf(_gp, "print 'arch: ',GPVAL_MACHINE;print ' '\n");
#ifdef __CLING__
_plotfile = tmpnam(NULL);
_plotfile = detail::tmpnam();
fprintf(_gp, "set term push\n");
fprintf(_gp, "set term svg standalone enhanced\n");
fprintf(_gp, "set output \"%s\"\n", _plotfile.c_str());
Expand All @@ -179,9 +193,9 @@ namespace g3p
#else
if (_gp) pclose(_gp);
#endif //_MSC_VER
#ifdef __CLING__
std::filesystem::path f(_logfile);
std::filesystem::remove(f);
#ifdef __CLING__
f = _plotfile;
std::filesystem::remove(f);
#endif //__CLING__
Expand Down
41 changes: 27 additions & 14 deletions test/unit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,48 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include <algorithm>
#include <catch2/catch_all.hpp>
#include <g3p/gnuplot>

TEST_CASE("log() function", "[gnuplot][log]")
{ SECTION("no argument")
{ g3p::gnuplot gp;
REQUIRE_NOTHROW(">>>>> g3p -- gnuplot log <<<<<\n \n" == gp.log());
{ g3p::gnuplot gp;
SECTION("no argument")
{ auto log = gp.log();
REQUIRE
( 6 == std::count_if
( log.begin()
, log.end()
, [](int c) { return c == '\n'; }
)
);
}
SECTION("line count = 1")
{ g3p::gnuplot gp;
REQUIRE_NOTHROW(" \n" == gp.log(1));
{ CHECK(" \n" == gp.log(1));
}
SECTION("line count = 2")
{ g3p::gnuplot gp;
REQUIRE_NOTHROW(">>>>> g3p -- gnuplot log <<<<<\n \n" == gp.log(2));
SECTION("line count = 6")
{ CHECK(">> gnuplot log generated by g3p <<" == gp.log(6).substr(0, 34));
}
}

TEST_CASE("output stream operator", "[gnuplot]")
{ g3p::gnuplot gp;
SECTION("whole output")
{ auto out = gp >> 2;
REQUIRE(">>>>> g3p -- gnuplot log <<<<<\n \n" == out.str());
{ auto out = gp >> 6;
auto str = out.str();
REQUIRE
( 6 == std::count_if
( str.begin()
, str.end()
, [](char c) { return c == '\n'; }
)
);
}
SECTION("selective")
{ std::string skip, gnuplot;
gp >> 2 >> skip >> skip >> skip >> gnuplot;
REQUIRE("--" == skip);
REQUIRE("gnuplot" == gnuplot);
{ std::string skip, g3p;
gp >> 6 >> skip >> skip >> skip >> skip >> skip >> g3p;
CHECK("by" == skip);
CHECK("g3p" == g3p);
}
}

Expand Down

0 comments on commit 0bca8fd

Please sign in to comment.