public void ValidCalcTest() { string[] formules = { "not a | b&c" }; Dictionary <string, BitArray>[] args = { new Dictionary <string, BitArray> { ["a"] = BitArrayEx.From(false, false, false, false, true, true, true, true), ["b"] = BitArrayEx.From(false, false, true, true, false, false, true, true), ["c"] = BitArrayEx.From(false, true, false, true, false, true, false, true) }, }; BitArray[] expected = { BitArrayEx.From(true, true, true, true, false, false, false, true) }; for (int i = 0; i < formules.Length; ++i) { bool parsed = FormulaParser.TryParse(formules[i], out Formula formula); Assert.IsTrue(parsed); BitArray res = formula.Calc(args[i]); Assert.IsTrue(expected[i].EqualsEx(res), $"expected: {expected[i].ToStringEx()}, result: {res.ToStringEx()}"); } }
public void ValidParseTest() { string[] formules = { "xorin xor c", " !ABC|crab=paa ", "not notable", "[((a))]", "(a=b)or a&b", "a=!bb|cc", "!!b", }; Formula[] expected = { new Formula(new Variable("xorin"), new Variable("c"), BinOp.Xor), new Formula(new Variable("ABC"), UnaryOp.Inversion, new Variable("crab"),BinOp.Disjunction, new Variable("paa"), BinOp.Equivalent), new Formula(new Variable("notable"), UnaryOp.Inversion), new Formula(new Variable("a")), new Formula(new Variable("a"), new Variable("b"), BinOp.Equivalent, new Variable("a"), new Variable("b"), BinOp.Conjunction,BinOp.Disjunction), new Formula(new Variable("a"), new Variable("bb"), UnaryOp.Inversion, new Variable("cc"), BinOp.Disjunction, BinOp.Equivalent), new Formula(new Variable("b"), UnaryOp.Inversion, UnaryOp.Inversion), }; for (int i = 0; i < formules.Length; ++i) { bool parsed = FormulaParser.TryParse(formules[i], out Formula res); Assert.IsTrue(parsed, $"Formula: {formules[i]}"); Assert.AreEqual(expected[i], res); } }
public void InvalidParseTest() { string[] formules = { "[(a])", "a xo val", "a- >b", "a ->| b", "(", ")", "(a|b", "a|b)", "! or b", }; for (int i = 0; i < formules.Length; ++i) { bool parsed = FormulaParser.TryParse(formules[i], out var _); Assert.IsFalse(parsed, $"Formula: {formules[i]}"); } }
static void Main(string[] args) { //D:\_Google_Synchronized_\Synchronized\Projects_Sync\C#_Projects\Lab0614\delete_this_later\source.txt //D:\_Google_Synchronized_\Synchronized\Projects_Sync\C#_Projects\Lab0614\delete_this_later\results.txt if (args.Length != 2) { Console.WriteLine("Args count must be 2."); return; } Console.WriteLine("Start input, output."); Console.WriteLine($"Input file: {args[0]}"); Console.WriteLine($"Output file: {args[1]}"); try { using (StreamReader input = new StreamReader(args[0])) using (StreamWriter output = new StreamWriter(args[1])) { string line; while ((line = input.ReadLine()) is object) { Console.WriteLine($"Start parse formula: {line}"); output.Write($"Formula: {line}\n"); bool parsed = FormulaParser.TryParse(line, out Formula formula); if (parsed) { Console.WriteLine("Formula parsed successfully"); formula.MakePerfectNormalForms(out string PDNF, out string PCNF); output.Write($"\tPDNF: {PDNF}\n"); output.Write($"\tPCNF: {PCNF}\n"); Console.WriteLine($"PDNF: {PDNF}"); Console.WriteLine($"PCNF: {PCNF}"); } else { Console.WriteLine("Parse error."); output.Write("\tParse error.\n"); } output.Write("\n"); } } Console.WriteLine("Results saved."); } catch (IOException e) { Console.WriteLine($"IO error: {e}"); } catch (ArgumentException) { Console.WriteLine("At least one path is empty."); } catch (UnauthorizedAccessException) { Console.WriteLine("Chtoo??"); } }