END_TEST START_TEST(test_print_vessel) { char stu[2048]; char mod[2048]; memset(stu, 0, 2048); memset(mod, 0, 2048); srand(time(0)); struct cargo *c = create_cargo(); char *names[] = { "Mariella", "Cargoship", "XRPS", "Symphony" }; double lengths[] = { 154.5, 455.7, 345.15, 480.65, 235.06 }; double depths[] = { 34.6, 76.4, 85.5, 14.8, 56.9 }; char* name = names[rand()%4]; double length = lengths[rand()%5]; double depth = depths[rand()%5]; struct vessel v = create_vessel(name, length, depth, *c); freopen("mockoutput", "w", stdout); print_vessel(&v); read_stdout(stu, 2048); sprintf(mod, "%s\n%.1f\n%.1f\n%s\n%d\n%.1f\n", v.name, v.length, v.depth, v.crg.title, v.crg.quantity, v.crg.weight); char infostr[100] = ""; if (mycompare(stu, mod, infostr)) { free(c); fail("[M3.04] Wrong print output! Your output:\n%s\nReference output:\n%s\nReason: %s\n", stu, mod, infostr); } free(c); }
void Process::async_read() { if(data.id==0) return; if(stdout_fd) { stdout_thread=std::thread([this](){ DWORD n; std::unique_ptr<char[]> buffer(new char[buffer_size]); for (;;) { BOOL bSuccess = ReadFile(*stdout_fd, static_cast<CHAR*>(buffer.get()), static_cast<DWORD>(buffer_size), &n, nullptr); if(!bSuccess || n == 0) break; read_stdout(buffer.get(), static_cast<size_t>(n)); } }); } if(stderr_fd) { stderr_thread=std::thread([this](){ DWORD n; std::unique_ptr<char[]> buffer(new char[buffer_size]); for (;;) { BOOL bSuccess = ReadFile(*stderr_fd, static_cast<CHAR*>(buffer.get()), static_cast<DWORD>(buffer_size), &n, nullptr); if(!bSuccess || n == 0) break; read_stderr(buffer.get(), static_cast<size_t>(n)); } }); } }
END_TEST START_TEST(test_draw_ball) { char stu[2048]; char *mod = "...*...\n.*****.\n.*****.\n*******\n.*****.\n.*****.\n...*...\n"; int size = 3; freopen("mockoutput", "w", stdout); draw_ball(size); read_stdout(stu, 2048); char infostr[100] = ""; if (mycompare(stu, mod, infostr)) { fail("[Task 1.6] Called draw_ball(%d). Your output:\n%s\nReference output:\n%s\nReason: %s\n", size, stu, mod, infostr); } }
void Process::async_read() { if(data.id<=0) return; if(stdout_fd) { stdout_thread=std::thread([this](){ auto buffer = std::unique_ptr<char[]>( new char[buffer_size] ); ssize_t n; while ((n=read(*stdout_fd, buffer.get(), buffer_size)) > 0) read_stdout(buffer.get(), static_cast<size_t>(n)); }); } if(stderr_fd) { stderr_thread=std::thread([this](){ auto buffer = std::unique_ptr<char[]>( new char[buffer_size] ); ssize_t n; while ((n=read(*stderr_fd, buffer.get(), buffer_size)) > 0) read_stderr(buffer.get(), static_cast<size_t>(n)); }); } }
void Process::async_read() { if(data.id<=0) return; if(stdout_fd) { stdout_thread=std::thread([this](){ char buffer[buffer_size]; ssize_t n; while ((n=read(*stdout_fd, buffer, buffer_size)) > 0) read_stdout(buffer, static_cast<size_t>(n)); }); } if(stderr_fd) { stderr_thread=std::thread([this](){ char buffer[buffer_size]; ssize_t n; while ((n=read(*stderr_fd, buffer, buffer_size)) > 0) read_stderr(buffer, static_cast<size_t>(n)); }); } }
END_TEST // STUB goes here START_TEST(test_draw_triangle) { char stu[2048]; char *mod = "....#\n...##\n..###\n.####\n#####\n"; int size = 5; freopen("mockoutput", "w", stdout); draw_triangle(size); read_stdout(stu, 2048); char infostr[100] = ""; if (mycompare(stu, mod, infostr)) { fail("[Task 1.5] Called draw_triangle(%d). Your output:\n%s\nReference output:\n%s\nReason: %s\n", size, stu, mod, infostr); } }
/** * @brief thread routine that handle a forked process * @param arg the ::child_node for the forked child * @return 0 on success, -1 on error. */ void *handle_child(void *arg) { child_node *c; int status; pid_t wait_res; void *ret; int stdout_error; #ifndef NDEBUG char *name; #endif c = (child_node *)arg; ret = (void *) -1; stdout_error = 0; wait_res = 0; #ifndef NDEBUG print( DEBUG, "new child started. (name=%s, pid=%d)", c->handler->name, c->pid ); #endif if(c->handler->have_stdout) { if((stdout_error = read_stdout(c))) { print(ERROR, "cannot read process stdout"); } } stdout_error &= read_stderr(c); if((wait_res = waitpid(c->pid, &status, 0)) != c->pid) { print(ERROR, "waitpid: %s", strerror(errno)); } else if(on_child_done(c, status)) { print(ERROR, "cannot notify child termination."); } else if(!stdout_error) ret = 0; #ifdef NDEBUG send_to_graveyard(c->tid); #else if(asprintf(&name, "%s(name=%s, pid=%d)", __func__, c->handler->name, c->pid)<0) { print( ERROR, "asprintf: %s", strerror(errno) ); name=NULL; } _send_to_graveyard(c->tid, name); #endif pthread_mutex_lock(&(c->conn->children.control.mutex)); list_del(&(c->conn->children.list), (node *)c); pthread_mutex_unlock(&(c->conn->children.control.mutex)); pthread_cond_broadcast(&(c->conn->children.control.cond)); if(wait_res!=c->pid) kill(c->pid, 9); // SIGKILL, process didn't returned as expected if(c->handler->have_stdout) close(c->stdout_fd); if(c->handler->have_stdin) close(c->stdin_fd); close(c->stderr_fd); free(c); return ret; }