Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:15

0001 /*
0002  * Base class for tests.
0003  *
0004  * Author: Marcel Rieger
0005  */
0006 
0007 #ifndef PHYSICSTOOLS_TENSORFLOW_TEST_TESTBASE_H
0008 #define PHYSICSTOOLS_TENSORFLOW_TEST_TESTBASE_H
0009 
0010 #include <boost/filesystem.hpp>
0011 #include <filesystem>
0012 #include <cppunit/extensions/HelperMacros.h>
0013 #include <stdexcept>
0014 
0015 class testBase : public CppUnit::TestFixture {
0016 public:
0017   std::string dataPath_;
0018 
0019   void setUp();
0020   void tearDown();
0021   std::string cmsswPath(std::string path);
0022 
0023   virtual void test() = 0;
0024 
0025   virtual std::string pyScript() const = 0;
0026 };
0027 
0028 void testBase::setUp() {
0029   dataPath_ =
0030       cmsswPath("/test/" + std::string(std::getenv("SCRAM_ARCH")) + "/" + boost::filesystem::unique_path().string());
0031 
0032   // create the graph
0033   std::string testPath = cmsswPath("/src/PhysicsTools/TensorFlow/test");
0034   std::string cmd = "python3 -W ignore " + testPath + "/" + pyScript() + " " + dataPath_;
0035   std::array<char, 128> buffer;
0036   std::string result;
0037   std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
0038   if (!pipe) {
0039     throw std::runtime_error("popen() failed!");
0040   }
0041   while (!feof(pipe.get())) {
0042     if (fgets(buffer.data(), 128, pipe.get()) != NULL) {
0043       result += buffer.data();
0044     }
0045   }
0046   std::cout << std::endl << result << std::endl;
0047 }
0048 
0049 void testBase::tearDown() {
0050   if (std::filesystem::exists(dataPath_)) {
0051     std::filesystem::remove_all(dataPath_);
0052   }
0053 }
0054 
0055 std::string testBase::cmsswPath(std::string path) {
0056   if (path.size() > 0 && path.substr(0, 1) != "/") {
0057     path = "/" + path;
0058   }
0059 
0060   std::string base = std::string(std::getenv("CMSSW_BASE"));
0061   std::string releaseBase = std::string(std::getenv("CMSSW_RELEASE_BASE"));
0062 
0063   return (std::filesystem::exists(base.c_str()) ? base : releaseBase) + path;
0064 }
0065 
0066 #endif  // PHYSICSTOOLS_TENSORFLOW_TEST_TESTBASE_H