综合平台编程器项目的远程存储
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.
 
 
 
 

293 lines
11 KiB

  1. #include "domain/control_logic_model.h"
  2. #include "domain/hmi_model.h"
  3. #include "domain/project_model.h"
  4. #include "domain/register_address.h"
  5. #include "domain/register_repository.h"
  6. #include "domain/runtime_state.h"
  7. #include <cstdint>
  8. #include <exception>
  9. #include <iostream>
  10. #include <stdexcept>
  11. #include <string>
  12. namespace {
  13. void require(bool condition, const std::string &message)
  14. {
  15. if (!condition)
  16. {
  17. throw std::runtime_error(message);
  18. }
  19. }
  20. void testRegisterAddressBoundaries()
  21. {
  22. // 覆盖 M/D 地址允许范围及未知枚举值的拒绝路径
  23. require(RegisterAddress{RegisterArea::M, 0}.isValid(),
  24. "M0 must be valid");
  25. require(RegisterAddress{RegisterArea::D, 4000}.isValid(),
  26. "D4000 must be valid");
  27. require(!(RegisterAddress{RegisterArea::M, -1}.isValid()),
  28. "negative register index must be rejected");
  29. require(!(RegisterAddress{RegisterArea::D, 4001}.isValid()),
  30. "register index above 4000 must be rejected");
  31. require(!(RegisterAddress{static_cast<RegisterArea>(99), 0}.isValid()),
  32. "unknown register area must be rejected");
  33. }
  34. void testRegisterRepositorySeparatesAreas()
  35. {
  36. // 验证离线仓库不会把 M 位和 D 字交叉解释
  37. VirtualRegisterRepository repository;
  38. const RegisterAddress m0{RegisterArea::M, 0};
  39. const RegisterAddress d0{RegisterArea::D, 0};
  40. require(repository.writeBit(m0, true).succeeded, "M bit write must succeed");
  41. require(repository.readBit(m0).value, "M bit read must return written value");
  42. require(repository.writeWord(d0, static_cast<std::int16_t>(-123)).succeeded,
  43. "D word write must succeed");
  44. require(repository.readWord(d0).value == -123, "D word read must return written value");
  45. require(repository.readBit(d0).error == RegisterError::AreaMismatch,
  46. "D address must not be read as a bit");
  47. require(repository.readWord(m0).error == RegisterError::AreaMismatch,
  48. "M address must not be read as a word");
  49. }
  50. Project makeValidProject()
  51. {
  52. // 构造包含 HMI 绑定和完整梯形图网络的最小合法工程作为测试基线
  53. HmiControl start_button;
  54. start_button.id = "start-button";
  55. start_button.type = HmiControlType::Button;
  56. start_button.text = "Start";
  57. start_button.binding = {RegisterArea::M, 0};
  58. HmiPage page;
  59. page.id = "main-page";
  60. page.name = "Main";
  61. page.controls.push_back(start_button);
  62. LogicNode contact;
  63. contact.id = "start-contact";
  64. contact.config = ContactNodeConfig{
  65. RegisterAddress{RegisterArea::M, 0},
  66. ContactMode::NormallyOpen};
  67. LogicNode coil;
  68. coil.id = "run-coil";
  69. coil.config = CoilNodeConfig{
  70. RegisterAddress{RegisterArea::M, 1},
  71. CoilMode::Normal};
  72. ControlLogic logic;
  73. logic.id = "start-logic";
  74. logic.name = "Start logic";
  75. LadderRung rung;
  76. rung.id = "rung-1";
  77. rung.name = "Network 1";
  78. rung.condition = ConditionExpression::fromNode(contact);
  79. rung.output = coil;
  80. logic.rungs.push_back(rung);
  81. Project project;
  82. project.metadata = {"sample-project", "Sample project", "1.0"};
  83. project.hmiPages.push_back(page);
  84. project.controlLogics.push_back(logic);
  85. return project;
  86. }
  87. void testLogicNodeConfigurationBoundaries()
  88. {
  89. // 触点只能绑定 M 区,数值比较只能绑定 D 区
  90. LogicNode contact;
  91. contact.id = "contact";
  92. contact.config = ContactNodeConfig{
  93. RegisterAddress{RegisterArea::M, 0},
  94. ContactMode::NormallyOpen};
  95. require(contact.validate(), "contact node bound to M address must be valid");
  96. contact.config = ContactNodeConfig{
  97. RegisterAddress{RegisterArea::D, 0},
  98. ContactMode::NormallyOpen};
  99. require(!contact.validate(), "contact node bound to D address must be rejected");
  100. LogicNode comparison;
  101. comparison.id = "comparison";
  102. comparison.config = CompareNodeConfig{
  103. RegisterAddress{RegisterArea::D, 0},
  104. ComparisonOperator::GreaterThan,
  105. static_cast<std::int16_t>(100)};
  106. require(comparison.validate(), "comparison node bound to D address must be valid");
  107. }
  108. void testLadderLogicBoundaries()
  109. {
  110. LogicNode stop;
  111. stop.id = "stop";
  112. stop.config = ContactNodeConfig{
  113. RegisterAddress{RegisterArea::M, 1},
  114. ContactMode::NormallyClosed};
  115. LogicNode start;
  116. start.id = "start";
  117. start.config = ContactNodeConfig{
  118. RegisterAddress{RegisterArea::M, 0},
  119. ContactMode::NormallyOpen};
  120. LogicNode run_contact;
  121. run_contact.id = "run-contact";
  122. run_contact.config = ContactNodeConfig{
  123. RegisterAddress{RegisterArea::M, 1},
  124. ContactMode::NormallyOpen};
  125. LogicNode coil;
  126. coil.id = "run-coil";
  127. coil.config = CoilNodeConfig{
  128. RegisterAddress{RegisterArea::M, 1},
  129. CoilMode::Normal};
  130. ControlLogic logic;
  131. logic.id = "hold-logic";
  132. logic.name = "Hold logic";
  133. ConditionExpression start_parallel;
  134. start_parallel.id = "parallel-start";
  135. start_parallel.kind = ConditionExpressionKind::Parallel;
  136. start_parallel.children = {
  137. ConditionExpression::fromNode(start),
  138. ConditionExpression::fromNode(run_contact)};
  139. ConditionExpression root;
  140. root.id = "series-root";
  141. root.kind = ConditionExpressionKind::Series;
  142. root.children = {
  143. ConditionExpression::fromNode(stop),
  144. start_parallel};
  145. LadderRung rung;
  146. rung.id = "rung-1";
  147. rung.name = "Self hold";
  148. rung.condition = root;
  149. rung.output = coil;
  150. logic.rungs.push_back(rung);
  151. require(logic.validate(), "stop AND (start OR run) self-hold ladder must be valid");
  152. logic.rungs.front().condition->children.front() =
  153. ConditionExpression::fromNode(coil);
  154. require(!logic.validate(), "a ladder condition expression must reject coils");
  155. logic.rungs.front().condition = root;
  156. logic.rungs.front().output = start;
  157. require(!logic.validate(), "a ladder output must be a coil");
  158. logic.rungs.front().output.reset();
  159. require(logic.validate(), "incomplete ladder may remain in an editable draft");
  160. require(!logic.validateForRunning(),
  161. "conditions without an output must block runtime validation");
  162. LadderRung empty_rung{"rung-empty", "Empty network", std::nullopt, std::nullopt};
  163. require(empty_rung.validate(), "an empty editing network must be valid");
  164. empty_rung.output = coil;
  165. require(empty_rung.validate(), "output-only network may remain in an editable draft");
  166. require(!empty_rung.validateForRunning(),
  167. "an output without conditions must block runtime validation");
  168. logic.rungs.front().output = coil;
  169. logic.rungs.front().condition = root;
  170. logic.rungs.front().condition->children.at(1).children.at(1).node->id = start.id;
  171. require(!logic.validate(), "logic node ids must be unique");
  172. ConditionExpression nested_parallel;
  173. nested_parallel.id = "parallel-nested";
  174. nested_parallel.kind = ConditionExpressionKind::Parallel;
  175. nested_parallel.children = {
  176. ConditionExpression::fromNode(start),
  177. root};
  178. require(nested_parallel.validate(),
  179. "nested series and parallel expressions must be valid");
  180. }
  181. void testModelsValidateBindingsAndIdentifiers()
  182. {
  183. // 聚合验证必须拒绝错误绑定、重复标识和越界控件
  184. Project project = makeValidProject();
  185. require(project.validate(), "valid project model must pass validation");
  186. project.hmiPages.front().controls.front().binding =
  187. RegisterAddress{RegisterArea::D, 0};
  188. require(!project.validate(), "button bound to D area must be rejected");
  189. project = makeValidProject();
  190. project.hmiPages.push_back(project.hmiPages.front());
  191. require(!project.validate(), "duplicate HMI page id must be rejected");
  192. project = makeValidProject();
  193. project.hmiPages.front().controls.front().bounds.x = -1;
  194. require(!project.validate(), "controls outside the page must be rejected");
  195. project = makeValidProject();
  196. project.hmiPages.front().controls.front().bounds.width = 801;
  197. require(!project.validate(), "controls wider than the page must be rejected");
  198. project = makeValidProject();
  199. project.hmiPages.front().controls.front().properties.emplace("", "value");
  200. require(!project.validate(), "empty HMI property names must be rejected");
  201. project = makeValidProject();
  202. project.hmiPages.front().controls.front().binding.reset();
  203. require(project.validate(), "unbound HMI control must be accepted in a draft");
  204. require(!project.validateForRunning(),
  205. "unbound HMI control must block runtime validation");
  206. project = makeValidProject();
  207. project.controlLogics.front().rungs.front().output->configured = false;
  208. require(project.validate(), "unconfigured ladder node must be accepted in a draft");
  209. require(!project.validateForRunning(),
  210. "unconfigured ladder node must block runtime validation");
  211. }
  212. void testRuntimeStateBoundaries()
  213. {
  214. // 运行模式测试覆盖离线和真机的互斥及 PLC 首读前置条件
  215. RuntimeState state;
  216. require(state.policy().allowsProjectEditing, "editing mode must allow project editing");
  217. require(state.enterOfflineRunning().succeeded, "editing may enter offline running");
  218. require(state.policy().usesVirtualRegisters, "offline mode must use virtual registers");
  219. require(state.policy().runsLogicExecutor, "offline mode must run logic executor");
  220. require(state.enterOnlineRunning(true).error
  221. == ModeTransitionError::MustReturnToEditing,
  222. "offline mode must not directly enter online mode");
  223. require(state.enterEditing().succeeded, "offline mode may return to editing");
  224. require(state.enterOnlineRunning(false).error
  225. == ModeTransitionError::InitialPlcReadRequired,
  226. "online mode must require an initial PLC read");
  227. require(state.enterOnlineRunning(true).succeeded,
  228. "editing may enter online mode after initial PLC read");
  229. require(!state.policy().runsLogicExecutor,
  230. "online mode must keep the software logic executor stopped");
  231. require(state.policy().usesPlcRegisters, "online mode must use PLC registers");
  232. }
  233. } // namespace
  234. int main()
  235. {
  236. try
  237. {
  238. // 每个测试函数独立覆盖一个领域边界,首个异常即终止测试进程
  239. testRegisterAddressBoundaries();
  240. testRegisterRepositorySeparatesAreas();
  241. testLogicNodeConfigurationBoundaries();
  242. testLadderLogicBoundaries();
  243. testModelsValidateBindingsAndIdentifiers();
  244. testRuntimeStateBoundaries();
  245. }
  246. catch (const std::exception &error)
  247. {
  248. std::cerr << "domain tests failed: " << error.what() << '\n';
  249. return 1;
  250. }
  251. std::cout << "domain tests passed\n";
  252. return 0;
  253. }