Ejemplo n.º 1
0
        public void ErlVarTest()
        {
            var t = ErlVar.Any;

            Assert.IsFalse(t.Equals(new ErlVar(ConstAtoms.ANY)));
            Assert.AreEqual(ConstAtoms.ANY, t.Name);
            Assert.AreEqual(ErlTypeOrder.ErlObject, t.ValueType);

            t = new ErlVar(N, ErlTypeOrder.ErlLong);
            Assert.AreEqual("N", t.Name.Value);
            Assert.AreEqual(ErlTypeOrder.ErlLong, t.ValueType);

            {
                var bind = new ErlVarBind();
                Assert.IsTrue(t.Match(new ErlByte(10), bind));
                Assert.AreEqual(10, bind["N"].ValueAsLong);
                bind.Clear();
                var q = new ErlVar("N", ErlTypeOrder.ErlByte);
                Assert.IsTrue(q.Match(new ErlLong(111), bind));
                Assert.AreEqual(111, bind["N"].ValueAsLong);
            }

            Assert.IsFalse(t.Matches(new ErlVar()));
            Assert.IsFalse(new ErlVar(A).Matches(new ErlVar(A)));
            Assert.IsFalse(new ErlVar(A).Matches(new ErlVar(B)));
            Assert.AreEqual(new ErlVarBind {
                { N, (ErlLong)10 }
            }, t.Match((ErlLong)10));
            Assert.AreEqual(new ErlVarBind {
                { A, (ErlLong)10 }
            }, new ErlVar(A).Match((ErlLong)10));

            Assert.AreEqual(-1, new ErlAtom("ok").CompareTo(t));
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsObject; });
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsInt; });
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsLong; });
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsDecimal; });
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsDateTime; });
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsTimeSpan; });
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsDouble; });
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsString; });
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsBool; });
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsChar; });
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsByteArray; });
            Assert.AreEqual("N::int()", t.ToString());
            Assert.IsTrue(t.IsScalar);
            Assert.AreEqual(ErlTypeOrder.ErlVar, t.TypeOrder);

            IErlObject temp = null;

            Assert.IsFalse(t.Subst(ref temp, new ErlVarBind {
                { M, new ErlLong(100) }
            }));
            Assert.IsTrue(t.Subst(ref temp, new ErlVarBind {
                { N, new ErlLong(100) }
            }));
            Assert.AreEqual(new ErlLong(100), temp);

            temp = new ErlVar(M, ErlTypeOrder.ErlLong);
            Assert.IsTrue(temp.Subst(ref temp, new ErlVarBind {
                { M, new ErlLong(100) }
            }));
            Assert.AreEqual(ErlTypeOrder.ErlVar, t.Visit(ErlTypeOrder.ErlByte, (acc, o) => ((ErlVar)o).TypeOrder));
            Assert.AreEqual(new ErlLong(100), temp);

            temp = new ErlVar(N, ErlTypeOrder.ErlObject);
            Assert.IsTrue(temp.Subst(ref temp, new ErlVarBind {
                { N, new ErlLong(100) }
            }));

            // Invalid variable type
            temp = new ErlVar(N, ErlTypeOrder.ErlAtom);
            Assert.Throws <ErlException>(() => temp.Subst(ref temp, new ErlVarBind {
                { N, new ErlLong(100) }
            }));
        }
Ejemplo n.º 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();
        }