You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
6.6 KiB

пре 1 година
  1. #include "UserScript/parser.h"
  2. #include <catch2/catch_test_macros.hpp>
  3. #include <ranges>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <random>
  8. #include <algorithm>
  9. TEST_CASE("Can parse") {
  10. std::string code = "/salad (/potato) 12 13 \"hello\" ident\n"
  11. "/salad 12 13 \"hello\" ident\n"
  12. "if(/test)\n"
  13. " /nice\n"
  14. "endif";
  15. std::vector<scripting::script_error> errors;
  16. auto lexed = scripting::ast::lex(code, errors);
  17. auto parsed = scripting::ast::parse(lexed, errors);
  18. if(not errors.empty()) {
  19. for(auto& line : errors) {
  20. std::cout << line.message << "\n at line " << line.location->line_number << ":" << line.location->column_number << "\n";
  21. std::cout << " " << *line.location->line_contents << "\n";
  22. std::cout << " " << std::string(line.location->column_number - 1, ' ') << "^\n";
  23. }
  24. }
  25. auto& block = parsed;
  26. REQUIRE(block.contents.size() == 3);
  27. REQUIRE(std::holds_alternative<std::unique_ptr<scripting::ast::command_expression>>(block.contents.front().contents));
  28. auto& cmd1 = std::get<std::unique_ptr<scripting::ast::command_expression>>(block.contents.front().contents);
  29. REQUIRE(cmd1->name.value == "salad");
  30. REQUIRE(std::holds_alternative<std::unique_ptr<scripting::ast::command_expression>>(std::span(block.contents).subspan(1).front().contents));
  31. auto& cmd2 = std::get<std::unique_ptr<scripting::ast::command_expression>>(std::span(block.contents).subspan(1).front().contents);
  32. REQUIRE(cmd2->name.value == "salad");
  33. REQUIRE(std::holds_alternative<std::unique_ptr<scripting::ast::conditional>>(std::span(block.contents).subspan(2).front().contents));
  34. auto& conditional = std::get<std::unique_ptr<scripting::ast::conditional>>(std::span(block.contents).subspan(2).front().contents);
  35. REQUIRE(std::holds_alternative<std::unique_ptr<scripting::ast::paren_expression>>(conditional->condition->contents));
  36. auto& paren = std::get<std::unique_ptr<scripting::ast::paren_expression>>(conditional->condition->contents)->content;
  37. REQUIRE(std::holds_alternative<std::unique_ptr<scripting::ast::command_expression>>(paren));
  38. auto& condition = std::get<std::unique_ptr<scripting::ast::command_expression>>(paren);
  39. REQUIRE(condition->name.value == "test");
  40. }
  41. TEST_CASE("Can parse 2") {
  42. std::string code = "/salad (/potato) 12+13*16/(/potato)+myvar \"hello\" ident\n"
  43. "/salad 12 13 \"hello\" ident\n"
  44. "if !(/test)\n"
  45. " /nice\n"
  46. "endif";
  47. std::vector<scripting::script_error> errors;
  48. auto lexed = scripting::ast::lex(code, errors);
  49. auto parsed = scripting::ast::parse(lexed, errors);
  50. if(not errors.empty()) {
  51. for(auto& line : errors) {
  52. std::cout << line.message << "\n at line " << line.location->line_number << ":" << line.location->column_number << "\n";
  53. std::cout << " " << *line.location->line_contents << "\n";
  54. std::cout << " " << std::string(line.location->column_number - 1, ' ') << "^\n";
  55. }
  56. }
  57. auto& block = parsed;
  58. REQUIRE(block.contents.size() == 3);
  59. REQUIRE(std::holds_alternative<std::unique_ptr<scripting::ast::command_expression>>(block.contents.front().contents));
  60. auto& cmd1 = std::get<std::unique_ptr<scripting::ast::command_expression>>(block.contents.front().contents);
  61. REQUIRE(cmd1->name.value == "salad");
  62. REQUIRE(cmd1->arguments.size() == 4);
  63. REQUIRE(std::holds_alternative<std::unique_ptr<scripting::ast::command_expression>>(std::span(block.contents).subspan(1).front().contents));
  64. auto& cmd2 = std::get<std::unique_ptr<scripting::ast::command_expression>>(std::span(block.contents).subspan(1).front().contents);
  65. REQUIRE(cmd2->name.value == "salad");
  66. REQUIRE(std::holds_alternative<std::unique_ptr<scripting::ast::conditional>>(std::span(block.contents).subspan(2).front().contents));
  67. auto& conditional = std::get<std::unique_ptr<scripting::ast::conditional>>(std::span(block.contents).subspan(2).front().contents);
  68. REQUIRE(std::holds_alternative<std::unique_ptr<scripting::ast::unary_algebraic_expression>>(conditional->condition->contents));
  69. }
  70. template<auto seed_template = -1>
  71. constexpr auto runner = [](){
  72. std::vector<std::string> sources = {
  73. "../tests/scripts/001.script",
  74. "../tests/scripts/002.script",
  75. "../tests/scripts/003.script",
  76. "../tests/scripts/004.script",
  77. "../tests/scripts/005.script",
  78. };
  79. auto seed = seed_template == -1 ? std::random_device{}() : seed_template;
  80. std::cout << "TEST \"Try to crash the parser 1\" with seed " << seed << std::endl;
  81. std::mt19937_64 rand(seed);
  82. auto mod = [&](std::string tmp) -> std::string {
  83. if(tmp.empty()) return tmp;
  84. auto alter_idx = rand()%tmp.size();
  85. switch(rand()%3) {
  86. case 0:{
  87. tmp.erase(alter_idx);
  88. }break;
  89. case 1:{
  90. tmp[alter_idx] = rand() % 256;
  91. }break;
  92. case 2:{
  93. tmp.insert(alter_idx, 1, char(rand() % 256));
  94. }break;
  95. }
  96. return tmp;
  97. };
  98. auto codes = sources | std::ranges::views::transform([](std::string file){
  99. std::ifstream file_str{file};
  100. std::stringstream read;
  101. read << file_str.rdbuf();
  102. return read.str();
  103. });
  104. std::vector<std::string> vec;
  105. std::copy(codes.begin(), codes.end(), std::back_inserter(vec));
  106. size_t count = 0;
  107. size_t error_cnt = 0;
  108. size_t success_cnt = 0;
  109. constexpr size_t max_count = 5000000;
  110. auto begin = std::chrono::high_resolution_clock::now();
  111. while(count < max_count) {
  112. std::cout << 100.0*double(count)/max_count <<"%"<< std::endl;
  113. for(auto& code : vec) {
  114. std::vector<scripting::script_error> errors;
  115. auto lexed = scripting::ast::lex(code, errors);
  116. auto parsed = scripting::ast::parse(lexed, errors);
  117. if(errors.empty()) success_cnt++;
  118. else error_cnt++;
  119. count++;
  120. }
  121. auto limit = std::min<size_t>(vec.size(), 5000) ;
  122. for(size_t idx = 0; idx < limit; ++idx) {
  123. vec.push_back(mod(vec[idx]));
  124. }
  125. std::transform(vec.begin(), vec.end(), vec.begin(), mod);
  126. std::shuffle(vec.begin(), vec.end(), rand);
  127. if(vec.size()>30000) vec.resize(30000);
  128. }
  129. auto end = std::chrono::high_resolution_clock::now();
  130. std::cout
  131. << "Successes: " << success_cnt << "\n"
  132. << "Failures: " << error_cnt << "\n"
  133. << "Ratio: " << double(success_cnt)/double(success_cnt+error_cnt) << "\n"
  134. << "Total time: " << std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() << "µs\n"
  135. << "Time per iteration: " << (std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin)/(error_cnt+success_cnt)).count() << "ns\n";
  136. };
  137. TEST_CASE("Try to crash the parser (known seeds)") {
  138. runner<1547293717>();
  139. runner<1759257947>();
  140. runner<2909912711>();
  141. runner<1236548620>();
  142. }
  143. TEST_CASE("Try to crash the parser (new seeds)") {
  144. runner<>();
  145. }