public void ConcurrentSymbolDictTest(int rounds, int tasks) { int count = m_stringTable.Length; int block = count / tasks; var rnd = new RandomSystem(); var perm = new int[count].SetByIndex(i => i); var map = new SymbolDict <int>().AsConcurrent(); // var map = new FastConcurrentSymbolDict<int>(); Test.Begin("parallel adding and removing tests {0}", count); for (int r = 0; r < rounds; r++) { Test.Begin("round {0}", r); Test.Begin("adding in parallel"); Parallel.For(0, tasks, t => { for (int i = t * block, e = i + block; i < e; i++) { map[m_symbolTable[perm[i]]] = i; } }); Test.IsTrue(count == map.Count); Test.End(); Test.Begin("checking"); for (int i = 0; i < count; i++) { Test.IsTrue(map[m_symbolTable[perm[i]]] == i); } Test.End(); rnd.Randomize(perm); Test.Begin("removing in parallel"); Parallel.For(0, tasks, t => { for (int i = t * block, e = i + block; i < e; i++) { map.Remove(m_symbolTable[perm[i]]); } }); Test.IsTrue(map.Count == 0); Test.End(); Test.End(); } Test.End(); }
public void SymbolDictTest(int rounds) { int count = m_stringTable.Length; int block = count / 10; var rnd = new RandomSystem(); var perm = new int[count].SetByIndex(i => i); var map = new SymbolDict <int>(); Test.Begin("adding removing tests {0}", count); for (int r = 0; r < rounds; r++) { Test.Begin("round {0}", r); int added = 0; while (added < count) { Test.Begin("adding 20%"); for (int i = added; i < added + 2 * block; i++) { map[m_symbolTable[perm[i]]] = i; } added += 2 * block; Test.IsTrue(added == map.Count); Test.End(); if (added == count) { break; } Test.Begin("removing 10%"); for (int i = added - block; i < added; i++) { int index = 0; var removed = map.TryRemove(m_symbolTable[perm[i]], out index); Test.IsTrue(removed); Test.IsTrue(index == i); } added -= block; Test.IsTrue(added == map.Count); Test.End(); } Test.Begin("checking"); for (int i = 0; i < count; i++) { Test.IsTrue(map[m_symbolTable[perm[i]]] == i); } Test.IsTrue(count == map.Count); Test.End(); added += 2 * block; rnd.Randomize(perm); Test.Begin("removing 100%"); if ((r & 1) == 0) { for (int i = 0; i < count; i++) { int index = 0; bool removed = map.TryRemove(m_symbolTable[perm[i]], out index); Test.IsTrue(removed); } } else { map.Clear(); } Test.IsTrue(map.Count == 0); Test.End(); Test.End(); } Test.End(); }