Example #1
0
        public static void PopulateCollections25_25_50PctUnique(int maxN, out int[] uniqueArray, out int[] mixedArray,
                                                                SCG.HashSet <int> h, FastHashSet <int> f = null, C5.HashSet <int> c5 = null, SCG.SortedSet <int> sortedSet = null, SCG.List <int> lst = null)
        {
            uniqueArray = new int[maxN];
            mixedArray  = new int[maxN];

            Random rand = new Random(89);

            BenchUtil.PopulateIntArray(uniqueArray, rand, int.MinValue, int.MaxValue, 1.0); // a array should have 100% unique values

            int uniqueValuesCount = maxN / 2;                                               // this should produce a c array that has 50% unique values (the other 50% are duplicates), but all are actually in the uniqueArray, so 1, 1, 2, 2 would be an example of this

            if (uniqueValuesCount == 0)
            {
                uniqueValuesCount = 1;
            }
            BenchUtil.PopulateIntArrayFromUniqueArray(mixedArray, rand, uniqueArray, uniqueValuesCount);
            BenchUtil.PopulateIntArrayAtRandomIndices(mixedArray, rand, int.MinValue, int.MaxValue, maxN - uniqueValuesCount);

            if (h != null)
            {
                for (int i = 0; i < maxN; i++)
                {
                    h.Add(uniqueArray[i]);
                }
            }

            if (f != null)
            {
                for (int i = 0; i < maxN; i++)
                {
                    f.Add(uniqueArray[i]);
                }
            }

            if (c5 != null)
            {
                for (int i = 0; i < maxN; i++)
                {
                    c5.Add(uniqueArray[i]);
                }
            }

            if (sortedSet != null)
            {
                for (int i = 0; i < maxN; i++)
                {
                    sortedSet.Add(uniqueArray[i]);
                }
            }

            if (lst != null)
            {
                for (int i = 0; i < maxN; i++)
                {
                    lst.Add(uniqueArray[i]);
                }
                lst.Sort();
            }
        }
Example #2
0
Test_CollectionFromSystemListAdapter()
{
    Print( "Create" );
    var c =
        SystemEnumerable.Create( 1, 2, 3, 4, 5, 6 )
        .ToList()
        .AsHalfdecentCollection();

    Print( ".Stream()" );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 1, 2, 3, 4, 5, 6 ) ) );

    Print( ".GetAndReplaceWhere( Predicate< T > )" );
    var to = new SCG.List< int >();
    Stream.Create( 20, 40, 60, 80, 100 )
        .To( c.GetAndReplaceWhere( i => i % 2 == 0 ) )
        .EmptyTo(
            to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 1, 20, 3, 40, 5, 60 ) ) );
    to.Sort();
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 2, 4, 6 ) ) );

    Print( ".GetAndRemoveWhere( Predicate< T > )" );
    to.Clear();
    c.GetAndRemoveWhere( i => i >= 10 )
        .EmptyTo( to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 1, 3, 5 ) ) );
    to.Sort();
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 20, 40, 60 ) ) );

    Print( ".Get( TKey )" );
    Assert( c.Get( 0 ) == 1 );
    Assert( c.Get( 1 ) == 3 );
    Assert( c.Get( 2 ) == 5 );

    Print( ".Replace( TKey, T )" );
    c.Replace( 1, 2 );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 1, 2, 5 ) ) );

    Print( ".Remove( TKey )" );
    c.Remove( 1 );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 1, 5 ) ) );

    Print( ".Contains( TKey )" );
    Assert( c.Contains( 0 ) );
    Assert( c.Contains( 1 ) );
    Assert( !c.Contains( 2 ) );

    Print( ".Stream( TKey )" );
    Assert(
        c.Stream( 1 )
        .SequenceEqual(
            Stream.Create( 5 ) ) );

    Print( ".GetAndReplaceAll( TKey )" );
    to.Clear();
    Stream.Create( 6 )
        .To( c.GetAndReplaceAll( 1 ) )
        .EmptyTo( to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 1, 6 ) ) );
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 5 ) ) );

    Print( ".GetAndRemoveAll( TKey )" );
    to.Clear();
    c.GetAndRemoveAll( 1 )
        .EmptyTo(
            to.AsSink() );
    Assert(
        c.Stream().SequenceEqual(
            Stream.Create( 1 ) ) );
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 6 ) ) );

    Print( ".Add( TKey, T )" );
    c.Add( 0, 0 );
    c.Add( 2, 2 );
    c.Add( 1, 1 );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 0, 1, 1, 2 ) ) );

    Print( ".Count" );
    Assert( c.Count == 4 );

    Print( ".StreamPairs()" );
    var ts = c.StreamPairs();
    Assert( ts.Pull().BothEqual( 0L, 0 ) );
    Assert( ts.Pull().BothEqual( 1L, 1 ) );
    Assert( ts.Pull().BothEqual( 2L, 1 ) );
    Assert( ts.Pull().BothEqual( 3L, 2 ) );
    Assert( !ts.TryPull().A );
}
Example #3
0
Test_CollectionFromSystemStringBuilderAdapter()
{
    Print( "Create" );
    var c =
        new StringBuilder( "abcde" )
        .AsHalfdecentCollection();

    Print( ".Stream()" );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'b', 'c', 'd', 'e' ) ) );

    Print( ".GetAndReplaceWhere( Predicate< char > )" );
    var to = new SCG.List< char >();
    Stream.Create( 'B' )
        .To( c.GetAndReplaceWhere( ch => ch == 'b' ) )
        .EmptyTo( to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'B', 'c', 'd', 'e' ) ) );
    to.Sort();
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 'b' ) ) );

    Print( ".GetAndRemoveWhere( Predicate< char > )" );
    to.Clear();
    c.GetAndRemoveWhere( ch => ch == 'B' )
        .EmptyTo( to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'c', 'd', 'e' ) ) );
    to.Sort();
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 'B' ) ) );

    Print( ".Get( IInteger )" );
    Assert( c.Get( 0 ) == 'a' );
    Assert( c.Get( 1 ) == 'c' );
    Assert( c.Get( 2 ) == 'd' );
    Assert( c.Get( 3 ) == 'e' );

    Print( ".Replace( IInteger, char )" );
    c.Replace( 1, 'C' );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'C', 'd', 'e' ) ) );

    Print( ".Remove( IInteger )" );
    c.Remove( 1 );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'd', 'e' ) ) );

    Print( ".Contains( IInteger )" );
    Assert( !c.Contains( -1 ) );
    Assert( c.Contains( 0 ) );
    Assert( c.Contains( 1 ) );
    Assert( c.Contains( 2 ) );
    Assert( !c.Contains( 3 ) );

    Print( ".Stream( IInteger )" );
    Assert(
        c.Stream( 1 )
        .SequenceEqual(
            Stream.Create( 'd' ) ) );

    Print( ".GetAndReplaceAll( IInteger )" );
    to.Clear();
    Stream.Create( 'D' )
        .To( c.GetAndReplaceAll( 1 ) )
        .EmptyTo( to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'D', 'e' ) ) );
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 'd' ) ) );

    Print( ".GetAndRemoveAll( IInteger )" );
    to.Clear();
    c.GetAndRemoveAll( 1 )
        .EmptyTo( to.AsSink() );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'e' ) ) );
    Assert(
        to.SequenceEqual(
            SystemEnumerable.Create( 'D' ) ) );

    Print( ".Add( IInteger, char )" );
    c.Add( 1, 'b' );
    c.Add( 2, 'c' );
    c.Add( 3, 'd' );
    c.Add( 5, 'f' );
    Assert(
        c.Stream()
        .SequenceEqual(
            Stream.Create( 'a', 'b', 'c', 'd', 'e', 'f' ) ) );

    Print( ".Count" );
    Assert( c.Count == 6 );

    Print( ".StreamPairs()" );
    var ts = c.StreamPairs();
    Assert( ts.Pull().BothEqual( 0L, 'a' ) );
    Assert( ts.Pull().BothEqual( 1L, 'b' ) );
    Assert( ts.Pull().BothEqual( 2L, 'c' ) );
    Assert( ts.Pull().BothEqual( 3L, 'd' ) );
    Assert( ts.Pull().BothEqual( 4L, 'e' ) );
    Assert( ts.Pull().BothEqual( 5L, 'f' ) );
    Assert( !ts.TryPull().A );
}
Example #4
0
        private static void TestGetOrAddOrUpdate(int cLevel, int initSize, int threads, int addsPerThread, bool isAdd)
        {
            LurchTable <int, int> dict = new LurchTable <int, int>(/*cLevel,*/ 1);

            int count = threads;

            using (ManualResetEvent mre = new ManualResetEvent(false))
            {
                for (int i = 0; i < threads; i++)
                {
                    int ii = i;
                    Task.Run(
                        () =>
                    {
                        for (int j = 0; j < addsPerThread; j++)
                        {
                            if (isAdd)
                            {
                                //call one of the overloads of GetOrAdd
                                switch (j % 2)
                                {
                                case 0:
                                    dict.GetOrAdd(j, -j);
                                    break;

                                case 1:
                                    dict.GetOrAdd(j, x => - x);
                                    break;
                                    //case 2: // J2N TODO: Implement this overload
                                    //    dict.GetOrAdd(j, (x, m) => x * m, -1);
                                    //    break;
                                }
                            }
                            else
                            {
                                switch (j % 2)
                                {
                                case 0:
                                    dict.AddOrUpdate(j, -j, (k, v) => - j);
                                    break;

                                case 1:
                                    dict.AddOrUpdate(j, (k) => - k, (k, v) => - k);
                                    break;
                                    //case 2: // J2N TODO: Implement this overload
                                    //    dict.AddOrUpdate(j, (k, m) => k * m, (k, v, m) => k * m, -1);
                                    //    break;
                                }
                            }
                        }
                        if (Interlocked.Decrement(ref count) == 0)
                        {
                            mre.Set();
                        }
                    });
                }
                mre.WaitOne();
            }

            foreach (var pair in dict)
            {
                Assert.Equal(pair.Key, -pair.Value);
            }

            SCG.List <int> gotKeys = new SCG.List <int>();
            foreach (var pair in dict)
            {
                gotKeys.Add(pair.Key);
            }
            gotKeys.Sort();

            SCG.List <int> expectKeys = new SCG.List <int>();
            for (int i = 0; i < addsPerThread; i++)
            {
                expectKeys.Add(i);
            }

            Assert.Equal(expectKeys.Count, gotKeys.Count);

            for (int i = 0; i < expectKeys.Count; i++)
            {
                Assert.True(expectKeys[i].Equals(gotKeys[i]),
                            string.Format("* Test '{4}': Level={0}, initSize={1}, threads={2}, addsPerThread={3})" + Environment.NewLine +
                                          "> FAILED.  The set of keys in the dictionary is are not the same as the expected.",
                                          cLevel, initSize, threads, addsPerThread, isAdd ? "GetOrAdd" : "GetOrUpdate"));
            }

            // Finally, let's verify that the count is reported correctly.
            Assert.Equal(addsPerThread, dict.Count);
            Assert.Equal(addsPerThread, dict.ToArray().Length);
        }
Example #5
0
        private static void TestRemove1(int cLevel, int threads, int removesPerThread)
        {
            LurchTable <int, int> dict = new LurchTable <int, int>(/*cLevel,*/ 1);
            string methodparameters    = string.Format("* TestRemove1(cLevel={0}, threads={1}, removesPerThread={2})", cLevel, threads, removesPerThread);
            int    N = 2 * threads * removesPerThread;

            for (int i = 0; i < N; i++)
            {
                dict[i] = -i;
            }

            // The dictionary contains keys [0..N), each key mapped to a value equal to the key.
            // Threads will cooperatively remove all even keys

            int running = threads;

            using (ManualResetEvent mre = new ManualResetEvent(false))
            {
                for (int i = 0; i < threads; i++)
                {
                    int ii = i;
                    Task.Run(
                        () =>
                    {
                        for (int j = 0; j < removesPerThread; j++)
                        {
                            int value;
                            int key = 2 * (ii + j * threads);
                            Assert.True(dict.TryRemove(key, out value), "Failed to remove an element! " + methodparameters);

                            Assert.Equal(-key, value);
                        }

                        if (Interlocked.Decrement(ref running) == 0)
                        {
                            mre.Set();
                        }
                    });
                }
                mre.WaitOne();
            }

            foreach (var pair in dict)
            {
                Assert.Equal(pair.Key, -pair.Value);
            }

            SCG.List <int> gotKeys = new SCG.List <int>();
            foreach (var pair in dict)
            {
                gotKeys.Add(pair.Key);
            }
            gotKeys.Sort();

            SCG.List <int> expectKeys = new SCG.List <int>();
            for (int i = 0; i < (threads * removesPerThread); i++)
            {
                expectKeys.Add(2 * i + 1);
            }

            Assert.Equal(expectKeys.Count, gotKeys.Count);

            for (int i = 0; i < expectKeys.Count; i++)
            {
                Assert.True(expectKeys[i].Equals(gotKeys[i]), "  > Unexpected key value! " + methodparameters);
            }

            // Finally, let's verify that the count is reported correctly.
            Assert.Equal(expectKeys.Count, dict.Count);
            Assert.Equal(expectKeys.Count, dict.ToArray().Length);
        }
Example #6
0
        private static void TestUpdate1(int cLevel, int threads, int updatesPerThread)
        {
            IDictionary <int, int> dict = new LurchTable <int, int>(/*cLevel,*/ 1);

            for (int i = 1; i <= updatesPerThread; i++)
            {
                dict[i] = i;
            }

            int running = threads;

            using (ManualResetEvent mre = new ManualResetEvent(false))
            {
                for (int i = 0; i < threads; i++)
                {
                    int ii = i;
                    Task.Run(
                        () =>
                    {
                        for (int j = 1; j <= updatesPerThread; j++)
                        {
                            dict[j] = (ii + 2) * j;
                        }
                        if (Interlocked.Decrement(ref running) == 0)
                        {
                            mre.Set();
                        }
                    });
                }
                mre.WaitOne();
            }

            foreach (var pair in dict)
            {
                var div = pair.Value / pair.Key;
                var rem = pair.Value % pair.Key;

                Assert.Equal(0, rem);
                Assert.True(div > 1 && div <= threads + 1,
                            string.Format("* Invalid value={3}! TestUpdate1(cLevel={0}, threads={1}, updatesPerThread={2})", cLevel, threads, updatesPerThread, div));
            }

            SCG.List <int> gotKeys = new SCG.List <int>();
            foreach (var pair in dict)
            {
                gotKeys.Add(pair.Key);
            }
            gotKeys.Sort();

            SCG.List <int> expectKeys = new SCG.List <int>();
            for (int i = 1; i <= updatesPerThread; i++)
            {
                expectKeys.Add(i);
            }

            Assert.Equal(expectKeys.Count, gotKeys.Count);

            for (int i = 0; i < expectKeys.Count; i++)
            {
                Assert.True(expectKeys[i].Equals(gotKeys[i]),
                            string.Format("The set of keys in the dictionary is are not the same as the expected." + Environment.NewLine +
                                          "TestUpdate1(cLevel={0}, threads={1}, updatesPerThread={2})", cLevel, threads, updatesPerThread)
                            );
            }
        }
Example #7
0
        private static void TestAdd1(int cLevel, int initSize, int threads, int addsPerThread)
        {
            LurchTable <int, int>  dictConcurrent = new LurchTable <int, int>(/*cLevel,*/ 1);
            IDictionary <int, int> dict           = dictConcurrent;

            int count = threads;

            using (ManualResetEvent mre = new ManualResetEvent(false))
            {
                for (int i = 0; i < threads; i++)
                {
                    int ii = i;
                    Task.Run(
                        () =>
                    {
                        for (int j = 0; j < addsPerThread; j++)
                        {
                            dict.Add(j + ii * addsPerThread, -(j + ii * addsPerThread));
                        }
                        if (Interlocked.Decrement(ref count) == 0)
                        {
                            mre.Set();
                        }
                    });
                }
                mre.WaitOne();
            }

            foreach (var pair in dict)
            {
                Assert.Equal(pair.Key, -pair.Value);
            }

            SCG.List <int> gotKeys = new SCG.List <int>();
            foreach (var pair in dict)
            {
                gotKeys.Add(pair.Key);
            }

            gotKeys.Sort();

            SCG.List <int> expectKeys = new SCG.List <int>();
            int            itemCount  = threads * addsPerThread;

            for (int i = 0; i < itemCount; i++)
            {
                expectKeys.Add(i);
            }

            Assert.Equal(expectKeys.Count, gotKeys.Count);

            for (int i = 0; i < expectKeys.Count; i++)
            {
                Assert.True(expectKeys[i].Equals(gotKeys[i]),
                            string.Format("The set of keys in the dictionary is are not the same as the expected" + Environment.NewLine +
                                          "TestAdd1(cLevel={0}, initSize={1}, threads={2}, addsPerThread={3})", cLevel, initSize, threads, addsPerThread)
                            );
            }

            // Finally, let's verify that the count is reported correctly.
            int expectedCount = threads * addsPerThread;

            Assert.Equal(expectedCount, dict.Count);
            Assert.Equal(expectedCount, dictConcurrent.ToArray().Length);
        }