public void CacheKeys_should_be_equal()
        {
            DateTime date = DateTime.Now;
            CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null, new DateTime(date.Ticks) });
            CacheKey key2 = new CacheKey(new Object[] { 1, "hello", null, new DateTime(date.Ticks) });

            Assert.That(key1, Is.EqualTo(key2));
            Assert.That(key2, Is.EqualTo(key1));
            Assert.That(key2.GetHashCode(), Is.EqualTo(key1.GetHashCode()));
            Assert.That(key1.GetHashCode(), Is.EqualTo(key2.GetHashCode()));
            Assert.That(key1.ToString(), Is.EqualTo(key2.ToString()));
        }
Example #2
0
        public void GetHashCode_WithIdenticalCacheKeys_ReturnsTheSameValue([Values(0, 1, 2)] int stateKey, [Values(0, 1, 2)] int location)
        {
            var subjectA = new CacheKey("OK", stateKey, location);
            var subjectB = new CacheKey("OK", stateKey, location);

            Assert.That(subjectA.GetHashCode(), Is.EqualTo(subjectB.GetHashCode()));
        }
        public void CacheKeys_should_not_be_equal_due_to_order()
        {
            CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null });
            CacheKey key2 = new CacheKey(new Object[] { 1, null, "hello" });

            Assert.That(key1, Is.Not.EqualTo(key2));
            Assert.That(key2, Is.Not.EqualTo(key1));
            Assert.That(key1.GetHashCode(), Is.Not.EqualTo(key2.GetHashCode()));
            Assert.That(key1.ToString(), Is.Not.EqualTo(key2.ToString()));
        }
        public void ZeroHash()
        {
            var cache = new CachingFactory <CacheKey, CacheValue>(512,
                                                                  k => new CacheValue(k.Value + 1),
                                                                  k => k.Value,
                                                                  (k, v) => k.Value == v.Value);

            var key = new CacheKey(0);

            Assert.Equal(CacheKey.GetHashCode(key), 0);

            CacheValue value;
            bool       found = cache.TryGetValue(key, out value);

            Assert.False(found);
        }
Example #5
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (ID.Length != 0)
            {
                hash ^= ID.GetHashCode();
            }
            if (Channel.Length != 0)
            {
                hash ^= Channel.GetHashCode();
            }
            if (Metadata.Length != 0)
            {
                hash ^= Metadata.GetHashCode();
            }
            if (Body.Length != 0)
            {
                hash ^= Body.GetHashCode();
            }
            if (ReplyChannel.Length != 0)
            {
                hash ^= ReplyChannel.GetHashCode();
            }
            if (Timeout != 0)
            {
                hash ^= Timeout.GetHashCode();
            }
            if (CacheKey.Length != 0)
            {
                hash ^= CacheKey.GetHashCode();
            }
            if (CacheTTL != 0)
            {
                hash ^= CacheTTL.GetHashCode();
            }
            if (Context.Length != 0)
            {
                hash ^= Context.GetHashCode();
            }
            if (_unknownFields != null)
            {
                hash ^= _unknownFields.GetHashCode();
            }
            return(hash);
        }
Example #6
0
        public void CacheKeyWithTwoParamsSameHashcode()
        {
            CacheKey key1 = new CacheKey();
            CacheKey key2 = new CacheKey();

            key1.Update("HS1CS001");
            key1.Update("HS1D4001");

            key2.Update("HS1D4001");
            key2.Update("HS1CS001");

#if dotnet2
            Assert.Ignore("The .NET 2.0 CLR uses a different algorithm for string hashing than the .NET 1.1 CLR.");
#else
            Assert.AreEqual(key1.GetHashCode(), key2.GetHashCode(), "Expect same hashcode.");
            Assert.IsFalse(key1.Equals(key2), "Expect not equal");
#endif
        }
Example #7
0
        public void CacheKeyWithSameHashcode()
        {
            CacheKey key1 = new CacheKey();
            CacheKey key2 = new CacheKey();

            key1.Update("HS1CS001");
            key2.Update("HS1D4001");

            /*
             * The string hash algorithm is not an industry standard and is not guaranteed to produce the same behaviour between versions.
             * And in fact it does not. The .NET 2.0 CLR uses a different algorithm for string hashing than the .NET 1.1 CLR.
             */

#if dotnet2
            Assert.Ignore("The .NET 2.0 CLR uses a different algorithm for string hashing than the .NET 1.1 CLR.");
#else
            Assert.AreEqual(key1.GetHashCode(), key2.GetHashCode(), "Expect same hashcode.");
            Assert.IsFalse(key1.Equals(key2), "Expect not equal");
#endif
        }
 public override int GetHashCode()
 {
     return(cacheKey.GetHashCode());
 }
 public override int GetHashCode() => cacheKey.GetHashCode();
 public bool Equals(CacheKey other)
 {
     return(other != null && GetHashCode().Equals(other.GetHashCode()));
 }