context: // <copyright file="IluptElementSorterTest.cs" company="Math.NET">
  // Math.NET Numerics, part of the Math.NET Project
  // http://numerics.mathdotnet.com
  // http://github.com/mathnet/mathnet-numerics
  // http://mathnetnumerics.codeplex.com
  // Copyright (c) 2009-2010 Math.NET
  // Permission is hereby granted, free of charge, to any person
  // obtaining a copy of this software and associated documentation
  // files (the "Software"), to deal in the Software without
  // restriction, including without limitation the rights to use,
  // copy, modify, merge, publish, distribute, sublicense, and/or sell
  // copies of the Software, and to permit persons to whom the
  // Software is furnished to do so, subject to the following
  // conditions:
  // The above copyright notice and this permission notice shall be
  // included in all copies or substantial portions of the Software.
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  // OTHER DEALINGS IN THE SOFTWARE.
  // </copyright>
  
  namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Preconditioners
  {
      using System.Numerics;
      using LinearAlgebra.Complex;
      using LinearAlgebra.Complex.Solvers.Preconditioners;
      using LinearAlgebra.Generic;
      using NUnit.Framework;
  
      /// <summary>
      /// Test for element sort algorithm of Ilupt class.
      /// </summary>
      [TestFixture]
      public sealed class IluptElementSorterTest
      {
          /// <summary>
          /// Heap sort with increasing integer array.
          /// </summary>
          [Test]
          public void HeapSortWithIncreasingIntegerArray()
          {
              var sortedIndices = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
              IlutpElementSorter.SortIntegersDecreasing(sortedIndices);
              for (var i = 0; i < sortedIndices.Length; i++)
              {
                  Assert.AreEqual(sortedIndices.Length - 1 - i, sortedIndices[i], "#01-" + i);
              }
          }
  
          /// <summary>
          /// Heap sort with decreasing integer array.
          /// </summary>
          [Test]
          public void HeapSortWithDecreasingIntegerArray()
          {
              var sortedIndices = new[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
              IlutpElementSorter.SortIntegersDecreasing(sortedIndices);
              for (var i = 0; i < sortedIndices.Length; i++)
              {
                  Assert.AreEqual(sortedIndices.Length - 1 - i, sortedIndices[i], "#01-" + i);
              }
          }
  
          /// <summary>
          /// Heap sort with random integer array.
          /// </summary>
          [Test]
          public void HeapSortWithRandomIntegerArray()
          {
              var sortedIndices = new[] { 5, 2, 8, 6, 0, 4, 1, 7, 3, 9 };
              IlutpElementSorter.SortIntegersDecreasing(sortedIndices);
              for (var i = 0; i < sortedIndices.Length; i++)
              {
                  Assert.AreEqual(sortedIndices.Length - 1 - i, sortedIndices[i], "#01-" + i);
              }
          }
  
          /// <summary>
          /// Heap sort with duplicate entries.
          /// </summary>
          [Test]
          public void HeapSortWithDuplicateEntries()
          {
              var sortedIndices = new[] { 1, 1, 1, 1, 2, 2, 2, 2, 3, 4 };
              IlutpElementSorter.SortIntegersDecreasing(sortedIndices);
              for (var i = 0; i < sortedIndices.Length; i++)
              {
                  if (i == 0)
                  {
                      Assert.AreEqual(4, sortedIndices[i], "#01-" + i);
                  }
                  else
                  {
                      if (i == 1)
                      {
                          Assert.AreEqual(3, sortedIndices[i], "#01-" + i);
                      }
                      else
                      {
                          if (i < 6)
                          {
                              if (sortedIndices[i] != 2)
                              {
                                  Assert.Fail("#01-" + i);
                              }
                          }
                          else
                          {
                              if (sortedIndices[i] != 1)
                              {
                                  Assert.Fail("#01-" + i);
                              }
                          }
                      }
                  }
              }
          }
  
          /// <summary>
          /// Heap sort with special constructed integer array.
          /// </summary>
          [Test]
          public void HeapSortWithSpecialConstructedIntegerArray()
          {
              var sortedIndices = new[] { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 };
              IlutpElementSorter.SortIntegersDecreasing(sortedIndices);
              for (var i = 0; i < sortedIndices.Length; i++)
              {
                  if (i == 0)
                  {
                      Assert.AreEqual(1, sortedIndices[i], "#01-" + i);
                      break;
                  }
              }
  
              sortedIndices = new[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
              IlutpElementSorter.SortIntegersDecreasing(sortedIndices);
              for (var i = 0; i < sortedIndices.Length; i++)
              {
                  if (i == 0)
                  {
                      Assert.AreEqual(1, sortedIndices[i], "#02-" + i);
                      break;
                  }
              }
  
              sortedIndices = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
              IlutpElementSorter.SortIntegersDecreasing(sortedIndices);
              for (var i = 0; i < sortedIndices.Length; i++)
              {
                  if (i == 0)
                  {
                      Assert.AreEqual(1, sortedIndices[i], "#03-" + i);
                      break;
                  }
              }
  
              sortedIndices = new[] { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 };
              IlutpElementSorter.SortIntegersDecreasing(sortedIndices);
              for (var i = 0; i < sortedIndices.Length; i++)
              {
                  if (i == 9)
                  {
                      Assert.AreEqual(0, sortedIndices[i], "#04-" + i);
                      break;
                  }
              }
          }
  
          /// <summary>
          /// Heap sort with increasing double array.
          /// </summary>
          [Test]
          public void HeapSortWithIncreasingDoubleArray()
          {
              var sortedIndices = new int[10];
              Vector<Complex> values = new DenseVector(10);
              values[0] = 0;
              values[1] = 1;
              values[2] = 2;
              values[3] = 3;
              values[4] = 4;
              values[5] = 5;
              values[6] = 6;
              values[7] = 7;
              values[8] = 8;
              values[9] = 9;
              for (var i = 0; i < sortedIndices.Length; i++)
              {
                  sortedIndices[i] = i;
              }
  
              IlutpElementSorter.SortDoubleIndicesDecreasing(0, sortedIndices.Length - 1, sortedIndices, values);
              for (var i = 0; i < sortedIndices.Length; i++)
              {
                  Assert.AreEqual(sortedIndices.Length - 1 - i, sortedIndices[i], "#01-" + i);
              }
          }
  
          /// <summary>
          /// Heap sort with decreasing doubleArray
          /// </summary>
          [Test]
          public void HeapSortWithDecreasingDoubleArray()
          {
              var sortedIndices = new int[10];
              Vector<Complex> values = new DenseVector(10);
              values[0] = 9;
              values[1] = 8;
              values[2] = 7;
              values[3] = 6;
              values[4] = 5;
              values[5] = 4;
              values[6] = 3;
              values[7] = 2;
              values[8] = 1;
              values[9] = 0;
              for (var i = 0; i < sortedIndices.Length; i++)
              {
                  sortedIndices[i] = i;
              }
  
              IlutpElementSorter.SortDoubleIndicesDecreasing(0, sortedIndices.Length - 1, sortedIndices, values);
              for (var i = 0; i < sortedIndices.Length; i++)
              {
                  Assert.AreEqual(i, sortedIndices[i], "#01-" + i);
              }
          }
  
          /// <summary>
          /// Heap sort with random double array.
          /// </summary>
          [Test]
          public void HeapSortWithRandomDoubleArray()
          {
              var sortedIndices = new int[10];
  
gt:             Vector<Complex> values = new DenseVector(10);