Esempio n. 1
0
        /*
         * Determine if two tuples are equal. Tuples are equal if they have
         * the same arity and all of the elements are equal.
         *
         * @param o the tuple to compare to.
         *
         * @return true if the tuples have the same arity and all the
         * elements are equal.
         **/
        public override bool Equals(System.Object o)
        {
            if (!(o is Tuple))
            {
                return(false);
            }

            Tuple t = (Tuple)o;
            int   a = this.arity();

            if (a != t.arity())
            {
                return(false);
            }

            for (int i = 0; i < a; i++)
            {
                if (!this.elems[i].Equals(t.elems[i]))
                {
                    return(false);
                }
                // early exit
            }

            return(true);
        }
Esempio n. 2
0
 public int encode_size(Erlang.Object o)
 {
     if (o is Erlang.Atom)
     {
         return(1 + 2 + o.atomValue().Length);
     }
     else if (o is Erlang.Boolean)
     {
         return(1 + 2 + (o.boolValue()
                                               ? Erlang.Boolean.s_true.atomValue().Length
                                               : Erlang.Boolean.s_false.atomValue().Length));
     }
     else if (o is Erlang.Binary)
     {
         return(5 + o.binaryValue().Length);
     }
     else if (o is Erlang.Long)
     {
         long l = o.longValue();
         if ((l & 0xff) == l)
         {
             return(2);
         }
         else if ((l <= OtpExternal.erlMax) && (l >= OtpExternal.erlMin))
         {
             return(5);
         }
         return(long_arity(l));
     }
     else if (o is Erlang.Byte)
     {
         return(1 + 1);
     }
     else if (o is Erlang.Double)
     {
         return(9);
     }
     else if (o is Erlang.String)
     {
         string l = o.stringValue();
         if (l.Length == 0)
         {
             return(1);
         }
         if (l.Length < 0xffff)
         {
             return(2 + l.Length);
         }
         return(1 + 4 + 2 * l.Length);
     }
     else if (o is Erlang.List)
     {
         Erlang.List l = o.listValue();
         if (l.arity() == 0)
         {
             return(1);
         }
         int sz = 5;
         for (int i = 0; i < l.arity(); i++)
         {
             sz += encode_size(l[i]);
         }
         return(sz);
     }
     else if (o is Erlang.Tuple)
     {
         Erlang.Tuple l  = o.tupleValue();
         int          sz = 1 + (l.arity() < 0xff ? 1 : 4);
         for (int i = 0; i < l.arity(); i++)
         {
             sz += encode_size(l[i]);
         }
         return(sz);
     }
     else if (o is Erlang.Pid)
     {
         Erlang.Pid p = o.pidValue();
         return(1 + (1 + 2 + p.node().Length) + 4 + 4 + 1);
     }
     else if (o is Erlang.Ref)
     {
         Erlang.Ref p   = o.refValue();
         int[]      ids = p.ids();
         return(1 + (1 + 2 + p.node().Length) + 1 + 4 * ids.Length);
     }
     else if (o is Erlang.Port)
     {
         Erlang.Port p = o.portValue();
         return(1 + (1 + 2 + p.node().Length) + 4 + 1);
     }
     else
     {
         throw new Erlang.Exception("Unknown encode size for object: " + o.ToString());
     }
 }
Esempio n. 3
0
 public void TestFormat()
 {
     {
         Erlang.Object obj1 = Erlang.Object.Format("a");
         Assert.IsInstanceOf(typeof(Erlang.Atom), obj1);
         Assert.AreEqual("a", (obj1 as Erlang.Atom).atomValue());
     }
     {
         Erlang.Object obj1 = Erlang.Object.Format("$a");
         Assert.IsInstanceOf(typeof(Erlang.Char), obj1);
         Assert.AreEqual('a', (obj1 as Erlang.Char).charValue());
     }
     {
         Erlang.Object obj1 = Erlang.Object.Format("'Abc'");
         Assert.IsInstanceOf(typeof(Erlang.Atom), obj1);
         Assert.AreEqual("Abc", (obj1 as Erlang.Atom).atomValue());
     }
     {
         Erlang.Object obj1 = Erlang.Object.Format("{'true', 'false', true, false}");
         Assert.IsInstanceOf(typeof(Erlang.Tuple), obj1);
         Erlang.Tuple t = obj1.Cast <Erlang.Tuple>();
         Assert.AreEqual(4, t.arity());
         foreach (Erlang.Object term in t.elements())
         {
             Assert.IsInstanceOf(typeof(Erlang.Boolean), term);
         }
         Assert.AreEqual(true, t[0].boolValue());
         Assert.AreEqual(false, t[1].boolValue());
         Assert.AreEqual(true, t[2].boolValue());
         Assert.AreEqual(false, t[3].boolValue());
     }
     {
         Erlang.Object obj1 = Erlang.Object.Format("\"Abc\"");
         Assert.IsInstanceOf(typeof(Erlang.String), obj1);
         Assert.AreEqual("Abc", (obj1 as Erlang.String).stringValue());
     }
     {
         Erlang.Object obj1 = Erlang.Object.Format("Abc");
         Assert.IsInstanceOf(typeof(Erlang.Var), obj1);
         Assert.AreEqual("Abc", (obj1 as Erlang.Var).name());
     }
     {
         Erlang.Object obj1 = Erlang.Object.Format("1");
         Assert.IsInstanceOf(typeof(Erlang.Long), obj1);
         Assert.AreEqual(1, (obj1 as Erlang.Long).longValue());
     }
     {
         Erlang.Object obj1 = Erlang.Object.Format("1.23");
         Assert.IsInstanceOf(typeof(Erlang.Double), obj1);
         Assert.AreEqual(1.23, (obj1 as Erlang.Double).doubleValue());
     }
     {
         Erlang.Object obj1 = Erlang.Object.Format("V");
         Assert.IsInstanceOf(typeof(Erlang.Var), obj1);
         Assert.AreEqual("V", (obj1 as Erlang.Var).name());
     }
     {
         Erlang.Object obj1 = Erlang.Object.Format("{1}");
         Assert.IsInstanceOf(typeof(Erlang.Tuple), obj1);
         Assert.AreEqual(1, (obj1 as Erlang.Tuple).arity());
         Assert.IsInstanceOf(typeof(Erlang.Long), (obj1 as Erlang.Tuple)[0]);
         Assert.AreEqual(1, ((obj1 as Erlang.Tuple)[0] as Erlang.Long).longValue());
     }
     {
         Erlang.Object obj0 = Erlang.Object.Format("[]");
         Assert.IsInstanceOf(typeof(Erlang.List), obj0);
         Assert.AreEqual(0, (obj0 as Erlang.List).arity());
         Erlang.Object obj1 = Erlang.Object.Format("[1]");
         Assert.IsInstanceOf(typeof(Erlang.List), obj1);
         Assert.AreEqual(1, (obj1 as Erlang.List).arity());
         Assert.IsInstanceOf(typeof(Erlang.Long), (obj1 as Erlang.List)[0]);
         Assert.AreEqual(1, ((obj1 as Erlang.List)[0] as Erlang.Long).longValue());
     }
     {
         Erlang.Object obj1 = Erlang.Object.Format("[{1,2}, []]");
         Assert.IsInstanceOf(typeof(Erlang.List), obj1);
         Assert.AreEqual(2, (obj1 as Erlang.List).arity());
         Assert.IsInstanceOf(typeof(Erlang.Tuple), (obj1 as Erlang.List)[0]);
         Assert.AreEqual(2, ((obj1 as Erlang.List)[0] as Erlang.Tuple).arity());
         Assert.AreEqual(0, ((obj1 as Erlang.List)[1] as Erlang.List).arity());
     }
     {
         Erlang.Object obj1 = Erlang.Object.Format("{a, [b, 1, 2.0, \"abc\"], {1, 2}}");
         Assert.IsInstanceOf(typeof(Erlang.Tuple), obj1);
         Assert.AreEqual(3, (obj1 as Erlang.Tuple).arity());
     }
     {
         Erlang.Object obj1 = Erlang.Object.Format("~w", 1);
         Assert.IsInstanceOf(typeof(Erlang.Long), obj1);
         Assert.AreEqual(1, (obj1 as Erlang.Long).longValue());
         Erlang.Object obj2 = Erlang.Object.Format("{~w, ~w,~w}", 1, 2, 3);
         Assert.IsInstanceOf(typeof(Erlang.Tuple), obj2);
         Assert.AreEqual(3, (obj2 as Erlang.Tuple).arity());
         Assert.IsInstanceOf(typeof(Erlang.Long), (obj2 as Erlang.Tuple)[0]);
         Assert.AreEqual(1, ((obj2 as Erlang.Tuple)[0] as Erlang.Long).longValue());
         Assert.IsInstanceOf(typeof(Erlang.Long), (obj2 as Erlang.Tuple)[1]);
         Assert.AreEqual(2, ((obj2 as Erlang.Tuple)[1] as Erlang.Long).longValue());
         Assert.IsInstanceOf(typeof(Erlang.Long), (obj2 as Erlang.Tuple)[2]);
         Assert.AreEqual(3, ((obj2 as Erlang.Tuple)[2] as Erlang.Long).longValue());
     }
     {
         Erlang.Object obj2 = Erlang.Object.Format("{~w, ~w,~w,~w, ~w}", 1.0, 'a', "abc", 2, true);
         Assert.IsInstanceOf(typeof(Erlang.Tuple), obj2);
         Assert.AreEqual(5, (obj2 as Erlang.Tuple).arity());
         Assert.IsInstanceOf(typeof(Erlang.Double), (obj2 as Erlang.Tuple)[0]);
         Assert.AreEqual(1.0, ((obj2 as Erlang.Tuple)[0] as Erlang.Double).doubleValue());
         Assert.IsInstanceOf(typeof(Erlang.Char), (obj2 as Erlang.Tuple)[1]);
         Assert.AreEqual('a', ((obj2 as Erlang.Tuple)[1] as Erlang.Char).charValue());
         Assert.IsInstanceOf(typeof(Erlang.String), (obj2 as Erlang.Tuple)[2]);
         Assert.AreEqual("abc", ((obj2 as Erlang.Tuple)[2] as Erlang.String).stringValue());
         Assert.IsInstanceOf(typeof(Erlang.Long), (obj2 as Erlang.Tuple)[3]);
         Assert.AreEqual(2, ((obj2 as Erlang.Tuple)[3] as Erlang.Long).longValue());
         Assert.IsInstanceOf(typeof(Erlang.Boolean), (obj2 as Erlang.Tuple)[4]);
         Assert.AreEqual(true, ((obj2 as Erlang.Tuple)[4] as Erlang.Boolean).booleanValue());
     }
 }