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

694 rivejä
27 KiB

  1. #include "domain/control_logic_model.h"
  2. #include "domain/hmi_control_registry.h"
  3. #include "domain/hmi_model.h"
  4. #include "domain/project_model.h"
  5. #include "domain/register_address.h"
  6. #include "domain/register_repository.h"
  7. #include "domain/runtime_state.h"
  8. #include <cstdint>
  9. #include <exception>
  10. #include <iostream>
  11. #include <set>
  12. #include <stdexcept>
  13. #include <string>
  14. namespace {
  15. void require(bool condition, const std::string &message)
  16. {
  17. if (!condition)
  18. {
  19. throw std::runtime_error(message);
  20. }
  21. }
  22. void testRegisterAddressBoundaries()
  23. {
  24. // 覆盖 M/D 地址允许范围及未知枚举值的拒绝路径
  25. require(RegisterAddress{RegisterArea::M, 0}.isValid(),
  26. "M0 must be valid");
  27. require(RegisterAddress{RegisterArea::D, 4000}.isValid(),
  28. "D4000 must be valid");
  29. require(!(RegisterAddress{RegisterArea::M, -1}.isValid()),
  30. "negative register index must be rejected");
  31. require(!(RegisterAddress{RegisterArea::D, 4001}.isValid()),
  32. "register index above 4000 must be rejected");
  33. require(!(RegisterAddress{static_cast<RegisterArea>(99), 0}.isValid()),
  34. "unknown register area must be rejected");
  35. }
  36. void testRegisterAddressParsing()
  37. {
  38. const RegisterAddressParseResult m0 = parseRegisterAddress(" m0 ");
  39. require(m0.succeeded && m0.address == RegisterAddress{RegisterArea::M, 0},
  40. "register parser must trim and normalize lowercase M addresses");
  41. const RegisterAddressParseResult d4000 = parseRegisterAddress("D4000");
  42. require(d4000.succeeded
  43. && d4000.address == RegisterAddress{RegisterArea::D, 4000},
  44. "register parser must accept the maximum D address");
  45. require(parseRegisterAddress("").error == RegisterAddressParseError::Empty,
  46. "register parser must distinguish empty input");
  47. require(parseRegisterAddress("X0").error
  48. == RegisterAddressParseError::UnsupportedArea,
  49. "register parser must reject unsupported areas");
  50. require(parseRegisterAddress("M1.0").error
  51. == RegisterAddressParseError::InvalidFormat,
  52. "register parser must reject non-decimal indices");
  53. require(parseRegisterAddress("D4001").error
  54. == RegisterAddressParseError::OutOfRange,
  55. "register parser must reject addresses above the project range");
  56. }
  57. void testRegisterRepositorySeparatesAreas()
  58. {
  59. // 验证离线仓库不会把 M 位和 D 字交叉解释
  60. VirtualRegisterRepository repository;
  61. const RegisterAddress m0{RegisterArea::M, 0};
  62. const RegisterAddress d0{RegisterArea::D, 0};
  63. require(repository.writeBit(m0, true).succeeded, "M bit write must succeed");
  64. require(repository.readBit(m0).value, "M bit read must return written value");
  65. require(repository.writeWord(d0, static_cast<std::int16_t>(-123)).succeeded,
  66. "D word write must succeed");
  67. require(repository.readWord(d0).value == -123, "D word read must return written value");
  68. require(repository.readBit(d0).error == RegisterError::AreaMismatch,
  69. "D address must not be read as a bit");
  70. require(repository.readWord(m0).error == RegisterError::AreaMismatch,
  71. "M address must not be read as a word");
  72. }
  73. void testHmiControlRegistryCompleteness()
  74. {
  75. std::set<const HmiControlDescriptor *> descriptors;
  76. std::set<std::string> storage_names;
  77. std::set<std::string> id_prefixes;
  78. const std::size_t control_type_count =
  79. static_cast<std::size_t>(HmiControlType::Count);
  80. for (std::size_t index = 0; index < control_type_count; ++index)
  81. {
  82. const HmiControlType type = static_cast<HmiControlType>(index);
  83. const HmiControlDescriptor *descriptor =
  84. findHmiControlDescriptor(type);
  85. require(descriptor != nullptr,
  86. "every HMI control type must have one descriptor");
  87. require(descriptor->type == type,
  88. "HMI type lookup must return the requested descriptor");
  89. require(descriptors.emplace(descriptor).second,
  90. "each HMI control type must resolve to a different descriptor");
  91. require(descriptor->storageName != nullptr
  92. && descriptor->storageName[0] != '\0',
  93. "HMI storage names must not be empty");
  94. require(descriptor->displayName != nullptr
  95. && descriptor->displayName[0] != '\0',
  96. "HMI display names must not be empty");
  97. require(descriptor->idPrefix != nullptr
  98. && descriptor->idPrefix[0] != '\0',
  99. "HMI id prefixes must not be empty");
  100. require(descriptor->defaultText != nullptr,
  101. "HMI default text must not be null");
  102. require(descriptor->defaultBounds.width > 0
  103. && descriptor->defaultBounds.height > 0,
  104. "HMI default bounds must have a positive size");
  105. require(findHmiControlDescriptor(descriptor->storageName) == descriptor,
  106. "HMI storage name lookup must return its registered descriptor");
  107. require(storage_names.emplace(descriptor->storageName).second,
  108. "HMI storage names must be unique");
  109. require(id_prefixes.emplace(descriptor->idPrefix).second,
  110. "HMI id prefixes must be unique");
  111. const std::optional<RegisterArea> binding_area =
  112. hmiBindingArea(descriptor->bindingKind);
  113. switch (descriptor->runtimeValueKind)
  114. {
  115. case HmiRuntimeValueKind::Bit:
  116. {
  117. require(descriptor->bindingKind == HmiBindingKind::Bit
  118. && binding_area == RegisterArea::M,
  119. "bit runtime controls must bind to the M area");
  120. break;
  121. }
  122. case HmiRuntimeValueKind::Word:
  123. {
  124. require(descriptor->bindingKind == HmiBindingKind::Word
  125. && binding_area == RegisterArea::D,
  126. "word runtime controls must bind to the D area");
  127. break;
  128. }
  129. case HmiRuntimeValueKind::None:
  130. {
  131. require(descriptor->bindingKind == HmiBindingKind::None
  132. && !binding_area.has_value(),
  133. "static controls must not declare a register binding");
  134. break;
  135. }
  136. }
  137. require(descriptor->requiresBindingForRunning
  138. == (descriptor->runtimeValueKind
  139. != HmiRuntimeValueKind::None),
  140. "runtime value controls must require a configured binding");
  141. }
  142. require(findHmiControlDescriptor(HmiControlType::Count) == nullptr,
  143. "the HMI control count marker must not be registered");
  144. require(findHmiControlDescriptor(static_cast<HmiControlType>(99)) == nullptr,
  145. "unknown HMI control types must not resolve");
  146. require(findHmiControlDescriptor("unknown") == nullptr,
  147. "unknown HMI storage names must not resolve");
  148. require(hmiBindingArea(HmiBindingKind::Bit) == RegisterArea::M,
  149. "bit bindings must use the M area");
  150. require(hmiBindingArea(HmiBindingKind::Word) == RegisterArea::D,
  151. "word bindings must use the D area");
  152. require(!hmiBindingArea(HmiBindingKind::None).has_value(),
  153. "controls without bindings must not resolve a register area");
  154. }
  155. Project makeValidProject()
  156. {
  157. // 构造包含 HMI 绑定和完整梯形图网络的最小合法工程作为测试基线
  158. HmiControl start_button;
  159. start_button.id = "start-button";
  160. start_button.type = HmiControlType::Button;
  161. start_button.text = "Start";
  162. start_button.binding = {RegisterArea::M, 0};
  163. HmiPage page;
  164. page.id = "main-page";
  165. page.name = "Main";
  166. page.controls.push_back(start_button);
  167. LogicNode contact;
  168. contact.id = "start-contact";
  169. contact.config = ContactNodeConfig{
  170. RegisterAddress{RegisterArea::M, 0},
  171. ContactMode::NormallyOpen};
  172. LogicNode coil;
  173. coil.id = "run-coil";
  174. coil.config = CoilNodeConfig{
  175. RegisterAddress{RegisterArea::M, 1},
  176. CoilMode::Normal};
  177. ControlLogic logic;
  178. logic.id = "start-logic";
  179. logic.name = "Start logic";
  180. LadderRung rung;
  181. rung.id = "rung-1";
  182. rung.name = "Network 1";
  183. rung.condition = ConditionExpression::fromNode(contact);
  184. rung.output = coil;
  185. logic.rungs.push_back(rung);
  186. Project project;
  187. project.metadata = {"sample-project", "Sample project", "1.0"};
  188. project.hmiPages.push_back(page);
  189. project.initialHmiPageId = page.id;
  190. project.controlLogics.push_back(logic);
  191. return project;
  192. }
  193. void testMultiPageAndLogicDomainRules()
  194. {
  195. Project project = makeValidProject();
  196. HmiPage settings;
  197. settings.id = "settings-page";
  198. settings.name = "Settings";
  199. project.hmiPages.push_back(settings);
  200. HmiControl label;
  201. label.id = "title";
  202. label.type = HmiControlType::Label;
  203. label.text = "Machine";
  204. project.hmiPages.front().controls.push_back(label);
  205. require(project.validate(), "an unbound label must be a valid static control");
  206. project.hmiPages.front().controls.back().binding =
  207. RegisterAddress{RegisterArea::M, 10};
  208. require(!project.validate(), "labels must reject register bindings");
  209. project = makeValidProject();
  210. project.hmiPages.push_back(settings);
  211. HmiControl jump;
  212. jump.id = "settings-jump";
  213. jump.type = HmiControlType::PageJump;
  214. jump.text = "Settings";
  215. jump.pageJump = HmiPageJumpConfig{settings.id};
  216. project.hmiPages.front().controls.push_back(jump);
  217. require(project.validate() && project.validateForRunning(),
  218. "a page jump must resolve its target by stable page id");
  219. project.hmiPages.front().controls.back().pageJump->targetPageId = "missing";
  220. require(!project.validate(), "a page jump must reject a missing target page");
  221. project = makeValidProject();
  222. project.initialHmiPageId = "missing";
  223. require(!project.validate(), "the initial HMI page id must resolve to a page");
  224. project = makeValidProject();
  225. HmiPage duplicate_name = settings;
  226. duplicate_name.name = project.hmiPages.front().name;
  227. project.hmiPages.push_back(duplicate_name);
  228. require(!project.validate(), "HMI page names must be unique");
  229. project = makeValidProject();
  230. ControlLogic disabled_draft;
  231. disabled_draft.id = "draft-logic";
  232. disabled_draft.name = "Draft logic";
  233. disabled_draft.enabled = false;
  234. disabled_draft.rungs.push_back(
  235. {"rung-1", "Draft network", {}, std::nullopt, std::nullopt});
  236. project.controlLogics.push_back(disabled_draft);
  237. require(project.validateForRunning(),
  238. "a disabled draft logic must not block offline running");
  239. project.controlLogics.back().name = project.controlLogics.front().name;
  240. require(!project.validate(), "control logic names must be unique");
  241. project = makeValidProject();
  242. project.hmiPages.front().controls.front().type =
  243. static_cast<HmiControlType>(99);
  244. project.hmiPages.front().controls.front().binding.reset();
  245. require(!project.validate(), "unknown HMI control types must be rejected");
  246. }
  247. void testLogicNodeConfigurationBoundaries()
  248. {
  249. // 触点只能绑定 M 区,数值比较只能绑定 D 区
  250. LogicNode contact;
  251. contact.id = "contact";
  252. contact.config = ContactNodeConfig{
  253. RegisterAddress{RegisterArea::M, 0},
  254. ContactMode::NormallyOpen};
  255. require(contact.validate(), "contact node bound to M address must be valid");
  256. contact.config = ContactNodeConfig{
  257. RegisterAddress{RegisterArea::D, 0},
  258. ContactMode::NormallyOpen};
  259. require(!contact.validate(), "contact node bound to D address must be rejected");
  260. LogicNode comparison;
  261. comparison.id = "comparison";
  262. comparison.config = CompareNodeConfig{
  263. RegisterAddress{RegisterArea::D, 0},
  264. ComparisonOperator::GreaterThan,
  265. static_cast<std::int16_t>(100)};
  266. require(comparison.validate(), "comparison node bound to D address must be valid");
  267. }
  268. void testTimerAndCommentBoundaries()
  269. {
  270. LogicNode edge;
  271. edge.id = "edge";
  272. edge.config = EdgeContactNodeConfig{
  273. RegisterAddress{RegisterArea::M, 0}, EdgeMode::Rising};
  274. require(edge.validate(), "a valid rising edge contact must pass validation");
  275. LogicNode timer_contact;
  276. timer_contact.id = "timer-contact";
  277. timer_contact.config = TimerContactNodeConfig{
  278. TimerAddress{4000}, ContactMode::NormallyOpen};
  279. require(timer_contact.validate(), "T4000 must be a valid timer contact");
  280. timer_contact.config = TimerContactNodeConfig{
  281. TimerAddress{-1}, ContactMode::NormallyOpen};
  282. require(!timer_contact.validate(), "a negative T address must be rejected");
  283. LogicNode ton;
  284. ton.id = "ton";
  285. ton.config = TonNodeConfig{TimerAddress{0}, TonNodeConfig::kMinimumPresetMs};
  286. require(ton.validate(), "the minimum TON preset must be valid");
  287. ton.config = TonNodeConfig{TimerAddress{0}, 0};
  288. require(!ton.validate(), "a zero TON preset must be rejected");
  289. ton.config = TonNodeConfig{
  290. TimerAddress{0}, TonNodeConfig::kMaximumPresetMs + 1};
  291. require(!ton.validate(), "an oversized TON preset must be rejected");
  292. RegisterComment comment{RegisterAddress{RegisterArea::M, 0}, "启动按钮"};
  293. require(comment.validate(), "a nonblank register comment must be valid");
  294. comment.text = " \t";
  295. require(!comment.validate(), "a blank register comment must be rejected");
  296. Project project = makeValidProject();
  297. project.registerComments = {
  298. {RegisterAddress{RegisterArea::M, 0}, "启动按钮"},
  299. {RegisterAddress{RegisterArea::M, 0}, "重复地址"}};
  300. require(!project.validate(), "duplicate register comments must be rejected");
  301. }
  302. LadderRung makeTimerRung(
  303. const std::string &rung_id,
  304. const std::string &condition_id,
  305. const std::string &output_id,
  306. int timer_index,
  307. int preset_ms)
  308. {
  309. LogicNode condition;
  310. condition.id = condition_id;
  311. condition.config = ContactNodeConfig{
  312. RegisterAddress{RegisterArea::M, timer_index}, ContactMode::NormallyOpen};
  313. LogicNode output;
  314. output.id = output_id;
  315. output.config = TonNodeConfig{TimerAddress{timer_index}, preset_ms};
  316. LadderRung rung;
  317. rung.id = rung_id;
  318. rung.name = rung_id;
  319. rung.condition = ConditionExpression::fromNode(condition);
  320. rung.output = output;
  321. return rung;
  322. }
  323. void testTimerReferencesForRunning()
  324. {
  325. ControlLogic valid;
  326. valid.id = "timer-valid";
  327. valid.name = "Timer valid";
  328. valid.rungs.push_back(makeTimerRung("rung-0", "input-0", "ton-0", 0, 100));
  329. require(validateTimerReferencesForRunning({valid}),
  330. "a timer contact-free TON network must pass timer reference validation");
  331. ControlLogic duplicate = valid;
  332. duplicate.id = "timer-duplicate";
  333. duplicate.name = "Timer duplicate";
  334. duplicate.rungs.front().output->id = "ton-duplicate";
  335. require(!validateTimerReferencesForRunning({valid, duplicate}),
  336. "the same T must not have two enabled TON drivers");
  337. ControlLogic missing;
  338. missing.id = "timer-missing";
  339. missing.name = "Timer missing";
  340. LogicNode contact;
  341. contact.id = "missing-contact";
  342. contact.config = TimerContactNodeConfig{
  343. TimerAddress{7}, ContactMode::NormallyOpen};
  344. LogicNode output;
  345. output.id = "missing-coil";
  346. output.config = CoilNodeConfig{
  347. RegisterAddress{RegisterArea::M, 7}, CoilMode::Normal};
  348. LadderRung missing_rung;
  349. missing_rung.id = "missing-rung";
  350. missing_rung.name = "Missing rung";
  351. missing_rung.condition = ConditionExpression::fromNode(contact);
  352. missing_rung.output = output;
  353. missing.rungs.push_back(missing_rung);
  354. require(!validateTimerReferencesForRunning({missing}),
  355. "a T contact without an enabled TON driver must be rejected");
  356. missing.enabled = false;
  357. require(validateTimerReferencesForRunning({missing}),
  358. "disabled timer drafts must not block runtime timer validation");
  359. }
  360. LadderRung makeCounterRung(
  361. const std::string &rung_id,
  362. const std::string &output_id,
  363. int counter_index)
  364. {
  365. LogicNode condition;
  366. condition.id = rung_id + "-input";
  367. condition.config = ContactNodeConfig{
  368. RegisterAddress{RegisterArea::M, counter_index},
  369. ContactMode::NormallyOpen};
  370. LogicNode output;
  371. output.id = output_id;
  372. output.config = CounterNodeConfig{
  373. CounterAddress{counter_index},
  374. CounterMode::Up,
  375. RegisterAddress{RegisterArea::D, counter_index},
  376. WordOperand{
  377. WordOperandKind::Constant,
  378. RegisterAddress{RegisterArea::D, 0},
  379. 10},
  380. RegisterAddress{RegisterArea::M, counter_index + 1}};
  381. LadderRung rung;
  382. rung.id = rung_id;
  383. rung.name = rung_id;
  384. rung.condition = ConditionExpression::fromNode(condition);
  385. rung.output = output;
  386. return rung;
  387. }
  388. void testCounterAndDataInstructionBoundaries()
  389. {
  390. require(CounterAddress{0}.isValid() && CounterAddress{4000}.isValid(),
  391. "C0 and C4000 must be valid counter resources");
  392. require(!CounterAddress{-1}.isValid() && !CounterAddress{4001}.isValid(),
  393. "counter resources outside 0 through 4000 must be rejected");
  394. LogicNode counter;
  395. counter.id = "counter";
  396. counter.config = CounterNodeConfig{
  397. CounterAddress{0},
  398. CounterMode::Up,
  399. RegisterAddress{RegisterArea::D, 10},
  400. WordOperand{
  401. WordOperandKind::Register,
  402. RegisterAddress{RegisterArea::D, 11},
  403. 0},
  404. RegisterAddress{RegisterArea::M, 12}};
  405. require(counter.validate(),
  406. "a counter with C identity and external M/D addresses must be valid");
  407. CounterNodeConfig invalid_counter = std::get<CounterNodeConfig>(counter.config);
  408. invalid_counter.currentValueAddress = RegisterAddress{RegisterArea::M, 10};
  409. counter.config = invalid_counter;
  410. require(!counter.validate(), "counter CV must reject M addresses");
  411. LogicNode move;
  412. move.id = "move";
  413. move.config = MoveNodeConfig{
  414. WordOperand{
  415. WordOperandKind::Constant,
  416. RegisterAddress{RegisterArea::D, 0},
  417. -100},
  418. RegisterAddress{RegisterArea::D, 20}};
  419. require(move.validate() && move.isOutput(),
  420. "MOVE with a constant source and D destination must be a valid output");
  421. LogicNode add;
  422. add.id = "add";
  423. add.config = ArithmeticNodeConfig{
  424. ArithmeticOperation::Add,
  425. WordOperand{
  426. WordOperandKind::Register,
  427. RegisterAddress{RegisterArea::D, 20},
  428. 0},
  429. WordOperand{
  430. WordOperandKind::Constant,
  431. RegisterAddress{RegisterArea::D, 0},
  432. 1},
  433. RegisterAddress{RegisterArea::D, 20}};
  434. require(add.validate() && add.isOutput(),
  435. "ADD must allow the same D register as source and destination");
  436. ControlLogic valid;
  437. valid.id = "counter-valid";
  438. valid.name = "Counter valid";
  439. valid.rungs.push_back(makeCounterRung("counter-rung", "ctu-0", 0));
  440. require(validateCounterReferencesForRunning({valid}),
  441. "a counter output without contacts must pass reference validation");
  442. ControlLogic duplicate = valid;
  443. duplicate.id = "counter-duplicate";
  444. duplicate.name = "Counter duplicate";
  445. duplicate.rungs.front().output->id = "ctu-duplicate";
  446. require(!validateCounterReferencesForRunning({valid, duplicate}),
  447. "the same C resource must not have multiple enabled drivers");
  448. ControlLogic missing;
  449. missing.id = "counter-missing";
  450. missing.name = "Counter missing";
  451. LogicNode missing_contact;
  452. missing_contact.id = "missing-counter-contact";
  453. missing_contact.config = CounterContactNodeConfig{
  454. CounterAddress{7}, ContactMode::NormallyOpen};
  455. LogicNode output;
  456. output.id = "missing-counter-coil";
  457. output.config = CoilNodeConfig{
  458. RegisterAddress{RegisterArea::M, 7}, CoilMode::Normal};
  459. LadderRung missing_rung;
  460. missing_rung.id = "missing-counter-rung";
  461. missing_rung.name = "Missing counter rung";
  462. missing_rung.condition = ConditionExpression::fromNode(missing_contact);
  463. missing_rung.output = output;
  464. missing.rungs.push_back(missing_rung);
  465. require(!validateCounterReferencesForRunning({missing}),
  466. "a C contact without an enabled counter driver must be rejected");
  467. }
  468. void testLadderLogicBoundaries()
  469. {
  470. LogicNode stop;
  471. stop.id = "stop";
  472. stop.config = ContactNodeConfig{
  473. RegisterAddress{RegisterArea::M, 1},
  474. ContactMode::NormallyClosed};
  475. LogicNode start;
  476. start.id = "start";
  477. start.config = ContactNodeConfig{
  478. RegisterAddress{RegisterArea::M, 0},
  479. ContactMode::NormallyOpen};
  480. LogicNode run_contact;
  481. run_contact.id = "run-contact";
  482. run_contact.config = ContactNodeConfig{
  483. RegisterAddress{RegisterArea::M, 1},
  484. ContactMode::NormallyOpen};
  485. LogicNode coil;
  486. coil.id = "run-coil";
  487. coil.config = CoilNodeConfig{
  488. RegisterAddress{RegisterArea::M, 1},
  489. CoilMode::Normal};
  490. ControlLogic logic;
  491. logic.id = "hold-logic";
  492. logic.name = "Hold logic";
  493. ConditionExpression start_parallel;
  494. start_parallel.id = "parallel-start";
  495. start_parallel.kind = ConditionExpressionKind::Parallel;
  496. start_parallel.children = {
  497. ConditionExpression::fromNode(start),
  498. ConditionExpression::fromNode(run_contact)};
  499. ConditionExpression root;
  500. root.id = "series-root";
  501. root.kind = ConditionExpressionKind::Series;
  502. root.children = {
  503. ConditionExpression::fromNode(stop),
  504. start_parallel};
  505. LadderRung rung;
  506. rung.id = "rung-1";
  507. rung.name = "Self hold";
  508. rung.condition = root;
  509. rung.output = coil;
  510. logic.rungs.push_back(rung);
  511. require(logic.validate(), "stop AND (start OR run) self-hold ladder must be valid");
  512. logic.rungs.front().condition->children.front() =
  513. ConditionExpression::fromNode(coil);
  514. require(!logic.validate(), "a ladder condition expression must reject coils");
  515. logic.rungs.front().condition = root;
  516. logic.rungs.front().output = start;
  517. require(!logic.validate(), "a ladder output must be a coil");
  518. logic.rungs.front().output.reset();
  519. require(logic.validate(), "incomplete ladder may remain in an editable draft");
  520. require(!logic.validateForRunning(),
  521. "conditions without an output must block runtime validation");
  522. LadderRung empty_rung{
  523. "rung-empty", "Empty network", {}, std::nullopt, std::nullopt};
  524. require(empty_rung.validate(), "an empty editing network must be valid");
  525. empty_rung.output = coil;
  526. require(empty_rung.validate(), "output-only network may remain in an editable draft");
  527. require(!empty_rung.validateForRunning(),
  528. "an output without conditions must block runtime validation");
  529. logic.rungs.front().output = coil;
  530. logic.rungs.front().condition = root;
  531. logic.rungs.front().condition->children.at(1).children.at(1).node->id = start.id;
  532. require(!logic.validate(), "logic node ids must be unique");
  533. ConditionExpression nested_parallel;
  534. nested_parallel.id = "parallel-nested";
  535. nested_parallel.kind = ConditionExpressionKind::Parallel;
  536. nested_parallel.children = {
  537. ConditionExpression::fromNode(start),
  538. root};
  539. require(nested_parallel.validate(),
  540. "nested series and parallel expressions must be valid");
  541. }
  542. void testModelsValidateBindingsAndIdentifiers()
  543. {
  544. // 聚合验证必须拒绝错误绑定、重复标识和越界控件
  545. Project project = makeValidProject();
  546. require(project.validate(), "valid project model must pass validation");
  547. project.hmiPages.front().controls.front().binding =
  548. RegisterAddress{RegisterArea::D, 0};
  549. require(!project.validate(), "button bound to D area must be rejected");
  550. project = makeValidProject();
  551. project.hmiPages.push_back(project.hmiPages.front());
  552. require(!project.validate(), "duplicate HMI page id must be rejected");
  553. project = makeValidProject();
  554. project.hmiPages.front().controls.front().bounds.x = -1;
  555. require(!project.validate(), "controls outside the page must be rejected");
  556. project = makeValidProject();
  557. project.hmiPages.front().controls.front().bounds.width = 801;
  558. require(!project.validate(), "controls wider than the page must be rejected");
  559. project = makeValidProject();
  560. project.hmiPages.front().controls.front().properties.emplace("", "value");
  561. require(!project.validate(), "empty HMI property names must be rejected");
  562. project = makeValidProject();
  563. project.hmiPages.front().controls.front().binding.reset();
  564. require(project.validate(), "unbound HMI control must be accepted in a draft");
  565. require(!project.validateForRunning(),
  566. "unbound HMI control must block runtime validation");
  567. project = makeValidProject();
  568. project.controlLogics.front().rungs.front().output->configured = false;
  569. require(project.validate(), "unconfigured ladder node must be accepted in a draft");
  570. require(!project.validateForRunning(),
  571. "unconfigured ladder node must block runtime validation");
  572. }
  573. void testRuntimeStateBoundaries()
  574. {
  575. // 运行模式测试覆盖离线和真机的互斥及 PLC 首读前置条件
  576. RuntimeState state;
  577. require(state.policy().allowsProjectEditing, "editing mode must allow project editing");
  578. require(state.enterOfflineRunning().succeeded, "editing may enter offline running");
  579. require(state.policy().usesVirtualRegisters, "offline mode must use virtual registers");
  580. require(state.policy().runsLogicExecutor, "offline mode must run logic executor");
  581. require(state.enterOnlineRunning(true).error
  582. == ModeTransitionError::MustReturnToEditing,
  583. "offline mode must not directly enter online mode");
  584. require(state.enterEditing().succeeded, "offline mode may return to editing");
  585. require(state.enterOnlineRunning(false).error
  586. == ModeTransitionError::InitialPlcReadRequired,
  587. "online mode must require an initial PLC read");
  588. require(state.enterOnlineRunning(true).succeeded,
  589. "editing may enter online mode after initial PLC read");
  590. require(!state.policy().runsLogicExecutor,
  591. "online mode must keep the software logic executor stopped");
  592. require(state.policy().usesPlcRegisters, "online mode must use PLC registers");
  593. }
  594. } // namespace
  595. int main()
  596. {
  597. try
  598. {
  599. // 每个测试函数独立覆盖一个领域边界,首个异常即终止测试进程
  600. testRegisterAddressBoundaries();
  601. testRegisterAddressParsing();
  602. testRegisterRepositorySeparatesAreas();
  603. testHmiControlRegistryCompleteness();
  604. testLogicNodeConfigurationBoundaries();
  605. testTimerAndCommentBoundaries();
  606. testTimerReferencesForRunning();
  607. testCounterAndDataInstructionBoundaries();
  608. testLadderLogicBoundaries();
  609. testModelsValidateBindingsAndIdentifiers();
  610. testMultiPageAndLogicDomainRules();
  611. testRuntimeStateBoundaries();
  612. }
  613. catch (const std::exception &error)
  614. {
  615. std::cerr << "domain tests failed: " << error.what() << '\n';
  616. return 1;
  617. }
  618. std::cout << "domain tests passed\n";
  619. return 0;
  620. }