Exemplo n.º 1
0
        public void ClosestNodes_ExactMatch()
        {
            var kBucket = new KBucket <Contact>();

            for (var i = 0; i < 0x12; ++i)
            {
                kBucket.Add(new Contact((byte)i));
            }

            var contact  = new Contact((byte)0x11); // 00010001
            var contacts = kBucket.Closest(contact).Take(3).ToArray();

            CollectionAssert.AreEqual(new byte[] { 0x11 }, contacts[0].Id); // distance: 00000000
            CollectionAssert.AreEqual(new byte[] { 0x10 }, contacts[1].Id); // distance: 00000001
            CollectionAssert.AreEqual(new byte[] { 0x01 }, contacts[2].Id); // distance: 00010000
        }
Exemplo n.º 2
0
        public void All()
        {
            var kBucket = new KBucket <Contact>
            {
                LocalContactId = new byte[] { 0, 0 }
            };

            for (var i = 0; i < 1000; ++i)
            {
                kBucket.Add(new Contact((byte)(i / 256), (byte)(i % 256)));
            }

            var contact  = new Contact((byte)0x80, (byte)0x80);
            var contacts = kBucket.Closest(contact);

            Assert.IsTrue(contacts.Count() > 100);
        }
Exemplo n.º 3
0
        public void TryGet()
        {
            var bucket = new KBucket <Contact>();
            var alpha  = new Contact("alpha");
            var beta   = new Contact("beta");

            bucket.Add(alpha);

            var q = bucket.TryGet(alpha.Id, out var found);

            Assert.IsTrue(q);
            Assert.AreSame(alpha, found);

            q = bucket.TryGet(beta.Id, out var notfound);
            Assert.IsFalse(q);
            Assert.IsNull(notfound);
        }
Exemplo n.º 4
0
        public void Enumerate()
        {
            var bucket    = new KBucket <Contact>();
            var nContacts = bucket.ContactsPerBucket + 1;

            for (var i = 0; i < nContacts; ++i)
            {
                bucket.Add(new Contact(i));
            }

            Assert.AreEqual(nContacts, bucket.Count);

            var n = 0;

            foreach (var _ in bucket)
            {
                ++n;
            }

            Assert.AreEqual(nContacts, n);
        }
Exemplo n.º 5
0
        public void Count()
        {
            var bucket = new KBucket <Contact>();

            Assert.AreEqual(0, bucket.Count);

            bucket.Add(new Contact("a"));
            bucket.Add(new Contact("a"));
            bucket.Add(new Contact("a"));
            bucket.Add(new Contact("b"));
            bucket.Add(new Contact("b"));
            bucket.Add(new Contact("c"));
            bucket.Add(new Contact("d"));
            bucket.Add(new Contact("c"));
            bucket.Add(new Contact("d"));
            bucket.Add(new Contact("e"));
            bucket.Add(new Contact("f"));
            bucket.Add(new Contact("a"));
            Assert.AreEqual(6, bucket.Count);
        }