Пример #1
0
        public void TestPatternMatch()
        {
            {
                Erlang.VarBind binding = new Otp.Erlang.VarBind();
                Erlang.Object  obj     = Erlang.Object.Format("{snapshot, x12, []}");
                Erlang.Object  pat     = Erlang.Object.Format("{snapshot, N, L}");

                Assert.IsTrue(pat.match(obj, binding));
                Erlang.Atom n = binding.find("N") as Erlang.Atom;
                Erlang.List l = binding.find("L") as Erlang.List;
                Assert.IsNotNull(n);
                Assert.IsNotNull(l);
                Assert.IsTrue(l.Length == 0);
            }
            {
                Erlang.Object pat = Erlang.Object.Format("{test, A, B, C}");
                Erlang.Object obj = Erlang.Object.Format("{test, 10, a, [1,2,3]}");

                Erlang.VarBind binding = new Otp.Erlang.VarBind();
                Assert.IsTrue(pat.match(obj, binding));
                Assert.AreEqual(3, binding.Count);
                Assert.AreEqual(10, binding["A"].longValue());
                Assert.AreEqual("a", binding["B"].atomValue());
                Assert.AreEqual("[1,2,3]", binding["C"].ToString());
            }

            {
                Erlang.VarBind binding = new Otp.Erlang.VarBind();
                Erlang.Object  obj     = Erlang.Object.Format("[1,a,$b,\"xyz\",{1,10.0},[]]");
                Erlang.Object  pat     = Erlang.Object.Format("[A,B,C,D,E,F]");

                Assert.IsTrue(pat.match(obj, binding));
                Assert.IsNotNull(binding.find("A") as Erlang.Long);
                Assert.IsNotNull(binding.find("B") as Erlang.Atom);
                Assert.IsNotNull(binding.find("C") as Erlang.Char);
                Assert.IsNotNull(binding.find("D") as Erlang.String);
                Assert.IsNotNull(binding.find("E") as Erlang.Tuple);
                Assert.IsNotNull(binding.find("F") as Erlang.List);

                Assert.IsTrue(binding.find("E").Cast <Erlang.Tuple>().arity() == 2);
                Assert.IsTrue(binding.find("F").Cast <Erlang.List>().Length == 0);
            }

            Erlang.Object pattern = Erlang.Object.Format("{test, T}");
            string        exp     = "{test, ~w}";
            {
                Erlang.VarBind binding = new Otp.Erlang.VarBind();
                Erlang.Object  obj     = Erlang.Object.Format(exp, (int)3);
                Assert.IsTrue(pattern.match(obj, binding));
                Assert.AreEqual(3, binding.find("T").intValue());
            }
            {
                Erlang.VarBind binding = new Otp.Erlang.VarBind();
                Erlang.Object  obj     = Erlang.Object.Format(exp, (long)100);
                Assert.IsTrue(pattern.match(obj, binding));
                Assert.AreEqual(100, binding.find("T").longValue());
            }
            {
                Erlang.VarBind binding = new Otp.Erlang.VarBind();
                Erlang.Object  obj     = Erlang.Object.Format(exp, 100.0);
                Assert.IsTrue(pattern.match(obj, binding));
                Assert.AreEqual(100.0, binding.find("T").doubleValue());
            }
            {
                Erlang.VarBind binding = new Otp.Erlang.VarBind();
                Erlang.Object  obj     = Erlang.Object.Format(exp, "test");
                Assert.IsTrue(pattern.match(obj, binding));
                Assert.AreEqual("test", binding.find("T").stringValue());
            }
            {
                Erlang.VarBind binding = new Otp.Erlang.VarBind();
                Erlang.Object  obj     = Erlang.Object.Format(exp, true);
                Assert.IsTrue(pattern.match(obj, binding));
                Assert.AreEqual(true, binding.find("T").boolValue());
            }
            {
                Erlang.VarBind binding = new Otp.Erlang.VarBind();
                Erlang.Object  obj     = Erlang.Object.Format(exp, 'c');
                Assert.IsTrue(pattern.match(obj, binding));
                Assert.AreEqual('c', binding.find("T").charValue());
            }
            {
                Erlang.VarBind binding = new Otp.Erlang.VarBind();
                Erlang.Pid     pid     = new Erlang.Pid("tmp", 1, 2, 3);
                Erlang.Object  obj     = Erlang.Object.Format(exp, pid as Erlang.Object);
                Assert.IsTrue(pattern.match(obj, binding));
                Assert.AreEqual(pid, binding.find("T").pidValue());

                obj = Erlang.Object.Format(exp, pid);
                Assert.IsTrue(pattern.match(obj, binding));
                Assert.AreEqual(pid, binding.find("T").pidValue());
            }
            {
                Erlang.VarBind binding   = new Otp.Erlang.VarBind();
                Erlang.Ref     reference = new Erlang.Ref("tmp", 1, 2);
                Erlang.Object  obj       = Erlang.Object.Format(exp, reference);
                Assert.IsTrue(pattern.match(obj, binding));
                Assert.AreEqual(reference, binding.find("T").refValue());
            }
            {
                Erlang.VarBind binding = new Otp.Erlang.VarBind();
                Erlang.List    obj     = new Erlang.List(new Erlang.Int(10), new Erlang.Double(30.0),
                                                         new Erlang.String("abc"), new Erlang.Atom("a"),
                                                         new Erlang.Binary(new byte[] { 1, 2, 3 }), false, new Erlang.Boolean(true));
                Erlang.Object pat = Erlang.Object.Format("T");
                Assert.IsTrue(pat.match(obj, binding));
                Erlang.Object expected = Erlang.Object.Format("[10, 30.0, \"abc\", 'a', ~w, \'false\', true]",
                                                              new Erlang.Binary(new byte[] { 1, 2, 3 }));
                Erlang.Object result = binding.find("T");
                Assert.IsTrue(expected.Equals(result));
            }
        }