Example #1
0
        public void AllErrorTest()
        {
            var value1      = 123;
            var message2    = "Message2";
            var errorCode2  = 555;
            var message3    = "Message3";
            var resultFuncs = new Func <IOperationResult <int> >[]
            {
                () => OperationResult.Success <int>(value1),
                () => OperationResult.Error <int>(errorCode2, message2),
                () => OperationResult.Error <int>(message3),
            };

            var res = resultFuncs.All();

            res.Match(_ =>
            {
                Assert.False(true);
                return(0);
            }, (code, error) =>
            {
                Assert.Equal(errorCode2, code);
                Assert.Equal(message2, error);
                return(0);
            }, _ =>
            {
                Assert.False(true);
                return(0);
            });
        }
 public bool IsValid(T objectToValidate)
 {
     try {
         _validators.All(pv => pv(objectToValidate))
     } catch (Exception) {
         return(false);
     }
 }
Example #3
0
        public int Solve(string content)
        {
            var vals = content.Split("-");
            var min  = int.Parse(vals[0]);
            var max  = int.Parse(vals[1]);

            var rules = new Func <int, bool>[]
            {
                (x) => x.ToString().Length == 6,
                (x) => x > min && x < max,
                (x) =>
                {
                    var t     = x.ToString();
                    var pairs = new List <string>();
                    for (var i = 1; i < 6; i++)
                    {
                        pairs.Add("" + t[i - 1] + t[i]);
                    }

                    return(pairs.Any(p => p[0] == p[1]));
                },
                (x) => {
                    var t     = x.ToString();
                    var pairs = new List <string>();
                    for (var i = 1; i < 6; i++)
                    {
                        if (t[i - 1] > t[i])
                        {
                            return(false);
                        }
                    }

                    return(true);
                },
                (x) =>
                {
                    var t      = x.ToString();
                    var counts = t.GroupBy(y => y).Select(y => y.Count());
                    return(counts.Any(y => y == 2));
                }
            };

            var validNumbers = new List <int>();

            for (var x = min; x <= max; x++)
            {
                if (rules.All(r => r(x)))
                {
                    validNumbers.Add(x);
                }
            }

            return(validNumbers.Count());
        }
        public static bool IsFeatureSupported(Type pluginType, CCProcessingFeature feature)
        {
            bool result = false;

            if (typeof(V2.ICCProcessingPlugin).IsAssignableFrom(pluginType))
            {
                V2.ICCProcessingPlugin plugin       = (V2.ICCProcessingPlugin)Activator.CreateInstance(pluginType);
                Func <object>[]        checkFuncArr = null;
                switch (feature)
                {
                case CCProcessingFeature.ProfileManagement:
                    checkFuncArr = new Func <object>[] { () => plugin.CreateProcessor <V2.ICCProfileProcessor>(null) };
                    break;

                case CCProcessingFeature.HostedForm:
                    checkFuncArr = new Func <object>[] { () => plugin.CreateProcessor <V2.ICCHostedFormProcessor>(null) };
                    break;

                case CCProcessingFeature.ExtendedProfileManagement:
                    checkFuncArr = new Func <object>[] { () => plugin.CreateProcessor <V2.ICCProfileProcessor>(null) };
                    break;

                case CCProcessingFeature.PaymentHostedForm:
                    checkFuncArr = new Func <object>[] {
                        () => plugin.CreateProcessor <V2.ICCHostedPaymentFormProcessor>(null),
                        () => plugin.CreateProcessor <V2.ICCTransactionGetter>(null),
                        () => plugin.CreateProcessor <V2.ICCProfileCreator>(null),
                        () => plugin.CreateProcessor <V2.ICCHostedPaymentFormResponseParser>(null)
                    };
                    break;

                case CCProcessingFeature.WebhookManagement:
                    checkFuncArr = new Func <object>[] {
                        () => plugin.CreateProcessor <V2.ICCWebhookProcessor>(null),
                        () => plugin.CreateProcessor <V2.ICCWebhookResolver>(null)
                    };
                    break;

                case CCProcessingFeature.TransactionGetter:
                    checkFuncArr = new Func <object>[] { () => plugin.CreateProcessor <V2.ICCTransactionGetter>(null) };
                    break;
                }

                if (checkFuncArr != null)
                {
                    result = checkFuncArr.All(f => CheckV2TypeWrapper(f));
                }
            }

            return(result);
        }
Example #5
0
        public bool IsValid()
        {
            var constraints = new Func <bool>[]
            {
                Cons1,
                Cons2,
                Cons3,
                Cons4,
                Cons5,
                Cons6
            };

            return(constraints.All(c => c()));
        }
Example #6
0
        private void ValidateHandshakeResponseHeaders()
        {
            Func <bool>[] validators = new Func <bool>[]
            {
                () => this.ValidateHandshakeResponseHeader(
                    "upgrade",
                    v => v.Equals("websocket", StringComparison.OrdinalIgnoreCase),
                    new ProtocolViolationException("Server protocol violation. Server did not respond with an Upgrade header with 'WebSocket' value.")),
                () => this.ValidateHandshakeResponseHeader(
                    "connection",
                    v => v.Equals("upgrade, http2-icb", StringComparison.OrdinalIgnoreCase),
                    new ProtocolViolationException("Server protocol violation. Server did not respond with an Connection header with 'Upgrade' value.")),
                () => this.ValidateHandshakeResponseHeader(
                    "sec-websocket-accept",
                    this.ValidateAccept,
                    new ProtocolViolationException("Server protocol violation. Server did not respond with sec-websocket-accept header that matches the sec-websocket-accept header sent on request.")),
                () => string.IsNullOrEmpty(this.Protocol) ? true : this.ValidateHandshakeResponseHeader(
                    "sec-websocket-protocol",
                    v => v.Equals(this.Protocol, StringComparison.Ordinal),
                    new ProtocolViolationException("Server protocol violation. Server did not respond with sec-websocket-protocol header that matches the protocol sent on request.")),
                () => this.ValidateHandshakeResponseHeader(
                    "sec-websocket-extensions",
                    v => v.Equals("http2", StringComparison.OrdinalIgnoreCase),
                    new ProtocolViolationException("Server protocol violation. Server did not respond with sec-websocket-extensions header that matches the protocol sent on request.")),
                () =>
                {
                    if (this.handshakeResponseHeaders.ContainsKey("set-cookie") || this.handshakeResponseHeaders.ContainsKey("set-cookie2"))
                    {
                        this.FailWebSocketConnection(new NotSupportedException("Server attempted to set cookies with handshake response. Cookies are not supported yet."), false);
                        return(false);
                    }

                    return(true);
                }
            };

            if (validators.All(v => v()))
            {
                this.Connected();

                if (EnableServerPing)
                {
                    // Setup ping timer to ping the server every ServerPingInterval.
                    this.pingTimer = new Timer(this.PingTimerCallback, null, TimeSpan.Zero, ServerPingInterval);
                }

                this.ProcessMessages();
            }
        }
Example #7
0
        public static async Task EnsureObjectsAreCollected(Func <object[]> objectsGetter)
        {
            var references = new Func <WeakReference[]>(() =>
            {
                var objects = objectsGetter();
                var result  = new WeakReference[objects.Length];

                for (var i = 0; i < objects.Length; i++)
                {
                    result[i] = new WeakReference(objects[i]);
                }

                return(result);
            })();

            await WaitUntilReferencesAreCollected(10000, references);

            Assert.That(references.All(r => !r.IsAlive), "Expected all references to be GC-ed within 10 seconds but they weren't");
        }
        private static Offer FindOffer(IEnumerable <Offer> offers, OrderItem item)
        {
            var comparers = new Func <Offer, OrderItem, bool>[] {
                (o, i) => o.PriceList.Id == item.Order.ActivePrice.Id,
                (o, i) => o.ProductId == i.ProductId,
                (o, i) => o.SynonymCode == i.SynonymCode,
                (o, i) => o.SynonymFirmCrCode == i.SynonymFirmCrCode,
                (o, i) => o.SynonymFirmCrCode == i.SynonymFirmCrCode,
                (o, i) => o.Code == i.Code,
                (o, i) => o.CodeCr == i.CodeCr,
                (o, i) => o.Junk == i.Junk,
                (o, i) => o.RequestRatio == i.RequestRatio,
                (o, i) => o.OrderCost == i.OrderCost,
                (o, i) => o.MinOrderCount == i.MinOrderCount,
            };

            return(offers.FirstOrDefault(o => o.Id.CoreId == item.CoreId)
                   ?? offers.FirstOrDefault(o => comparers.All(c => c(o, item))));
        }
Example #9
0
        static void Main(string[] args)
        {
            var start = 146810;
            var end   = 612564;

            var criteria1 = new Func <string, bool>[]
            {
                s => s.Length == 6,
                s => int.TryParse(s, out _),
                s => s.Select((c, i) => i == 0 || c >= s[i - 1]).All(b => b),
                s => s.Select((c, i) => i != 0 && c == s[i - 1]).Any(b => b)
            };

            var criteria2 = new Func <string, bool>[]
            {
                s => s.Length == 6,
                s => int.TryParse(s, out _),
                s => s.Select((c, i) => i == 0 || c >= s[i - 1]).All(b => b),
                s => string.Join("", s.Select((v, i) => (i > 0 && s[i - 1] != v) ? $"|{v}" : $"{v}")).Split('|').Any(x => x.Length == 2)
            };

            Console.WriteLine(Enumerable.Range(start, end - start).Select(i => criteria1.All(c => c(i.ToString()))).Count(i => i));
            Console.WriteLine(Enumerable.Range(start, end - start).Select(i => criteria2.All(c => c(i.ToString()))).Count(i => i));
        }
        private void ValidateHandshakeResponseHeaders()
        {
            Func<bool>[] validators = new Func<bool>[]
            {
                () => this.ValidateHandshakeResponseHeader(
                    "upgrade",
                    v => v.Equals("websocket", StringComparison.OrdinalIgnoreCase),
                    new ProtocolViolationException("Server protocol violation. Server did not respond with an Upgrade header with 'WebSocket' value.")),
                () => this.ValidateHandshakeResponseHeader(
                    "connection",
                    v => v.Equals("upgrade, http2-icb", StringComparison.OrdinalIgnoreCase),
                    new ProtocolViolationException("Server protocol violation. Server did not respond with an Connection header with 'Upgrade' value.")),
                () => this.ValidateHandshakeResponseHeader(
                    "sec-websocket-accept",
                    this.ValidateAccept,
                    new ProtocolViolationException("Server protocol violation. Server did not respond with sec-websocket-accept header that matches the sec-websocket-accept header sent on request.")),
                () => string.IsNullOrEmpty(this.Protocol) ? true : this.ValidateHandshakeResponseHeader(
                    "sec-websocket-protocol",
                    v => v.Equals(this.Protocol, StringComparison.Ordinal),
                    new ProtocolViolationException("Server protocol violation. Server did not respond with sec-websocket-protocol header that matches the protocol sent on request.")),
                () => this.ValidateHandshakeResponseHeader(
                        "sec-websocket-extensions",
                        v => v.Equals("http2", StringComparison.OrdinalIgnoreCase),
                        new ProtocolViolationException("Server protocol violation. Server did not respond with sec-websocket-extensions header that matches the protocol sent on request.")),
                () =>
                {
                    if (this.handshakeResponseHeaders.ContainsKey("set-cookie") || this.handshakeResponseHeaders.ContainsKey("set-cookie2"))
                    {
                        this.FailWebSocketConnection(new NotSupportedException("Server attempted to set cookies with handshake response. Cookies are not supported yet."), false);
                        return false;
                    }

                    return true;
                }
            };

            if (validators.All(v => v()))
            {
                this.Connected();

                if (EnableServerPing)
                {
                    // Setup ping timer to ping the server every ServerPingInterval.
                    this.pingTimer = new Timer(this.PingTimerCallback, null, TimeSpan.Zero, ServerPingInterval);
                }

                this.ProcessMessages();
            }
        }
        private bool TipoEmpresaValido(ParametroSistemaEmpresa parametroSistemaEmpresa, ParametrosSistema parametroSistema)
        {
            var reglas = new Func<ParametroSistemaEmpresa, bool>[]
            {
                ps => (ps.EmpresaId == 1 || ps.EmpresaId == 2) && parametroSistema.TipoDeParametro != 1,
                ps => (ps.EmpresaId != 1 && ps.EmpresaId != 2) && parametroSistema.TipoDeParametro != 2
            };

            return reglas.All(regla => regla(parametroSistemaEmpresa) == false);
        }