Class HammingWeightTester

java.lang.Object
hlcd.testing.optimizationTester.HammingWeightTester

public class HammingWeightTester extends Object
Ensures the optimal Hamming weight approach used in this program matched the expected result. The optimal approach doesn't use a loop, only binary manipulation. The test is comparing what the optimal approach yield to the naive approach that uses a loop.
Since:
1.8
Version:
1.0 (February 17th, 2022)
Author:
Maysara Al Jumaily
  • Constructor Summary

    Constructors
    Constructor
    Description
    HammingWeightTester​(byte n, byte base)
    Initializes the test of vectors based on the length and base specified.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    areEqual​(long v)
    Returns true if both methods return the same Hamming weight of the specified vector, false otherwise.
    void
    benchmarkOptimalSolution​(long iterations)
    Generates x random vectors, where x is the iterations specified and find their Hamming weight using the optimal solution the program uses.
    void
    benchmarkOptimalSolution​(long iterations, long v)
    Finds the Hamming weight of the vector specified x times, where x is the iterations specified.
    void
    benchmarkSlowSolution​(long iterations)
    Generates x random vectors, where x is the iterations specified and find their Hamming weight using the slow solution that uses a loop.
    void
    benchmarkSlowSolution​(long iterations, long v)
    Finds the Hamming weight of the vector specified x times, where x is the iterations specified.
    void
    initComparison​(long iterations)
    Generates x random vectors, where x is the iterations specified to ensure both optimal and slow solutions yield the same output.
    boolean
    Returns true if the test was successful in the sense that both implementations yielded the same results, false otherwise.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HammingWeightTester

      public HammingWeightTester(byte n, byte base)
      Initializes the test of vectors based on the length and base specified.
      Parameters:
      n - the length of each vector to test
      base - the base of the vectors
  • Method Details

    • benchmarkOptimalSolution

      public void benchmarkOptimalSolution(long iterations)
      Generates x random vectors, where x is the iterations specified and find their Hamming weight using the optimal solution the program uses. Note that generating a random long will take time.
      Parameters:
      iterations - the number of random vectors to generate and find the Hamming weight of
      See Also:
      benchmarkOptimalSolution(long, long)
    • benchmarkOptimalSolution

      public void benchmarkOptimalSolution(long iterations, long v)
      Finds the Hamming weight of the vector specified x times, where x is the iterations specified. It uses the optimal solution the program uses. Since the vector is specified, there is no randomness used, hence, the time will be more accurate.
      Parameters:
      iterations - the number of random vectors to generate and find the Hamming weight of
      v - the vector to find the Hamming weight of
      See Also:
      benchmarkOptimalSolution(long)
    • benchmarkSlowSolution

      public void benchmarkSlowSolution(long iterations)
      Generates x random vectors, where x is the iterations specified and find their Hamming weight using the slow solution that uses a loop. Note that generating a random long will take time.
      Parameters:
      iterations - the number of random vectors to generate and find the Hamming weight of
      See Also:
      benchmarkSlowSolution(long, long)
    • benchmarkSlowSolution

      public void benchmarkSlowSolution(long iterations, long v)
      Finds the Hamming weight of the vector specified x times, where x is the iterations specified. It uses the slow solution that uses a loop. Since the vector is specified, there is no randomness used, hence, the time will be more accurate.
      Parameters:
      iterations - the number of random vectors to generate and find the Hamming weight of
      v - the vector to find the Hamming weight of
      See Also:
      benchmarkSlowSolution(long)
    • initComparison

      public void initComparison(long iterations)
      Generates x random vectors, where x is the iterations specified to ensure both optimal and slow solutions yield the same output. In the case where the weight is not consist between the two methods, then the vector and the weights will be printed on screen. This is benchmarked but the amount of time doesn't have any significance.
      Parameters:
      iterations - the number of random vectors to generate and find the Hamming weight of
      See Also:
      benchmarkSlowSolution(long, long)
    • areEqual

      public boolean areEqual(long v)
      Returns true if both methods return the same Hamming weight of the specified vector, false otherwise.
      Parameters:
      v - the vector to find the Hamming weight of
      Returns:
      true if both methods return the same Hamming weight of the specified vector, false otherwise
    • isValidImplementation

      public boolean isValidImplementation()
      Returns true if the test was successful in the sense that both implementations yielded the same results, false otherwise.
      Returns:
      true if the test was successful in the sense that both implementations yielded the same results, false otherwise