1 // 2 // Copyright (c) 2010-2024 Antmicro 3 // 4 // This file is licensed under the MIT License. 5 // Full license text is available in 'licenses/MIT.txt'. 6 // 7 using System.Collections.Generic; 8 using NUnit.Framework; 9 using Antmicro.Renode.PlatformDescription; 10 11 namespace Antmicro.Renode.UnitTests.AccessCondition 12 { 13 [TestFixture] 14 public class ParserTests 15 { 16 [TestCaseSource(nameof(AccessConditionParserTestCases))] ShouldParseAndConvertToDnf(string conditionString, string expectedDnfString)17 public void ShouldParseAndConvertToDnf(string conditionString, string expectedDnfString) 18 { 19 var dnfExpression = AccessConditionParser.ParseCondition(conditionString); 20 Assert.AreEqual(expectedDnfString, dnfExpression.ToString(), $"Input condition: '{conditionString}'"); 21 } 22 AccessConditionParserTestCases()23 private static IEnumerable<TestCaseData> AccessConditionParserTestCases() 24 { 25 // Trivial as the input is already a DNF term 26 yield return new TestCaseData( 27 "a", 28 "(a)" 29 ); 30 // Trivial as the input is already a DNF term 31 yield return new TestCaseData( 32 "!a", 33 "(!a)" 34 ); 35 // Trivial as the input is already a DNF term, but it has superfluous parentheses 36 yield return new TestCaseData( 37 "!((((((a))))))", 38 "(!a)" 39 ); 40 // Trivial as the input is already a DNF term, but it is a double negation 41 yield return new TestCaseData( 42 "!(((!(((a))))))", 43 "(a)" 44 ); 45 // Trivial as the input is already a DNF term 46 yield return new TestCaseData( 47 "!secure && initiator == cpu1", 48 "(!secure && initiator == cpu1)" 49 ); 50 // De Morgan's law yields a single DNF term 51 yield return new TestCaseData( 52 "!(privileged || secure)", 53 "(!privileged && !secure)" 54 ); 55 // De Morgan's law, output is a DNF formula of 2 terms 56 yield return new TestCaseData( 57 "!(privileged && secure)", 58 "(!privileged) || (!secure)" 59 ); 60 // Initiator conditions distributed over the state conditions 61 yield return new TestCaseData( 62 "secure && !privileged && (initiator == cpu1 || initiator == cpu2)", 63 "(secure && !privileged && initiator == cpu1) || (secure && !privileged && initiator == cpu2)" 64 ); 65 // An input condition which results in a fairly large DNF formula (4 terms, each with 3 conditions in it) 66 yield return new TestCaseData( 67 "(secure || busSecure) && (initiator == cpu1 || initiator == cpu2) && privileged", 68 "(secure && initiator == cpu1 && privileged) || (secure && initiator == cpu2 && privileged) || (busSecure && initiator == cpu1 && privileged) || (busSecure && initiator == cpu2 && privileged)" 69 ); 70 } 71 } 72 } 73 74