mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-11 06:26:05 +08:00
Added test function
This commit is contained in:
committed by
GitHub
parent
759313d0ce
commit
20aaca07a9
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <vector>
|
||||
#include<iostream>
|
||||
#include <cassert>
|
||||
|
||||
//Implements the algorithm
|
||||
/*We name the indices good and bad based on whether we can reach the destination if we start at that position.
|
||||
@@ -19,7 +20,7 @@
|
||||
*After the end of the loop, if we reach the lastPos as 0, then the destination can be reached from the start position.
|
||||
*/
|
||||
/**
|
||||
* This function implements the above algorithm
|
||||
* @brief This function implements the above algorithm
|
||||
* @param vector of nums containing the maximum jump (in steps) from that index
|
||||
* @return returns bool value whether final index can be reached or not
|
||||
*/
|
||||
@@ -33,13 +34,35 @@ bool canJump(std::vector<int> nums) {
|
||||
return lastPos == 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Function to test above algorithm
|
||||
*/
|
||||
void test(){
|
||||
//Test 1
|
||||
std::vector<int> num1={4,3,1,0,5};
|
||||
assert(canJump(num1)==true);
|
||||
std::cout<<"Input: ";
|
||||
for(auto i: num1){
|
||||
std::cout<<i<<" ";
|
||||
}
|
||||
std::cout<<"Output: true"<<std::endl;
|
||||
//Test 2
|
||||
std::vector<int> num2={3,2,1,0,4};
|
||||
assert(canJump(num2)==false);
|
||||
std::cout<<"Input: ";
|
||||
for(auto i: num2){
|
||||
std::cout<<i<<" ";
|
||||
}
|
||||
std::cout<<"Output: false"<<std::endl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main(){
|
||||
//Sample test case
|
||||
std::vector<int> num={4,3,1,0,5};
|
||||
std::cout<<canJump(num); //Should display true, as when you take one step from position 0, you reach position 1, from which 3 steps lead you to the destination
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user