Example #1
0
        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);
        }
Example #2
0
        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));
        }
Example #3
0
        public void TestNegatives()
        {
            var f      = new ZenFunction <short, bool>(x => - 2 > -1);
            var result = f.Find((i, o) => o, backend: Backend.DecisionDiagrams);

            Assert.IsFalse(result.HasValue);
        }
Example #4
0
        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));
        }
Example #5
0
        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);
        }
Example #6
0
        public void TestMultiplySolve()
        {
            var f      = new ZenFunction <BigInteger, BigInteger, bool>((x, y) => x * y == new BigInteger(4));
            var inputs = f.Find((x, y, result) => result);

            Assert.AreEqual(4, inputs.Value.Item1 * inputs.Value.Item2);
        }
Example #7
0
        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);
        }
Example #8
0
        public void TestConstructingOptions()
        {
            var f     = new ZenFunction <Option <Int5>, Option <Int5> >(x => Null <Int5>());
            var input = f.Find((x, y) => true);

            Assert.IsTrue(input.HasValue);
        }
Example #9
0
        /// <summary>
        /// Check that a field is set correctly.
        /// </summary>
        /// <typeparam name="T">The object type.</typeparam>
        /// <param name="field">The field name.</param>
        private void CheckWithIsSet <T>(string field)
        {
            var f = new ZenFunction <T, T>(o => o.WithField(field, Constant(1)));
            var r = f.Find((i, o) => o.GetField <T, int>(field) == Constant(0));

            Assert.IsFalse(r.HasValue);
        }
Example #10
0
        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"));
        }
Example #11
0
 public void VerifyRouteMapProvenance()
 {
     var f      = new ZenFunction <Route, Pair <Option <Route>, int> >(this.routeMap.ProcessProvenance);
     var packet = f.Find(
         (p, o) => o.Item2() == (this.routeMap.Lines.Count + 1),
         listSize: this.ListSize,
         backend: this.Backend);
 }
Example #12
0
        public void TestListArbitrary()
        {
            var f     = new ZenFunction <IList <int>, bool>(l => And(l.Contains(1), l.Contains(2)));
            var input = f.Find((l, o) => o, ArbitraryList <int>(2));

            Assert.IsTrue(input.Value.Contains(1));
            Assert.IsTrue(input.Value.Contains(2));
        }
Example #13
0
        public void TestPairSymbolicSwapNested()
        {
            var f      = new ZenFunction <Pair <int, Pair <int, int> >, Pair <int, int> >(x => Pair(x.Item2().Item2(), x.Item2().Item1()));
            var result = f.Find((x, o) => o.Item1() == 2);

            Assert.IsTrue(result.HasValue);
            Assert.AreEqual(2, result.Value.Item2.Item2);
        }
Example #14
0
 public void VerifyRouteMap()
 {
     var f      = new ZenFunction <Route, Option <Route> >(this.routeMap.Process);
     var packet = f.Find(
         (p, o) => Not(o.HasValue()),
         listSize: this.ListSize,
         backend: this.Backend);
 }
Example #15
0
        public void TestOptionFind()
        {
            var zf      = new ZenFunction <Option <int>, bool>(o => o.HasValue());
            var example = zf.Find((i, o) => o);

            Assert.IsTrue(example.HasValue);
            Assert.AreEqual(Option.Some(0), example.Value);
        }
Example #16
0
        public void TestConcatContains()
        {
            var f = new ZenFunction <FiniteString, FiniteString, FiniteString, bool>(
                (fs1, fs2, fs3) => Implies(fs1.Contains(fs3), fs1.Concat(fs2).Contains(fs3)));

            var ex = f.Find((fs1, fs2, fs3, b) => Not(b), listSize: 4, checkSmallerLists: true);

            Assert.IsFalse(ex.HasValue);
        }
Example #17
0
        public void TestIndexOfImpliesContains()
        {
            var f = new ZenFunction <FiniteString, FiniteString, bool>(
                (fs1, fs2) => Implies(fs1.Contains(fs2), fs1.IndexOf(fs2).HasValue()));

            var ex = f.Find((fs1, fs2, b) => Not(b));

            Assert.IsFalse(ex.HasValue);
        }
Example #18
0
        public void TestAtWithinBounds()
        {
            var f = new ZenFunction <FiniteString, ushort, bool>(
                (fs, i) => Implies(i < fs.Length(), fs.At(i) != new FiniteString("")));

            var ex = f.Find((fs1, fs2, b) => Not(b));

            Assert.IsFalse(ex.HasValue);
        }
Example #19
0
        public void TestSuffixImpliesContains()
        {
            var f = new ZenFunction <FiniteString, FiniteString, bool>(
                (fs1, fs2) => Implies(fs2.EndsWith(fs1), fs2.Contains(fs1)));

            var ex = f.Find((fs1, fs2, b) => Not(b));

            Assert.IsFalse(ex.HasValue);
        }
Example #20
0
 public void TestSuffixOfRandom()
 {
     RandomStrings(s =>
     {
         var f  = new ZenFunction <FiniteString, bool>(fs => FiniteString(s).EndsWith(fs));
         var ex = f.Find((fs, b) => b).Value.ToString();
         Assert.IsTrue(s.EndsWith(ex));
     });
 }
Example #21
0
 public void TestContainsRandom()
 {
     RandomStrings(s =>
     {
         var f  = new ZenFunction <FiniteString, bool>(fs => fs.Contains(new FiniteString(s)));
         var ex = f.Find((fs, b) => b).Value.ToString();
         Assert.IsTrue(ex.Contains(s));
     });
 }
Example #22
0
        public void TestLengthRandom()
        {
            var f = new ZenFunction <FiniteString, ushort>(fs => fs.Length());

            RandomStrings(s =>
            {
                var ex = f.Find((fs, l) => l == (ushort)s.Length).Value.ToString();
                Assert.AreEqual(s.Length, ex.Length);
            });
        }
Example #23
0
        public void Test128BitInteger()
        {
            var a = new UInt128(IPAddress.Parse("1000::").GetAddressBytes());
            var b = new UInt128(IPAddress.Parse("2000::").GetAddressBytes());

            var f     = new ZenFunction <UInt128, bool>(x => And(x >= a, x <= b));
            var input = f.Find((x, y) => y, backend: Backend.DecisionDiagrams);

            Assert.AreEqual(a, input.Value);
        }
Example #24
0
 public void TestIndexOfRandom()
 {
     RandomStrings(s =>
     {
         var f   = new ZenFunction <FiniteString, Option <ushort> >(fs => fs.IndexOf(new FiniteString(s)));
         var ex  = f.Find((fs, i) => i.HasValue()).Value.ToString();
         var idx = f.Evaluate(ex);
         Assert.IsTrue(ex.Contains(s));
         Assert.AreEqual(idx.Value, ex.IndexOf(s));
     });
 }
Example #25
0
        /// <summary>
        /// Check a predicate is valid.
        /// </summary>
        /// <typeparam name="T1">First input type..</typeparam>
        /// <typeparam name="T2">Second input type.</typeparam>
        /// <typeparam name="T3">Third input type.</typeparam>
        /// <typeparam name="T4">Fourth input input.</typeparam>
        /// <param name="function">The predicate.</param>
        public static void CheckValid <T1, T2, T3, T4>(Func <Zen <T1>, Zen <T2>, Zen <T3>, Zen <T4>, Zen <bool> > function)
        {
            var selectedParams = SetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4));

            foreach (var p in selectedParams)
            {
                // prove that it is valid
                var f      = new ZenFunction <T1, T2, T3, T4, bool>(function);
                var result = f.Find((i1, i2, i3, i4, o) => Simplify(Not(o), p), listSize: p.ListSize, backend: p.Backend);
                Assert.IsFalse(result.HasValue);

                // compare input with evaluation
                result = f.Find((i1, i2, i3, i4, o) => Simplify(o, p), listSize: p.ListSize, backend: p.Backend);
                Assert.IsTrue(result.HasValue);

                Assert.IsTrue(f.Evaluate(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4));
                f.Compile();
                Assert.IsTrue(f.Evaluate(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4));
            }
        }
Example #26
0
        public void TestUnroll()
        {
            var function = new ZenFunction <LocatedPacket, LocatedPacket>(lp => StepMany(lp, 3));

            var input = function.Find((inputLp, outputLp) =>
                                      And(inputLp.GetNode() == 0,
                                          outputLp.GetNode() == 2,
                                          outputLp.GetHeader().GetDstIp().GetValue() == 4));

            Assert.IsTrue(input.HasValue);
        }
Example #27
0
        public void TestListAnyObjects()
        {
            var f1 = new ZenFunction <IList <Object2>, bool>(l => l.Any(e => e.GetField <Object2, int>("Field1") == 7));
            var f2 = new ZenFunction <IList <Object2>, bool>(l => l.Any(e => e.GetField <Object2, int>("Field1") == 7).Simplify());

            var input1 = f1.Find((i, o) => o);
            var input2 = f2.Find((i, o) => o);

            Assert.IsTrue(input1.Value.Where(x => x.Field1 == 7).Count() > 0);
            Assert.IsTrue(input2.Value.Where(x => x.Field1 == 7).Count() > 0);
        }
Example #28
0
        public void TestListSizeConstraints()
        {
            var zf       = new ZenFunction <IList <byte>, bool>(l => l.Length() == 4);
            var example1 = zf.Find((l, b) => b, listSize: 3);

            Assert.IsFalse(example1.HasValue);

            var zfNested = new ZenFunction <IList <IList <byte> >, bool>(l => l.Any(x => x.Length() == 4));
            var example2 = zfNested.Find((l, b) => b, listSize: 3);

            Assert.IsFalse(example2.HasValue);
        }
Example #29
0
        /// <summary>
        /// Check a predicate is 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 CheckValid <T1, T2>(Func <Zen <T1>, Zen <T2>, Zen <bool> > function)
        {
            // If testing on strings, only use Z3 backend (DD does not support strings)
            var selectedParams = SetParameters(typeof(T1), typeof(T2));

            foreach (var p in selectedParams)
            {
                // prove that it is valid
                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.IsFalse(result.HasValue);

                // compare input with evaluation
                result = f.Find((i1, i2, o) => Simplify(o, p), listSize: p.ListSize, backend: p.Backend);
                Assert.IsTrue(result.HasValue);

                Assert.IsTrue(f.Evaluate(result.Value.Item1, result.Value.Item2));
                f.Compile();
                Assert.IsTrue(f.Evaluate(result.Value.Item1, result.Value.Item2));
            }
        }
Example #30
0
        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);
        }