Пример #1
0
    public static ErlType natureSampleAnalyse(ByteBuffer data)
    {
        int position = data.position;
        int tag      = data.readByte();

        data.position = position;

        if (tag == ErlByte.TAG)
        {
            ErlByte erlByte = new ErlByte(0);
            erlByte.bytesRead(data);
            return(erlByte);
        }
        //		else if (tag == ErlByteArray.TAG) {
        //			ErlByteArray erlByteArray = new ErlByteArray (null);
        //			erlByteArray.bytesRead(data);
        //			return erlByteArray;
        //		}
        else if (tag == ErlInt.TAG)
        {
            ErlInt erlInt = new ErlInt(0);
            erlInt.bytesRead(data);
            return(erlInt);
        }
        else if (tag == ErlString.TAG)
        {
            ErlString erlString = new ErlString("");
            erlString.sampleBytesRead(data);
            return(erlString);
        }
        else
        {
            return(null);
        }
    }
Пример #2
0
 private static ErlType[] getErlArr(List <object> jsonList)
 {
     ErlType[] arr = new ErlType[jsonList.Count];
     for (int i = 0; i < jsonList.Count; i++)
     {
         List <object> data = jsonList[i] as List <object>;
         ErlType       erl  = null;
         if (data[0] as string == typeof(ErlInt).Name)
         {
             erl = new ErlInt(StringKit.toInt(data[1].ToString()));
         }
         else if (data[0] as string == typeof(ErlString).Name)
         {
             erl = new ErlString(data[1].ToString());
         }
         else if (data[0] as string == typeof(ErlByte).Name)
         {
             erl = new ErlByte(byte.Parse(data[1].ToString()));
         }
         else if (data[0] as string == typeof(ErlAtom).Name)
         {
             erl = new ErlAtom(data[1].ToString());
         }
         else if (data[0] as string == typeof(ErlNullList).Name)
         {
             erl = new ErlNullList();
         }
         else if (data[0] as string == typeof(ErlArray).Name)
         {
             erl = new ErlArray(getErlArr(data[1] as List <object>));
         }
         arr[i] = erl;
     }
     return(arr);
 }
Пример #3
0
        public void ErlByteTest()
        {
            var t = new ErlByte(10);

            Assert.IsTrue(t.Equals(new ErlByte(10)));
            Assert.AreEqual(t, new ErlByte(10));
            Assert.IsTrue(new ErlByte(1).CompareTo(t) < 0);
            Assert.AreEqual(10, t.Value);
            Assert.AreEqual(10, t.ValueAsInt);
            Assert.AreEqual(10, t.ValueAsLong);
            Assert.AreEqual("10", t.ToString());
            Assert.IsTrue(t.IsScalar);
            Assert.AreEqual(ErlTypeOrder.ErlByte, t.TypeOrder);

            Assert.IsTrue(t.Matches(new ErlByte(10)));
            Assert.AreEqual(new ErlVarBind(), t.Match(new ErlByte(10)));

            Assert.DoesNotThrow(() => { var x = t.ValueAsObject; });
            Assert.AreEqual(10, t.ValueAsInt);
            Assert.AreEqual(10, t.ValueAsLong);
            Assert.AreEqual(10, t.ValueAsDecimal);
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsDateTime; });
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsTimeSpan; });
            Assert.AreEqual(10.0, t.ValueAsDouble);
            Assert.AreEqual("10", t.ValueAsString);
            Assert.AreEqual(true, t.ValueAsBool);
            Assert.AreEqual('\n', t.ValueAsChar);
            Assert.Throws <ErlIncompatibleTypesException>(() => { var x = t.ValueAsByteArray; });

            IErlObject temp = null;

            Assert.IsFalse(t.Subst(ref temp, new ErlVarBind()));
            Assert.AreEqual(10, t.Visit(0, (acc, o) => o.ValueAsInt));

            char    n = (char)t; Assert.AreEqual('\n', n);
            byte    m = t; Assert.AreEqual(10, m);
            ErlByte b = 10; Assert.AreEqual(10, b.Value);
            ErlByte k = (ErlByte)10; Assert.AreEqual(10, k.Value);
            ErlByte z = (ErlByte)'\n'; Assert.AreEqual(10, k.Value);

            {
                var bind = new ErlVarBind();
                Assert.IsTrue(b.Match(new ErlLong(10), bind));
                Assert.IsTrue(new ErlLong(10).Match(b, bind));
                b = 111;
                Assert.IsTrue(b.Match(new ErlLong(111), bind));
                Assert.IsTrue(new ErlLong(111).Match(b, bind));
            }
        }
Пример #4
0
    /** 得到通讯返回port */
    public int getPort()
    {
        ErlInt erlInt = getValue(null) as ErlInt;

        if (erlInt != null)
        {
            return(erlInt.Value);
        }

        ErlByte erlByte = getValue(null) as ErlByte;

        if (erlByte != null)
        {
            return(erlByte.Value);
        }
        //throw new IOError("port is err");

        return(0);
    }
Пример #5
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);
                Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
                var es = new ErlInputStream(b);
                Assert.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);
                Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
                var es = new ErlInputStream(b);
                Assert.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);
                Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
                var es1 = new ErlInputStream(b1);
                Assert.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);
                Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
                var es2 = new ErlInputStream(b2);
                Assert.AreEqual(t2, es2.Read());
            }
            {
                var b  = new byte[] { 131, 97, 127 };
                var t  = new ErlByte(127);
                var os = new ErlOutputStream(t);
                Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
                var es = new ErlInputStream(b);
                Assert.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);
                Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
                var es = new ErlInputStream(b);
                Assert.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);
                Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
                var es = new ErlInputStream(b);
                Assert.AreEqual(t, es.Read());
            }
            {
                var b1  = new byte[] { 131, 98, 255, 255, 255, 251 };
                var t1  = new ErlLong(-5);
                var os1 = new ErlOutputStream(t1);
                Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
                var es1 = new ErlInputStream(b1);
                Assert.AreEqual(t1, es1.Read());

                var b2  = new byte[] { 131, 97, 5 };
                var t2  = new ErlLong(5);
                var os2 = new ErlOutputStream(t2);
                Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
                var es2 = new ErlInputStream(b2);
                Assert.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);
                Assert.AreEqual(b3, os3.GetBuffer().TakeWhile((_, i) => i < b3.Length).ToArray());
                var es3 = new ErlInputStream(b3);
                Assert.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);
                Assert.AreEqual(b4, os4.GetBuffer().TakeWhile((_, i) => i < b4.Length).ToArray());
                var es4 = new ErlInputStream(b4);
                Assert.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);
                Assert.AreEqual(b5, os5.GetBuffer().TakeWhile((_, i) => i < b5.Length).ToArray());
                var es5 = new ErlInputStream(b5);
                Assert.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);
                Assert.AreEqual(b6, os6.GetBuffer().TakeWhile((_, i) => i < b6.Length).ToArray());
                var es6 = new ErlInputStream(b6);
                Assert.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);
                Assert.AreEqual(t7, es7.Read());
                var bi7 = new byte[] { 131, 98, 255, 255, 255, 255 };
                var os7 = new ErlOutputStream(t7);
                Assert.AreEqual(bi7, 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);
                Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
                var es = new ErlInputStream(b);
                Assert.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);
                Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
                var es = new ErlInputStream(b);
                Assert.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);
                Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
                var es = new ErlInputStream(b);
                Assert.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);
                Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
                var es = new ErlInputStream(b);
                Assert.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);
                Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
                var es = new ErlInputStream(b);
                Assert.AreEqual(t, es.Read());
            }
        }
Пример #6
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());
            }
        }
Пример #7
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);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.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);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.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);
            Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
            var es1 = new ErlInputStream(b1);
            Assert.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);
            Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
            var es2 = new ErlInputStream(b2);
            Assert.AreEqual(t2, es2.Read());
              }
              {
            var b = new byte[] { 131, 97, 127 };
            var t = new ErlByte(127);
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.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);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.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);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.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);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.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);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.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);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b1 = new byte[] { 131, 98, 255, 255, 255, 251 };
            var t1 = new ErlLong(-5);
            var os1 = new ErlOutputStream(t1);
            Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
            var es1 = new ErlInputStream(b1);
            Assert.AreEqual(t1, es1.Read());

            var b2 = new byte[] { 131, 97, 5 };
            var t2 = new ErlLong(5);
            var os2 = new ErlOutputStream(t2);
            Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
            var es2 = new ErlInputStream(b2);
            Assert.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);
            Assert.AreEqual(b3, os3.GetBuffer().TakeWhile((_, i) => i < b3.Length).ToArray());
            var es3 = new ErlInputStream(b3);
            Assert.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);
            Assert.AreEqual(b4, os4.GetBuffer().TakeWhile((_, i) => i < b4.Length).ToArray());
            var es4 = new ErlInputStream(b4);
            Assert.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);
            Assert.AreEqual(b5, os5.GetBuffer().TakeWhile((_, i) => i < b5.Length).ToArray());
            var es5 = new ErlInputStream(b5);
            Assert.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);
            Assert.AreEqual(b6, os6.GetBuffer().TakeWhile((_, i) => i < b6.Length).ToArray());
            var es6 = new ErlInputStream(b6);
            Assert.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);
            Assert.AreEqual(t7, es7.Read());
            var bi7 = new byte[] {131, 98, 255, 255, 255, 255};
            var os7 = new ErlOutputStream(t7);
            Assert.AreEqual(bi7, 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);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.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);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.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);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.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);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.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);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
        }
Пример #8
0
    /// <summary>
    /// Compile a string fmt into an Erlang term
    /// </summary>
    internal static IErlObject Parse(
        string fmt, ref int pos, ref int argc, params object[] args)
    {
      var items = new List<IErlObject>();
      IErlObject result = null;
      pos = skipWSAndComments(fmt, pos);

      if (pos < fmt.Length)
      {
        switch (fmt[pos++])
        {
          case '{':
            if (State.Ok != pTuple(fmt, ref pos, ref items, ref argc, args))
              throw new ErlException(StringConsts.ERL_PARSING_AT_ERROR, "tuple", pos);
            result = new ErlTuple(items, false);
            break;

          case '[':
            if (fmt[pos] == ']')
            {
              result = new ErlList();
              pos++;
              break;
            }
            else if (State.Ok == pList(fmt, ref pos, ref items, ref argc, args))
            {
              result = new ErlList(items, false);
              break;
            }
            throw new ErlException(StringConsts.ERL_PARSING_AT_ERROR, "list", pos);

          case '$': /* char-value? */
            result = new ErlByte(Convert.ToByte(fmt[pos++]));
            break;

          case '~':
            if (State.Ok != pFormat(fmt, ref pos, ref items, ref argc, args))
              throw new ErlException(StringConsts.ERL_PARSING_AT_ERROR, "term", pos);
            result = items[0];
            break;

          default:
            char c = fmt[--pos];
            if (char.IsLower(c))
            {         /* atom  ? */
              string s = pAtom(fmt, ref pos);
              result = createAtom(s);
            }
            else if (char.IsUpper(c) || c == '_')
            {
              result = pVariable(fmt, ref pos);
            }
            else if (char.IsDigit(c) || c == '-')
            {    /* integer/float ? */
              string s = pDigit(fmt, ref pos);
              if (s.IndexOf('.') < 0)
                result = new ErlLong(long.Parse(s, CultureInfo.InvariantCulture));
              else
                result = new ErlDouble(double.Parse(s, CultureInfo.InvariantCulture));
            }
            else if (c == '"')
            {      /* string ? */
              string s = pString(fmt, ref pos);
              result = new ErlString(s);
            }
            else if (c == '\'')
            {     /* quoted atom ? */
              string s = pQuotedAtom(fmt, ref pos);
              result = createAtom(s);
            }
            break;
        }
      }

      if (result == null)
        throw new ErlException(StringConsts.ERL_INVALID_VALUE_ERROR.Args(fmt));

      return result;
    }
Пример #9
0
 private static ErlType[] getErlArr(List<object> jsonList)
 {
     ErlType[] typeArray = new ErlType[jsonList.Count];
     for (int i = 0; i < jsonList.Count; i++)
     {
         List<object> list = jsonList[i] as List<object>;
         ErlType type = null;
         if ((list[0] as string) == typeof(ErlInt).Name)
         {
             type = new ErlInt(StringKit.toInt(list[1].ToString()));
         }
         else if ((list[0] as string) == typeof(ErlString).Name)
         {
             type = new ErlString(list[1].ToString());
         }
         else if ((list[0] as string) == typeof(ErlByte).Name)
         {
             type = new ErlByte(byte.Parse(list[1].ToString()));
         }
         else if ((list[0] as string) == typeof(ErlAtom).Name)
         {
             type = new ErlAtom(list[1].ToString());
         }
         else if ((list[0] as string) == typeof(ErlNullList).Name)
         {
             type = new ErlNullList();
         }
         else if ((list[0] as string) == typeof(ErlArray).Name)
         {
             type = new ErlArray(getErlArr(list[1] as List<object>));
         }
         typeArray[i] = type;
     }
     return typeArray;
 }
Пример #10
0
    /** 检查列表内的所有元素都是数字,并且在0-16#80000000范围之间,条件内的列表数据转换成字符串返回 */
    public string transNumber()
    {
        if (!isString)
        {
            return(null);
        }
        ByteBuffer byteArray = new ByteBuffer();
        ErlByte    erlByte   = null;
        ErlInt     erlInt    = null;
        string     str       = null;
        string     s         = null;

        for (int j = 0; j < _value.Length; j++)
        {
            erlByte = _value [j] as ErlByte;
            //			if (j == 0 && erlByte != null && erlByte.Value == 0) {// 如果列表第一位是0,则不执行转换为字符串的操作
            //				return null;
            //			}
            if (erlByte != null && erlByte.Value > 0)
            {
                s = checkTrans(erlByte.Value);
                if (str == null)
                {
                    str = "";
                }
                if (s != null)
                {
                    str += s;
                }
                else                    // 否则把byteArray中已写入的字节转换成字符串并连接到str中
                                        //						byteArray.clear();
                                        //						byteArray.writeByte(erlByte.value);// 不是转义数字则继续写到字节数组中
                                        //						byteArray.position=0;
                                        //						if(byteArray.bytesAvailable!=0)
                                        //						{
                                        //							str+=byteArray.readMultiByte(byteArray.bytesAvailable,"//unicode");
                                        //						}
                                        //						str+=String.fromCharCode(erlByte.Value); 暂时采用下面的写法
                {
                    str += new string (new char[] { (char)(erlByte.Value) });
                }
                continue;
            }
            else
            {
                erlInt = _value [j] as ErlInt;
                if (erlInt != null && erlInt.Value > 0 && erlInt.Value < int.MaxValue)
                {
                    s = checkTrans(erlInt.Value);
                    if (str == null)
                    {
                        str = "";
                    }
                    if (s != null)
                    {
                        str += s;
                    }
                    else
                    {
                        //						byteArray.clear();
                        //						byteArray.writeInt(erlInt.value);// 不是转义数字则继续写到字节数组中
                        //						byteArray.position=0;
                        //						if(byteArray.bytesAvailable!=0)
                        //						{
                        //							trace("字符串转换  byteArray.bytesAvailable="+byteArray.bytesAvailable);
                        //							str+=byteArray.readMultiByte(byteArray.bytesAvailable,"unicode");
                        //						}
                        //str+=String.fromCharCode(erlByte.Value); 暂时采用下面的写法
//						str += new string (new char[]{(char)(erlInt.Value)});
                        str += char.ConvertFromUtf32(erlInt.Value);
                    }
                    continue;
                }
            }
            str = null;
            return(str);
        }
        return(str);
    }
Пример #11
0
        /// <summary>
        /// Compile a string fmt into an Erlang term
        /// </summary>
        internal static IErlObject Parse(
            string fmt, ref int pos, ref int argc, params object[] args)
        {
            var items = new List<IErlObject>();
              IErlObject result = null;
              pos = skipWSAndComments(fmt, pos);

              if (pos < fmt.Length)
              {
            switch (fmt[pos++])
            {
              case '{':
            if (State.Ok != pTuple(fmt, ref pos, ref items, ref argc, args))
              throw new ErlException(StringConsts.ERL_PARSING_AT_ERROR, "tuple", pos);
            result = new ErlTuple(items, false);
            break;

              case '[':
            if (fmt[pos] == ']')
            {
              result = new ErlList();
              pos++;
              break;
            }
            else if (State.Ok == pList(fmt, ref pos, ref items, ref argc, args))
            {
              result = new ErlList(items, false);
              break;
            }
            throw new ErlException(StringConsts.ERL_PARSING_AT_ERROR, "list", pos);

              case '<':
            if (pos < fmt.Length - 1 && fmt[pos] == '<')
            {
              var i = ++pos;
              var str = fmt[i] == '"';
              if (str) pos++;
              for (; i < fmt.Length && fmt[i - 1] != '>' && fmt[i] != '>'; ++i);
              if (i == fmt.Length)
                break;
              var end   = ++i - (str ? 3 : 2);
              var len   = end - pos + 1;
              byte[] bytes;
              if (str)
              {
                var cnt = Encoding.UTF8.GetByteCount(fmt.ToCharArray(), pos, len);
                bytes = new byte[cnt];
                Encoding.UTF8.GetBytes(fmt, pos, len, bytes, 0);
              }
              else
              {
                var beg = pos - 2;
                bytes   = fmt.Substring(pos, len)
                             .Split(new char[] {',', ' '},
                                    StringSplitOptions.RemoveEmptyEntries)
                             .Select(s =>
                             {
                               var n = int.Parse(s);
                               if (n < 0 || n > 255)
                                 throw new ErlException
                                  ("Invalid binary in format string: {0}".Args(fmt.Substring(beg, len+4)));
                               return (byte)n;
                             })
                             .ToArray();
              }
              result = new ErlBinary(bytes, 0, bytes.Length);
              pos = i+1;
            }
            break;
              case '$': /* char-value? */
            result = new ErlByte(Convert.ToByte(fmt[pos++]));
            break;

              case '~':
            if (State.Ok != pFormat(fmt, ref pos, ref items, ref argc, args))
              throw new ErlException(StringConsts.ERL_PARSING_AT_ERROR, "term", pos);
            result = items[0];
            break;

              default:
            char c = fmt[--pos];
            if (char.IsLower(c))
            {         /* atom  ? */
              string s = pAtom(fmt, ref pos);
              result = createAtom(s);
            }
            else if (char.IsUpper(c) || c == '_')
            {
              result = pVariable(fmt, ref pos);
            }
            else if (char.IsDigit(c) || c == '-')
            {    /* integer/float ? */
              string s = pDigit(fmt, ref pos);
              if (s.IndexOf('.') < 0)
                result = new ErlLong(long.Parse(s, CultureInfo.InvariantCulture));
              else
                result = new ErlDouble(double.Parse(s, CultureInfo.InvariantCulture));
            }
            else if (c == '"')
            {      /* string ? */
              string s = pString(fmt, ref pos);
              result = new ErlString(s);
            }
            else if (c == '\'')
            {     /* quoted atom ? */
              string s = pQuotedAtom(fmt, ref pos);
              result = createAtom(s);
            }
            break;
            }
              }

              if (result == null)
            throw new ErlException(StringConsts.ERL_INVALID_VALUE_ERROR.Args(fmt));

              return result;
        }
Пример #12
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);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.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);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.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);
        Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
        var es1 = new ErlInputStream(b1);
        Assert.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);
        Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
        var es2 = new ErlInputStream(b2);
        Assert.AreEqual(t2, es2.Read());
      }
      {
        var b = new byte[] { 131, 97, 127 };
        var t = new ErlByte(127);
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.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);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.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);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b1 = new byte[] { 131, 98, 255, 255, 255, 251 };
        var t1 = new ErlLong(-5);
        var os1 = new ErlOutputStream(t1);
        Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
        var es1 = new ErlInputStream(b1);
        Assert.AreEqual(t1, es1.Read());

        var b2 = new byte[] { 131, 97, 5 };
        var t2 = new ErlLong(5);
        var os2 = new ErlOutputStream(t2);
        Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
        var es2 = new ErlInputStream(b2);
        Assert.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);
        Assert.AreEqual(b3, os3.GetBuffer().TakeWhile((_, i) => i < b3.Length).ToArray());
        var es3 = new ErlInputStream(b3);
        Assert.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);
        Assert.AreEqual(b4, os4.GetBuffer().TakeWhile((_, i) => i < b4.Length).ToArray());
        var es4 = new ErlInputStream(b4);
        Assert.AreEqual(t4, es4.Read());

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

        var b6 = new byte[] { 131, 110, 8, 0, 0, 0, 0, 0, 0, 0, 0, 128 };
        var t6 = new ErlLong(-1L << 63);
        var os6 = new ErlOutputStream(t6);
        Assert.AreEqual(b6, os6.GetBuffer().TakeWhile((_, i) => i < b6.Length).ToArray());
        var es6 = new ErlInputStream(b6);
        Assert.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);
        Assert.AreEqual(t7, es7.Read());
        var bi7 = new byte[] {131, 98, 255, 255, 255, 255};
        var os7 = new ErlOutputStream(t7);
        Assert.AreEqual(bi7, 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);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.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);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.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);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.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);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.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);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
    }
Пример #13
0
        public void ErlByteTest()
        {
            var t = new ErlByte(10);
              Assert.IsTrue(t.Equals(new ErlByte(10)));
              Assert.AreEqual(t, new ErlByte(10));
              Assert.IsTrue(new ErlByte(1).CompareTo(t) < 0);
              Assert.AreEqual(10, t.Value);
              Assert.AreEqual(10, t.ValueAsInt);
              Assert.AreEqual(10, t.ValueAsLong);
              Assert.AreEqual("10", t.ToString());
              Assert.IsTrue(t.IsScalar);
              Assert.AreEqual(ErlTypeOrder.ErlByte, t.TypeOrder);

              Assert.IsTrue(t.Matches(new ErlByte(10)));
              Assert.AreEqual(new ErlVarBind(), t.Match(new ErlByte(10)));

              Assert.DoesNotThrow(() => { var x = t.ValueAsObject; });
              Assert.AreEqual(10, t.ValueAsInt);
              Assert.AreEqual(10, t.ValueAsLong);
              Assert.AreEqual(10, t.ValueAsDecimal);
              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsDateTime; });
              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsTimeSpan; });
              Assert.AreEqual(10.0, t.ValueAsDouble);
              Assert.AreEqual("10", t.ValueAsString);
              Assert.AreEqual(true, t.ValueAsBool);
              Assert.AreEqual('\n', t.ValueAsChar);
              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsByteArray; });

              IErlObject temp = null;
              Assert.IsFalse(t.Subst(ref temp, new ErlVarBind()));
              Assert.AreEqual(10, t.Visit(0, (acc, o) => o.ValueAsInt));

              char n = (char)t; Assert.AreEqual('\n', n);
              byte m = t; Assert.AreEqual(10, m);
              ErlByte b = 10; Assert.AreEqual(10, b.Value);
              ErlByte k = (ErlByte)10; Assert.AreEqual(10, k.Value);
              ErlByte z = (ErlByte)'\n'; Assert.AreEqual(10, k.Value);

              {
            var bind = new ErlVarBind();
            Assert.IsTrue(b.Match(new ErlLong(10), bind));
            Assert.IsTrue(new ErlLong(10).Match(b, bind));
            b = 111;
            Assert.IsTrue(b.Match(new ErlLong(111), bind));
            Assert.IsTrue(new ErlLong(111).Match(b, bind));
              }
        }
Пример #14
0
        /// <summary>
        /// Compile a string fmt into an Erlang term
        /// </summary>
        internal static IErlObject Parse(
            string fmt, ref int pos, ref int argc, params object[] args)
        {
            var        items  = new List <IErlObject>();
            IErlObject result = null;

            pos = skipWSAndComments(fmt, pos);

            if (pos < fmt.Length)
            {
                switch (fmt[pos++])
                {
                case '{':
                    if (State.Ok != pTuple(fmt, ref pos, ref items, ref argc, args))
                    {
                        throw new ErlException(StringConsts.ERL_PARSING_AT_ERROR, "tuple", pos);
                    }
                    result = new ErlTuple(items, false);
                    break;

                case '[':
                    if (fmt[pos] == ']')
                    {
                        result = new ErlList();
                        pos++;
                        break;
                    }
                    else if (State.Ok == pList(fmt, ref pos, ref items, ref argc, args))
                    {
                        result = new ErlList(items, false);
                        break;
                    }
                    throw new ErlException(StringConsts.ERL_PARSING_AT_ERROR, "list", pos);

                case '<':
                    if (pos < fmt.Length - 1 && fmt[pos] == '<')
                    {
                        var i   = ++pos;
                        var str = fmt[i] == '"';
                        if (str)
                        {
                            pos++;
                        }
                        for (; i < fmt.Length && fmt[i - 1] != '>' && fmt[i] != '>'; ++i)
                        {
                            ;
                        }
                        if (i == fmt.Length)
                        {
                            break;
                        }
                        var    end = ++i - (str ? 3 : 2);
                        var    len = end - pos + 1;
                        byte[] bytes;
                        if (str)
                        {
                            var cnt = Encoding.UTF8.GetByteCount(fmt.ToCharArray(), pos, len);
                            bytes = new byte[cnt];
                            Encoding.UTF8.GetBytes(fmt, pos, len, bytes, 0);
                        }
                        else
                        {
                            var beg = pos - 2;
                            bytes = fmt.Substring(pos, len)
                                    .Split(new char[] { ',', ' ' },
                                           StringSplitOptions.RemoveEmptyEntries)
                                    .Select(s =>
                            {
                                var n = int.Parse(s);
                                if (n < 0 || n > 255)
                                {
                                    throw new ErlException
                                        ("Invalid binary in format string: {0}".Args(fmt.Substring(beg, len + 4)));
                                }
                                return((byte)n);
                            })
                                    .ToArray();
                        }
                        result = new ErlBinary(bytes, 0, bytes.Length);
                        pos    = i + 1;
                    }
                    break;

                case '$': /* char-value? */
                    result = new ErlByte(Convert.ToByte(fmt[pos++]));
                    break;

                case '~':
                    if (State.Ok != pFormat(fmt, ref pos, ref items, ref argc, args))
                    {
                        throw new ErlException(StringConsts.ERL_PARSING_AT_ERROR, "term", pos);
                    }
                    result = items[0];
                    break;

                default:
                    char c = fmt[--pos];
                    if (char.IsLower(c))
                    { /* atom  ? */
                        string s = pAtom(fmt, ref pos);
                        result = createAtom(s);
                    }
                    else if (char.IsUpper(c) || c == '_')
                    {
                        result = pVariable(fmt, ref pos);
                    }
                    else if (char.IsDigit(c) || c == '-')
                    { /* integer/float ? */
                        string s = pDigit(fmt, ref pos);
                        if (s.IndexOf('.') < 0)
                        {
                            result = new ErlLong(long.Parse(s, CultureInfo.InvariantCulture));
                        }
                        else
                        {
                            result = new ErlDouble(double.Parse(s, CultureInfo.InvariantCulture));
                        }
                    }
                    else if (c == '"')
                    { /* string ? */
                        string s = pString(fmt, ref pos);
                        result = new ErlString(s);
                    }
                    else if (c == '\'')
                    { /* quoted atom ? */
                        string s = pQuotedAtom(fmt, ref pos);
                        result = createAtom(s);
                    }
                    break;
                }
            }

            if (result == null)
            {
                throw new ErlException(StringConsts.ERL_INVALID_VALUE_ERROR.Args(fmt));
            }

            return(result);
        }
Пример #15
0
        /// <summary>
        /// Compile a string fmt into an Erlang term
        /// </summary>
        internal static IErlObject Parse(
            string fmt, ref int pos, ref int argc, params object[] args)
        {
            var        items  = new List <IErlObject>();
            IErlObject result = null;

            pos = skipWSAndComments(fmt, pos);

            if (pos < fmt.Length)
            {
                switch (fmt[pos++])
                {
                case '{':
                    if (State.Ok != pTuple(fmt, ref pos, ref items, ref argc, args))
                    {
                        throw new ErlException(StringConsts.ERL_PARSING_AT_ERROR, "tuple", pos);
                    }
                    result = new ErlTuple(items, false);
                    break;

                case '[':
                    if (fmt[pos] == ']')
                    {
                        result = new ErlList();
                        pos++;
                        break;
                    }
                    else if (State.Ok == pList(fmt, ref pos, ref items, ref argc, args))
                    {
                        result = new ErlList(items, false);
                        break;
                    }
                    throw new ErlException(StringConsts.ERL_PARSING_AT_ERROR, "list", pos);

                case '$':     /* char-value? */
                    result = new ErlByte(Convert.ToByte(fmt[pos++]));
                    break;

                case '~':
                    if (State.Ok != pFormat(fmt, ref pos, ref items, ref argc, args))
                    {
                        throw new ErlException(StringConsts.ERL_PARSING_AT_ERROR, "term", pos);
                    }
                    result = items[0];
                    break;

                default:
                    char c = fmt[--pos];
                    if (char.IsLower(c))
                    {             /* atom  ? */
                        string s = pAtom(fmt, ref pos);
                        result = createAtom(s);
                    }
                    else if (char.IsUpper(c) || c == '_')
                    {
                        result = pVariable(fmt, ref pos);
                    }
                    else if (char.IsDigit(c) || c == '-')
                    {        /* integer/float ? */
                        string s = pDigit(fmt, ref pos);
                        if (s.IndexOf('.') < 0)
                        {
                            result = new ErlLong(long.Parse(s, CultureInfo.InvariantCulture));
                        }
                        else
                        {
                            result = new ErlDouble(double.Parse(s, CultureInfo.InvariantCulture));
                        }
                    }
                    else if (c == '"')
                    {          /* string ? */
                        string s = pString(fmt, ref pos);
                        result = new ErlString(s);
                    }
                    else if (c == '\'')
                    {         /* quoted atom ? */
                        string s = pQuotedAtom(fmt, ref pos);
                        result = createAtom(s);
                    }
                    break;
                }
            }

            if (result == null)
            {
                throw new ErlException(StringConsts.ERL_INVALID_VALUE_ERROR.Args(fmt));
            }

            return(result);
        }
Пример #16
0
 /// <summary>
 /// Write a single byte to the stream as an Erlang integer
 /// </summary>
 public void WriteByte(ErlByte b)
 {
   writeLong(b.ValueAsLong, false);
 }
Пример #17
0
 public static ErlType simpleAnalyse(ByteBuffer data)
 {
     int position = data.position;
     int num2 = data.readByte();
     data.position = position;
     switch (num2)
     {
     case 0x61:
         {
             ErlByte num3 = new ErlByte(0);
             num3.bytesRead(data);
             return num3;
         }
     case 0x6d:
         {
             ErlByteArray array = new ErlByteArray(null);
             array.simpleBytesRead(data);
             return array;
         }
     case 0x62:
         {
             ErlInt num4 = new ErlInt(0);
             num4.bytesRead(data);
             return num4;
         }
     case 0x6b:
         {
             ErlString str = new ErlString(string.Empty);
             str.bytesRead(data);
             return str;
         }
     case 110:
         {
             ErlLong @long = new ErlLong();
             @long.bytesRead(data);
             return @long;
         }
     }
     return null;
 }