Example #1
0
        private static void TestTuple(Xtensive.Tuples.Tuple tuple)
        {
            Assert.IsFalse(tuple.GetFieldState(0).IsAvailable());

            try {
                tuple.GetFieldState(0).IsNull();
                throw new AssertionException("Value is not available. No one knows if it is null or not.");
            } catch (InvalidOperationException) {
            }

            tuple.SetValue(0, 1);
            Assert.IsTrue(tuple.GetFieldState(0).IsAvailable());
            Assert.IsFalse(tuple.GetFieldState(0).IsNull());
            Assert.IsTrue(tuple.GetFieldState(0).HasValue());
            Assert.AreEqual(1, tuple.GetValue(0));
            Assert.AreEqual(1, tuple.GetValue <int>(0));
            Assert.AreEqual(new int?(1), tuple.GetValue <int?>(0));

            tuple.SetValue(0, null);
            Assert.IsTrue(tuple.GetFieldState(0).IsAvailable());
            Assert.IsTrue(tuple.GetFieldState(0).IsNull());
            Assert.IsFalse(tuple.GetFieldState(0).HasValue());
            Assert.AreEqual(null, tuple.GetValue(0));
            Assert.AreEqual(null, tuple.GetValue <int?>(0));

            tuple.SetValue <int?>(0, null);
            Assert.IsTrue(tuple.GetFieldState(0).IsAvailable());
            Assert.IsTrue(tuple.GetFieldState(0).IsNull());
            Assert.IsFalse(tuple.GetFieldState(0).HasValue());
            Assert.AreEqual(null, tuple.GetValue(0));
            Assert.AreEqual(null, tuple.GetValue <int?>(0));
            Assert.AreEqual(null, tuple.GetValueOrDefault(0));
            Assert.AreEqual(null, tuple.GetValueOrDefault <int?>(0));

            try {
                tuple.GetValue(1);
                throw new AssertionException("Value should not be available.");
            } catch (InvalidOperationException) {
            }

            try {
                tuple.GetValue <string>(2);
                throw new AssertionException("Value should not be available.");
            } catch (InvalidOperationException) {
            }

            try {
                tuple.GetValue <byte>(0);
                throw new AssertionException("Null reference or Invalid cast exception should be thrown.");
            }
            catch (NullReferenceException) {}
            catch (InvalidCastException) {}

            Assert.IsTrue(tuple.Equals(tuple));
        }
        public void FieldAccessTest()
        {
            const int       iterationCount = 10000000;
            TupleDescriptor descriptor     = TupleDescriptor.Create(shortFieldTypes);
            Tuple           tuple          = Tuple.Create(descriptor);

            for (int i = 0; i < iterationCount; i++)
            {
                tuple.SetValue(0, (object)i);
            }
            for (int i = 0; i < iterationCount; i++)
            {
                TupleFieldState state;
                tuple.GetValue(0, out state);
            }

            for (int i = 0; i < iterationCount; i++)
            {
                tuple.SetValue(0, i);
            }
            for (int i = 0; i < iterationCount; i++)
            {
                tuple.GetValue <int>(0);
            }
            for (int i = 0; i < iterationCount; i++)
            {
                TupleFieldState state;
                tuple.GetValue <int>(0, out state);
            }
            for (int i = 0; i < iterationCount; i++)
            {
                TupleFieldState state;
                tuple.GetValue <int?>(0, out state);
            }

            using (new Measurement("Tuple.SetValue?", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    tuple.SetValue(0, null);
                }
            using (new Measurement("Tuple.SetValue", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    tuple.SetValue(0, (object)i);
                }
            using (new Measurement("Tuple.GetValue", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    tuple.GetValue(0);
                }
            using (new Measurement("Tuple.GetValue(_,_)", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    TupleFieldState state;
                    tuple.GetValue(0, out state);
                }
            using (new Measurement("Tuple.GetValueOrDefault", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    tuple.GetValueOrDefault(0);
                }

            using (new Measurement("Tuple.SetValue<T>", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    tuple.SetValue(0, i);
                }
            using (new Measurement("Tuple.GetValue<T>", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    tuple.GetValue <int>(0);
                }
            using (new Measurement("Tuple.GetValue<T>(_,_)", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    TupleFieldState state;
                    tuple.GetValue <int>(0, out state);
                }
            using (new Measurement("Tuple.GetValueOrDefault<T>", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    tuple.GetValueOrDefault <int>(0);
                }

            using (new Measurement("Tuple.SetValue<T?>", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    tuple.SetValue(0, new int?(i));
                }
            using (new Measurement("Tuple.GetValue<T?>", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    tuple.GetValue <int?>(0);
                }
            using (new Measurement("Tuple.GetValue<T?>(_,_)", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    TupleFieldState state;
                    tuple.GetValue <int?>(0, out state);
                }
            using (new Measurement("Tuple.GetValueOrDefault<T?>", iterationCount))
                for (int i = 0; i < iterationCount; i++)
                {
                    tuple.GetValueOrDefault <int?>(0);
                }
        }