コード例 #1
0
//        [InlineData(5_000_000, 31)]
        public async Task AddGetRemove(int loops, int step)
        {
            FasterDictionary <string[], string> .ReadResult result;
            using (var dictionary = new FasterDictionary <string[], string>(TestHelper.GetKeyComparer <string[]>(), GetOptions($"{nameof(AddGetRemove)}-{loops}")))
            {
                var keys = new List <string[]>();

                for (var i = 0; i < loops; i++)
                {
                    keys.Add(new[] { "A", i.ToString() });
                    dictionary.Upsert(keys[i], (i + 1).ToString()).Dismiss();
                }

                await dictionary.Ping();

                for (var i = 0; i < loops; i += step)
                {
                    result = await dictionary.TryGet(keys[i]);

                    Assert.True(result.Found);
                    Assert.Equal((i + 1).ToString(), result.Value);
                }

                for (var i = 0; i < loops; i += step)
                {
                    if (i % 5 != 0)
                    {
                        await dictionary.Remove(keys[i]);
                    }
                }


                for (var i = 0; i < loops; i += step)
                {
                    result = await dictionary.TryGet(keys[i]);

                    if (i % 5 == 0)
                    {
                        Assert.True(result.Found);
                        Assert.Equal((i + 1).ToString(), result.Value);
                    }
                    else
                    {
                        Assert.False(result.Found);
                    }
                }
            }
        }
コード例 #2
0
        public void TestFasterDictionary()
        {
            FasterDictionary <int, Test> test = new FasterDictionary <int, Test>();
            uint dictionarysize = 10000;

            int[] numbers = new int[dictionarysize];
            for (int i = 1; i < dictionarysize; i++)
            {
                numbers[i] = numbers[i - 1] + i * HashHelpers.Expand((int)dictionarysize);
            }

            for (int i = 0; i < dictionarysize; i++)
            {
                test[i] = new Test(numbers[i]);
            }

            for (int i = 0; i < dictionarysize; i++)
            {
                if (test[i].i != numbers[i])
                {
                    throw new Exception();
                }
            }

            for (int i = 0; i < dictionarysize; i += 2)
            {
                if (test.Remove(i) == false)
                {
                    throw new Exception();
                }
            }

            test.Trim();

            for (int i = 0; i < dictionarysize; i++)
            {
                test[i] = new Test(numbers[i]);
            }

            for (int i = 1; i < dictionarysize - 1; i += 2)
            {
                if (test[i].i != numbers[i])
                {
                    throw new Exception();
                }
            }

            for (int i = 0; i < dictionarysize; i++)
            {
                if (test[i].i != numbers[i])
                {
                    throw new Exception();
                }
            }

            for (int i = (int)(dictionarysize - 1); i >= 0; i -= 3)
            {
                if (test.Remove(i) == false)
                {
                    throw new Exception();
                }
            }

            test.Trim();

            for (int i = (int)(dictionarysize - 1); i >= 0; i -= 3)
            {
                test[i] = new Test(numbers[i]);
            }

            for (int i = 0; i < dictionarysize; i++)
            {
                if (test[i].i != numbers[i])
                {
                    throw new Exception();
                }
            }

            for (int i = 0; i < dictionarysize; i++)
            {
                if (test.Remove(i) == false)
                {
                    throw new Exception();
                }
            }

            for (int i = 0; i < dictionarysize; i++)
            {
                if (test.Remove(i) == true)
                {
                    throw new Exception();
                }
            }

            test.Trim();

            test.Clear();
            for (int i = 0; i < dictionarysize; i++)
            {
                test[numbers[i]] = new Test(i);
            }

            for (int i = 0; i < dictionarysize; i++)
            {
                Test JapaneseCalendar = test[numbers[i]];
                if (JapaneseCalendar.i != i)
                {
                    throw new Exception("read back test failed");
                }
            }
        }
コード例 #3
0
        //[InlineData(5_000_000)]
        public async Task AddIterateUpdateIterateKeys(int loops, CheckpointType checkpointType)
        {
            var options = GetOptions($"{nameof(AddIterateUpdateIterate)}-{loops}");

            options.CheckPointType = checkpointType;
            options.DeleteOnClose  = false;

            FasterDictionary <int, string> .ReadResult result;
            using (var dictionary = new FasterDictionary <int, string>(TestHelper.GetKeyComparer <int>(), options))
            {
                for (var i = 0; i < loops; i++)
                {
                    dictionary.Upsert(i, (i + 1).ToString()).Dismiss();
                }

                await dictionary.Ping();

                await dictionary.Save();
            }

            var count = 0;

            using (var dictionary = new FasterDictionary <int, string>(TestHelper.GetKeyComparer <int>(), options))
            {
                for (var i = 0; i < 100; i++)
                {
                    result = await dictionary.TryGet(i);

                    Assert.True(result.Found);
                    Assert.Equal((result.Key + 1).ToString(), result.Value);
                }

                await foreach (var entry in dictionary.AsKeysIterator())
                {
                    count++;
                }

                result = await dictionary.TryGet(loops);

                Assert.False(result.Found);

                Assert.Equal(loops, count);
            }

            count = 0;
            using (var dictionary = new FasterDictionary <int, string>(TestHelper.GetKeyComparer <int>(), options))
            {
                for (var i = 0; i < loops; i++)
                {
                    if (i % 4 == 0)
                    {
                        dictionary.Remove(i).Dismiss();
                    }
                    else
                    {
                        count++;
                        dictionary.Upsert(i, (i + 2).ToString()).Dismiss();
                    }
                }

                await dictionary.Ping();

                await dictionary.Save();
            }

            options.DeleteOnClose = true;

            using (var dictionary = new FasterDictionary <int, string>(TestHelper.GetKeyComparer <int>(), options))
            {
                for (var i = 0; i < 100; i++)
                {
                    result = await dictionary.TryGet(i);

                    if (i % 4 == 0)
                    {
                        Assert.False(result.Found);
                    }
                    else
                    {
                        Assert.True(result.Found);
                        Assert.Equal((result.Key + 2).ToString(), result.Value);
                    }
                }

                var loopCount = 0;

                await foreach (var entry in dictionary.AsKeysIterator())
                {
                    loopCount++;
                    Assert.False(entry % 4 == 0);
                }

                result = await dictionary.TryGet(loops);

                Assert.False(result.Found);

                Assert.Equal(loopCount, count);
            }
        }