testUVProcessChainHelper.cxx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <chrono>
  2. #include <iostream>
  3. #include <set>
  4. #include <sstream>
  5. #include <string>
  6. #include <thread>
  7. #include <cctype>
  8. #include <cstdlib>
  9. std::string getStdin()
  10. {
  11. char buffer[1024];
  12. std::ostringstream str;
  13. do {
  14. std::cin.read(buffer, 1024);
  15. str.write(buffer, std::cin.gcount());
  16. } while (std::cin.gcount() > 0);
  17. return str.str();
  18. }
  19. int main(int argc, char** argv)
  20. {
  21. if (argc < 2) {
  22. return -1;
  23. }
  24. std::string command = argv[1];
  25. if (command == "echo") {
  26. std::this_thread::sleep_for(std::chrono::milliseconds(3000));
  27. std::cout << "HELLO world!" << std::flush;
  28. std::cerr << "1" << std::flush;
  29. return 0;
  30. }
  31. if (command == "capitalize") {
  32. std::this_thread::sleep_for(std::chrono::milliseconds(9000));
  33. std::string input = getStdin();
  34. for (auto& c : input) {
  35. c = static_cast<char>(std::toupper(c));
  36. }
  37. std::cout << input << std::flush;
  38. std::cerr << "2" << std::flush;
  39. return 1;
  40. }
  41. if (command == "dedup") {
  42. // Use a nested scope to free all resources before aborting below.
  43. try {
  44. std::string input = getStdin();
  45. std::set<char> seen;
  46. std::string output;
  47. for (auto c : input) {
  48. if (!seen.count(c)) {
  49. seen.insert(c);
  50. output += c;
  51. }
  52. }
  53. std::cout << output << std::flush;
  54. std::cerr << "3" << std::flush;
  55. } catch (...) {
  56. }
  57. // On Windows, the exit code of abort() is different between debug and
  58. // release builds, and does not yield a term_signal in libuv in either
  59. // case. For the sake of simplicity, we just return another non-zero code.
  60. #ifdef _WIN32
  61. return 2;
  62. #else
  63. std::abort();
  64. #endif
  65. }
  66. return -1;
  67. }