Ejemplo n.º 1
0
        public void Addr_OutOfRange_ThrowException()
        {
            // arrange
            var layout     = new Layout(new[] { 3, 4 }, 5, new[] { 4, 1 });
            var fastAccess = new FastAccess(layout);

            // action
            var _ = fastAccess.Addr(new[] { 10, 0 });
        }
Ejemplo n.º 2
0
        public void Addr_WithNegativeIndex_ThrowException()
        {
            // arrange
            var layout     = new Layout(new[] { 3, 4 }, 5, new[] { 4, 1 });
            var fastAccess = new FastAccess(layout);

            // action
            var _ = fastAccess.Addr(new[] { -1, 0 });
        }
Ejemplo n.º 3
0
        public void Addr_WithInvalidLength_ThrowException()
        {
            // arrange
            var layout     = new Layout(new[] { 3, 4 }, 0, new[] { 4, 1 });
            var fastAccess = new FastAccess(layout);

            // action
            var _ = fastAccess.Addr(new[] { 2 });
        }
Ejemplo n.º 4
0
        public void IsPosValid_WithInvalidPosWrongLength_ReturnFalse()
        {
            // arrange
            var layout     = new Layout(new[] { 3, 4 }, 0, new[] { 4, 1 });
            var fastAccess = new FastAccess(layout);

            // action
            var valid = fastAccess.IsPosValid(new[] { 1 });

            // assert
            Assert.IsFalse(valid);
        }
Ejemplo n.º 5
0
        public void IsPosValid_WithValidPos_ReturnTrue()
        {
            // arrange
            var layout     = new Layout(new[] { 3, 4 }, 0, new[] { 4, 1 });
            var fastAccess = new FastAccess(layout);

            // action
            var valid = fastAccess.IsPosValid(new[] { 1, 2 });

            // assert
            Assert.IsTrue(valid);
        }
Ejemplo n.º 6
0
        public void NumElements()
        {
            // arrange
            var layout     = new Layout(new[] { 3, 4 }, 0, new[] { 4, 1 });
            var fastAccess = new FastAccess(layout);

            // action
            var numElements = fastAccess.NumElements;

            // assert
            Assert.AreEqual(12, numElements);
        }
Ejemplo n.º 7
0
        public void Offset()
        {
            // arrange
            var layout     = new Layout(new[] { 3, 4 }, 9, new[] { 4, 1 });
            var fastAccess = new FastAccess(layout);

            // action
            var offset = fastAccess.Offset;

            // assert
            Assert.AreEqual(9, offset);
        }
Ejemplo n.º 8
0
        public void Addr_ValidIndex_ReturnOffset()
        {
            // arrange
            var layout     = new Layout(new[] { 3, 4 }, 5, new[] { 4, 1 });
            var fastAccess = new FastAccess(layout);

            // action
            var addr = fastAccess.Addr(new[] { 1, 1 });

            // Assert
            Assert.AreEqual(10, addr);
        }
Ejemplo n.º 9
0
        public void IsPosValid_WithNegativeIndex_ReturnFalse()
        {
            // arrange
            var layout     = new Layout(new[] { 3, 4 }, 0, new[] { 4, 1 });
            var fastAccess = new FastAccess(layout);

            // action
            var valid = fastAccess.IsPosValid(new[] { -1, 2 });

            // assert
            Assert.IsFalse(valid);
        }
Ejemplo n.º 10
0
        public static FastSetter <TClass, TValue> CreateSetter <TClass, TValue>(string name)
        {
            var propertyInfo = AccessTools.Property(typeof(TClass), name);
            var fieldInfo    = AccessTools.Field(typeof(TClass), name);

            if (propertyInfo == null && fieldInfo == null)
            {
                throw new Exception($"{typeof(TClass).Name} does not contain a field or property {name}");
            }
            bool   isProperty     = propertyInfo != null;
            Type   memberType     = isProperty ? propertyInfo.PropertyType : fieldInfo.FieldType;
            string memberTypeName = isProperty ? "property" : "field";

            if (!typeof(TValue).IsAssignableFrom(memberType))
            {
                throw new Exception($"Cannot cast property type {typeof(TValue).Name} as {memberType} for class {typeof(TClass).Name} {memberTypeName} {name}");
            }
            var handler = isProperty ?
                          FastAccess.CreateSetterHandler <TClass, TValue>(propertyInfo) :
                          FastAccess.CreateSetterHandler <TClass, TValue>(fieldInfo);

            return(new FastSetter <TClass, TValue>(handler));
        }
Ejemplo n.º 11
0
        public static FastGetter <TClass, TResult> CreateGetter <TClass, TResult>(string name)
        {
            var fieldInfo = AccessTools.Field(typeof(TClass), name);
            var propInfo  = AccessTools.Property(typeof(TClass), name);

            if (fieldInfo == null && propInfo == null)
            {
                throw new Exception($"{typeof(TClass).Name} does not contain field or property {name}");
            }
            bool   isProp         = propInfo != null;
            Type   memberType     = isProp ? propInfo.PropertyType : fieldInfo.FieldType;
            string memberTypeName = isProp ? "property" : "field";

            if (!typeof(TResult).IsAssignableFrom(memberType))
            {
                throw new InvalidCastException($"Cannot cast field type {typeof(TResult).Name} as {memberType} for class {typeof(TClass).Name} {memberTypeName} {name}");
            }
            var handler = isProp ?
                          FastAccess.CreateGetterHandler <TClass, TResult>(propInfo) :
                          FastAccess.CreateGetterHandler <TClass, TResult>(fieldInfo);

            return(new FastGetter <TClass, TResult>(handler));
        }
Ejemplo n.º 12
0
 public static FastSetter CreateSetterProperty(this Type type, string name)
 {
     return(new FastSetter(FastAccess.CreateSetterHandler(AccessTools.Property(type, name))));
 }
Ejemplo n.º 13
0
 public static FastGetter CreateGetter(this Type type, string name)
 {
     return(new FastGetter(FastAccess.CreateGetterHandler(AccessTools.Field(type, name))));
 }
Ejemplo n.º 14
0
 public DataAndLayout(T[] data, FastAccess fastAccess)
 {
     Data       = data;
     FastAccess = fastAccess;
 }