public void AddThis()
    {
        var dictionary = new DictionaryNoAlloc <string, int>(2);

        dictionary["A"] = 5;
        Assert.AreEqual(5, dictionary["A"]);
    }
    public void AddFailDuplicate()
    {
        var dictionary = new DictionaryNoAlloc <string, int>(10);

        dictionary.Add("MyValue", 10);
        Assert.Throws <ArgumentException>(() => dictionary.Add("MyValue", 10));
    }
    public void DictionaryNoAlloc()
    {
        MemoryWatch watch      = new MemoryWatch();
        var         dictionary = new DictionaryNoAlloc <int, string>(1000);

        var handler = new HandlerEvent();

        const string ConstString = "Const string";

        watch.Start();

        int l = 0;

        for (int j = 0; j < Seconds; ++j)
        {
            for (int i = 0; i < FramesPerSecond; ++i)
            {
                // Add or overwrite
                for (int ii = 0; ii < 100; ii++)
                {
                    dictionary[i * 10 + 30 + ii] = ConstString;
                }
                l += 1;
            }
        }

        watch.Stop();
        results.Add(watch.GetAllocationDesc($"Dictionary no alloc add/replace 100 elements for each frame at {FramesPerSecond} FPS for {Seconds} s of game"));

        // Just so we force l to be used
        Assert.Less(0, l);
    }
    public void AddOverRange()
    {
        var dictionary = new DictionaryNoAlloc <string, int>(2);

        dictionary.Add("A", 1);
        dictionary.Add("B", 1);
        Assert.Throws <OverflowException>(() => dictionary.Add("C", 1));
    }
    public void Remove()
    {
        var dictionary = new DictionaryNoAlloc <string, int>(10);

        dictionary.Add("MyValue", 10);
        Assert.False(dictionary.Remove("NotUsedValue"));
        Assert.True(dictionary.Remove("MyValue"));
    }
    public void Get()
    {
        var dictionary = new DictionaryNoAlloc <string, int>(10);

        dictionary.Add("MyValue", 10);

        Assert.AreEqual(10, dictionary["MyValue"]);
    }
    public void HashClashSevereDuplicated()
    {
        var dictionary = new DictionaryNoAlloc <HashableKey, int>(3);

        dictionary.Add(new HashableKey("A", 1), 10);
        dictionary.Add(new HashableKey("B", 1), 12);
        dictionary.Remove(new HashableKey("A", 1));

        Assert.AreEqual(12, dictionary[new HashableKey("B", 1)]);
    }
    public void Clear()
    {
        var dictionary = new DictionaryNoAlloc <int, int>(100);

        for (int i = 0; i < 100; ++i)
        {
            dictionary.Add(i * i, i);
        }

        dictionary.Clear();
        Assert.AreEqual(0, dictionary.Count);
        Assert.Throws <KeyNotFoundException>(() => { int x = dictionary[4]; });
    }
    public void AddMany()
    {
        var dictionary = new DictionaryNoAlloc <int, int>(100);

        for (int i = 0; i < 100; ++i)
        {
            dictionary.Add(i * i, i);
        }

        for (int i = 0; i < 100; ++i)
        {
            Assert.AreEqual(i, dictionary[i * i]);
        }
    }
    public void HashClashSevere()
    {
        var dictionary = new DictionaryNoAlloc <HashableKey, int>(100);

        for (int i = 0; i < 100; ++i)
        {
            dictionary.Add(new HashableKey(i.ToString()), i);
        }

        for (int i = 0; i < 100; ++i)
        {
            var key = new HashableKey(i.ToString());
            Assert.AreEqual(i, dictionary[key]);
        }
    }
    public void Iterate()
    {
        var dictionary = new DictionaryNoAlloc <int, string>(100);

        for (int i = 0; i < 50; ++i)
        {
            dictionary[i] = i.ToString();
        }

        int elementsCount = 0;

        var iterator = dictionary.GetIteratorNoAlloc();

        while (iterator.MoveNext())
        {
            ++elementsCount;
        }

        Assert.AreEqual(50, elementsCount);
    }
    public void Add()
    {
        var dictionary = new DictionaryNoAlloc <string, int>(10);

        dictionary.Add("MyValue", 10);
    }
Beispiel #13
0
 internal DictionaryNoAllocIterator(DictionaryNoAlloc <TKey, TValue> dictionary)
 {
     array       = dictionary.array;
     arrayLength = array.Length;
     index       = -1;
 }