WorldDisassembler.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <boost/test/unit_test.hpp>
  16. #include <sstream>
  17. #include <utility>
  18. #include <vector>
  19. #include "installer/decompiler/ControlFlow.h"
  20. #include "installer/decompiler/world/WorldEngine.h"
  21. BOOST_AUTO_TEST_CASE(TestWorldDisassembler){
  22. std::vector<unsigned char> data(1026, 0);
  23. data[4] = 1;
  24. data[1024] = 0x03;
  25. data[1025] = 0x02;
  26. WorldEngine engine(1);
  27. InstVec instructions;
  28. std::unique_ptr<Disassembler> disassembler = engine.GetDisassembler(instructions);
  29. disassembler->Open(std::move(data));
  30. disassembler->Disassemble();
  31. BOOST_REQUIRE_EQUAL(instructions.size(), 1u);
  32. BOOST_REQUIRE_EQUAL(engine.functions.size(), 1u);
  33. BOOST_CHECK_EQUAL(engine.functions.begin()->second.name, "world_function_1");
  34. ControlFlow control_flow(instructions, engine);
  35. control_flow.CreateGroups();
  36. std::ostringstream generated_script;
  37. std::unique_ptr<CodeGenerator> code_generator
  38. = engine.GetCodeGenerator(instructions, generated_script);
  39. code_generator->Generate(instructions, control_flow.GetGraph());
  40. BOOST_CHECK_NE(
  41. generated_script.str().find(
  42. "System.WorldMap.world_function_1 = function(self)"
  43. ),
  44. std::string::npos
  45. );
  46. BOOST_CHECK_NE(generated_script.str().find("return;"), std::string::npos);
  47. BOOST_CHECK_NE(generated_script.str().find("end"), std::string::npos);
  48. BOOST_CHECK_EQUAL(generated_script.str().find("}\n"), std::string::npos);
  49. }