Ejemplo n.º 1
0
        /// <summary>
        /// Create scanner
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <param name="portProbe"></param>
        /// <param name="maxProbeCount"></param>
        /// <param name="minProbePercent"></param>
        /// <param name="timeout"></param>
        /// <param name="ct"></param>
        public PortScanner(ILogger logger, IEnumerable <IPEndPoint> source,
                           Action <IPEndPoint> target, IPortProbe portProbe, int?maxProbeCount,
                           int?minProbePercent, TimeSpan?timeout, CancellationToken ct)
        {
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
            _source = source?.GetEnumerator() ??
                      throw new ArgumentNullException(nameof(source));
            _target = target ?? throw new ArgumentNullException(nameof(target));

            _maxProbeCount = maxProbeCount ?? kDefaultMaxProbeCount;
            _minProbeCount = (int)(_maxProbeCount *
                                   ((minProbePercent ?? kDefaultMinProbePercent) / 100.0));
            _timeout   = timeout ?? kDefaultProbeTimeout;
            _portProbe = portProbe ?? new NullPortProbe();
            _requeued  = new ConcurrentQueue <IPEndPoint>();
            _rand      = new Random();

            _probePool = LinqEx
                         .Repeat(i => new ConnectProbe(this, i), _maxProbeCount)
                         .ToList();

            _cts = new CancellationTokenSource();
            ct.Register(_cts.Cancel);
            _completion = new TaskCompletionSource <bool>();
            _active     = _maxProbeCount;
            foreach (var probe in _probePool)
            {
                probe.Start();
            }
        }
Ejemplo n.º 2
0
        public void AllTest()
        {
            var arr = new[] { 0, 1, 2, 3, 4 };

            Assert.AreEqual(arr.All(x => x > -1), LinqEx.All(arr, x => x > -1));
            Assert.AreEqual(arr.All(x => x < -1), LinqEx.All(arr, x => x < -1));
        }
Ejemplo n.º 3
0
        public void CombineMemberInitTestMultiParam()
        {
            Expression <Func <Client, int, ClientDTO> > a = (x, y) => new ClientDTO
            {
                Name = "Hello " + x.Name + " " + y,
                Age  = x.Age
            };

            Expression <Func <Client, int, ClientDTO> > b = (x, y) => new ClientDTO
            {
                LegalDrinking = true,
                Age           = x.Age + y
            };

            var comb = LinqEx.CombineMemberInitExpression(a, b);
            var res  = comb.Compile()(new Client
            {
                Name = "Rafael",
                Age  = 20,
            }, 3);

            Assert.AreEqual("Hello Rafael 3", res.Name);
            Assert.AreEqual(23, res.Age);
            Assert.AreEqual(true, res.LegalDrinking);
        }
Ejemplo n.º 4
0
        public void CombineMemberInitTest()
        {
            Expression <Func <Client, ClientDTO> > a = x => new ClientDTO
            {
                Name = "Hello " + x.Name,
                Age  = x.Age
            };

            Expression <Func <Client, ClientDTO> > b = x => new ClientDTO
            {
                LegalDrinking = true,
                Age           = x.Age + 1
            };

            var comb = LinqEx.CombineMemberInitExpression(a, b);
            var res  = comb.Compile()(new Client
            {
                Name = "Rafael",
                Age  = 20,
            });

            Assert.AreEqual("Hello Rafael", res.Name);
            Assert.AreEqual(21, res.Age);
            Assert.AreEqual(true, res.LegalDrinking);
        }
Ejemplo n.º 5
0
        public void ComplexTypePopulateSimpleTest()
        {
            var Source = new Client
            {
                Name      = "Rafael",
                Age       = 22,
                NotSimple = new Client {
                    Name = "Jose", Age = 17
                },
                Address = new Address
                {
                    City   = "Obregon",
                    Street = "E Baca Calderon"
                }
            };

            var Dest = new ClientDTO();

            Assert.IsNull(Dest.Address);
            LinqEx.PopulateObjectSimple(Source, Dest);

            Assert.AreEqual(Dest.Name, Source.Name);
            Assert.AreEqual(Dest.Age, Source.Age);
            Assert.IsNull(Dest.NotSimple);

            Assert.IsNotNull(Dest.Address);

            //Deep cloning should result in different instances
            Assert.AreNotEqual(Source.Address, Dest.Address);
            Assert.AreEqual(Source.Address.City, Dest.Address.City);
            Assert.AreEqual(Source.Address.Street, Dest.Address.Street);
        }
Ejemplo n.º 6
0
        public void PopulateObject()
        {
            var A = new Client {
                Age = 21, Name = "Rafael", InternalData = "Hello", NotSimple = null
            };
            var B = new ClientDTO();

            LinqEx.PopulateObject(A, B);
        }
Ejemplo n.º 7
0
        public void PopulateObjectTest()
        {
            var Source = new Client {
                Name = "Rafael", Age = 22
            };
            var Dest = new ClientDTO();

            LinqEx.PopulateObject(Source, Dest, x => true);

            Assert.AreEqual(false, Dest.LegalDrinking);
        }
Ejemplo n.º 8
0
        public void MapTypeTest()
        {
            var Map = LinqEx.MapTypes(typeof(Client), typeof(ClientDTO)).ToDictionary(x => x.Dest.Name);

            Assert.AreEqual(2, Map.Count());

            Assert.AreEqual(typeof(Client).GetProperty("Name"), Map["Name"].Source);
            Assert.AreEqual(typeof(Client).GetProperty("Age"), Map["Age"].Source);

            Assert.AreEqual(typeof(ClientDTO).GetProperty("Name"), Map["Name"].Dest);
            Assert.AreEqual(typeof(ClientDTO).GetProperty("Age"), Map["Age"].Dest);
        }
Ejemplo n.º 9
0
        public void ExpressionBindingTest()
        {
            Expression <Func <Client, ClientDTO> > lambda = x => new ClientDTO
            {
                Age           = x.Age + 20,
                LegalDrinking = x.Name == "Rafa"
            };

            var Extract = LinqEx.ExtractBindings(lambda);

            Assert.AreEqual("(x.Age + 20)", Extract["Age"].Expression.ToString());
            Assert.AreEqual("(x.Name == \"Rafa\")", Extract["LegalDrinking"].Expression.ToString());
        }
Ejemplo n.º 10
0
        public void MemberInitTestImmediate()
        {
            var Clients = new Client[]
            {
                new Client {
                    Name = "Rafael", Age = 22
                },
                new Client {
                    Name = "Jose", Age = 17
                },
            }.AsQueryable();

            //Syntax test:
            Clients.Select(LinqEx.Clone <Client, ClientDTO>(x => new ClientDTO {
                Age = 20
            }));

            Clients.SelectClone(x => new ClientDTO {
                Age = 20
            });

            var Ret = LinqEx.Clone <Client, ClientDTO>().Compile().Invoke(Clients.First());

            Assert.AreEqual(22, Ret.Age);
            Assert.AreEqual("Rafael", Ret.Name);
            Assert.AreEqual(false, Ret.LegalDrinking);

            Ret = LinqEx.Clone <Client, ClientDTO>(x => new ClientDTO {
                LegalDrinking = x.Age > 18
            }).Compile().Invoke(Clients.First());

            Assert.AreEqual(22, Ret.Age);
            Assert.AreEqual("Rafael", Ret.Name);
            Assert.AreEqual(true, Ret.LegalDrinking);

            var Expr = LinqEx.Clone <Client, ClientDTO>(x => new ClientDTO {
                LegalDrinking = x.Age > 18, Name = "Luis"
            });

            Ret = Expr.Compile().Invoke(Clients.First());

            Assert.AreEqual(22, Ret.Age);
            Assert.AreEqual("Luis", Ret.Name);
            Assert.AreEqual(true, Ret.LegalDrinking);
        }
Ejemplo n.º 11
0
        public void TestSetImmut()
        {
            var val    = new ImmutClass("Rafa", 20, 1);
            var newVal = LinqEx.SetImmutable(val, x => x.Edad, 23);

            //El valor original permanece igual
            Assert.AreEqual(20, val.Edad);

            //El nuevo elemento es una nueva referencia
            Assert.AreNotEqual(val, newVal);

            //Verificar propiedades
            Assert.AreEqual("Rafa", newVal.Nombre);
            Assert.AreEqual(1, newVal.Id);

            //Cambio la edad
            Assert.AreEqual(23, newVal.Edad);
        }
Ejemplo n.º 12
0
        public void SkipTest()
        {
            var arr = new[] { 0, 1, 2, 3, 4 };

            Assert.AreEqual(arr.Skip(1).ToArray(), LinqEx.Skip(arr, 1));
        }
Ejemplo n.º 13
0
        public void FirstTest()
        {
            var arr = new[] { 0, 1, 2, 3, 4 };

            Assert.AreEqual(arr.First(), LinqEx.First(arr));
        }