Ejemplo n.º 1
0
 public void CompatibleCopyTest()
 {
     var a = new AClass() { Property = "P" };
     var b = new BClass();
     EntityTools<AClass, BClass>.CopyTo(a, b);
     Assert.AreEqual("P", b.Prop);
 }
Ejemplo n.º 2
0
        void AddItemsAndTime()
        {
            var array = new AClass[100000];
            for (int x = 0; x < array.Length; x++)
                array[x] = new AClass();

            m_objects.Add(array);
            GC.Collect();
            GC.WaitForPendingFinalizers();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Stopwatch sw = new Stopwatch();
            var swap = m_objects[0][0];
            m_objects[0][0] = m_objects[0][1];
            m_objects[0][1] = swap;
            m_objects[0][m_objects.Count] = null;

            sw.Start();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            sw.Stop();
            long memorySize = Process.GetCurrentProcess().VirtualMemorySize64;//GC.GetTotalMemory(false);
            Console.WriteLine("{0}00k items: {1}ms  {2}", m_objects.Count.ToString(), sw.Elapsed.TotalMilliseconds.ToString("0.00"),(memorySize/1024.0/1024.0).ToString("0.0MB"));
        }
Ejemplo n.º 3
0
 static void Main(string[] args)
 {
     AClass a;
     a = null;
     AClass b = new AClass();
     var hashCode = b.GetHashCode();
     var equal = b.Equals(null);
     //Default funciton generated GetHashCode
     //Equals, ToString(),GetType()
     bool ret = b == a;
 }
Ejemplo n.º 4
0
 public void IgnoreCaseTest()
 {
     var a = new AClass() { A = "AValue" };
     Assert.AreEqual("AValue", EntityTools<AClass>.GetValue(a, "A", false));
     Assert.AreEqual(null, EntityTools<AClass>.GetValue(a, "a", false));
     var b = new BClass();
     EntityTools<AClass, BClass>.CopyTo(a, b);
     Assert.AreEqual(null, EntityTools<BClass>.GetValue(b, "a", false));
     EntityTools<AClass, BClass>.CopyTo(a, b, true);
     Assert.AreEqual("AValue", EntityTools<BClass>.GetValue(b, "a", false));
 }
Ejemplo n.º 5
0
        public void HashCode_2ObjectsWithNulls_AreEqual()
        {
            // Arrange
            var obj1 = new AClass(null, 0);
            var obj2 = new AClass(null, 0);

            // Act
            var hash1 = obj1.GetHashCode();
            var hash2 = obj2.GetHashCode();

            // Assert
            Assert.Equal(hash1, hash2);
        }
Ejemplo n.º 6
0
        public void HashCode_1ObjectWith1Not_AreNotEqual()
        {
            // Arrange
            var obj1 = new AClass(null, 0);
            var obj2 = new AClass("foo", 3);

            // Act
            var hash1 = obj1.GetHashCode();
            var hash2 = obj2.GetHashCode();

            // Assert
            Assert.NotEqual(hash1, hash2);
        }
Ejemplo n.º 7
0
 public void Setup()
 {
     MockA = new Mock <IA>();
     MockA.Setup(a => a.A()).Returns(1);
     MockB = new Mock <IB>();
     MockB.Setup(b => b.B()).Returns(1);
     MockC = new Mock <IC>();
     MockC.Setup(c => c.C()).Returns(1);
     target = new AClass();
     target.SetPrivateProperty <AClass, IA>("A", MockA.Object);
     target.SetPrivateProperty <AClass, IB>("B", MockB.Object);
     target.SetPrivateProperty <AClass, IC>("C", MockC.Object);
 }
Ejemplo n.º 8
0
        public void HashCode_ObjectsDiffOrder_AreNotEqual()
        {
            // Arrange
            var obj1 = new AClass("foo", 3);
            var obj2 = new BClass(3, "foo");

            // Act
            var hash1 = obj1.GetHashCode();
            var hash2 = obj2.GetHashCode();

            // Assert
            Assert.NotEqual(hash1, hash2);
        }
    public static void main()
    {
        AClass instanceOne = new AClass(6);

        StaticFunctions.FunctionTakingAReference(instanceOne);
        Console.WriteLine(instanceOne.Integer);

        AClass instanceTwo = new AClass(1234);      // C# forces me to assign this before

        // it can be passed. Use "out" instead of
        // "ref" and that requirement goes away.
        StaticFunctions.FunctionTakingAReferenceToAReference(ref instanceTwo);
        Console.WriteLine(instanceTwo.Integer);
    }
Ejemplo n.º 10
0
        public void GetPropertyT_ClassInstance_ValueProperty_Convert()
        {
            var pi     = typeof(AClass).GetProperty(nameof(AClass.AValProperty));
            var getter = pi.DelegateForGetProperty <Int16>();

            var inst = new AClass()
            {
                AValProperty = 66
            };

            var inst2 = getter(inst);

            Assert.Equal(inst.AValProperty, (int)inst2);
        }
Ejemplo n.º 11
0
        public void GetPropertyT_ClassInstance_ClassProperty()
        {
            var pi     = typeof(AClass).GetProperty(nameof(AClass.AClassProperty));
            var getter = pi.DelegateForGetProperty <object>();

            var inst = new AClass()
            {
                AClassProperty = "howdy"
            };

            var inst2 = getter(inst);

            Assert.Equal(inst.AClassProperty, inst2);
        }
Ejemplo n.º 12
0
        public void SetProperty_ClassInstance_StructProperty()
        {
            var pi     = typeof(AClass).GetProperty(nameof(AClass.AStructVal));
            var setter = pi.DelegateForSetProperty();

            var inst = new AClass();

            var setVal = new StructVal {
                IntVal = 55, ObjVal = "yo"
            };

            var inst2 = setter(inst, setVal);

            Assert.Equal(setVal, inst.AStructVal);
        }
        public void SetAsync_HandlesException()
        {
            // Arrange
            var key     = Guid.NewGuid().ToString();
            var value   = new AClass();
            var options = new CacheEntryOptions(TimeSpan.FromMilliseconds(1));
            var cache   = Substitute.For <ICache>();

            cache.SetAsync(key, value, options).Throws <Exception>();
            var logger    = Substitute.For <ILogger <ExceptionHandlingCacheDecorator> >();
            var decorator = new ExceptionHandlingCacheDecorator(cache, logger);

            // Act / Assert
            Assert.DoesNotThrowAsync(() => decorator.SetAsync(key, value, options));
        }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            var pi     = typeof(AClass).GetProperty(nameof(AClass.AValProperty));
            var setter = pi.DelegateForSetProperty();

            var methodofSetter = setter.Method;

            var aMethDel         = typeof(AClass).GetMethod(nameof(AClass.AMethod)).DelegateForMethod();
            var methodOfAMethDel = aMethDel.Method;

            var inst = new AClass();


            //var t = new MethodCallExpression()
        }
Ejemplo n.º 15
0
        public void GetPropertyT_ClassInstance_StructProperty()
        {
            var pi     = typeof(AClass).GetProperty(nameof(AClass.AStructVal));
            var getter = pi.DelegateForGetProperty <StructVal>();

            var inst = new AClass()
            {
                AStructVal = new StructVal {
                    IntVal = 5
                }
            };

            var inst2 = getter(inst);

            Assert.Equal(inst.AStructVal, inst2);
        }
Ejemplo n.º 16
0
        private void TestClass()
        {
            Watch.Restart();
            for (var i = 0; i < 10; i++)
            {
                var list = new AClass[OneMillion];
                for (var j = 0; j < OneMillion; j++)
                {
                    list[i] = new AClass(i);
                }
            }
            Watch.Stop();

            _results.Add(new BenchWarmerResult {
                Name = "Class", ElapsedMiliseconds = Watch.ElapsedMilliseconds / 10
            });
        }
Ejemplo n.º 17
0
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        List <AClass> beforeList = new List <AClass>();

        var beforeA = new AClass();

        beforeList.Add(beforeA);

        var str = JsonConvert.SerializeObject(beforeList);

        List <AClass> afterList = JsonConvert.DeserializeObject <List <AClass> >(str);

        var afterA = afterList.FirstOrDefault();

        Console.ReadKey();
    }
        public void ForAFuncOf()
        {
            var myObject = new AClass();
            var myString = "String!";

            //
            var uut     = new AnythingActivator(DefaultRules.All.After(new ActivateInstances(myObject, myString)));
            var result1 = uut.New <Func <AClass> >();
            var result2 = uut.New <Func <string> >();

            //Debug
            xconsole.WriteLine(string.Join(Environment.NewLine, uut.LastErrorList));
            xconsole.WriteLine(string.Join(Environment.NewLine, uut.LastActivationTree));

            //
            Assert.That(result1, x => x() == myObject);
            Assert.That(result2, x => x() == myString);
        }
Ejemplo n.º 19
0
        public void InstanceCounter_TestUsage()
        {
            Assert.AreEqual(0, TypeCreatedCounter <AClass> .Count);
            Assert.AreEqual(0, ActiveInstanceCounter <AClass> .GlobalActiveCount);

            using (var i1 = new AClass(this.Logger))
            {
                Assert.AreEqual(1, TypeCreatedCounter <AClass> .Count);
                Assert.AreEqual(1, ActiveInstanceCounter <AClass> .GlobalActiveCount);

                Assert.AreEqual(i1.InstanceId, 0);

                using (var i2 = new AClass(this.Logger))
                    using (var i3 = new AClass(this.Logger))
                    {
                        Assert.AreEqual(1, i2.InstanceId);

                        Assert.AreEqual(3, TypeCreatedCounter <AClass> .Count);
                        Assert.AreEqual(3, ActiveInstanceCounter <AClass> .GlobalActiveCount);

                        using (var tracker = new ActiveInstanceCounter <AClass>(this.Logger))
                        {
                            Assert.AreEqual(4, ActiveInstanceCounter <AClass> .GlobalActiveCount);
                            Assert.AreEqual(4, TypeCreatedCounter <AClass> .Count);
                            Assert.AreEqual(4, tracker.ActiveCount);
                        }

                        Assert.AreEqual(3, ActiveInstanceCounter <AClass> .GlobalActiveCount);
                        Assert.AreEqual(4, TypeCreatedCounter <AClass> .Count);
                    }

                Assert.AreEqual(4, TypeCreatedCounter <AClass> .Count);
                Assert.AreEqual(1, ActiveInstanceCounter <AClass> .GlobalActiveCount);
            }

            Assert.AreEqual(4, TypeCreatedCounter <AClass> .Count);
            Assert.AreEqual(0, ActiveInstanceCounter <AClass> .GlobalActiveCount);

            var c = new TypeCreatedCounter <AClass>();

            Assert.AreEqual(5, TypeCreatedCounter <AClass> .Count);
            Assert.AreEqual(TypeCreatedCounter <AClass> .Count, c.CreatedCount);
        }
        public void GetOrCreate_HandlesGetException()
        {
            // Arrange
            var key          = Guid.NewGuid().ToString();
            var factoryValue = new AClass();
            var cache        = Substitute.For <ICache>();

            cache.Get(key, typeof(AClass)).Throws(new Exception());
            var logger    = Substitute.For <ILogger <ExceptionHandlingCacheDecorator> >();
            var decorator = new ExceptionHandlingCacheDecorator(cache, logger);

            // Act
            var result = decorator.GetOrCreate(key,
                                               typeof(AClass),
                                               new CacheEntryOptions(new TimeSpan()),
                                               _ => factoryValue);

            // Assert
            Assert.AreEqual(factoryValue, result);
        }
        public async Task GetOrCreateAsync_HandlesGetException()
        {
            // Arrange
            var key          = Guid.NewGuid().ToString();
            var factoryValue = new AClass();
            Func <string, Task <AClass> > factory = _ => Task.FromResult(factoryValue);
            var options = new CacheEntryOptions(TimeSpan.FromMilliseconds(1));
            var cache   = Substitute.For <ICache>();

            cache.GetAsync <AClass>(key).Throws(new Exception());
            cache.GetOrCreateAsync(key, options, Arg.Any <Func <string, Task <AClass> > >()).Throws(new Exception());
            var logger    = Substitute.For <ILogger <ExceptionHandlingCacheDecorator> >();
            var decorator = new ExceptionHandlingCacheDecorator(cache, logger);

            // Act
            var result = await decorator.GetOrCreateAsync(key, options, factory);

            // Assert
            Assert.AreEqual(factoryValue, result);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Creates a new Player based on a <see cref="DbCharacter"/> stored in database.
        /// </summary>
        /// <param name="parentClient">Parent client instance</param>
        /// <param name="dbCharacter">Character stored in database</param>
        public Player(WorldClient parentClient, DbCharacter dbCharacter)
            : base(dbCharacter?.Gender == 0 ? 11 : 12)
        {
            this.Client    = parentClient;
            this.Chat      = new Chat(this);
            this.Inventory = new Inventory(this, dbCharacter.Items);
            this.Class     = AClass.Create(dbCharacter.ClassId);

            this.Id        = dbCharacter.Id;
            this.AccountId = dbCharacter.AccountId;
            this.Name      = dbCharacter.Name;
            this.Gender    = dbCharacter.Gender;
            this.ClassId   = dbCharacter.ClassId;
            this.Gold      = dbCharacter.Gold;
            this.Slot      = dbCharacter.Slot;
            this.Level     = dbCharacter.Level;
            this.Authority = this.Client.CurrentUser.Authority;
            this.Attributes[DefineAttributes.STR] = dbCharacter.Strength;
            this.Attributes[DefineAttributes.STA] = dbCharacter.Stamina;
            this.Attributes[DefineAttributes.DEX] = dbCharacter.Dexterity;
            this.Attributes[DefineAttributes.INT] = dbCharacter.Intelligence;
            this.Attributes[DefineAttributes.HP]  = dbCharacter.Hp;
            this.Attributes[DefineAttributes.MP]  = dbCharacter.Mp;
            this.Attributes[DefineAttributes.FP]  = dbCharacter.Fp;
            this.Experience          = dbCharacter.Experience;
            this.SkinSetId           = dbCharacter.SkinSetId;
            this.HairId              = dbCharacter.HairId;
            this.HairColor           = dbCharacter.HairColor;
            this.FaceId              = dbCharacter.FaceId;
            this.BankCode            = dbCharacter.BankCode;
            this.MapId               = dbCharacter.MapId;
            this.Position            = new Vector3(dbCharacter.PosX, dbCharacter.PosY, dbCharacter.PosZ);
            this.Angle               = dbCharacter.Angle;
            this.DestinationPosition = this.Position.Clone();
            this.IsFlying            = this.Inventory.HasFlyingObjectEquiped();

            this.StatPoints  = dbCharacter.StatPoints;
            this.SkillPoints = dbCharacter.SkillPoints;

            // Initialize quests, guild, friends, skills etc...
        }
Ejemplo n.º 23
0
        public async Task GetAsync_ReturnsCachedValue()
        {
            // Arrange

            var key         = Guid.NewGuid().ToString();
            var cachedValue = new AClass();

            var cache = new FakeDistributedCache(new MemoryCache(new MemoryCacheOptions()));

            cache.SetString(key, JsonConvert.SerializeObject(cachedValue));

            var logger = Substitute.For <ILogger <DistributedCache> >();
            var sut    = new DistributedCache(cache, logger);


            // Act
            var result = await sut.GetAsync <AClass>(key);

            // Assert
            Assert.AreEqual(cachedValue.Value, result.Value);
        }
        public void GetOrCreate_HandlesSetException()
        {
            // Arrange
            var key          = Guid.NewGuid().ToString();
            var factoryValue = new AClass();
            var options      = new CacheEntryOptions(TimeSpan.FromMilliseconds(1));
            var cache        = Substitute.For <ICache>();

            cache.When(c => c.Set(key, factoryValue, options)).Do(_ => throw new Exception());
            var logger    = Substitute.For <ILogger <ExceptionHandlingCacheDecorator> >();
            var decorator = new ExceptionHandlingCacheDecorator(cache, logger);

            // Act
            var result = decorator.GetOrCreate(key,
                                               typeof(AClass),
                                               options,
                                               _ => factoryValue);

            // Assert
            Assert.AreEqual(factoryValue, result);
        }
        public void GetOrCreate_ReturnsCachedValue()
        {
            // Arrange
            var key         = Guid.NewGuid().ToString();
            var cachedValue = new AClass();
            var options     = new CacheEntryOptions(TimeSpan.FromMilliseconds(1));
            var cache       = Substitute.For <ICache>();

            cache.Get(key, typeof(AClass)).Returns(cachedValue);
            var logger    = Substitute.For <ILogger <ExceptionHandlingCacheDecorator> >();
            var decorator = new ExceptionHandlingCacheDecorator(cache, logger);

            // Act
            var result = decorator.GetOrCreate(key,
                                               typeof(AClass),
                                               options,
                                               _ => null);

            // Assert
            Assert.AreEqual(cachedValue, result);
        }
        public void GetIfCached_ReturnsCachedValue()
        {
            // Arrange
            var key         = Guid.NewGuid().ToString();
            var cachedValue = new AClass();
            var cache       = Substitute.For <ICache>();

            cache.GetIfCached(key, out Arg.Any <AClass>()).Returns(x =>
            {
                x[1] = cachedValue;
                return(true);
            });
            var logger    = Substitute.For <ILogger <ExceptionHandlingCacheDecorator> >();
            var decorator = new ExceptionHandlingCacheDecorator(cache, logger);

            // Act
            var result = decorator.GetIfCached <AClass>(key, out var value);

            // Assert
            Assert.IsTrue(result);
            Assert.AreEqual(cachedValue, value);
        }
Ejemplo n.º 27
0
        public void GetIfCached_ReturnsCachedValue()
        {
            // Arrange

            var key         = Guid.NewGuid().ToString();
            var cachedValue = new AClass();

            var cache = new FakeDistributedCache(new MemoryCache(new MemoryCacheOptions()));

            cache.SetString(key, JsonConvert.SerializeObject(cachedValue));

            var logger = Substitute.For <ILogger <DistributedCache> >();
            var sut    = new DistributedCache(cache, logger);


            // Act
            var wasCached = sut.GetIfCached <AClass>(key, out var result);

            // Assert
            Assert.True(wasCached);
            Assert.AreEqual(cachedValue.Value, result.Value);
        }
Ejemplo n.º 28
0
        public async Task GetOrCreateAsync_ReturnsCachedValue()
        {
            // Arrange

            var key         = Guid.NewGuid().ToString();
            var cachedValue = new AClass();
            Func <string, Task <AClass> > factory = _ => Task.FromResult(new AClass());
            var options = new CacheEntryOptions(TimeSpan.FromMilliseconds(1));

            var cache = new FakeDistributedCache(new MemoryCache(new MemoryCacheOptions()));

            cache.SetString(key, JsonConvert.SerializeObject(cachedValue));

            var logger = Substitute.For <ILogger <DistributedCache> >();
            var sut    = new DistributedCache(cache, logger);


            // Act
            var result = await sut.GetOrCreateAsync(key, options, factory);

            // Assert
            Assert.AreEqual(cachedValue.Value, result.Value);
        }
Ejemplo n.º 29
0
        public void GetOrCreate_ReturnsFactoryValue()
        {
            // Arrange

            var key          = Guid.NewGuid().ToString();
            var factoryValue = new AClass();
            Func <string, AClass> factory = _ => factoryValue;
            var options = new CacheEntryOptions(TimeSpan.FromMilliseconds(1));

            var cache = Substitute.For <IDistributedCache>();

            cache.Get(key).Returns((byte[])null);

            var logger = Substitute.For <ILogger <DistributedCache> >();
            var sut    = new DistributedCache(cache, logger);


            // Act
            var result = (AClass)sut.GetOrCreate(key, typeof(AClass), options, factory);

            // Assert
            Assert.AreEqual(factoryValue.Value, result.Value);
        }
Ejemplo n.º 30
0
        public async Task GetOrCreateAsync_ReturnsFactoryValue()
        {
            // Arrange

            var key          = Guid.NewGuid().ToString();
            var factoryValue = new AClass();
            Func <string, Task <AClass> > factory = _ => Task.FromResult(factoryValue);
            var options = new CacheEntryOptions(TimeSpan.FromMilliseconds(1));

            var cache = Substitute.For <IDistributedCache>();

            cache.GetAsync(key, Arg.Any <CancellationToken>()).Returns((byte[])null);

            var logger = Substitute.For <ILogger <DistributedCache> >();
            var sut    = new DistributedCache(cache, logger);


            // Act
            var result = await sut.GetOrCreateAsync(key, options, factory);

            // Assert
            Assert.AreEqual(factoryValue.Value, result.Value);
        }
Ejemplo n.º 31
0
        public ActionResult Index()
        {
            AClass model = new AClass
            {
                Id    = 1001,
                AName = "Murat",
                ListB = new List <BClass>
                {
                    new BClass
                    {
                        Id   = 101,
                        Name = "Ahmet"
                    },
                    new BClass
                    {
                        Id   = 102,
                        Name = "Merve"
                    }
                }
            };

            return(View(model));
        }
Ejemplo n.º 32
0
        static void Main(string[] args)
        {
            var a1 = new AClass(1);
            var a2 = new AClass(2)
            {
                x = 3, y = 4, z = 5
            };
            var a3 = new AClass()
            {
                x = 6, y = 7, z = 8
            };
            var z = 'X';

            Console.WriteLine($"x = {a1.x}, y = {a1.y}, z = {a1.z}");
            Console.WriteLine($"x = {a2.x}, y = {a2.y}, z = {a2.z}");
            Console.WriteLine($"x = {a3.x}, y = {a3.y}, z = {a3.z}");

            //Anonymous_Type
            var anonymous = new { x = "good", a3.y, z };

            Console.WriteLine($"x = {anonymous.x}, y = {anonymous.y}, z = {anonymous.z}");
            Console.ReadKey();
        }
Ejemplo n.º 33
0
        void AddItemsAndTime()
        {
            var array = new AClass[100000];

            for (int x = 0; x < array.Length; x++)
            {
                array[x] = new AClass();
            }

            m_objects.Add(array);
            GC.Collect();
            GC.WaitForPendingFinalizers();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Stopwatch sw   = new Stopwatch();
            var       swap = m_objects[0][0];

            m_objects[0][0] = m_objects[0][1];
            m_objects[0][1] = swap;
            m_objects[0][m_objects.Count] = null;

            sw.Start();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            sw.Stop();
            long memorySize = Process.GetCurrentProcess().VirtualMemorySize64;//GC.GetTotalMemory(false);

            Console.WriteLine("{0}00k items: {1}ms  {2}", m_objects.Count.ToString(), sw.Elapsed.TotalMilliseconds.ToString("0.00"), (memorySize / 1024.0 / 1024.0).ToString("0.0MB"));
        }
Ejemplo n.º 34
0
        public static void GenericMethodTest3()
        {
            var tclass = new TClass();

            var a = new AClass()
            {
                MSG = "AClass"
            };

            Console.WriteLine("Simple Type");
            tclass.Test(a, param => {
                //Console.WriteLine($"param: {LitJson.JsonMapper.ToJson(param)}");
            });

            var ga = new GenericClass <AClass>()
            {
                t = a
            };

            Console.WriteLine("Complex Type");
            tclass.Test(ga, param => {
                //Console.WriteLine($"param: {LitJson.JsonMapper.ToJson(param)}");
            });
        }
Ejemplo n.º 35
0
 public void HandleAClass(AClass obj)
 {
 }
Ejemplo n.º 36
0
 public MyClass()
 {
     sclass = new MySubClass();
     aclassobj = new AClass();
 }
        public void If_there_is_only_one_instance_of_a_type_use_that_as_default()
        {
            var target = new AClass("Me");

            var container = new Container(registry => registry.InstanceOf<AClass>().Is.Object(target));

            Assert.AreSame(target, container.GetInstance<AClass>());
        }