Example #1
0
        public void ErlParserMFAFailTest()
        {
            var tests = new Dictionary <string, object[]>
            {
                { "a:b(1,%comment\n", (object[])null },
                { "a:b(1,%comment 2).", (object[])null },
                { "(", (object[])null },
                { ")", (object[])null },
                { ".", (object[])null },
                { "aa", (object[])null },
                { "a(", (object[])null },
                { "a:b(", (object[])null },
                { "a.b()", (object[])null },
                { "a:b(10         20)", (object[])null },
                { "a:b(10.        20)", (object[])null },
                { "a:b(10.(20)", (object[])null },
                { "a:b(~w,~w)", new object[] { 10 } },
                { "a:b([~w,20],~w)", new object[] { 10 } },
            };

            foreach (var t in tests)
            {
                Assert.Throws <ErlException>(
                    () => ErlObject.ParseMFA(t.Key, t.Value),
                    "Errorneously parsing term: {0}".Args(t.Key));
            }
        }
Example #2
0
        public void ErlParserTest()
        {
            var tests = new Dictionary <string, IErlObject>
            {
                { "<<1,2,3>>", new ErlBinary(new byte[] { 1, 2, 3 }) },
                { "<<>>", new ErlBinary(new byte[] {}) },
                { "<<\"abc\">>", new ErlBinary(new byte[] { (byte)'a', (byte)'b', (byte)'c' }) },
                { "<<\"\">>", new ErlBinary(new byte[] {}) },
            };

            foreach (var t in tests)
            {
                try
                {
                    ErlObject.Parse(t.Key, t.Value);
                    Aver.Pass();
                }
                catch (Exception)
                {
                    Aver.Fail("Error parsing: {0} (test: {1})".Args(t.Key, t.ToString()));
                }

                var res = ErlObject.Parse(t.Key, t.Value);
                Aver.AreEqual(t.Value, res,
                              "Unexpected value: {0} (expected: {1})".Args(res, t.Value));
            }
        }
Example #3
0
        public void ErlParserMFATest()
        {
            var tests = new Dictionary <string, Tuple <string, string, ErlList, object[]> >
            {
                { "a:b()", Tuple.Create("a", "b", new ErlList(), (object[])null) },
                { "a:b().", Tuple.Create("a", "b", new ErlList(), (object[])null) },
                { "a:b()\t      .", Tuple.Create("a", "b", new ErlList(), (object[])null) },
                { "a:b()       ", Tuple.Create("a", "b", new ErlList(), (object[])null) },
                { "a:b()\n.", Tuple.Create("a", "b", new ErlList(), (object[])null) },
                { "a:b(%comment\n).", Tuple.Create("a", "b", new ErlList(), (object[])null) },
                { "a:b().%comment", Tuple.Create("a", "b", new ErlList(), (object[])null) },
                { "a:b()\t", Tuple.Create("a", "b", new ErlList(), (object[])null) },
                { "a:b(10)", Tuple.Create("a", "b", new ErlList(10), (object[])null) },
                { "a:b(10).", Tuple.Create("a", "b", new ErlList(10), (object[])null) },
                { "aa:bb(10)", Tuple.Create("aa", "bb", new ErlList(10), (object[])null) },
                { "a:b(10,20)", Tuple.Create("a", "b", new ErlList(10, 20), (object[])null) },
                { "a:b(~w)", Tuple.Create("a", "b", new ErlList(10), new object[] { 10 }) },
                { "a:b(~w).", Tuple.Create("a", "b", new ErlList(10), new object[] { 10 }) },
                {
                    "a:b(~f,~d).",
                    Tuple.Create("a", "b", new ErlList(10d, 20), new object[] { 10d, 20 })
                },
                {
                    "a:b([~w,~w],30)",
                    Tuple.Create("a", "b", new ErlList(new ErlList(10, 20), 30),
                                 new object[] { 10, 20 })
                },
                {
                    "a:b([~w,~w],~w)",
                    Tuple.Create("a", "b", new ErlList(new ErlList(10, 20), 30),
                                 new object[] { 10, 20, 30 })
                },
            };

            foreach (var t in tests)
            {
                try
                {
                    ErlObject.ParseMFA(t.Key, t.Value.Item4);
                    Aver.Pass();
                }
                catch (Exception)
                {
                    Aver.Fail("Error parsing: {0} (test: {1})".Args(t.Key, t.ToString()));
                }

                var res = ErlObject.ParseMFA(t.Key, t.Value.Item4);
                Aver.AreEqual(t.Value.Item1, res.Item1.Value,
                              "Unexpected module value: {0} (expected: {1})".Args(
                                  res.Item1.Value, t.Value.Item1));
                Aver.AreEqual(t.Value.Item2, res.Item2.Value,
                              "Unexpected function value: {0} (expected: {1})".Args(
                                  res.Item2.Value, t.Value.Item2));
                Aver.AreObjectsEqual(t.Value.Item3, res.Item3,
                                     "Unexpected args value: {0} (expected: {1})".Args(res.Item3,
                                                                                       t.Value.Item3));
            }
        }
Example #4
0
        public void ErlTestFormatVariable()
        {
            var cases = new Dictionary <string, ErlTypeOrder> {
                { "B", ErlTypeOrder.ErlObject },
                { "B::int()", ErlTypeOrder.ErlLong },
                { "B::integer()", ErlTypeOrder.ErlLong },
                { "B::string()", ErlTypeOrder.ErlString },
                { "B::atom()", ErlTypeOrder.ErlAtom },
                { "B::float()", ErlTypeOrder.ErlDouble },
                { "B::double()", ErlTypeOrder.ErlDouble },
                { "B::binary()", ErlTypeOrder.ErlBinary },
                { "B::bool()", ErlTypeOrder.ErlBoolean },
                { "B::boolean()", ErlTypeOrder.ErlBoolean },
                { "B::byte()", ErlTypeOrder.ErlByte },
                { "B::char()", ErlTypeOrder.ErlByte },
                { "B::list()", ErlTypeOrder.ErlList },
                { "B::tuple()", ErlTypeOrder.ErlTuple },
                { "B::pid()", ErlTypeOrder.ErlPid },
                { "B::ref()", ErlTypeOrder.ErlRef },
                { "B::reference()", ErlTypeOrder.ErlRef },
                { "B::port()", ErlTypeOrder.ErlPort }
            };

            foreach (var p in cases)
            {
                IErlObject o = ErlObject.Parse(p.Key);
                Assert.IsInstanceOf(typeof(ErlVar), o);
                Assert.AreEqual(p.Value, ((ErlVar)o).ValueType);
            }

            var pat1 = ErlObject.Parse("{A::char(), B::tuple(), C::float(), D::list(), [E::string(), F::int()], G::bool()}");
            var obj1 = ErlObject.Parse("{$a, {1,2,3}, 10.0, [5,6], [\"abc\", 190], true}");

            var binding = new ErlVarBind();

            Assert.IsTrue(pat1.Match(obj1, binding)); // Match unbound variables
            Assert.IsTrue(pat1.Match(obj1, binding)); // Match bound variables

            var obj2 = ErlObject.Parse("{$a, {1,2,3}, 20.0, [5,6], [\"abc\", 190], true}");

            Assert.IsFalse(pat1.Match(obj2, binding)); // Match bound variables

            binding.Clear();

            var obj3 = ErlObject.Parse("{$a, {1,2,3}, 10.0, [5,6], [\"abc\", bad], false}");

            Assert.IsFalse(pat1.Match(obj3, binding));
        }
Example #5
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());
        }
Example #6
0
        private IErlObject ioProcessPutChars(ErlAtom encoding,
                                             ErlAtom mod, ErlAtom fun, ErlList args, IErlObject replyAs)
        {
            string term;

            if (mod == ConstAtoms.Io_Lib && fun == ConstAtoms.Format && args.Count == 2)
            {
                try   { term = ErlObject.Format(args); }
                catch { term = "{0}:{1}({2})".Args(mod, fun, args.ToString(true)); }
            }
            else
            {
                term = "{0}:{1}({2})".Args(mod, fun, args.ToString(true));
            }
            Node.IoOutput(encoding, new ErlString(term));
            return(s_ReplyPattern.Subst(
                       new ErlVarBind {
                { RA, replyAs }, { R, ConstAtoms.Ok }
            }));
        }
Example #7
0
        public void ErlParserTest()
        {
            var tests = new Dictionary <string, IErlObject>
            {
                { "<<1,2,3>>", new ErlBinary(new byte[] { 1, 2, 3 }) },
                { "<<>>", new ErlBinary(new byte[] {}) },
                { "<<\"abc\">>", new ErlBinary(new byte[] { (byte)'a', (byte)'b', (byte)'c' }) },
                { "<<\"\">>", new ErlBinary(new byte[] {}) },
            };

            foreach (var t in tests)
            {
                Assert.DoesNotThrow(() => ErlObject.Parse(t.Key, t.Value),
                                    "Error parsing: {0} (test: {1})".Args(t.Key, t.ToString()));

                var res = ErlObject.Parse(t.Key, t.Value);
                Assert.AreEqual(t.Value, res,
                                "Unexpected value: {0} (expected: {1})".Args(res, t.Value));
            }
        }
Example #8
0
        public void ErlPatternMatchCollectionTest()
        {
            var state = new KVP();

            var pm = new ErlPatternMatcher
            {
                { 0, "{A::integer(), stop}", (_ctx, p, t, b, _args) => { state = new KVP(p, b); return(t); } },
                { "{A::integer(), status}", (p, t, b, _args) => { state = new KVP(p, b); return(t); } },
                { 1, "{A::integer(), {status, B::atom()}}", (_ctx, p, t, b, _args) => { state = new KVP(p, b); return(t); } },
                { "{A::integer(), {config, B::list()}}", (p, t, b, _args) => { state = new KVP(p, b); return(t); } }
            };

            var term = ErlObject.Parse("{10, stop}");

            Assert.AreEqual(1, pm.Match(ref term));
            Assert.AreEqual(10, state.Value["A"].ValueAsInt);

            term = ErlObject.Parse("{11, status}");
            Assert.AreEqual(2, pm.Match(ref term));
            Assert.AreEqual(11, state.Value["A"].ValueAsInt);

            term = ErlObject.Parse("{12, {status, ~w}}", new ErlAtom("a"));
            Assert.AreEqual(3, pm.Match(ref term));
            Assert.AreEqual(12, state.Value["A"].ValueAsInt);
            Assert.AreEqual("a", state.Value["B"].ValueAsString);

            term = ErlObject.Parse("{13, {config, ~w}}", new ErlList());
            Assert.AreEqual(4, pm.Match(ref term));
            Assert.AreEqual(13, state.Value["A"].ValueAsInt);
            Assert.AreEqual(0, (state.Value["B"] as ErlList).Count);

            term = ErlObject.Parse("{10, exit}");
            Assert.AreEqual(-1, pm.Match(ref term));

            var pts = pm.PatternsToString;

            Assert.AreEqual(
                "[{A::int(),stop},{A::int(),status},{A::int(),{status,B::atom()}},{A::int(),{config,B::list()}}]",
                pts);
        }
Example #9
0
        private static void run(IConfigSectionNode argsConfig, bool hasConfigFile)
        {
            //try to read from  /config file
            var localNodeName  = argsConfig["local"].AttrByIndex(0).Value;
            var remoteNodeName = argsConfig["remote"].AttrByIndex(0).Value;
            var cookie         = new ErlAtom(argsConfig["cookie"].AttrByIndex(0).ValueAsString(string.Empty));
            var trace          = (ErlTraceLevel)Enum.Parse(typeof(ErlTraceLevel), argsConfig["trace"].AttrByIndex(0).ValueAsString("Off"), true);
            var timeout        = argsConfig["timeout"].AttrByIndex(0).ValueAsInt(120);

            if (!hasConfigFile && (localNodeName == null || remoteNodeName == null))
            {
                Console.WriteLine("Usage: {0} [-config ConfigFile.config] [-local NodeName -remote NodeName]\n" +
                                  "          [-cookie Cookie] [-trace Level] [-timeout Timeout]\n\n" +
                                  "     -config ConfFile    - Provide configuration file\n" +
                                  "     -trace Level        - Turn on trace for level:\n" +
                                  "                             Off (default) | Send | Ctrl | Handshake | Wire\n" +
                                  "     -timeout Timeout    - Wait for messages for this number of seconds before\n" +
                                  "                              exiting (default: 15)\n" +
                                  "Example:\n" +
                                  "========\n" +
                                  "  [Shell A] $ erl -sname a\n" +
                                  "  [Shell B] $ {0} -local b -remote a -trace Send -timeout 60\n\n" +
                                  "  In the Erlang shell send messages to the C# node:\n" +
                                  "  [Shell A] (a@localhost)1> {{test, b@localhost}} ! \"Hello World!\".\n"
                                  , MiscUtils.ExeName(false));
                Environment.Exit(1);
            }

            // Create an local Erlang node that will process all communications with other nodes

            var node = hasConfigFile ? ErlApp.Node : new ErlLocalNode(localNodeName, cookie, true);

            node.Trace += (_, t, l, m) =>
                          Console.WriteLine("[TRACE {0}]   {1} {2}", t, l == Direction.Inbound ? "<-" : "->", m);

            node.NodeStatusChange += (_, n, up, info) =>
                                     Console.WriteLine("<NodeStatus>  Node {0} {1} ({2})", n.Value, up ? "up" : "down", info);

            node.ConnectAttempt += (_, n, dir, info) =>
                                   Console.WriteLine("<ConnAttempt> Node {0}: {1} connection {2}", n, dir.ToString().ToLower(), info);

            node.EpmdFailedConnectAttempt += (_, n, info) =>
                                             Console.WriteLine("<EmpdFailure> Node {0} Epmd connectivity failure: {1}", n, info);

            node.UnhandledMsg += (_, c, msg) =>
                                 Console.WriteLine("<UnhandMsg>   Node {0} unhandled message from node {1}: {2}", c.LocalNode.NodeName, c.RemoteNode.NodeName, msg);

            node.ReadWrite += (_, c, d, n, tn, tm) =>
                              Console.WriteLine("<ReadWrite>   {0} {1} bytes (total: {2} bytes, {3} msgs)", d == Direction.Inbound ? "Read" : "Written", n, tn, tm);

            node.IoOutput += (_, _encoding, output) =>
                             Console.WriteLine("<I/O output>  ==> Received output: {0}", output);

            // Create a named mailbox "test"
            ErlMbox mbox = null;

            if (hasConfigFile)
            {
                mbox = node.CreateMbox("test");
            }
            else
            {
                node.TraceLevel = trace;
                Console.WriteLine("Node = {0}, cookie = {1}", node.NodeName, node.Cookie);

                // Start the node

                try
                {
                    node.Start();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: " + e.Message);
                    goto exit;
                }

                mbox = node.CreateMbox("test");

                // Connect to remote node

                var remote = node.Connection(remoteNodeName);

                Console.WriteLine("{0} to remote node {1}".Args(
                                      remote == null ? "Couldn't connect" : "Connected",
                                      remoteNodeName));

                if (remote == null)
                {
                    Console.WriteLine("Couldn't connect to {0}", remoteNodeName);
                    goto exit;
                }

                // Synchronous RPC call of erlang:now() at the remote node
                /* new ErlAtom("erlang") */
                /* new ErlList() */
                var result = mbox.RPC(remote.Name, ConstAtoms.Erlang, new ErlAtom("now"), ErlList.Empty);

                Console.WriteLine("RPC call to erlang:now() resulted in response: {0}", result.ValueAsDateTime.ToLocalTime());

                // Asynchronous RPC call of erlang:now() at the remote node

                mbox.AsyncRPC(remote.Name, ConstAtoms.Erlang, new ErlAtom("now"), ErlList.Empty);

                int i = node.WaitAny(mbox);

                if (i < 0)
                {
                    Console.WriteLine("Timeout waiting for RPC result");
                    goto exit;
                }

                result = mbox.ReceiveRPC();

                Console.WriteLine("AsyncRPC call to erlang:now() resulted in response: {0}", result.ValueAsDateTime.ToLocalTime());

                // I/O output call on the remote node of io:format(...)
                // that will return the output to this local node (because by default RPC
                // passes node.GroupLeader as the mailbox to receive the output

                var mfa = ErlObject.ParseMFA("io:format(\"output: 1, 10.0, abc\n\", [])");

                result = mbox.RPC(remote.Name, mfa.Item1, mfa.Item2, mfa.Item3);

                Console.WriteLine("io:format() -> {0}", result.ToString());

                // Poll for incoming messages destined to the 'test' mailbox
            }

            var deadline = DateTime.UtcNow.AddSeconds(timeout);

            do
            {
                var result = mbox.Receive(1000);
                if (result != null)
                {
                    Console.WriteLine("Mailbox {0} got message: {1}", mbox.Self, result);
                }
            }while (DateTime.UtcNow < deadline);

exit:
            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
        }
Example #10
0
        public void ErlTermSerializeTest()
        {
            {
                var b  = new byte[] { 131, 100, 0, 3, 97, 98, 99 };
                var t  = new ErlAtom("abc");
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b  = new byte[] { 131, 109, 0, 0, 0, 3, 1, 2, 3 };
                var t  = new ErlBinary(new byte[] { 1, 2, 3 });
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b1  = new byte[] { 131, 100, 0, 4, 116, 114, 117, 101 };
                var t1  = new ErlBoolean(true);
                var os1 = new ErlOutputStream(t1);
                Aver.IsTrue(b1.MemBufferEquals(os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray()));
                var es1 = new ErlInputStream(b1);
                Aver.AreEqual(t1, es1.Read());

                var b2  = new byte[] { 131, 100, 0, 5, 102, 97, 108, 115, 101 };
                var t2  = new ErlBoolean(false);
                var os2 = new ErlOutputStream(t2);
                Aver.IsTrue(b2.MemBufferEquals(os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray()));
                var es2 = new ErlInputStream(b2);
                Aver.AreEqual(t2, es2.Read());
            }
            {
                var b  = new byte[] { 131, 97, 127 };
                var t  = new ErlByte(127);
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b  = new byte[] { 131, 70, 64, 36, 62, 249, 219, 34, 208, 229 };
                var t  = new ErlDouble(10.123);
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b  = new byte[] { 131, 108, 0, 0, 0, 2, 107, 0, 1, 1, 107, 0, 1, 2, 106 };
                var t  = new ErlList(new ErlList(1), new ErlList(2));
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b  = new byte[] { 131, 108, 0, 0, 0, 2, 108, 0, 0, 0, 2, 97, 1, 107, 0, 1, 2, 106, 107, 0, 1, 3, 106 };
                var t  = new ErlList(new ErlList(1, new ErlList(2)), new ErlList(3));
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b = new byte[] { 131, 108, 0, 0, 0, 3, 97, 1, 70, 64, 36, 61, 112, 163, 215, 10, 61, 108, 0, 0, 0, 2,
                                     100, 0, 4, 116, 114, 117, 101, 107, 0, 1, 97, 106, 106 };
                var t  = new ErlList(1, 10.12, new ErlList(true, "a"));
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b = new byte[] {
                    131, 108, 0, 0, 0, 3, 97, 23, 97, 4, 104, 1, 108, 0, 0, 0, 1, 104, 2, 109, 0, 0, 0, 5, 101, 118, 101,
                    110, 116, 104, 1, 108, 0, 0, 0, 2, 104, 2, 109, 0, 0, 0, 10, 102, 108, 101, 101, 116, 95, 104, 97,
                    115, 104, 109, 0, 0, 0, 36, 97, 54, 97, 50, 50, 100, 49, 52, 45, 56, 52, 56, 51, 45, 52, 49, 102, 99,
                    45, 97, 52, 54, 98, 45, 50, 56, 51, 98, 57, 55, 55, 55, 99, 50, 97, 50, 104, 2, 109, 0, 0, 0, 4, 116,
                    121, 112, 101, 109, 0, 0, 0, 13, 102, 108, 101, 101, 116, 95, 99, 104, 97, 110, 103, 101, 100,
                    106, 106, 106
                };
                var t = ErlObject.Parse("[23,4,{[{<<\"event\">>," +
                                        "{[{<<\"fleet_hash\">>,<<\"a6a22d14-8483-41fc-a46b-283b9777c2a2\">>}," +
                                        "{<<\"type\">>,<<\"fleet_changed\">>}]}}]}]");
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b1  = new byte[] { 131, 98, 255, 255, 255, 251 };
                var t1  = new ErlLong(-5);
                var os1 = new ErlOutputStream(t1);
                Aver.IsTrue(b1.MemBufferEquals(os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray()));
                var es1 = new ErlInputStream(b1);
                Aver.AreEqual(t1, es1.Read());

                var b2  = new byte[] { 131, 97, 5 };
                var t2  = new ErlLong(5);
                var os2 = new ErlOutputStream(t2);
                Aver.IsTrue(b2.MemBufferEquals(os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray()));
                var es2 = new ErlInputStream(b2);
                Aver.AreEqual(t2, es2.Read());

                var b3  = new byte[] { 131, 98, 0, 16, 0, 0 };
                var t3  = new ErlLong(1024 * 1024);
                var os3 = new ErlOutputStream(t3);
                Aver.IsTrue(b3.MemBufferEquals(os3.GetBuffer().TakeWhile((_, i) => i < b3.Length).ToArray()));
                var es3 = new ErlInputStream(b3);
                Aver.AreEqual(t3, es3.Read());

                var b4  = new byte[] { 131, 110, 6, 0, 0, 0, 0, 0, 0, 4 };
                var t4  = new ErlLong(1024L * 1024 * 1024 * 1024 * 4);
                var os4 = new ErlOutputStream(t4);
                Aver.IsTrue(b4.MemBufferEquals(os4.GetBuffer().TakeWhile((_, i) => i < b4.Length).ToArray()));
                var es4 = new ErlInputStream(b4);
                Aver.AreEqual(t4, es4.Read());

                var b5  = new byte[] { 131, 110, 8, 1, 0, 0, 0, 0, 0, 0, 0, 128 };
                var t5  = new ErlLong(1L << 63);
                var os5 = new ErlOutputStream(t5);
                Aver.IsTrue(b5.MemBufferEquals(os5.GetBuffer().TakeWhile((_, i) => i < b5.Length).ToArray()));
                var es5 = new ErlInputStream(b5);
                Aver.AreEqual(t5, es5.Read());

                var b6  = new byte[] { 131, 110, 8, 1, 0, 0, 0, 0, 0, 0, 0, 128 };
                var t6  = new ErlLong(-1L << 63);
                var os6 = new ErlOutputStream(t6);
                Aver.IsTrue(b6.MemBufferEquals(os6.GetBuffer().TakeWhile((_, i) => i < b6.Length).ToArray()));
                var es6 = new ErlInputStream(b6);
                Aver.AreEqual(t6, es6.Read());

                var b7  = new byte[] { 131, 110, 8, 0, 255, 255, 255, 255, 255, 255, 255, 255 };
                var es7 = new ErlInputStream(b7);
                var t7  = new ErlLong(-1);
                Aver.AreEqual(t7, es7.Read());
                var bi7 = new byte[] { 131, 98, 255, 255, 255, 255 };
                var os7 = new ErlOutputStream(t7);
                Aver.IsTrue(bi7.MemBufferEquals(os7.GetBuffer().TakeWhile((_, i) => i < bi7.Length).ToArray()));
            }
            {
                var b  = new byte[] { 131, 103, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 0, 0, 0, 38, 0, 0, 0, 0, 1 };
                var t  = new ErlPid("b@pipit", 38, 0, 1);
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b  = new byte[] { 131, 102, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 0, 0, 0, 38, 1 };
                var t  = new ErlPort("b@pipit", 38, 1);
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b  = new byte[] { 131, 114, 0, 3, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 1, 0, 0, 0, 181, 0, 0, 0, 0, 0, 0, 0, 0 };
                var t  = new ErlRef("b@pipit", 181, 0, 0, 1);
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b  = new byte[] { 131, 107, 0, 3, 115, 116, 114 };
                var t  = new ErlString("str");
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b  = new byte[] { 131, 104, 3, 97, 1, 100, 0, 1, 97, 104, 2, 97, 10, 70, 63, 241, 247, 206, 217, 22, 135, 43 };
                var t  = new ErlTuple(1, new ErlAtom("a"), new ErlTuple(10, 1.123));
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
            {
                var b = new byte[] { 131, 116, 0, 0, 0, 2, 100, 0, 1, 97, 97, 1, 107, 0, 3, 115, 116, 114, 70, 64, 0, 0, 0, 0, 0, 0, 0 };
                var t = new ErlMap {
                    { new ErlAtom("a"), 1.ToErlObject() },
                    { new ErlString("str"), new ErlDouble(2.0) }
                };
                var os = new ErlOutputStream(t);
                Aver.IsTrue(b.MemBufferEquals(os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray()));
                var es = new ErlInputStream(b);
                Aver.AreEqual(t, es.Read());
            }
        }
Example #11
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));
            }
        }
Example #12
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();
        }
Example #13
0
        public void ErlTestFormat()
        {
            {
                IErlObject obj1 = ErlObject.Parse("a");
                Assert.IsInstanceOf(typeof(ErlAtom), obj1);
                Assert.AreEqual("a", obj1.ValueAsString);
            }
            {
                IErlObject obj1 = ErlObject.Parse("$a");
                Assert.IsInstanceOf(typeof(ErlByte), obj1);
                Assert.AreEqual('a', (char)obj1.ValueAsInt);
            }
            {
                IErlObject obj1 = ErlObject.Parse("'Abc'");
                Assert.IsInstanceOf(typeof(ErlAtom), obj1);
                Assert.AreEqual("Abc", obj1.ValueAsString);
            }
            {
                IErlObject obj1 = ErlObject.Parse("{'true', 'false', true, false}");
                Assert.IsInstanceOf(typeof(ErlTuple), obj1);
                var t = (ErlTuple)obj1;
                Assert.AreEqual(4, t.Count);
                t.Select(o => { Assert.IsInstanceOf(typeof(ErlBoolean), o); return(0); });
                Assert.AreEqual(true, t[0].ValueAsBool);
                Assert.AreEqual(false, t[1].ValueAsBool);
                Assert.AreEqual(true, t[2].ValueAsBool);
                Assert.AreEqual(false, t[3].ValueAsBool);
            }
            {
                IErlObject obj1 = ErlObject.Parse("\"Abc\"");
                Assert.IsInstanceOf(typeof(ErlString), obj1);
                Assert.AreEqual("Abc", obj1.ValueAsString);
            }
            {
                IErlObject obj1 = ErlObject.Parse("Abc");
                Assert.IsInstanceOf(typeof(ErlVar), obj1);
                Assert.AreEqual("Abc", ((ErlVar)obj1).Name.Value);

                IErlObject obj2 = ErlObject.Parse("V");
                Assert.IsInstanceOf(typeof(ErlVar), obj2);
                Assert.AreEqual("V", ((ErlVar)obj2).Name.Value);
            }
            {
                IErlObject obj1 = ErlObject.Parse("1");
                Assert.IsInstanceOf(typeof(ErlLong), obj1);
                Assert.AreEqual(1, obj1.ValueAsInt);
            }
            {
                IErlObject obj1 = ErlObject.Parse("1.23");
                Assert.IsInstanceOf(typeof(ErlDouble), obj1);
                Assert.AreEqual(1.23, obj1.ValueAsDouble);
            }
            {
                IErlObject obj1 = ErlObject.Parse("$a");
                Assert.IsInstanceOf(typeof(ErlByte), obj1);
                Assert.AreEqual('a', (char)obj1.ValueAsInt);
            }
            {
                IErlObject obj1 = ErlObject.Parse("{1}");
                Assert.IsInstanceOf(typeof(ErlTuple), obj1);
                Assert.AreEqual(1, ((ErlTuple)obj1).Count);
                Assert.IsInstanceOf(typeof(ErlLong), ((ErlTuple)obj1)[0]);
                Assert.AreEqual(1, (obj1 as ErlTuple)[0].ValueAsInt);
            }
            {
                IErlObject obj0 = ErlObject.Parse("[]");
                Assert.IsInstanceOf(typeof(ErlList), obj0);
                Assert.AreEqual(0, ((ErlList)obj0).Count);
                IErlObject obj1 = ErlObject.Parse("[1]");
                Assert.IsInstanceOf(typeof(ErlList), obj1);
                Assert.AreEqual(1, ((ErlList)obj1).Count);
                Assert.IsInstanceOf(typeof(ErlLong), (obj1 as ErlList)[0]);
                Assert.AreEqual(1, (obj1 as ErlList)[0].ValueAsInt);
            }
            {
                IErlObject obj1 = ErlObject.Parse("[{1,2}, []]");
                Assert.IsInstanceOf(typeof(ErlList), obj1);
                Assert.AreEqual(2, (obj1 as ErlList).Count);
                Assert.IsInstanceOf(typeof(ErlTuple), (obj1 as ErlList)[0]);
                Assert.AreEqual(2, ((obj1 as ErlList)[0] as ErlTuple).Count);
                Assert.AreEqual(0, ((obj1 as ErlList)[1] as ErlList).Count);
            }
            {
                IErlObject obj1 = ErlObject.Parse("{a, [b, 1, 2.0, \"abc\"], {1, 2}}");
                Assert.IsInstanceOf(typeof(ErlTuple), obj1);
                Assert.AreEqual(3, (obj1 as ErlTuple).Count);
            }
            {
                IErlObject obj1 = ErlObject.Parse("~w", 1);
                Assert.IsInstanceOf(typeof(ErlLong), obj1);
                Assert.AreEqual(1, (((ErlLong)obj1)).ValueAsInt);
                IErlObject obj2 = ErlObject.Parse("{~w, ~w,~w}", 1, 2, 3);
                Assert.IsInstanceOf(typeof(ErlTuple), obj2);
                Assert.AreEqual(3, (obj2 as ErlTuple).Count);
                Assert.IsInstanceOf(typeof(ErlLong), (obj2 as ErlTuple)[0]);
                Assert.AreEqual(1, (obj2 as ErlTuple)[0].ValueAsInt);
                Assert.IsInstanceOf(typeof(ErlLong), (obj2 as ErlTuple)[1]);
                Assert.AreEqual(2, (obj2 as ErlTuple)[1].ValueAsInt);
                Assert.IsInstanceOf(typeof(ErlLong), (obj2 as ErlTuple)[2]);
                Assert.AreEqual(3, (obj2 as ErlTuple)[2].ValueAsInt);
            }
            {
                IErlObject obj2 = ErlObject.Parse("{~w, ~w,~w,~w, ~w}", 1.0, 'a', "abc", 2, true);
                Assert.IsInstanceOf(typeof(ErlTuple), obj2);
                Assert.AreEqual(5, (obj2 as ErlTuple).Count);
                Assert.IsInstanceOf(typeof(ErlDouble), (obj2 as ErlTuple)[0]);
                Assert.AreEqual(1.0, (obj2 as ErlTuple)[0].ValueAsDouble);
                Assert.IsInstanceOf(typeof(ErlByte), (obj2 as ErlTuple)[1]);
                Assert.AreEqual((byte)'a', (obj2 as ErlTuple)[1].ValueAsInt);
                Assert.IsInstanceOf(typeof(ErlString), (obj2 as ErlTuple)[2]);
                Assert.AreEqual("abc", (obj2 as ErlTuple)[2].ValueAsString);
                Assert.IsInstanceOf(typeof(ErlLong), (obj2 as ErlTuple)[3]);
                Assert.AreEqual(2, (obj2 as ErlTuple)[3].ValueAsInt);
                Assert.IsInstanceOf(typeof(ErlBoolean), (obj2 as ErlTuple)[4]);
                Assert.AreEqual(true, (obj2 as ErlTuple)[4].ValueAsBool);
            }
        }