Advertisement
QinghaoHu

abc

Jun 29th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <csignal>
  6. #include <cstdlib>
  7. #include <chrono>
  8.  
  9. bool running = true;
  10.  
  11. void signalHandler(int signum) {
  12. running = false;
  13. std::cout << "\nExecution stopped by user" << std::endl;
  14. exit(signum);
  15. }
  16.  
  17. bool run_test_case(const std::string &executable, const std::string &input_file, const std::string &expected_output_file, const std::string &actual_output_file, int case_number) {
  18. if (!running) return false;
  19.  
  20. auto start = std::chrono::high_resolution_clock::now();
  21.  
  22. // Execute the executable and write output to the output file
  23. std::string command = executable + " < " + input_file + " > " + actual_output_file;
  24. system(command.c_str());
  25.  
  26. auto end = std::chrono::high_resolution_clock::now();
  27. std::chrono::duration<double> duration = end - start;
  28.  
  29. // Read the expected output
  30. std::ifstream expected_file(expected_output_file);
  31. std::string expected_output((std::istreambuf_iterator<char>(expected_file)), std::istreambuf_iterator<char>());
  32. expected_file.close();
  33.  
  34. // Read the actual output
  35. std::ifstream actual_file(actual_output_file);
  36. std::string actual_output((std::istreambuf_iterator<char>(actual_file)), std::istreambuf_iterator<char>());
  37. actual_file.close();
  38.  
  39. if (actual_output == expected_output) {
  40. std::cout << "Case #" << case_number << ": ACCEPT [Time: " << duration.count() << "s]" << std::endl;
  41. return true;
  42. } else {
  43. std::cout << "Case #" << case_number << ": WRONG ANSWER [Time: " << duration.count() << "s]" << std::endl;
  44. std::cout << "Expected Output:\n" << expected_output << std::endl;
  45. std::cout << "Your Output:\n" << actual_output << std::endl;
  46. return false;
  47. }
  48. }
  49.  
  50. void run_all_tests(const std::string &executable) {
  51. int test_case_index = 0;
  52.  
  53. while (running) {
  54. std::string input_file = "in" + std::to_string(test_case_index);
  55. std::string expected_output_file = "ans" + std::to_string(test_case_index);
  56. std::string actual_output_file = "output" + std::to_string(test_case_index);
  57.  
  58. std::ifstream input(input_file);
  59. std::ifstream expected(expected_output_file);
  60.  
  61. if (!input.good() || !expected.good()) {
  62. break;
  63. }
  64.  
  65. if (!run_test_case(executable, input_file, expected_output_file, actual_output_file, test_case_index)) {
  66. break;
  67. }
  68.  
  69. test_case_index++;
  70. }
  71. }
  72.  
  73. int main(int argc, char *argv[]) {
  74. if (argc != 2) {
  75. std::cerr << "Usage: " << argv[0] << " <executable>" << std::endl;
  76. return 1;
  77. }
  78.  
  79. std::string executable = argv[1];
  80.  
  81. // Register signal handler
  82. signal(SIGINT, signalHandler);
  83.  
  84. run_all_tests(executable);
  85.  
  86. return 0;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement