コード例 #1
0
        public void ErlTestPatternMatch()
        {
            {
                var        binding = new ErlVarBind();
                IErlObject obj     = ErlObject.Parse("{snapshot, x12, []}");
                IErlObject pat     = ErlObject.Parse("{snapshot, N, L}");

                Assert.IsTrue(pat.Match(obj, binding));
                ErlAtom n = binding.Cast <ErlAtom>(N);
                ErlList l = binding.Cast <ErlList>(L);
                Assert.IsNotNull(n);
                Assert.IsNotNull(l);
                Assert.IsTrue(l.Count == 0);
            }
            {
                IErlObject pat = ErlObject.Parse("{test, A, B, C}");
                IErlObject obj = ErlObject.Parse("{test, 10, a, [1,2,3]}");

                var binding = new ErlVarBind();
                Assert.IsTrue(pat.Match(obj, binding));
                Assert.AreEqual(3, binding.Count);
                Assert.AreEqual(10, binding.Cast <ErlLong>(A));
                Assert.AreEqual("a", binding.Cast <ErlAtom>(B).ValueAsString);
                Assert.AreEqual("[1,2,3]", binding["C"].ToString());
            }

            {
                var        binding = new ErlVarBind();
                IErlObject obj     = ErlObject.Parse("[1,a,$b,\"xyz\",{1,10.0},[]]");
                IErlObject pat     = ErlObject.Parse("[A,B,C,D,E,F]");

                Assert.IsTrue(pat.Match(obj, binding));
                Assert.IsNotNull(binding.Cast <ErlLong>(A));
                Assert.IsNotNull(binding.Cast <ErlAtom>(B));
                Assert.IsNotNull(binding.Cast <ErlByte>(C));
                Assert.IsNotNull(binding.Cast <ErlString>(D));
                Assert.IsNotNull(binding.Cast <ErlTuple>(E));
                Assert.IsNotNull(binding.Cast <ErlList>(F));

                Assert.IsTrue(binding.Cast <ErlTuple>(E).Count == 2);
                Assert.IsTrue(binding.Cast <ErlList>(F).Count == 0);
            }

            IErlObject pattern = ErlObject.Parse("{test, T}");
            string     exp     = "{test, ~w}";
            {
                var        binding = new ErlVarBind();
                IErlObject obj     = ErlObject.Parse(exp, (int)3);
                Assert.IsTrue(pattern.Match(obj, binding));
                Assert.AreEqual(3, binding.Cast <ErlLong>(T));
            }
            {
                var        binding = new ErlVarBind();
                IErlObject obj     = ErlObject.Parse(exp, (long)100);
                Assert.IsTrue(pattern.Match(obj, binding));
                Assert.AreEqual(100, binding.Cast <ErlLong>(T));
            }
            {
                var        binding = new ErlVarBind();
                IErlObject obj     = ErlObject.Parse(exp, 100.0);
                Assert.IsTrue(pattern.Match(obj, binding));
                Assert.AreEqual(100.0, binding.Cast <ErlDouble>(T).ValueAsDouble);
            }
            {
                var        binding = new ErlVarBind();
                IErlObject obj     = ErlObject.Parse(exp, "test");
                Assert.IsTrue(pattern.Match(obj, binding));
                Assert.AreEqual("test", binding.Cast <ErlString>(T).ValueAsString);
            }
            {
                var        binding = new ErlVarBind();
                IErlObject obj     = ErlObject.Parse(exp, true);
                Assert.IsTrue(pattern.Match(obj, binding));
                Assert.AreEqual(true, binding.Cast <ErlBoolean>(T).ValueAsBool);
            }
            {
                var        binding = new ErlVarBind();
                IErlObject obj     = ErlObject.Parse(exp, 'c');
                Assert.IsTrue(pattern.Match(obj, binding));
                Assert.AreEqual((byte)'c', binding.Cast <ErlByte>(T).ValueAsInt);
            }
            {
                var        binding = new ErlVarBind();
                var        pid     = new ErlPid("tmp", 1, 2, 3);
                IErlObject obj     = ErlObject.Parse(exp, pid as IErlObject);
                Assert.IsTrue(pattern.Match(obj, binding));
                Assert.AreEqual(pid, binding.Cast <ErlPid>(T));
                Assert.AreEqual(pid, binding.Cast <ErlPid>(T).Value);

                obj = ErlObject.Parse(exp, pid);
                Assert.IsTrue(pattern.Match(obj, binding));
                Assert.AreEqual(pid, binding.Cast <ErlPid>(T).Value);
            }
            {
                var        binding = new ErlVarBind();
                var        port    = new ErlPort("tmp", 1, 2);
                IErlObject obj     = ErlObject.Parse(exp, port);
                Assert.IsTrue(pattern.Match(obj, binding));
                Assert.AreEqual(port, binding.Cast <ErlPort>(T));
                Assert.AreEqual(port, binding.Cast <ErlPort>(T).Value);
            }
            {
                var        binding   = new ErlVarBind();
                var        reference = new ErlRef("tmp", 1, 0, 0, 2);
                IErlObject obj       = ErlObject.Parse(exp, reference);
                Assert.IsTrue(pattern.Match(obj, binding));
                Assert.AreEqual(reference, binding.Cast <ErlRef>(T));
                Assert.AreEqual(reference, binding.Cast <ErlRef>(T).Value);
            }
            {
                var     binding = new ErlVarBind();
                ErlList obj     = new ErlList(new ErlLong(10), new ErlDouble(30.0),
                                              new ErlString("abc"), new ErlAtom("a"),
                                              new ErlBinary(new byte[] { 1, 2, 3 }), false, new ErlBoolean(true));
                IErlObject pat = ErlObject.Parse("T");
                Assert.IsTrue(pat.Match(obj, binding));
                IErlObject expected = ErlObject.Parse("[10, 30.0, \"abc\", 'a', ~w, \'false\', true]",
                                                      new ErlBinary(new byte[] { 1, 2, 3 }));
                IErlObject result = binding[T];
                Assert.IsTrue(expected.Equals(result));
            }
        }
コード例 #2
0
        public void ErlTestMatchVariable()
        {
            var cases = new KeyValueList <string, IErlObject> {
                { "B", new ErlLong(1) },
                { "B", new ErlAtom("abc") },
                { "B", new ErlString("efg") },
                { "B", new ErlDouble(10.0) },
                { "B::int()", new ErlLong(10) },
                { "B::integer()", new ErlLong(20) },
                { "B::string()", new ErlString("xxx") },
                { "B::atom()", new ErlAtom("xyz") },
                { "B::float()", new ErlDouble(5.0) },
                { "B::double()", new ErlDouble(3.0) },
                { "B::binary()", new ErlBinary(new byte[] { 1, 2, 3 }) },
                { "B::bool()", new ErlBoolean(true) },
                { "B::boolean()", new ErlBoolean(false) },
                { "B::byte()", new ErlByte(1) },
                { "B::char()", new ErlByte('a') },
                { "B::list()", new ErlList(1, 2, 3) },
                { "B::tuple()", new ErlTuple(new ErlByte('a'), 1, "aaa") },
                { "B::pid()", new ErlPid("xxx", 1, 2, 3) },
                { "B::ref()", new ErlRef("xxx", 1, 0, 0, 3) },
                { "B::reference()", new ErlRef("xxx", 1, 0, 0, 3) },
                { "B::port()", new ErlPort("xxx", 1, 3) }
            };

            foreach (var p in cases)
            {
                {
                    IErlObject pat = p.Key.ToErlObject();
                    IErlObject obj = p.Value;

                    var binding = new ErlVarBind();
                    binding[B] = obj;

                    Assert.IsTrue(pat.Match(obj, binding));
                }

                {
                    IErlObject pat = p.Key.ToErlObject();
                    IErlObject obj = p.Value;

                    var binding = new ErlVarBind();

                    Assert.IsTrue(pat.Match(obj, binding));

                    var b = binding["B"];

                    Assert.AreEqual(obj.TypeOrder, b.TypeOrder);
                    Assert.IsTrue(obj.Equals(b));
                }
            }

            var revCases = cases.Reverse <KeyValuePair <string, IErlObject> >().ToList();

            cases.Zip(revCases,
                      (p1, p2) => {
                ErlVar pat     = ErlObject.Parse <ErlVar>(p1.Key);
                IErlObject obj = p2.Value;

                var binding = new ErlVarBind();

                if (pat.ValueType == ErlTypeOrder.ErlObject || pat.ValueType == obj.TypeOrder)
                {
                    Assert.IsTrue(pat.Match(obj, binding));
                }
                else
                {
                    Assert.IsFalse(pat.Match(obj, binding));
                }

                return(false);
            }).ToList();
        }