mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added tests for findMin and findMax
This commit is contained in:
155
Algorithms.cpp
155
Algorithms.cpp
@@ -8,6 +8,7 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <random>
|
||||
#include <gmpxx.h>
|
||||
#include "Algorithms.hpp"
|
||||
#include "Stopwatch.hpp"
|
||||
@@ -27,13 +28,17 @@ bool testGetFib();
|
||||
bool testBubbleSort();
|
||||
bool testQuickSort();
|
||||
bool testSearch();
|
||||
bool testFindMin();
|
||||
bool testFindMax();
|
||||
|
||||
|
||||
int main(){
|
||||
mee::Stopwatch timer;
|
||||
bool passedTest = false;
|
||||
std::vector<boolFn> functions {testGetPrimes, tetsGetDivisors, testGetSum, testIsFound, testGetPermutations, testGetFib, testBubbleSort, testQuickSort, testSearch};
|
||||
std::vector<std::string> names {"testGetPrimes", "testGetDivisors", "testGetSum", "testIsFound", "testGetPermutations", "testGetFib", "testBubbleSort", "testQuickSort", "testSearch"};
|
||||
std::vector<boolFn> functions {testGetPrimes, tetsGetDivisors, testGetSum, testIsFound, testGetPermutations, testGetFib,
|
||||
testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax};
|
||||
std::vector<std::string> names {"testGetPrimes", "testGetDivisors", "testGetSum", "testIsFound", "testGetPermutations", "testGetFib",
|
||||
"testBubbleSort", "testQuickSort", "testSearch", "testFindMin", "testFindMax"};
|
||||
//Start doing tests and print out the results of each
|
||||
|
||||
for(int cnt = 0;cnt < functions.size();++cnt){
|
||||
@@ -171,15 +176,161 @@ bool testGetFib(){
|
||||
}
|
||||
|
||||
bool testBubbleSort(){
|
||||
unsigned int NUM_TO_GENERATE = 10000;
|
||||
std::default_random_engine generator(std::random_device{}());
|
||||
std::uniform_int_distribution<int> dist(1, INT_MAX);
|
||||
std::vector<int> nums;
|
||||
|
||||
//Run through the appropriate number of numbers to generate and add them to the vector
|
||||
for(int cnt = 0;cnt < NUM_TO_GENERATE;++cnt){
|
||||
nums.push_back(dist(generator));
|
||||
}
|
||||
|
||||
//Sort the numbers with my algorithm
|
||||
mee::bubbleSort(nums);
|
||||
|
||||
//Make sure each number is not < the one in behind it
|
||||
for(int cnt = 1;cnt < nums.size();++cnt){
|
||||
if(nums.at(cnt) < nums.at(cnt - 1)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//If the false was not triggered then everything must have been sorted correctly
|
||||
return true;
|
||||
}
|
||||
|
||||
bool testQuickSort(){
|
||||
unsigned int NUM_TO_GENERATE = 10000;
|
||||
std::default_random_engine generator(std::random_device{}());
|
||||
std::uniform_int_distribution<int> dist(1, INT_MAX);
|
||||
std::vector<int> nums;
|
||||
|
||||
//Run through the appropriate number of numbers to generate and add them to the vector
|
||||
for(int cnt = 0;cnt < NUM_TO_GENERATE;++cnt){
|
||||
nums.push_back(dist(generator));
|
||||
}
|
||||
|
||||
//Sort the numbers with my algorithm
|
||||
mee::quickSort(nums);
|
||||
|
||||
//Make sure each number is not < the one in behind it
|
||||
for(int cnt = 1;cnt < nums.size();++cnt){
|
||||
if(nums.at(cnt) < nums.at(cnt - 1)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//If the false was not triggered then everything must have been sorted correctly
|
||||
return true;
|
||||
}
|
||||
|
||||
bool testSearch(){
|
||||
bool found = false;
|
||||
//Create a vector of numbers
|
||||
std::vector<int> nums {1, 20, 3, 40, 5, 60, 7, 80, 9};
|
||||
//Search for one that is in the vector
|
||||
found = mee::search(nums, 1);
|
||||
if(found != 0){ //This number is in the vector, if it was not found in the vector there is a problem
|
||||
return false;
|
||||
}
|
||||
|
||||
//Search for another that is in the vector
|
||||
found = mee::search(nums, 9);
|
||||
if(found != 8){ //This number is in the vector, if it was not found in the vector there is a problem
|
||||
return false;
|
||||
}
|
||||
|
||||
//Search for another that is in the vector
|
||||
found = mee::search(nums, 60);
|
||||
if(found != 5){ //This number is in the vector, if it was not found in the vector there is a problem
|
||||
return false;
|
||||
}
|
||||
|
||||
//Search for a number that is not in the vector
|
||||
found = mee::search(nums, 6);
|
||||
if(found != -1){
|
||||
return false;
|
||||
}
|
||||
|
||||
//If it didn't trigger a false then everything went through correctly
|
||||
return true;
|
||||
}
|
||||
|
||||
bool testFindMin(){
|
||||
std::vector<int> arr {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
int answer = 0;
|
||||
|
||||
//Test first
|
||||
answer = mee::findMin(arr);
|
||||
if(answer != 1){
|
||||
return false;
|
||||
}
|
||||
|
||||
//Test last
|
||||
answer = 0;
|
||||
arr = {9, 8, 7, 6, 5, 4, 3, 2, 1};
|
||||
answer = mee::findMin(arr);
|
||||
if(answer != 1){
|
||||
return false;
|
||||
}
|
||||
|
||||
//Test middle
|
||||
answer = 0;
|
||||
arr = {9, 5, 3, 6, 8, 1, 7, 2, 4};
|
||||
answer = mee::findMin(arr);
|
||||
if(answer != 1){
|
||||
return false;
|
||||
}
|
||||
|
||||
//Test negative
|
||||
answer = 0;
|
||||
arr = {-1, -2, -3, -4, -5, -6, -7, -8, -9};
|
||||
answer = mee::findMin(arr);
|
||||
if(answer != -9){
|
||||
return false;
|
||||
}
|
||||
|
||||
//If it hasn't failed a test then return true for passing all the tests
|
||||
return true;
|
||||
}
|
||||
|
||||
bool testFindMax(){
|
||||
std::vector<int> arr {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
int answer = 0;
|
||||
|
||||
//Test last
|
||||
answer = mee::findMax(arr);
|
||||
if(answer != 9){
|
||||
return false;
|
||||
}
|
||||
|
||||
//Test first
|
||||
answer = 0;
|
||||
arr = {9, 8, 7, 6, 5, 4, 3, 2, 1};
|
||||
answer = mee::findMax(arr);
|
||||
if(answer != 9){
|
||||
return false;
|
||||
}
|
||||
|
||||
//Test middle
|
||||
answer = 0;
|
||||
arr = {9, 5, 3, 6, 8, 1, 7, 2, 4};
|
||||
answer = mee::findMax(arr);
|
||||
if(answer != 9){
|
||||
return false;
|
||||
}
|
||||
|
||||
//Test negative
|
||||
answer = 0;
|
||||
arr = {-1, -2, -3, -4, -5, -6, -7, -8, -9};
|
||||
answer = mee::findMax(arr);
|
||||
if(answer != -1){
|
||||
return false;
|
||||
}
|
||||
|
||||
//If it hasn't failed a test then return true for passing all the tests
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Results:
|
||||
|
||||
Reference in New Issue
Block a user