Example #1
0
 int TryGeneratedUtf8Encode_F(string input, out string output)
 {
     try
     {
         var res = UTF8Encode_F.Apply(input);
         output = res;
         return 0;
     }
     catch (Exception e)
     {
         output = null;
         return 1;
     }
 }
Example #2
0
        //[TestMethod]
        public void TestUTF8Encode()
        {
            Z3Provider solver = new Z3Provider();
            var stb = BekConverter.BekFileToSTb(solver, sampleDir + "bek/UTF8Encode.bek");
            var tmp = stb.ToST();
            var sft = stb.Explore();
            var sft1 = sft.ToST();
            //sft.ShowGraph();
            //sft1.SaveAsDot("C:/tmp/dot/utf8encode.dot");

            #region data for the popl paper
            var st = sft.ToST();
            int n = st.StateCount;
            var moves = new List<Move<Rule<Expr>>>(st.GetMoves());
            moves.RemoveAll(x => x.Label.IsFinal);
            int m = moves.Count;
            int t = System.Environment.TickCount;
            var st_o_st = st + st;
            int n1 = st_o_st.StateCount;
            var moves1 = new List<Move<Rule<Expr>>>(st_o_st.GetMoves());
            moves1.RemoveAll(y => y.Label.IsFinal);
            int m1 = moves1.Count;
            bool diff = st.Eq1(st_o_st);
            t = System.Environment.TickCount - t;
            #endregion

            var restr = sft.ToST().RestrictDomain(".+");
            restr.AssertTheory();

            Expr inputConst = solver.MkFreshConst("input", restr.InputListSort);
            Expr outputConst = solver.MkFreshConst("output", restr.OutputListSort);

            solver.MainSolver.Assert(restr.MkAccept(inputConst, outputConst));

            //validate correctness for some values against the actual UTF8Encode
            //TBD: validate also exceptional behavior, when the generated code throws an exception 
            //the builtin one must contain the character 0xFFFD
            int K = 50;
            for (int i = 0; i < K; i++)
            {
                var model = solver.MainSolver.GetModel(solver.True, inputConst, outputConst);
                string input = model[inputConst].StringValue;
                string output = model[outputConst].StringValue;

                Assert.IsFalse(string.IsNullOrEmpty(input));
                Assert.IsFalse(string.IsNullOrEmpty(output));

                byte[] encoding = Encoding.UTF8.GetBytes(input);
                char[] chars = Array.ConvertAll(encoding, b => (char)b);
                string output_expected = new String(chars);
                string output_generated = UTF8Encode_F.Apply(input);
                string output_generated2 = UTF8Encode.Apply(input);
                string output_generated3 = UTF8Encode_B.Apply(input);

                Assert.AreEqual<string>(output_expected, output_generated);

                Assert.AreEqual<string>(output_expected, output_generated2);

                Assert.AreEqual<string>(output_expected, output_generated3);

                Assert.AreEqual<string>(output_expected, output);

                //exclude this solution, before picking the next one
                solver.MainSolver.Assert(solver.MkNeq(inputConst, model[inputConst].Value));
            }
        }