dlvhex  2.5.0
testsuite/TestDLVProcess.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include "config.h"
00003 #endif // HAVE_CONFIG_H
00004 
00005 #include "dlvhex2/DLVProcess.h"
00006 
00007 #define BOOST_TEST_DYN_LINK
00008 #define BOOST_TEST_MODULE "TestDLVProcess"
00009 #include <boost/test/unit_test.hpp>
00010 #include <boost/thread.hpp>
00011 
00012 
00013 DLVHEX_NAMESPACE_USE
00014 
00015 class SimpleEvaluator
00016 {
00017 public:
00018   SimpleEvaluator(const std::string& in)
00019     : input(in)
00020   { }
00021 
00022   void
00023   solve()
00024   {
00025     proc = new DLVProcess;
00026     proc->setPath(DLVPATH);
00027     proc->addOption("-silent");
00028     proc->addOption("-facts");
00029     proc->addOption("--");
00030     proc->spawn();
00031 
00032     std::ostream& programStream = proc->getOutput();
00033     programStream << input;
00034     programStream.flush();
00035     proc->endoffile();
00036 
00037     std::cerr << "input = " << std::endl << input << std::endl;
00038 
00039     std::istream& is = proc->getInput();
00040 
00041     std::cerr << "Getting results: " << std::endl;
00042     std::size_t count = 0;
00043 
00044     std::cerr << "Ans " << ++count;
00045     std::string res;
00046     std::getline(is, res);
00047     std::cerr << ":  ";
00048     if (res.empty())
00049       {
00050     std::cerr << "Break now! res.empty()" << std::endl;
00051     //break;
00052       }
00053     else if (is.bad())
00054       {
00055     std::cerr << "Break now! is.bad()" << std::endl;
00056     //break;
00057       }
00058     std::cerr << res << std::endl;
00059     
00060     std::cerr << std::endl;
00061     delete proc;
00062     proc = 0;
00063   }
00064 
00065 private:
00066   DLVProcess* proc;
00067   std::string input;
00068 };
00069 
00070 BOOST_AUTO_TEST_CASE ( testDLVProcess )
00071 {
00072   std::string line1 = "a v b :- c.\n";
00073   std::string line2 = "c v d :- e.\n";
00074   std::string line3 = "e v f :- g.\n";
00075   std::string line4 = "g v h :- i.\n";
00076   std::string line5 = "i v j.\n";
00077 
00078   std::string program = "";
00079   program = program + line1 + line2 + line3 + line4 + line5;
00080 
00081   SimpleEvaluator se(program);
00082 
00083   se.solve();
00084   se.solve();
00085   se.solve();
00086   se.solve();
00087   se.solve();
00088   se.solve();
00089   se.solve();
00090   se.solve();
00091 }
00092 
00093 // Local Variables:
00094 // mode: C++
00095 // End: