public void TestSearch()
        {
            var tmp    = new ExponentialSearch();
            var data   = new int[] { 1, 2, 3, 5, 7 };
            var actual = tmp.Search(data, 7);

            Assert.That(actual, Is.EqualTo(4));

            data   = new int[] { 1, 2 };
            actual = tmp.Search(data, 2);
            Assert.That(actual, Is.EqualTo(1));

            actual = tmp.Search(data, 21);
            Assert.That(actual, Is.EqualTo(-1));

            data   = new int[] { 1 };
            actual = tmp.Search(data, 1);
            Assert.That(actual, Is.EqualTo(0));

            actual = tmp.Search(data, 11);
            Assert.That(actual, Is.EqualTo(-1));

            data   = new int[] {  };
            actual = tmp.Search(data, 1);
            Assert.That(actual, Is.EqualTo(-1));

            actual = tmp.Search(null, 1);
            Assert.That(actual, Is.EqualTo(-1));
        }
예제 #2
0
        private static void ExecuteExponentialSearch()
        {
            var array = new int[] { 1, 3, 4, 8, 14, 21 };
            var item  = 87;

            Console.WriteLine($"Array - {string.Join(",", array)}");
            Console.WriteLine($"Exponential Search - {item}");
            var algo  = new ExponentialSearch();
            var index = algo.Search(array, item);

            Console.WriteLine($"Item {item} present at index- {index}");
        }
        public void TestExponentialSearch()
        {
            int[] arr = { 10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47 };

            arr = arr.OrderBy(c => c).ToArray();
            int x = 18;
            int n = arr.Length;

            var actual   = search.Search(arr, n, x);
            var expected = 4;

            Assert.Equal(expected, actual);
        }
예제 #4
0
        public void Search()
        {
            // Setup
            int[] arr        = { 0, 1, 10, 13, 16, 20, 22, 28, 31, 39, 45, 55 };
            int   searchElem = 10;


            // Search
            var result1 = ExponentialSearch.Search(arr, searchElem);

            searchElem = 11;
            var result2 = ExponentialSearch.Search(arr, searchElem);;

            // Assert
            Assert.AreEqual(true, result1);
            Assert.AreEqual(false, result2);
        }
예제 #5
0
        public IEnumerable <Argon2CalibrationResult> Run()
        {
            //
            // password and salt will be 128-bits, which is sufficient
            // for all applications [Section 9].
            //
            var password = new string('0', _input.SaltAndPasswordLength);
            var salt     = new string('1', _input.SaltAndPasswordLength);

            //
            // the maximum time it should take to calculate the
            // password hash.
            //
            var maximumTime = _input.MaximumTime;

            //
            // degree of parallelism should be twice the number of
            // cpu cores [Section 6.4]
            //
            var degreeOfParallelism = _input.DegreeOfParallelism;

            //
            // we will start at 1MB and work our way up to an acceptable
            // level. this memory usage is specified in KB.
            //
            var results = new List <Argon2CalibrationResult>();

            for (int memoryUsage = 1024; memoryUsage <= 4 * 1024 * 1024; memoryUsage *= 2)
            {
                //
                // figure out the maximum number of iterations such that the
                // running time does not exceed the maximum time. if the
                // running time exceeds the maximum time for a single iteration
                // then reduce the memory usage accordingly and try again.
                //
                Argon2CalibrationResult bestResult = null;
                var iterationSearch = new ExponentialSearch(_input.MinimumIterations, int.MaxValue, iterations =>
                {
                    var parameters = new Argon2Parameters()
                    {
                        DegreeOfParallelism = degreeOfParallelism,
                        Iterations          = iterations,
                        MemoryUsage         = memoryUsage
                    };

                    //
                    // argon2id is preferred for password storage.
                    //
                    var argon2 = _argon2Factory.Create(_input.Mode, password, salt, parameters);
                    _logger.WriteBeginCalibrationTest(parameters);

                    var stopwatch = Stopwatch.StartNew();
                    argon2.GetBytes(_input.HashLength);
                    stopwatch.Stop();

                    var elapsedTime = stopwatch.ElapsedMilliseconds;
                    _logger.WriteCompleteCalibrationTest(elapsedTime);

                    //
                    // store off the best (slowest) number of iterations for this memory usage.
                    //
                    if (elapsedTime <= maximumTime && (bestResult == null || elapsedTime > bestResult.ElapsedMilliseconds))
                    {
                        bestResult = new Argon2CalibrationResult()
                        {
                            ElapsedMilliseconds = elapsedTime,
                            Parameters          = parameters
                        };
                    }

                    if (elapsedTime > maximumTime)
                    {
                        return(ExponentialSearchComparison.ToHigh);
                    }
                    else if (elapsedTime < maximumTime)
                    {
                        return(ExponentialSearchComparison.ToLow);
                    }
                    else
                    {
                        return(ExponentialSearchComparison.Equal);
                    }
                });

                //
                // if there was a best result for this memory usage and
                // iteration combo, then store for later consumption.
                //
                iterationSearch.Search();
                if (bestResult != null)
                {
                    results.Add(bestResult);
                }
            }

            //
            // highest usage of memory and thread count should be recommended
            // first. we'll reverse the results so the consumer receives
            // them in recommended order.
            //
            results.Reverse();
            return(results);
        }