Package hlcd.testing.optimizationTester
Class HammingWeightTester
java.lang.Object
hlcd.testing.optimizationTester.HammingWeightTester
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
ConstructorsConstructorDescriptionHammingWeightTester(byte n, byte base)Initializes the test of vectors based on the length and base specified. -
Method Summary
Modifier and TypeMethodDescriptionbooleanareEqual(long v)Returnstrueif both methods return the same Hamming weight of the specified vector,falseotherwise.voidbenchmarkOptimalSolution(long iterations)Generates x random vectors, where x is the iterations specified and find their Hamming weight using the optimal solution the program uses.voidbenchmarkOptimalSolution(long iterations, long v)Finds the Hamming weight of the vector specified x times, where x is the iterations specified.voidbenchmarkSlowSolution(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.voidbenchmarkSlowSolution(long iterations, long v)Finds the Hamming weight of the vector specified x times, where x is the iterations specified.voidinitComparison(long iterations)Generates x random vectors, where x is the iterations specified to ensure both optimal and slow solutions yield the same output.booleanReturnstrueif the test was successful in the sense that both implementations yielded the same results,falseotherwise.
-
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 testbase- 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 ofv- 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 ofv- 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)Returnstrueif both methods return the same Hamming weight of the specified vector,falseotherwise.- Parameters:
v- the vector to find the Hamming weight of- Returns:
trueif both methods return the same Hamming weight of the specified vector,falseotherwise
-
isValidImplementation
public boolean isValidImplementation()Returnstrueif the test was successful in the sense that both implementations yielded the same results,falseotherwise.- Returns:
trueif the test was successful in the sense that both implementations yielded the same results,falseotherwise
-