示例#1
0
        public static void EnsureCreated <TLink>(this ILinks <TLink> links, Func <TLink> creator, params TLink[] addresses)
        {
            var addressToUInt64Converter = CheckedConverter <TLink, ulong> .Default;
            var uInt64ToAddressConverter = CheckedConverter <ulong, TLink> .Default;
            var nonExistentAddresses     = new HashSet <TLink>(addresses.Where(x => !links.Exists(x)));

            if (nonExistentAddresses.Count > 0)
            {
                var max = nonExistentAddresses.Max();
                max = uInt64ToAddressConverter.Convert(System.Math.Min(addressToUInt64Converter.Convert(max), addressToUInt64Converter.Convert(links.Constants.InternalReferencesRange.Maximum)));
                var   createdLinks     = new List <TLink>();
                var   equalityComparer = EqualityComparer <TLink> .Default;
                TLink createdLink      = creator();
                while (!equalityComparer.Equals(createdLink, max))
                {
                    createdLinks.Add(createdLink);
                }
                for (var i = 0; i < createdLinks.Count; i++)
                {
                    if (!nonExistentAddresses.Contains(createdLinks[i]))
                    {
                        links.Delete(createdLinks[i]);
                    }
                }
            }
        }
示例#2
0
        public static TLink DeleteIfExists <TLink>(this ILinks <TLink> links, TLink source, TLink target)
        {
            var link = links.SearchOrDefault(source, target);

            if (!EqualityComparer <TLink> .Default.Equals(link, default))
            {
                links.Delete(link);
                return(link);
            }
            return(default);
示例#3
0
        public static void DeleteAll <TLink>(this ILinks <TLink> links)
        {
            var equalityComparer = EqualityComparer <TLink> .Default;
            var comparer         = Comparer <TLink> .Default;

            for (var i = links.Count(); comparer.Compare(i, default) > 0; i = Arithmetic.Decrement(i))
            {
                links.Delete(i);
                if (!equalityComparer.Equals(links.Count(), Arithmetic.Decrement(i)))
                {
                    i = links.Count();
                }
            }
        }
        private static void TestNonexistentReferences(this ILinks <ulong> memoryAdapter)
        {
            var link = memoryAdapter.Create();

            memoryAdapter.Update(link, ulong.MaxValue, ulong.MaxValue);
            var resultLink = _constants.Null;

            memoryAdapter.Each(foundLink =>
            {
                resultLink = foundLink[_constants.IndexPart];
                return(_constants.Break);
            }, _constants.Any, ulong.MaxValue, ulong.MaxValue);
            Assert.True(resultLink == link);
            Assert.True(memoryAdapter.Count(ulong.MaxValue) == 0);
            memoryAdapter.Delete(link);
        }
 public static void TestMultipleRandomCreationsAndDeletions<TLink>(this ILinks<TLink> links, int maximumOperationsPerCycle)
 {
     var comparer = Comparer<TLink>.Default;
     var addressToUInt64Converter = CheckedConverter<TLink, ulong>.Default;
     var uInt64ToAddressConverter = CheckedConverter<ulong, TLink>.Default;
     for (var N = 1; N < maximumOperationsPerCycle; N++)
     {
         var random = new System.Random(N);
         var created = 0UL;
         var deleted = 0UL;
         for (var i = 0; i < N; i++)
         {
             var linksCount = addressToUInt64Converter.Convert(links.Count());
             var createPoint = random.NextBoolean();
             if (linksCount >= 2 && createPoint)
             {
                 var linksAddressRange = new Range<ulong>(1, linksCount);
                 TLink source = uInt64ToAddressConverter.Convert(random.NextUInt64(linksAddressRange));
                 TLink target = uInt64ToAddressConverter.Convert(random.NextUInt64(linksAddressRange)); //-V3086
                 var resultLink = links.GetOrCreate(source, target);
                 if (comparer.Compare(resultLink, uInt64ToAddressConverter.Convert(linksCount)) > 0)
                 {
                     created++;
                 }
             }
             else
             {
                 links.Create();
                 created++;
             }
         }
         Assert.True(created == addressToUInt64Converter.Convert(links.Count()));
         for (var i = 0; i < N; i++)
         {
             TLink link = uInt64ToAddressConverter.Convert((ulong)i + 1UL);
             if (links.Exists(link))
             {
                 links.Delete(link);
                 deleted++;
             }
         }
         Assert.True(addressToUInt64Converter.Convert(links.Count()) == 0L);
     }
 }
示例#6
0
        public static void RunRandomDeletions <TLink>(this ILinks <TLink> links, ulong amountOfDeletions)
        {
            var random = RandomHelpers.Default;
            var addressToUInt64Converter = UncheckedConverter <TLink, ulong> .Default;
            var uInt64ToAddressConverter = UncheckedConverter <ulong, TLink> .Default;
            var linksCount = addressToUInt64Converter.Convert(links.Count());
            var min        = amountOfDeletions > linksCount ? 0UL : linksCount - amountOfDeletions;

            for (var i = 0UL; i < amountOfDeletions; i++)
            {
                linksCount = addressToUInt64Converter.Convert(links.Count());
                if (linksCount <= min)
                {
                    break;
                }
                var linksAddressRange = new Range <ulong>(min, linksCount);
                var link = uInt64ToAddressConverter.Convert(random.NextUInt64(linksAddressRange));
                links.Delete(link);
            }
        }
示例#7
0
 public static void Delete <TLink>(this ILinks <TLink> links, TLink linkToDelete) => links.Delete(new LinkAddress <TLink>(linkToDelete));
示例#8
0
 public void Delete(TLinkAddress link) => _links.Delete(link);
        public static void TestCRUDOperations<T>(this ILinks<T> links)
        {
            var constants = links.Constants;

            var equalityComparer = EqualityComparer<T>.Default;

            var zero = default(T);
            var one = Arithmetic.Increment(zero);

            // Create Link
            Assert.True(equalityComparer.Equals(links.Count(), zero));

            var setter = new Setter<T>(constants.Null);
            links.Each(constants.Any, constants.Any, setter.SetAndReturnTrue);

            Assert.True(equalityComparer.Equals(setter.Result, constants.Null));

            var linkAddress = links.Create();

            var link = new Link<T>(links.GetLink(linkAddress));

            Assert.True(link.Count == 3);
            Assert.True(equalityComparer.Equals(link.Index, linkAddress));
            Assert.True(equalityComparer.Equals(link.Source, constants.Null));
            Assert.True(equalityComparer.Equals(link.Target, constants.Null));

            Assert.True(equalityComparer.Equals(links.Count(), one));

            // Get first link
            setter = new Setter<T>(constants.Null);
            links.Each(constants.Any, constants.Any, setter.SetAndReturnFalse);

            Assert.True(equalityComparer.Equals(setter.Result, linkAddress));

            // Update link to reference itself
            links.Update(linkAddress, linkAddress, linkAddress);

            link = new Link<T>(links.GetLink(linkAddress));

            Assert.True(equalityComparer.Equals(link.Source, linkAddress));
            Assert.True(equalityComparer.Equals(link.Target, linkAddress));

            // Update link to reference null (prepare for delete)
            var updated = links.Update(linkAddress, constants.Null, constants.Null);

            Assert.True(equalityComparer.Equals(updated, linkAddress));

            link = new Link<T>(links.GetLink(linkAddress));

            Assert.True(equalityComparer.Equals(link.Source, constants.Null));
            Assert.True(equalityComparer.Equals(link.Target, constants.Null));

            // Delete link
            links.Delete(linkAddress);

            Assert.True(equalityComparer.Equals(links.Count(), zero));

            setter = new Setter<T>(constants.Null);
            links.Each(constants.Any, constants.Any, setter.SetAndReturnTrue);

            Assert.True(equalityComparer.Equals(setter.Result, constants.Null));
        }
        public static void TestRawNumbersCRUDOperations<T>(this ILinks<T> links)
        {
            // Constants
            var constants = links.Constants;
            var equalityComparer = EqualityComparer<T>.Default;

            var zero = default(T);
            var one = Arithmetic.Increment(zero);
            var two = Arithmetic.Increment(one);

            var h106E = new Hybrid<T>(106L, isExternal: true);
            var h107E = new Hybrid<T>(-char.ConvertFromUtf32(107)[0]);
            var h108E = new Hybrid<T>(-108L);

            Assert.Equal(106L, h106E.AbsoluteValue);
            Assert.Equal(107L, h107E.AbsoluteValue);
            Assert.Equal(108L, h108E.AbsoluteValue);

            // Create Link (External -> External)
            var linkAddress1 = links.Create();

            links.Update(linkAddress1, h106E, h108E);

            var link1 = new Link<T>(links.GetLink(linkAddress1));

            Assert.True(equalityComparer.Equals(link1.Source, h106E));
            Assert.True(equalityComparer.Equals(link1.Target, h108E));

            // Create Link (Internal -> External)
            var linkAddress2 = links.Create();

            links.Update(linkAddress2, linkAddress1, h108E);

            var link2 = new Link<T>(links.GetLink(linkAddress2));

            Assert.True(equalityComparer.Equals(link2.Source, linkAddress1));
            Assert.True(equalityComparer.Equals(link2.Target, h108E));

            // Create Link (Internal -> Internal)
            var linkAddress3 = links.Create();

            links.Update(linkAddress3, linkAddress1, linkAddress2);

            var link3 = new Link<T>(links.GetLink(linkAddress3));

            Assert.True(equalityComparer.Equals(link3.Source, linkAddress1));
            Assert.True(equalityComparer.Equals(link3.Target, linkAddress2));

            // Search for created link
            var setter1 = new Setter<T>(constants.Null);
            links.Each(h106E, h108E, setter1.SetAndReturnFalse);

            Assert.True(equalityComparer.Equals(setter1.Result, linkAddress1));

            // Search for nonexistent link
            var setter2 = new Setter<T>(constants.Null);
            links.Each(h106E, h107E, setter2.SetAndReturnFalse);

            Assert.True(equalityComparer.Equals(setter2.Result, constants.Null));

            // Update link to reference null (prepare for delete)
            var updated = links.Update(linkAddress3, constants.Null, constants.Null);

            Assert.True(equalityComparer.Equals(updated, linkAddress3));

            link3 = new Link<T>(links.GetLink(linkAddress3));

            Assert.True(equalityComparer.Equals(link3.Source, constants.Null));
            Assert.True(equalityComparer.Equals(link3.Target, constants.Null));

            // Delete link
            links.Delete(linkAddress3);

            Assert.True(equalityComparer.Equals(links.Count(), two));

            var setter3 = new Setter<T>(constants.Null);
            links.Each(constants.Any, constants.Any, setter3.SetAndReturnTrue);

            Assert.True(equalityComparer.Equals(setter3.Result, linkAddress2));
        }
        private static void TestBasicMemoryOperations(this ILinks <ulong> memoryAdapter)
        {
            var link = memoryAdapter.Create();

            memoryAdapter.Delete(link);
        }