Beispiel #1
0
        public void Structs()
        {
            var s1 = new MyStruct(1, 2);
            var s2 = new MyStruct(1, 2);
            var s3 = new MyStruct(1, 3);

            IsTrue(s1.Equals(s2));
            IsTrue((object)s1 == (object)s2);
            IsTrue((System.ValueType)s1 == (System.ValueType)s2);
            IsFalse(s1.Equals(s3));
            IsTrue(s1.GetHashCode() != -1);
            Equal(s1.Sum, 3);
        }
Beispiel #2
0
        public void InstanceEquals_WithTwoStructs_ReturnsFalse()
        {
            var s1 = new MyStruct {
                Value = 1
            };
            var s2 = new MyStruct {
                Value = 1
            };

            Assert.Equal(false, s1.Equals(s2));
        }
Beispiel #3
0
 protected bool Equals(SimplePO other)
 {
     return(Number == other.Number &&
            string.Equals(Text, other.Text) &&
            Equals(Nested, other.Nested) &&
            ((List == null && other.List == null) || List.SequenceEqual(other.List)) &&
            ((Array == null && other.Array == null) || Array.SequenceEqual(other.Array)) &&
            Dictionary.All(e => other.Dictionary.ContainsKey(e.Key) && Equals(e.Value, other.Dictionary[e.Key])) &&
            PrivateInt == other.PrivateInt &&
            Equals(Polymorf1, other.Polymorf1) &&
            Equals(Polymorf2, other.Polymorf2) &&
            Equals(Custom, other.Custom) &&
            Struct.Equals(other.Struct));
 }
Beispiel #4
0
    public bool NegTest10()
    {
        bool     retVal = true;
        Array    array;
        int      length;
        MyStruct element;
        int      newIndex;

        TestLibrary.TestFramework.BeginScenario("NegTest10: Array.IndexOf(Array, object, int, int) non-primitive type (not derived from object) not found");

        try
        {
            // creat the array
            length = (TestLibrary.Generator.GetInt32(-55) % (c_MAX_SIZE - c_MIN_SIZE)) + c_MIN_SIZE;
            array  = Array.CreateInstance(typeof(MyStruct), length);

            element = new MyStruct(TestLibrary.Generator.GetSingle(-55));

            // fill the array
            for (int i = 0; i < array.Length; i++)
            {
                do
                {
                    array.SetValue(new MyStruct(TestLibrary.Generator.GetSingle(-55)), i);
                }while(element.Equals((MyStruct)array.GetValue(i)));
            }

            newIndex = Array.IndexOf(array, (object)element, 0, array.Length);

            if (-1 != newIndex)
            {
                TestLibrary.TestFramework.LogError("030", "Unexpected index: Expected(-1) Actual(" + newIndex + ")");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("031", "Unexpected exception: " + e);
            retVal = false;
        }

        return(retVal);
    }
Beispiel #5
0
    public bool NegTest10()
    {
        bool     retVal = true;
        Array    array;
        int      length;
        MyStruct element;
        int      newIndex;

        TestLibrary.TestFramework.BeginScenario("NegTest10: Array.LastInexOf(Array, object, int, int) non-primitive type (not derived from object) not found");

        try
        {
            // creat the array
            length = (TestLibrary.Generator.GetInt32(-55) % (c_MAX_SIZE-c_MIN_SIZE)) + c_MIN_SIZE;
            array  = Array.CreateInstance(typeof(MyStruct), length);

            element = new MyStruct(TestLibrary.Generator.GetSingle(-55));

            // fill the array
            for (int i=0; i<array.Length; i++)
            {
                do
                {
                    array.SetValue(new MyStruct(TestLibrary.Generator.GetSingle(-55)), i);
                }
                while(element.Equals((MyStruct)array.GetValue(i)));
            }

            newIndex = Array.LastIndexOf(array, (object)element, array.Length-1, array.Length);

            if (-1 != newIndex)
            {
                TestLibrary.TestFramework.LogError("030", "Unexpected index: Expected(-1) Actual(" + newIndex + ")");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("031", "Unexpected exception: " + e);
            retVal = false;
        }

        return retVal;
    }
Beispiel #6
0
        public Equivalence()
        {
            //Entry point

            // *******************
            // Reference semantics
            // *******************
            MyObj r1 = new MyObj(2, 3);
            MyObj r2 = new MyObj(-2, -3);
            MyObj r3 = r1;

            Display(r1, "r1");
            Display(r2, "r2");
            Display(r3, "r3");

            Console.WriteLine("Update r1");
            r1.a *= -1;
            r1.b *= -1;

            Display(r1, "r1");
            Display(r2, "r2");
            Display(r3, "r3");

            //Compare two absolutely equal objects
            if (r1.Equals(r3))
            {
                Console.WriteLine("r1 Equals r3");
            }
            else
            {
                Console.WriteLine("r1 is not Equal to r3");
            }

            if (r1 == r3)
            {
                Console.WriteLine("r1 is a reference to r3");
            }
            else
            {
                Console.WriteLine("r1 is not a reference to r3");
            }

            Console.WriteLine("***** Test for equality *****");
            Console.WriteLine("Now set the values of r2 to the same values as r1");

            //Compare two similar objects
            r2.a = r1.a;
            r2.b = r1.b;
            Display(r1, "r1");
            Display(r2, "r2");

            if (r1.Equals(r2))
            {
                Console.WriteLine("r1 has the same values as r2");
            }
            else
            {
                Console.WriteLine("r1 has different values to r2");
            }

            if (r1 == r2)
            {
                Console.WriteLine("r1 is a reference to r2");
            }
            else
            {
                Console.WriteLine("r1 is not a reference to r2");
            }

            // **** Strucures ***
            MyStruct s1 = new MyStruct(10, 20);
            MyStruct s2 = new MyStruct(10, 20);

            //if (s1 == s2) does not compile
            if (s1.Equals(s2))
            {
                Console.WriteLine("s1 and s2 are the same!");
            }
            else
            {
                Console.WriteLine("s1 and s2 are not the same!");
            }
        }
Beispiel #7
0
        public void InstanceEquals_WithSameStructs_ReturnsTrue()
        {
            var s = new MyStruct();

            Assert.Equal(true, s.Equals(s));
        }