int main() { int flags = fcntl(0, F_GETFL, 0); fcntl(0, F_SETFL, flags | O_NONBLOCK); char buffer[1024]; getcwd(directory, 1024); List* l = list_constructor(); command_t command; reset_command(&command); prompt(); while(1) { if(read(0, buffer, 1023) > 0) { // read the command into the struct read_command(buffer, &command); // parse the command for background execution, change directory, or file redirection parse_command(&command); /* call the command handler. this will execute the command. handles both background and non-background */ int pid = command_handler(&command, l); if(pid == 0) { return 0; } // reset the command. reset_command(&command); } // check for background commands and remove them from the wait list. check_background_commands(l); } return 0; }
void test_vector() { vector<string> default_constructor; vector<string> size_constructor(20); vector<string> list_constructor({ string("hello"), string(" world"), string(" second "), string("test") }); print(default_constructor); print(size_constructor); print(list_constructor); string pushback_str("this string is being pushed back"); vector<string> pushback_normal; pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); pushback_normal.push_back(pushback_str); print(pushback_normal); auto check_operators = [](const vector<string>& vec, int32 start, size_t length) { assert(vec.size() == length); bool error_found = false; for (size_t i = 0; i < vec.size(); ++i) { if (atoi(vec[i].c_str()) != start + i) { error_found = true; printf("Error in pushback or subscript operators, value: %u\n", i); } } if (error_found == false) printf("subscript and pushback operators successful\n"); }; auto itostr = [&check_operators](vector<string>& vec, int32 start, int32 length){ char_t buffer[256]; for (int32 i = start; i < start + length; ++i) { vec.push_back(_itoa(i, buffer, 10)); } check_operators(vec, start, length); }; int32 start = -10; int32 length = 45; vector<string> pushback_rvalue; itostr(pushback_rvalue, start, length); print(pushback_rvalue); //test copy construct vector<string> copy_normal(pushback_normal); vector<string> assign_normal; assign_normal = pushback_normal; printf("\ntest copy normal:\n"); print(copy_normal); printf("\ntest assign normal:\n"); print(assign_normal); auto run_test = [](vector<string>& vec, string test_name, void(*callback)(vector<string>&)) { printf("\ntest %s:\n", test_name.c_str()); printf("before:\n"); print(vec); callback(vec); printf("after:\n"); print(vec); }; auto clear = [](vector<string>& vec) { vec.clear(); }; auto emplace_back = [](vector<string>& vec) { vec.emplace_back("emp1", "emp2", "emp3"); }; auto reset = [](vector<string>& vec) { vec.reset(); }; auto reserve200 = [](vector<string>& vec) { vec.reserve(200); }; auto reserve500 = [](vector<string>& vec) { vec.reserve(500); }; run_test(pushback_normal, "emplace back", emplace_back); run_test(pushback_rvalue, "emplace back", emplace_back); run_test(pushback_normal, "reserve200", reserve200); run_test(pushback_rvalue, "reserve500", reserve500); run_test(pushback_normal, "clear", clear); run_test(pushback_rvalue, "clear", clear); run_test(pushback_normal, "emplace back", emplace_back); run_test(pushback_rvalue, "emplace back", emplace_back); run_test(pushback_normal, "reset", reset); run_test(pushback_rvalue, "reset", reset); run_test(pushback_normal, "reserve200", reserve200); run_test(pushback_rvalue, "reserve500", reserve500); }