Пример #1
0
        public void CanHandleNonTrivialCommands(string command)
        {
            var cmd = new CommandBytes(command);

            Assert.Equal(command.Length, cmd.Length);
            Assert.Equal(command.ToUpperInvariant(), cmd.ToString());

            Assert.Equal(31, CommandBytes.MaxLength);
        }
Пример #2
0
        public void CheckCommandContents()
        {
            for (int len = 0; len <= CommandBytes.MaxLength; len++)
            {
                var          s = new string('A', len);
                CommandBytes b = s;
                Assert.Equal(len, b.Length);

                var t = b.ToString();
                Assert.Equal(s, t);

                CommandBytes b2 = t;
                Assert.Equal(b, b2);

                Assert.Equal(len == 0, ReferenceEquals(s, t));
            }
        }
Пример #3
0
    static int Main()
    {
        var lookup = new Dictionary <CommandBytes, string>();

        void Add(string val)
        {
            var cb = new CommandBytes(val);

            // prove we didn't screw up
            if (cb.ToString() != val)
            {
                throw new InvalidOperationException("oops!");
            }
            lookup.Add(cb, val);
        }

        Add("client");
        Add("cluster");
        Add("command");
        Add("config");
        Add("dbsize");
        Add("decr");
        Add("del");
        Add("echo");
        Add("exists");
        Add("flushall");
        Add("flushdb");
        Add("get");
        Add("incr");
        Add("incrby");
        Add("info");
        Add("keys");
        Add("llen");
        Add("lpop");
        Add("lpush");
        Add("lrange");
        Add("memory");
        Add("mget");
        Add("mset");
        Add("ping");
        Add("quit");
        Add("role");
        Add("rpop");
        Add("rpush");
        Add("sadd");
        Add("scard");
        Add("select");
        Add("set");
        Add("shutdown");
        Add("sismember");
        Add("spop");
        Add("srem");
        Add("strlen");
        Add("subscribe");
        Add("time");
        Add("unlink");
        Add("unsubscribe");

        bool HuntFor(string lookFor)
        {
            Console.WriteLine($"Looking for: '{lookFor}'");
            var  hunt   = new CommandBytes(lookFor);
            bool result = lookup.TryGetValue(hunt, out var found);

            if (result)
            {
                Console.WriteLine($"Found via TryGetValue: '{found}'");
            }
            else
            {
                Console.WriteLine("**NOT FOUND** via TryGetValue");
            }

            Console.WriteLine("looking manually");
            foreach (var pair in lookup)
            {
                if (pair.Value == lookFor)
                {
                    Console.WriteLine($"Found manually: '{pair.Value}'");
                    var key = pair.Key;
                    void Compare <T>(string caption, Func <CommandBytes, T> func)
                    {
                        T x = func(hunt), y = func(key);

                        Console.WriteLine($"{caption}: {EqualityComparer<T>.Default.Equals(x, y)}, '{x}' vs '{y}'");
                    }

                    Compare("GetHashCode", _ => _.GetHashCode());
                    Compare("ToString", _ => _.ToString());
                    Compare("Length", _ => _.Length);
                    Compare("ToInnerString", _ => _.ToInnerString());
                    Console.WriteLine($"Equals: {key.Equals(hunt)}, {hunt.Equals(key)}");
                    var eq = EqualityComparer <CommandBytes> .Default;

                    Console.WriteLine($"EqualityComparer: {eq.Equals(key, hunt)}, {eq.Equals(hunt, key)}");
                    Compare("eq GetHashCode", _ => eq.GetHashCode(_));
                }
            }
            Console.WriteLine();

            return(result);
        }

        bool result1 = HuntFor("ping");
        bool result2 = HuntFor("subscribe");

        return((result1 && result2) ? 100 : -1);
    }