Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <string>
- #include <iostream>
- int error_check( int result, std::string text )
- {
- if ( result == -1 )
- throw text ;
- return result ;
- }
- template < typename Input, typename Output >
- void check( std::string name, Input input, Output output )
- {
- int infd[2] ;
- error_check( pipe( infd ), "Error: pipe failed." ) ;
- int outfd[2] ;
- error_check( pipe( outfd ), "Error: pipe failed." ) ;
- pid_t cpid = error_check( fork(), "Error: fork failed." ) ;
- // child
- if ( cpid == 0 )
- {
- close( infd[1] ) ;
- close( outfd[0] ) ;
- dup2( infd[0], STDIN_FILENO ) ;
- dup2( outfd[1], STDOUT_FILENO ) ;
- close( infd[0] ) ;
- close( outfd[1] ) ;
- error_check(
- execl( "./main", "main", static_cast<char *>(nullptr) )
- , "Error: exec failed." ) ;
- }
- // parent
- else
- {
- close( infd[0] ) ;
- close( outfd[1] ) ;
- int in = dup( STDIN_FILENO ) ;
- int out = dup( STDOUT_FILENO ) ;
- dup2( outfd[0], STDIN_FILENO ) ;
- dup2( infd[1], STDOUT_FILENO ) ;
- input() ;
- bool result = output() ;
- dup2( in, STDIN_FILENO ) ;
- dup2( out, STDOUT_FILENO ) ;
- std::cout << name << " : " << (result ? "Accepted" : "Wrong Answer") << std::endl ;
- }
- }
- int main()
- try {
- check( "YES test",
- [](){ std::cout << "1" << std::endl ;},
- [](){
- std::string output ;
- std::cin >> output ;
- return output == "YES" ;
- } ) ;
- check( "NO test",
- [](){ std::cout << "0" << std::endl ; },
- [](){
- std::string output ;
- std::cin >> output ;
- return output == "NO" ;
- } ) ;
- } catch ( std::string text )
- {
- std::cerr << text << std::endl ;
- return 1 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement