public void GetFallback(int i)
        {
            var thing = new ConcurrentIndexed <int, string>();

            Assert.Equal("not hello", thing.GetOrAdd(i, () => "not hello"));
            Assert.Equal("not hello", thing.GetOrAdd(i, () => "hello"));
        }
        public void GetOrAdd_Func()
        {
            var target = new ConcurrentIndexed <int, string>();

            Assert.Equal("1", target.GetOrAdd(1, () => "1"));
            Assert.Equal("1", target.GetOrAdd(1, () => "1!"));
        }
        public void UpdateFallback(int i)
        {
            var thing = new ConcurrentIndexed <int, string>();

            Assert.Equal("not hello", thing.DoOrAdd(i, (x) => x + " world", "not hello"));
            Assert.Equal("not hello", thing.GetOrThrow(i));
        }
Beispiel #4
0
        public void Setup()
        {
            random = new Random((int)DateTime.Now.Ticks);
            mine   = new ConcurrentIndexed <int, string>();
            concurrentDictionary = new ConcurrentDictionary <int, string>();

            void cdRead()
            {
                for (var i = 0; i < Reps; i++)
                {
                    concurrentDictionary[random.Next(1, RandomRange + 1)] = "";
                }
            }

            cdActions = new List <Action>();
            for (int i = 0; i < Threads; i++)
            {
                cdActions.Add(cdRead);
            }

            void myRead()
            {
                for (var i = 0; i < Reps; i++)
                {
                    mine.Set(random.Next(1, RandomRange + 1), "");
                }
            }

            myActions = new List <Action>();
            for (int i = 0; i < Threads; i++)
            {
                myActions.Add(myRead);
            }
        }
        public void AddOrThrow_Func()
        {
            var target = new ConcurrentIndexed <int, string>();

            target.AddOrThrow(1, () => "1");

            Assert.ThrowsAny <Exception>(() => target.AddOrThrow(1, () => "1!"));
        }
        public void UpdateOrThrow()
        {
            var target = new ConcurrentIndexed <int, string>();

            Assert.ThrowsAny <Exception>(() => target.UpdateOrThrow(1, x => x + "!"));
            target.Set(1, "1");
            Assert.Equal("1!", target.UpdateOrThrow(1, x => x + "!"));
        }
        public void AddUpdate(int i)
        {
            var thing = new ConcurrentIndexed <int, string>();

            thing.DoOrAdd(i, (s) => s + " world", "hello");
            Assert.Equal("hello", thing.GetOrThrow(i));
            thing.DoOrAdd(i, (s) => s + " world", "hello");
            Assert.Equal("hello world", thing.GetOrThrow(i));
        }
        public void DoOrAdd()
        {
            var target = new ConcurrentIndexed <int, string>();

            target.DoOrAdd(1, x => "1!", "1");
            Assert.Equal("1", target.GetOrThrow(1));
            target.DoOrAdd(1, x => "1!", "1");
            Assert.Equal("1!", target.GetOrThrow(1));
        }
        public void Set()
        {
            var target = new ConcurrentIndexed <int, string>();

            target.Set(1, "1");
            Assert.Equal("1", target.GetOrThrow(1));
            target.Set(1, "1!");
            Assert.Equal("1!", target.GetOrThrow(1));
        }
        public void GetOrTrhow()
        {
            var target = new ConcurrentIndexed <int, string>();

            Assert.ThrowsAny <Exception>(() => target.GetOrThrow(1));

            target.AddOrThrow(1, "1");

            Assert.Equal("1", target.GetOrThrow(1));
        }
        public void TryGetValue()
        {
            var target = new ConcurrentIndexed <int, string>();

            Assert.False(target.TryGetValue(1, out var _));

            target.AddOrThrow(1, "1");

            Assert.True(target.TryGetValue(1, out var second));
            Assert.Equal("1", second);
        }
        public void TryRemove(int i)
        {
            var thing = new ConcurrentIndexed <int, string>();

            Assert.True(thing.TryAdd(i, "world"));
            Assert.Single(thing);
            Assert.True(thing.TryRemove(i, out var value));
            Assert.Empty(thing);
            Assert.Equal("world", value);
            Assert.True(thing.TryAdd(i, "world"));
            Assert.Single(thing);
        }
        public void AddUpdateOrThrow(int i)
        {
            var thing = new ConcurrentIndexed <int, string>();

            thing.AddOrThrow(i, "hello");
            Assert.Equal("hello", thing.GetOrThrow(i));
            Assert.Throws <Exception>(() =>
            {
                thing.AddOrThrow(i, "hello");
            });

            thing.UpdateOrThrow(i, (s) => s + " world");
            Assert.Equal("hello world", thing.GetOrThrow(i));
        }
        public void Enumerate()
        {
            var thing = new ConcurrentIndexed <int, string>();

            for (var i = 0; i < 100; i++)
            {
                thing.AddOrThrow(i, () => $"{i}");
            }
            var count = 0;

            foreach (var uhh in thing)
            {
                count++;
            }
            Assert.Equal(100, count);
        }
        public void DoOrThrow_Func()
        {
            var target = new ConcurrentIndexed <int, string>();

            Assert.ThrowsAny <Exception>(() => target.DoOrThrow(1, x => x + "!"));

            target.AddOrThrow(1, "1");
            string s = null;

            target.DoOrThrow(1, x => {
                s = x + "!";
                return(x);
            });

            Assert.Equal("1!", s);
        }
        public async Task AddUpdateParallel()
        {
            {
                var thing = new ConcurrentIndexed <int, string>();

                var tasks = new List <Task>();
                for (var i = 0; i < 100; i += 3)
                {
                    tasks.Add(Task.Run(() => AddUpdate(i)));
                    tasks.Add(Task.Run(() => GetFallback(i + 1)));
                    tasks.Add(Task.Run(() => UpdateFallback(i + 2)));
                }

                await Task.WhenAll(tasks.ToArray());
            }
        }
        public void AddAndRemove()
        {
            var random = new Random();

            for (int i = 0; i < 10000; i++)
            {
                const int V     = 1000;
                var       check = new int[V];
                var       thing = new ConcurrentIndexed <int, string>();

                int count = 0;

                Parallel.Invoke(new int[10000].Select <int, Action>(_ => () => {
                    var value = random.Next(1000);
                    if (random.Next(2) == 1)
                    {
                        if (thing.TryAdd(value, value.ToString()))
                        {
                            Interlocked.Increment(ref check[value]);
                            Interlocked.Increment(ref count);
                        }
                    }
                    else
                    {
                        if (thing.TryRemove(value, out var _))
                        {
                            Interlocked.Decrement(ref check[value]);
                            Interlocked.Decrement(ref count);
                        }
                    }
                }).ToArray());


                Assert.Equal(count, thing.Count);
                for (int j = 0; j < V; j++)
                {
                    Assert.Equal(check[j] > 0, thing.ContainsKey(j));
                }
                foreach (var item in thing)
                {
                    Assert.True(check[item.Key] > 0);
                }
            }
        }
Beispiel #18
0
        public void Setup()
        {
            concurrentHashIndexedTree2 = new ConcurrentIndexed <HashTest, int>();
            for (var i = 1; i <= Items; i++)
            {
                concurrentHashIndexedTree2.GetOrAdd(new HashTest(1, i), i);
            }

            concurrentDictionary2 = new ConcurrentDictionary <HashTest, int>();
            for (var i = 1; i <= Items; i++)
            {
                concurrentDictionary2.GetOrAdd(new HashTest(1, i), i);
            }

            rawConcurrentHashIndexed = new ConcurrentIndexed <int, int>();
            rawConcurrentHashIndexed.GetOrAdd(1, 1);

            dict = new Dictionary <int, int>
            {
                [1] = 1
            };

            concurrentHashIndexedTree = new ConcurrentIndexed <int, int>();
            concurrentHashIndexedTree.Set(1, 1);
            //concurrentHashIndexedTree.GetOrAdd(new IndexedListNode<int, Concurrent<int>>(1, new Concurrent<int>(1)));
            //concurrentHashIndexedTree.GetOrAdd(new IndexedListNode<int, int>(1,1));
            concurrentDictionary = new ConcurrentDictionary <int, int>
            {
                [1] = 1
            };
            //var r = new Random((int)DateTime.Now.Ticks);
            //data = new Tuple<int, int>[Threads][];
            //for (int i = 0; i < Threads; i++)
            //{
            //    data[i] = new Tuple<int, int>[Items];
            //    for (int j = 0; j < Items; j++)
            //    {
            //        data[i][j] = new Tuple<int, int>(r.Next(1, 1000), r.Next(1, 1000));
            //    }
            //}
        }
        public void DoubleIterate()
        {
            for (int k = 0; k < 1000; k++)
            {
                var student = new ConcurrentIndexed <int, int>();
                for (int i = 0; i < 10; i++)
                {
                    student.GetOrAdd(i, i);
                }
                var count = 0;

                foreach (var x in student)
                {
                    foreach (var y in student)
                    {
                        count++;
                    }
                }
                Assert.Equal(100, count);
            }
        }
Beispiel #20
0
        public void TryGetValue()
        {
            var target = new ConcurrentIndexed <HashTestHelper, int>();

            target.AddOrThrow(new HashTestHelper(1, 1), 1);
            target.AddOrThrow(new HashTestHelper(1, 2), 2);
            target.AddOrThrow(new HashTestHelper(1, 3), 3);
            target.AddOrThrow(new HashTestHelper(1, 4), 4);
            target.AddOrThrow(new HashTestHelper(1, 5), 5);
            target.AddOrThrow(new HashTestHelper(1, 6), 6);

            Assert.True(target.TryGetValue(new HashTestHelper(1, 1), out var res1));
            Assert.Equal(1, res1);
            Assert.True(target.TryGetValue(new HashTestHelper(1, 2), out var res2));
            Assert.Equal(2, res2);
            Assert.True(target.TryGetValue(new HashTestHelper(1, 3), out var res3));
            Assert.Equal(3, res3);
            Assert.True(target.TryGetValue(new HashTestHelper(1, 4), out var res4));
            Assert.Equal(4, res4);
            Assert.True(target.TryGetValue(new HashTestHelper(1, 5), out var res5));
            Assert.Equal(5, res5);
            Assert.True(target.TryGetValue(new HashTestHelper(1, 6), out var res6));
            Assert.Equal(6, res6);
        }
Beispiel #21
0
 public InterpetedStaticScope(ConcurrentIndexed <IKey, IInterpetedMember> backing, IRunTimeAnyRoot root) : base(root)
 {
     Backing = backing ?? throw new ArgumentNullException(nameof(backing));
 }
Beispiel #22
0
 // TODO, I think the type just passes through here
 // like everywhere else
 public static IInterpetedStaticScope StaticScope(ConcurrentIndexed <IKey, IInterpetedMember> backing, IInterfaceModuleType type)
 => Root(new Func <IRunTimeAnyRoot, RunTimeAnyRootEntry>[] { StaticScopeIntention(backing, type) }).Has <IInterpetedStaticScope>();
Beispiel #23
0
 public static Func <IRunTimeAnyRoot, RunTimeAnyRootEntry> StaticScopeIntention(ConcurrentIndexed <IKey, IInterpetedMember> backing, IInterfaceModuleType type)
 => root => new RunTimeAnyRootEntry(new InterpetedStaticScope(backing, root), type);