Example #1
0
        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));
        }
Example #2
0
        public void TestDoubleCompile0()
        {
            var f = new ZenFunction <int>(() => 1);

            f.Compile();
            f.Compile();
            Assert.AreEqual(1, f.Evaluate());
        }
Example #3
0
        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));
        }
Example #4
0
        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));
        }
Example #5
0
        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));
        }
Example #6
0
        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));
        }
Example #7
0
        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));
        }
Example #8
0
        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));
        }
Example #9
0
        /// <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);
            }
        }
Example #10
0
        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));
            }
        }
Example #11
0
        /// <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));
            }
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Zen <int> Multi(Zen <int> x, Zen <int> y)
            {
                return(3 * x + y);
            }

            ZenFunction <int, int, int> function = Function <int, int, int>(Multi);

            function.Compile();
            var output = function.Evaluate(3, 2); // output = 11
            var input  = function.Find((x, y, result) => And(x <= 0, result == 11));

            Console.WriteLine(input.ToString());
        }
Example #13
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));
                }
            }
        }
Example #14
0
        public void TestMultiplication()
        {
            var f      = new ZenFunction <int, int, bool>((a, b) => a * b == 10);
            var inputs = f.Find((a, b, res) => res, backend: ModelChecking.Backend.Z3);

            Assert.IsTrue(inputs.HasValue);
            Assert.AreEqual(10, inputs.Value.Item1 * inputs.Value.Item2);

            Assert.IsTrue(f.Evaluate(5, 2));
            Assert.IsTrue(f.Evaluate(1, 10));
            Assert.IsFalse(f.Evaluate(4, 3));

            f.Compile();

            Assert.IsTrue(f.Evaluate(5, 2));
            Assert.IsTrue(f.Evaluate(1, 10));
            Assert.IsFalse(f.Evaluate(4, 3));
        }
Example #15
0
        static void evaluateReachability(DVP dvp, List <Tuple <int, int> > failedLinks = null)
        {
            if (failedLinks == null)
            {
                failedLinks = new List <Tuple <int, int> >();
            }

            ZenFunction <SimplePacket, IList <Tuple <int, int> >, bool> f = Function <SimplePacket, IList <Tuple <int, int> >, bool>(dvp.Forward);

            f.Compile();

            Console.WriteLine("Evaluating output");
            var correct_input = new SimplePacket
            {
                SrcIp = new Ip {
                    Value = 1
                },
                DstIp = new Ip {
                    Value = 2
                },
            };

            var wrong_input = new SimplePacket
            {
                SrcIp = new Ip {
                    Value = 0
                },
                DstIp = new Ip {
                    Value = 0
                },
            };

            Console.WriteLine("Evaluating correct input: \t" + correct_input);
            var output = f.Evaluate(correct_input, failedLinks);

            Console.WriteLine("\t Reachable? " + output);

            Console.WriteLine("Evaluating wrong input: \t" + wrong_input);
            output = f.Evaluate(wrong_input, failedLinks);
            Console.WriteLine("\t Reachable? " + output);
            Console.WriteLine();
        }
Example #16
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 #17
0
        static void findLinks(DVP dvp, bool desired_result, SimplePacket packet)
        {
            Console.WriteLine("findLinks");

            ZenFunction <SimplePacket, IList <Tuple <int, int> >, bool> f = Function <SimplePacket, IList <Tuple <int, int> >, bool>(dvp.Forward);

            f.Compile();

            // Console.WriteLine("Using FindAll");
            // Console.WriteLine("Number of packets that cannot be delivered in the network:");
            var input = f.FindAll((pkt, failed_links, result) => And(
                                      And(
                                          And(
                                              And(
                                                  pkt.GetDstIp().GetField <Ip, uint>("Value") == packet.DstIp.Value,
                                                  pkt.GetSrcIp().GetField <Ip, uint>("Value") == packet.SrcIp.Value
                                                  ),
                                              pkt.GetDstIp() != pkt.GetSrcIp()),
                                          result == desired_result),
                                      // For DVP, no route requires more then two links to fail (no multiple path routing)
                                      // Therefore, we limit length to 1 here
                                      failed_links.Length() == 1));

            Console.WriteLine("\tCount:\t" + input.Count());
            //Console.WriteLine();
            //Console.WriteLine(input);

            if (input.Count() != 0)
            {
                Console.WriteLine("\tPrinting inputs:");
                foreach (var x in input)
                {
                    Console.Write("\t\t" + x.Item1 + " with List [");
                    foreach (var i in x.Item2)
                    {
                        Console.Write(i + ", ");
                    }
                    Console.WriteLine("]");
                }
            }
        }
Example #18
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 #19
0
        static void findPackets(DVP dvp, bool desired_result, List <Tuple <int, int> > failedLinks = null)
        {
            if (failedLinks == null)
            {
                failedLinks = new List <Tuple <int, int> >();
            }

            Console.WriteLine("findPackets");

            ZenFunction <SimplePacket, IList <Tuple <int, int> >, bool> f = Function <SimplePacket, IList <Tuple <int, int> >, bool>(dvp.Forward);

            f.Compile();

            // Console.WriteLine("Using FindAll");
            // Console.WriteLine("Number of packets that cannot be delivered in the network:");
            Console.WriteLine(failedLinks);
            var input = f.FindAll((pkt, failed_links, result) => And(
                                      And(
                                          And(
                                              And(
                                                  pkt.GetDstIp().GetField <Ip, uint>("Value") < 7,
                                                  pkt.GetSrcIp().GetField <Ip, uint>("Value") < 7
                                                  ),
                                              pkt.GetDstIp() != pkt.GetSrcIp()),
                                          result == desired_result),
                                      failed_links == failedLinks));

            Console.WriteLine("\tCount:\t" + input.Count());
            //Console.WriteLine();
            //Console.WriteLine(input);

            if (input.Count() != 0)
            {
                Console.WriteLine("\tPrinting inputs:");
                foreach (var x in input)
                {
                    Console.WriteLine("\t\t" + x);
                }
            }
        }
Example #20
0
        public void TestCompilation()
        {
            var f = new ZenFunction <Int3, Int3, Int3>((x, y) => x + y);

            f.Compile();
            Assert.AreEqual(new Int3(3), f.Evaluate(new Int3(1), new Int3(2)));

            f = new ZenFunction <Int3, Int3, Int3>((x, y) => x - y);
            f.Compile();
            Assert.AreEqual(new Int3(1), f.Evaluate(new Int3(3), new Int3(2)));

            f = new ZenFunction <Int3, Int3, Int3>((x, y) => x | y);
            f.Compile();
            Assert.AreEqual(new Int3(3), f.Evaluate(new Int3(3), new Int3(2)));

            f = new ZenFunction <Int3, Int3, Int3>((x, y) => x & y);
            f.Compile();
            Assert.AreEqual(new Int3(2), f.Evaluate(new Int3(3), new Int3(2)));

            f = new ZenFunction <Int3, Int3, Int3>((x, y) => x ^ y);
            f.Compile();
            Assert.AreEqual(new Int3(1), f.Evaluate(new Int3(3), new Int3(2)));
        }