public void TestConcatMultipleValues() { var f1 = new ZenFunction <string, string, bool>((w, x) => w + x == "hello"); var f2 = new ZenFunction <string, string, string, bool>((w, x, y) => w + x + y == "hello"); var f3 = new ZenFunction <string, string, string, string, bool>((w, x, y, z) => w + x + y + z == "hello"); var r1 = f1.Find((i1, i2, o) => o); var r2 = f2.Find((i1, i2, i3, o) => o); var r3 = f3.Find((i1, i2, i3, i4, o) => o); Assert.IsTrue(r1.HasValue); Assert.IsTrue(r2.HasValue); Assert.IsTrue(r3.HasValue); string s1 = r1.Value.Item1 + r1.Value.Item2; string s2 = r2.Value.Item1 + r2.Value.Item2 + r2.Value.Item3; string s3 = r3.Value.Item1 + r3.Value.Item2 + r3.Value.Item3 + r3.Value.Item4; Assert.AreEqual("hello", s1); Assert.AreEqual("hello", s2); Assert.AreEqual("hello", s3); Assert.IsTrue(f1.Evaluate("he", "llo")); Assert.IsFalse(f1.Evaluate("he", "ello")); Assert.IsTrue(f2.Evaluate("h", "e", "llo")); Assert.IsFalse(f2.Evaluate("hell", "l", "o")); Assert.IsTrue(f3.Evaluate("h", "e", "ll", "o")); Assert.IsFalse(f3.Evaluate("hel", "l", "l", "o")); }
public void TestAddMultipleValues() { var f1 = new ZenFunction <byte, byte, bool>((w, x) => w + x == 7); var f2 = new ZenFunction <byte, byte, byte, bool>((w, x, y) => w + x + y == 7); var f3 = new ZenFunction <byte, byte, byte, byte, bool>((w, x, y, z) => w + x + y + z == 7); var r1 = f1.Find((i1, i2, o) => o); var r2 = f2.Find((i1, i2, i3, o) => o); var r3 = f3.Find((i1, i2, i3, i4, o) => o); Assert.IsTrue(r1.HasValue); Assert.IsTrue(r2.HasValue); Assert.IsTrue(r3.HasValue); Assert.AreEqual(7, (byte)(r1.Value.Item1 + r1.Value.Item2)); Assert.AreEqual((byte)7, (byte)(r2.Value.Item1 + r2.Value.Item2 + r2.Value.Item3)); Assert.AreEqual(7, (byte)(r3.Value.Item1 + r3.Value.Item2 + r3.Value.Item3 + r3.Value.Item4)); Assert.IsTrue(f1.Evaluate(2, 5)); Assert.IsFalse(f1.Evaluate(2, 6)); Assert.IsTrue(f2.Evaluate(2, 1, 4)); Assert.IsFalse(f2.Evaluate(5, 5, 0)); Assert.IsTrue(f3.Evaluate(1, 1, 2, 3)); Assert.IsFalse(f3.Evaluate(3, 0, 1, 1)); }
public void TestIndexOfEvaluation(string s, string sub, int offset, int expected) { var f = new ZenFunction <string, BigInteger>(s => s.IndexOf(sub, new BigInteger(offset))); Assert.AreEqual((short)expected, f.Evaluate(s)); f.Compile(); Assert.AreEqual((short)expected, f.Evaluate(s)); }
public void TestSubstringEvaluation(string s, int offset, int length, string expected) { var f = new ZenFunction <string, string>(s => s.Substring(new BigInteger(offset), new BigInteger(length))); Assert.AreEqual(expected, f.Evaluate(s)); f.Compile(); Assert.AreEqual(expected, f.Evaluate(s)); }
public void TestReplaceEvaluation(string s, string sub, string replace, string expected) { var f = new ZenFunction <string, string>(s => s.ReplaceFirst(sub, replace)); Assert.AreEqual(expected, f.Evaluate(s)); f.Compile(); Assert.AreEqual(expected, f.Evaluate(s)); }
public void TestLengthEvaluation(string s, int expected) { var f = new ZenFunction <string, BigInteger>(s => s.Length()); Assert.AreEqual(new BigInteger(expected), f.Evaluate(s)); f.Compile(); Assert.AreEqual(new BigInteger(expected), f.Evaluate(s)); }
public void TestContainsEvaluation(string s, string sub, bool expected) { var f = new ZenFunction <string, string, bool>((s1, s2) => s1.Contains(s2)); Assert.AreEqual(expected, f.Evaluate(s, sub)); f.Compile(); Assert.AreEqual(expected, f.Evaluate(s, sub)); }
public void TestMatrixAdd() { var function = new ZenFunction <Matrix3x3, Matrix3x3, Matrix3x3>(Matrix3x3.Add); var input = function.Find((x, y, result) => result.GetField <Matrix3x3, int>("v22") == 10); var x = input.Value.Item1; var y = input.Value.Item2; Assert.AreEqual(x.v22 + y.v22, 10); x.v11 = 1; x.v12 = 2; x.v13 = 3; x.v21 = 4; x.v22 = 5; x.v23 = 6; x.v31 = 7; x.v32 = 8; x.v33 = 9; y.v11 = 1; y.v12 = 2; y.v13 = 3; y.v21 = 4; y.v22 = 5; y.v23 = 6; y.v31 = 7; y.v32 = 8; y.v33 = 9; Assert.AreEqual(2, function.Evaluate(x, y).v11); Assert.AreEqual(4, function.Evaluate(x, y).v12); Assert.AreEqual(6, function.Evaluate(x, y).v13); Assert.AreEqual(8, function.Evaluate(x, y).v21); Assert.AreEqual(10, function.Evaluate(x, y).v22); Assert.AreEqual(12, function.Evaluate(x, y).v23); Assert.AreEqual(14, function.Evaluate(x, y).v31); Assert.AreEqual(16, function.Evaluate(x, y).v32); Assert.AreEqual(18, function.Evaluate(x, y).v33); }
/// <summary> /// Check that the backends agree on the result. /// </summary> /// <param name="function">The function.</param> public static void CheckAgreement(Func <Zen <bool> > function) { foreach (var p in parameters) { var f = new ZenFunction <bool>(function); var result = f.Assert(o => Simplify(o, p), backend: p.Backend); Assert.AreEqual(f.Evaluate(), result); f.Compile(); Assert.AreEqual(f.Evaluate(), result); } }
public void TestDictionaryEvaluateOutput() { var f = new ZenFunction <int, int, Dict <int, int> >((x, y) => EmptyDict <int, int>().Add(x, y)); var d = f.Evaluate(1, 2); // Assert.AreEqual(1, d.Count); Assert.AreEqual(2, d.Get(1)); f.Compile(); d = f.Evaluate(1, 2); Assert.AreEqual(2, d.Get(1)); }
public void TestAclWithLinesEvaluate() { var function = new ZenFunction <IpHeader, Pair <bool, ushort> >(p => ExampleAcl().ProcessProvenance(p, 0)); var result = function.Evaluate(new IpHeader { DstIp = Ip.Parse("8.8.8.8"), SrcIp = Ip.Parse("9.9.9.9") }); Assert.AreEqual(result.Item1, false); Assert.AreEqual(result.Item2, (ushort)2); var packet = function.Find((p, l) => l.Item2() == 3); Assert.AreEqual(3, function.Evaluate(packet.Value).Item2); }
public void TestAtEvaluation(string s, int index, string expected) { var f = new ZenFunction <string, BigInteger, string>((s, idx) => s.At(idx)); Assert.AreEqual(expected, f.Evaluate(s, (ushort)index)); f.Compile(); Assert.AreEqual(expected, f.Evaluate(s, (ushort)index)); if (expected != "") { var x = f.Find((str, idx, o) => And(str == s, o == expected)); Assert.AreEqual(x.Value.Item2, new BigInteger(index)); } }
public void TestDictionaryEvaluateInput() { CheckAgreement <Dict <int, int> >(d => d.ContainsKey(1)); var f = new ZenFunction <Dict <int, int>, bool>(d => d.ContainsKey(1)); var d1 = new Dict <int, int>(); d1.Add(1, 2); Assert.AreEqual(true, f.Evaluate(d1)); var d2 = new Dict <int, int>(); d2.Add(2, 1); Assert.AreEqual(false, f.Evaluate(d2)); }
public void VerifyAclProvenance() { var f = new ZenFunction <IpHeader, Pair <bool, ushort> >(h => this.acl.ProcessProvenance(h)); var packet = f.Find((p, o) => o.Item2() == (ushort)(this.acl.Lines.Length + 1), backend: this.Backend); f.Evaluate(packet.Value); }
public void TestLength(string s, int expected) { var f = new ZenFunction <FiniteString, ushort>(fs => fs.Length()); var actual = f.Evaluate(s); Assert.AreEqual(expected, (int)actual); }
public void TestContains(string s, string sub, bool expected) { var f = new ZenFunction <FiniteString, bool>(fs => fs.Contains(new FiniteString(sub))); var actual = f.Evaluate(s); Assert.AreEqual(expected, actual); }
/// <summary> /// Check that a predicate is not valid. /// </summary> /// <typeparam name="T1">First input type.</typeparam> /// <typeparam name="T2">Second input type.</typeparam> /// <param name="function">The predicate.</param> public static void CheckNotValid <T1, T2>(Func <Zen <T1>, Zen <T2>, Zen <bool> > function) { var selectedParams = SetParameters(typeof(T1), typeof(T2)); foreach (var p in selectedParams) { var f = new ZenFunction <T1, T2, bool>(function); var result = f.Find((i1, i2, o) => Simplify(Not(o), p), listSize: p.ListSize, backend: p.Backend); Assert.IsTrue(result.HasValue); // compare input with evaluation Assert.IsTrue(f.Evaluate(result.Value.Item1, result.Value.Item2)); f.Compile(); Assert.IsTrue(f.Evaluate(result.Value.Item1, result.Value.Item2)); } }
static void oneHopReachability(DVP dvp) { // 1. Evaluate Ip srcAddr = new Ip { Value = 1 }; Ip dstAddr = new Ip { Value = 2 }; ZenFunction <Ip, Ip, bool> f = Function <Ip, Ip, bool>(dvp.OneHopForward); var output = f.Evaluate(srcAddr, dstAddr); Console.WriteLine(output); // 2. Find var input = f.Find((src, dst, result) => And(And(And(src.GetField <Ip, uint>("Value") < 7, dst.GetField <Ip, uint>("Value") < 7), result == False()), src != dst)); Console.WriteLine("Using powerful Zen Find!!!"); Console.WriteLine(input); Console.WriteLine("printing single value"); Console.WriteLine(input.Value); }
public void TestSuffixOf(string s, string sub, bool expected) { var f = new ZenFunction <FiniteString, bool>(fs => Language.EndsWith(fs, new FiniteString(sub))); var actual = f.Evaluate(s); Assert.AreEqual(expected, actual); }
public void TestSubstring(string s, int offset, int len, string expected) { var f = new ZenFunction <FiniteString, FiniteString>(fs => fs.SubString((ushort)offset, (ushort)len)); var actual = f.Evaluate(s).ToString(); Assert.AreEqual(expected, actual); }
public void TestAt(string s, int idx, string expected) { var f = new ZenFunction <FiniteString, FiniteString>(fs => fs.At((ushort)idx)); var actual = f.Evaluate(s).ToString(); Assert.AreEqual(expected, actual); }
public void TestRemoveAll(string s, char c, string expected) { var f = new ZenFunction <FiniteString, FiniteString>(fs => fs.RemoveAll(c)); var actual = f.Evaluate(s).ToString(); Assert.AreEqual(expected, actual); }
public void TestConcat(string s1, string s2, string expected) { var f = new ZenFunction <FiniteString, FiniteString>(fs => fs.Concat(new FiniteString(s2))); var actual = f.Evaluate(s1).ToString(); Assert.AreEqual(expected, actual); }
public void TestIndexOfFind() { var f = new ZenFunction <string, BigInteger>(s => s.IndexOf("a", new BigInteger(0))); var input = f.Find((s, o) => o == new BigInteger(5)); Assert.AreEqual((short)5, f.Evaluate(input.Value)); }
public void TestDoubleCompile4() { var f = new ZenFunction <int, int, int, int, int>((i1, i2, i3, i4) => 1); f.Compile(); f.Compile(); Assert.AreEqual(1, f.Evaluate(0, 0, 0, 0)); }
/// <summary> /// Check that the backends agree on the result. /// </summary> /// <param name="function">The function.</param> public static void CheckAgreement <T1>(Func <Zen <T1>, Zen <bool> > function) { var selectedParams = SetParameters(typeof(T1)); foreach (var p in selectedParams) { var f = new ZenFunction <T1, bool>(function); var result = f.Find((i1, o) => Simplify(o, p), listSize: p.ListSize, backend: p.Backend); if (result.HasValue) { Assert.IsTrue(f.Evaluate(result.Value)); f.Compile(); Assert.IsTrue(f.Evaluate(result.Value)); } } }
public void TestIndexOf(string s, string sub, int offset, int expected) { var f = new ZenFunction <FiniteString, Option <ushort> >(fs => fs.IndexOf(new FiniteString(sub), (ushort)offset)); var idx = f.Evaluate(s); var actual = idx.HasValue ? (int)idx.Value : -1; Assert.AreEqual(expected, actual); }
public void TestDoubleCompile0() { var f = new ZenFunction <int>(() => 1); f.Compile(); f.Compile(); Assert.AreEqual(1, f.Evaluate()); }
public void TestValueTupleEvaluateSwap() { var f = new ZenFunction <int, int, Pair <int, int> >((x, y) => Pair(y, x)); var r = f.Evaluate(1, 2); Assert.AreEqual(r.Item1, 2); Assert.AreEqual(r.Item2, 1); }
public void TestDictionaryEvaluation() { var f = new ZenFunction <Dict <int, int>, Dict <int, int> >(d => d.Add(1, 1).Add(2, 2)); var result = f.Evaluate(new Dict <int, int>()); Assert.AreEqual(result.Get(1), 1); Assert.AreEqual(result.Get(2), 2); }