示例#1
0
        public void ErlFormatTest()
        {
            var tests = new Dictionary <string, ErlList>
            {
                { "abc 10.5", new ErlList("abc ~w.~w", 10, 5) },
                { "xx 8", new ErlList("xx ~i~w", 12, 8) },
                { "~z", new ErlList("~~z", 16) },
                { "a 16", new ErlList("~c ~w", (byte)'a', 16) },
                { "xyz 12\n", new ErlList("xyz ~10.6.B~n", 12) },
                { "x~y21", new ErlList("x~~y~w1", 2) },
                { "{ok, A}", new ErlList("{ok, ~v}", "A") },
                { "{ok, A}.", new ErlList("{ok, ~v}.", new ErlAtom("A")) },
                { "{ok, A} ", new ErlList("{ok, ~v} ", new ErlString("A")) },
                { "{ok, A}  ", new ErlList("{ok, ~v}  ", new ErlVar("A")) },
                {
                    "{ok, A::a()}",
                    new ErlList("{ok, ~v::a()}", new ErlVar("A", ErlTypeOrder.ErlLong))
                },
                { "{ok, A::int()}", new ErlList("{ok, ~v}", new ErlVar("A", ErlTypeOrder.ErlLong)) },
            };

            foreach (var t in tests)
            {
                Assert.AreEqual(t.Key, ErlObject.Format(t.Value),
                                "Error in test: {0} <- format({1})".Args(t.Key, t.Value.ToString()));
            }

            var failTests = new List <ErlList>
            {
                new ErlList("abc ~w.~w"),
                new ErlList("xx ~i~w", 12),
                new ErlList("~y", 12),
            };

            foreach (var t in failTests)
            {
                Assert.Throws <ErlException>(
                    () => ErlObject.Format(t), "Errorneously formatted term: {0}".Args(t));
            }

            var V        = new ErlVar("V", ErlTypeOrder.ErlLong);
            var expected = new ErlTuple(new ErlAtom("ok"), V).ToString();

            Assert.AreEqual(expected, "{ok, V::int()}".ToErlObject().ToString());
            Assert.AreEqual(expected, "{ok, ~w}".ToErlObject(V).ToString());
        }
示例#2
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) }
            }));
        }
示例#3
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();
        }