Answered step by step
Verified Expert Solution
Question
1 Approved Answer
/* Sandbox.cpp file*/ #include #include IntVec.h void doNothing(IntVec a){ a.push_back(5); } using namespace std; int main(int argc, char* argv[]){ // Use this program to test
/* Sandbox.cpp file*/
#include
#include "IntVec.h"
void doNothing(IntVec a){
a.push_back(5);
}
using namespace std;
int main(int argc, char* argv[]){
// Use this program to test your IntVec implementation!
IntVec a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
IntVec b;
b.push_back(2);
b.push_back(3);
b.push_back(4);
// Used to test the copy constructor
doNothing(a);
// These should be the same if the copy constructor is working
// correctly.
cout
cout
cout
cout
return 0;
}
/* @file IntVec.cpp
@author
@date
@description Implements vector-like container for integers.
*/
#include
#include
#include
#include
#include
#include
#include "IntVec.h"
const int MAX_LENGTH = 100;
IntVec::IntVec(){
head = new int[MAX_LENGTH];
my_size = 0;
my_capacity = MAX_LENGTH;
}
IntVec::IntVec(const IntVec& other){
head = new int[MAX_LENGTH];
my_size = 0;
my_capacity = MAX_LENGTH;
for(unsigned int i = 0; i
push_back(other.head[i]);
}
}
IntVec& IntVec::operator=(const IntVec& other){
if( this == &other){
return *this;
}
delete[] head;
head = NULL;
head = new int[MAX_LENGTH];
my_size = 0;
my_capacity = MAX_LENGTH;
for(unsigned int i = 0; i
push_back(other.head[i]);
}
return *this;
}
IntVec::~IntVec(){
delete[] head;
head = NULL;
}
unsigned int IntVec::size() const{
return my_size;
}
void IntVec::push_back(int val){
// Do we have space?
if( my_capacity > my_size){
head[my_size] = val;
my_size++;
return;
}
throw logic_error("Out of space");
}
string IntVec::getAsString() const{
if(my_size == 0){
return "[]";
}
string ret = "[";
for(unsigned int i = 0; i
// Use a stringstream to convert int to string
stringstream out;
out
ret = ret + out.str() + ", ";
}
// tack on the end without a comma
stringstream out;
out
ret = ret + out.str();
ret = ret + "]";
return ret;
}
void IntVec::clear(){
delete[] head;
head = new int[MAX_LENGTH];
my_size = 0;
my_capacity = MAX_LENGTH;
}
long IntVec::sum() const{
long ret = 0;
for(unsigned int i = 0; i
ret = ret + head[i];
}
return ret;
}
int IntVec::getAt(int loc) const{
if(loc
loc = (int)my_size + loc; // Adding a negative
}
if(loc >= (int)my_size || loc
throw logic_error("Location not valid");
}
return head[loc];
}
void IntVec::setAt(int val, int loc){
if(loc
loc = (int)my_size + loc; // Adding a negative
}
if(loc >= (int)my_size || loc
throw logic_error("Location not valid");
}
head[loc] = val;
}
bool IntVec::operator==(const IntVec& rhs) const{
if(my_size != rhs.my_size){
return false;
}
for(unsigned int i = 0; i
if(head[i] != rhs.head[i]){
return false;
}
}
return true;
}
/***********************************************/
#ifndef INTVEC_H
#define INTVEC_H
/** @file IntVec.h
@author
@date March 27, 2017
@version 0.02
This IntVec class implements a vector-like class that stores integers.
It also can return statistics about the data and return new IntVec's
based on arithmetic operations on them.
This class may not use vector!
*/
#include
using namespace std;
/**
* Storage class able to dynamically store integer arrays
*/
class IntVec{
public:
/**
* Default constructor initializes to an empty array
*/
IntVec();
/**
* Copy constructor
*/
IntVec(const IntVec& other);
/**
* Assignment operator
*/
IntVec& operator=(const IntVec& other);
/**
* Class destructor
*/
~IntVec();
/**
* Push the value on the end of the array.
*/
void push_back(int value);
/**
* Returns the value at position loc in the array, or an exception if
* it isn't a valid location.
* Negateive locations are allowed, are are defined as from the end.
* -1 is the last element, -2 second to last, etc...
*/
int getAt(int loc) const;
/**
* Put val in loc position. Throws an exception if loc isn't a valid
* location. Negatives allowed, see getAt.
*/
void setAt(int val, int loc);
/**
* Return the number of elements stored in IntVec
*/
unsigned int size() const;
/**
* Removes all values in the current IntVec
*/
void clear();
/**
* Return the sum or the values in the IntVec
*/
long sum() const;
/**
* Returns a string of this IntVec with the format:
* [1, 2, 3, .. 4] The last value should not have a trailing comma.
*/
string getAsString() const;
/**
* Returns true if both IntVec(s) contain the same values, in the same
* order, false otherwise. False if their sizes differ.
*/
bool operator==(const IntVec& rhs) const;
private:
int* head;
unsigned int my_size;
unsigned int my_capacity;
void preAllocate(unsigned int cap);
};
#endif
/* @file IntVec_test.cpp
@author
@date
@description Tests for the IntVec class
*/
#include "gtest/gtest.h"
#include "IntVec.h"
// Copy constructor
TEST(copyconstruct_getAt, notThere2){
IntVec a;
a.push_back(5);
a.push_back(-6);
IntVec b(a);
ASSERT_ANY_THROW(b.getAt(2));
EXPECT_EQ(b.getAt(0), 5);
EXPECT_EQ(b.getAt(1), -6);
EXPECT_EQ(b.size(), 2);
}
TEST(copyconstruct_getAt, 1){
IntVec a;
for(int i = 0; i
a.push_back(i);
IntVec b(a);
EXPECT_EQ(b.getAt(i), i);
EXPECT_EQ(b.size(), i+1);
}
}
TEST(copyconstruct_getAt, copy_ptr1){
IntVec a;
a.push_back(5);
a.push_back(-6);
IntVec b(a);
EXPECT_EQ(b.getAt(0), 5);
EXPECT_EQ(b.getAt(1), -6);
a.push_back(7);
EXPECT_EQ(a.getAt(2), 7);
ASSERT_ANY_THROW(b.getAt(2));
}
TEST(copyconstruct_getAt, copy_ptr2){
IntVec a;
a.push_back(5);
a.push_back(-6);
IntVec b(a);
EXPECT_EQ(b.getAt(0), 5);
EXPECT_EQ(b.getAt(1), -6);
b.push_back(7);
EXPECT_EQ(b.getAt(2), 7);
ASSERT_ANY_THROW(a.getAt(2));
}
// Assignment
TEST(assignment_getAt, notThere2){
IntVec a;
a.push_back(5);
a.push_back(-6);
IntVec b;
b.push_back(12345);
b = a;
ASSERT_ANY_THROW(b.getAt(2));
EXPECT_EQ(b.getAt(0), 5);
EXPECT_EQ(b.getAt(1), -6);
EXPECT_EQ(b.size(), 2);
}
TEST(assignment_getAt, 3){
IntVec a;
a.push_back(9);
a = a;
EXPECT_EQ(a.getAt(0), 9);
}
// Size
TEST(size, 1){
IntVec a;
EXPECT_EQ(a.size(), 0);
}
TEST(size, 2){
IntVec a;
a.push_back(5);
EXPECT_EQ(a.size(), 1);
}
TEST(size, 3){
IntVec a;
for(int i = 0; i
a.push_back(i);
}
EXPECT_EQ(a.size(), 100);
}
TEST(size, 4){
IntVec a;
for(int i = 0; i
a.push_back(i);
EXPECT_EQ(a.size(), i + 1);
}
}
TEST(size, 5){
IntVec a;
for(int i = 0; i
a.push_back(i);
}
ASSERT_ANY_THROW(a.push_back(5));
EXPECT_EQ(a.size(), 100);
}
// Pushback/getAt
TEST(push_getAt, 1){
IntVec a;
a.push_back(5);
EXPECT_EQ(a.getAt(0), 5);
}
TEST(push_getAt, neg){
IntVec a;
a.push_back(5);
EXPECT_EQ(a.getAt(-1), 5);
}
TEST(push_getAt, notThere){
IntVec a;
ASSERT_ANY_THROW(a.getAt(0));
}
TEST(push_getAt, notThereNeg){
IntVec a;
ASSERT_ANY_THROW(a.getAt(-1));
}
TEST(push_getAt, 2){
IntVec a;
a.push_back(5);
a.push_back(-6);
EXPECT_EQ(a.getAt(1), -6);
}
TEST(push_getAt, neg2){
IntVec a;
a.push_back(5);
a.push_back(-6);
EXPECT_EQ(a.getAt(-1), -6);
EXPECT_EQ(a.getAt(-2), 5);
}
TEST(push_getAt, notThere2){
IntVec a;
a.push_back(5);
a.push_back(-6);
ASSERT_ANY_THROW(a.getAt(2));
}
TEST(push_getAt, notThereNeg2){
IntVec a;
a.push_back(5);
a.push_back(-6);
ASSERT_ANY_THROW(a.getAt(-3));
}
TEST(push_getAt, 3){
IntVec a;
for(int i = 0; i
a.push_back(i);
EXPECT_EQ(a.getAt(i), i);
EXPECT_EQ(a.getAt(-1), i);
}
}
// Set at
TEST(setAt, notThere){
IntVec a;
ASSERT_ANY_THROW(a.setAt(-5,0));
ASSERT_ANY_THROW(a.getAt(-1));
ASSERT_ANY_THROW(a.getAt(1));
}
TEST(setAt, 1pos){
IntVec a;
a.push_back(10);
a.setAt(-5,0);
ASSERT_ANY_THROW(a.setAt(-6,1));
EXPECT_EQ(a.getAt(0), -5);
EXPECT_EQ(a.getAt(-1), -5);
ASSERT_ANY_THROW(a.setAt(-3,2));
}
TEST(setAt, big){
IntVec a;
for(int i = 0; i
a.push_back(i);
EXPECT_EQ(a.getAt(i), i);
EXPECT_EQ(a.getAt(-1), i);
}
for(int i = 0; i
a.setAt(i+1,i);
EXPECT_EQ(a.getAt(i), i+1);
}
}
// clear
TEST(clear, 0){
IntVec a;
a.clear();
EXPECT_EQ(a.size(), 0);
}
TEST(clear, 1){
IntVec a;
a.push_back(5);
EXPECT_EQ(a.size(), 1);
a.clear();
EXPECT_EQ(a.size(), 0);
}
TEST(clear, 2){
IntVec a;
a.push_back(6);
a.push_back(7);
EXPECT_EQ(a.size(), 2);
a.clear();
EXPECT_EQ(a.size(), 0);
}
// Sum
TEST(sum, 0){
IntVec a;
EXPECT_EQ(a.sum(), 0);
}
TEST(sum, 1){
IntVec a;
a.push_back(5);
EXPECT_EQ(a.sum(), 5);
}
TEST(sum, 2){
IntVec a;
int sum = 0;
for(int i = 0; i
The objective of this homework is to understand how the IntVec class works and to convert into a templated class Task 1: Download all source and object code to your own machine and verify they work: Go to this folder and download ALL files. Again, do NOT copy and paste, but download source: 1. 2. Once the files are downloaded, type make and Intvecsaoox Should be created. Typing make test should compile and run the single test in IntVec test.cpp. If this does not work, please let me know ASAP Task 2: Learn the IntVec class by reading its unit tests and add add GetMax) and Get Min functions We've talked in class about the benefits of testing your code, but one that we haven't discussed, and is often overlooked is that it provides a great starting point for understanding new code. Read each unit test to see the expected behaviour of the Int Vec class, try to follow the execution and understand why each test passes. Then add the two new member functions 1. int GetMax( should return the largest int in the vector 2. int GetMin) should return the smallest int in the vector 3. Add 3 unit tests for each to prove your new methods work correctly Task 3: Modify the Int Vec class to be templated Change every reference to the value type from int to the template, so it can be The objective of this homework is to understand how the IntVec class works and to convert into a templated class Task 1: Download all source and object code to your own machine and verify they work: Go to this folder and download ALL files. Again, do NOT copy and paste, but download source: 1. 2. Once the files are downloaded, type make and Intvecsaoox Should be created. Typing make test should compile and run the single test in IntVec test.cpp. If this does not work, please let me know ASAP Task 2: Learn the IntVec class by reading its unit tests and add add GetMax) and Get Min functions We've talked in class about the benefits of testing your code, but one that we haven't discussed, and is often overlooked is that it provides a great starting point for understanding new code. Read each unit test to see the expected behaviour of the Int Vec class, try to follow the execution and understand why each test passes. Then add the two new member functions 1. int GetMax( should return the largest int in the vector 2. int GetMin) should return the smallest int in the vector 3. Add 3 unit tests for each to prove your new methods work correctly Task 3: Modify the Int Vec class to be templated Change every reference to the value type from int to the template, so it can be a.push_back( i * 14583);
sum += 14583 * i;
}
EXPECT_EQ(a.sum(), sum);
}
// Get as string
TEST(getAsString, 0){
IntVec a;
EXPECT_EQ(a.getAsString(), "[]");
}
TEST(getAsString, 1){
IntVec a;
a.push_back(5);
EXPECT_EQ(a.getAsString(), "[5]");
}
TEST(getAsString, 2){
IntVec a;
a.push_back(5);
a.push_back(6);
EXPECT_EQ(a.getAsString(), "[5, 6]");
}
TEST(getAsString, 3){
IntVec a;
a.push_back(5);
a.push_back(6);
a.push_back(10);
EXPECT_EQ(a.getAsString(), "[5, 6, 10]");
}
// ==
TEST(equality, 0){
IntVec a;
IntVec b;
EXPECT_EQ(a == b, true);
}
TEST(equality, 1){
IntVec a;
a.push_back(5);
IntVec b;
EXPECT_EQ(a == b, false);
}
TEST(equality, 2){
IntVec a;
IntVec b;
b.push_back(6);
EXPECT_EQ(a == b, false);
EXPECT_EQ(b == a, false);
}
TEST(equality, 3){
IntVec a;
a.push_back(6);
IntVec b;
b.push_back(6);
EXPECT_EQ(a == b, true);
EXPECT_EQ(b == a, true);
}
TEST(equality, 4){
IntVec a;
a.push_back(5);
IntVec b;
b.push_back(6);
EXPECT_EQ(a == b, false);
EXPECT_EQ(b == a, false);
}
TEST(equality, 5){
IntVec a;
a.push_back(5);
a.push_back(6);
IntVec b;
b.push_back(5);
b.push_back(6);
EXPECT_EQ(a == b, true);
EXPECT_EQ(b == a, true);
}
TEST(equality, 6){
IntVec a;
a.push_back(6);
a.push_back(5);
IntVec b;
b.push_back(5);
b.push_back(6);
EXPECT_EQ(a == b, false);
EXPECT_EQ(b == a, false);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started