public void TestMatchVariable() { var cases = new KeyValueList <string, Erlang.Object> { { "B", new Erlang.Int(1) }, { "B", new Erlang.Atom("abc") }, { "B", new Erlang.String("efg") }, { "B", new Erlang.Double(10.0) }, { "B::int()", new Erlang.Int(10) }, { "B::integer()", new Erlang.Int(20) }, { "B::string()", new Erlang.String("xxx") }, { "B::atom()", new Erlang.Atom("xyz") }, { "B::float()", new Erlang.Double(5.0) }, { "B::double()", new Erlang.Double(3.0) }, { "B::binary()", new Erlang.Binary(new byte[] { 1, 2, 3 }) }, { "B::bool()", new Erlang.Boolean(true) }, { "B::boolean()", new Erlang.Boolean(false) }, { "B::byte()", new Erlang.Byte(1) }, { "B::char()", new Erlang.Char('a') }, { "B::list()", new Erlang.List(1, 2, 3) }, { "B::tuple()", new Erlang.Tuple(new Erlang.Char('a'), 1, "aaa") }, { "B::pid()", new Erlang.Pid("xxx", 1, 2, 3) }, { "B::ref()", new Erlang.Ref("xxx", 1, 3) }, { "B::reference()", new Erlang.Ref("xxx", 1, 3) }, { "B::port()", new Erlang.Port("xxx", 1, 3) } }; foreach (var p in cases) { { Erlang.Object pat = Erlang.Object.Format(p.Key); Erlang.Object obj = p.Value; var binding = new Erlang.VarBind(); binding["B"] = obj; Assert.IsTrue(pat.match(obj, binding)); } { Erlang.Object pat = Erlang.Object.Format(p.Key); Erlang.Object obj = p.Value; var binding = new Erlang.VarBind(); Assert.IsTrue(pat.match(obj, binding)); var b = binding["B"]; Assert.AreEqual(obj.Type, b.Type); Assert.IsTrue(obj.Equals(b)); } } var revCases = cases.Reverse <KeyValuePair <string, Erlang.Object> >().ToList(); cases.Zip(revCases, (p1, p2) => { Erlang.Var pat = Erlang.Object.Format(p1.Key).AsVar(); Erlang.Object obj = p2.Value; var binding = new Erlang.VarBind(); if (pat.VarTermType == Erlang.TermType.Object || pat.VarTermType == obj.TermType) { Assert.IsTrue(pat.match(obj, binding)); } else { Assert.IsFalse(pat.match(obj, binding)); } return(false); }).ToList(); }
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)); } }