lab2 submission ready

This commit is contained in:
2022-03-22 00:13:29 -05:00
parent 9b358c7bc0
commit ae7af06af6
3 changed files with 52 additions and 24 deletions

BIN
Lab2/lab2

Binary file not shown.

View File

@@ -1,7 +1,11 @@
/**
* @file main.cpp
* @author Trevor Barnes (barnestr@msoe.edu)
* @brief
* @brief A main driver that provides some rudimentary tests for row.cpp
* Tests involving memory leaks were tested using valgrind via the command:
* `valgrind ./lab2` Test output was left in submission to signify modules
* that have successfully passed and provide further clarity of testing
* methods.
* @version 1.0
* @date 2022-03-22
*
@@ -16,8 +20,11 @@ using namespace std;
int main() {
// Test Setup
int length;
cout << "Enter the desired length for the matrix constructor test:" << endl;
double testValue;
cout << "Enter the desired length for the row matrix:" << endl;
cin >> length;
cout << "Enter a double value to test as row data element:" << endl;
cin >> testValue;
cout << "Test - Constructor / Access Operator" << endl;
// Test: Constructor
@@ -25,49 +32,63 @@ int main() {
// Test: Access Operator (non const)
try{
// All elements are accessed
for(int i = 0; i < length; i++){
cout << row1[i] << " ";
}
cout << endl;
cout << row1[1] << endl;
//row1[1] = 12.18;
cout << row1[1] << endl;
cout << row1[length+1] << endl; // Might change
}catch(out_of_range){
// First element shown as 0
cout << row1[0] << endl;
// First element is assigned the test value
row1[0] = testValue;
// First element is now the test value
cout << row1[0] << endl;
// Throws OOB Exception
cout << row1[length+1] << endl;
}catch(const out_of_range e){
cout << "Out of Range Exception Successfully Caught" << endl;
}
cout << "Test - Constructor/AccessOperator: PASS" << endl;
cout << "Test - Constructor / Access Operator: PASS" << endl;
cout << "Test - Copy Constructor/AccessOperator(const)" << endl;
cout << "Test - Copy Constructor / Access Operator (const)" << endl;
// Test: Copy Constructor
const Row row1Copy(row1);
cout << "before seg";
// Test: Access Operator (const)
try{
for(int i = 0; i < length; i++){
cout << row1Copy[i] << " ";
}
cout << row1Copy[1];
cout << row1Copy[length+1]; // Might change
}catch(out_of_range){
cout << endl;
// First element is the same as the original Row object's
cout << row1Copy[0] << endl;
// Throws OOB Exception
cout << row1Copy[length+1];
}catch(const out_of_range e){
cout << "Out of Range Exception Successfully Caught" << endl;
}
cout << "Test - Copy Constructor / Access Operator (const): PASS" << endl;
// Test: Assignment Operator
Row row2(length*2);
Row row2(length);
// Set new row object equal to another row object
row2 = row1;
for(int i = 0; i < length*2; i++){
// Show that the elements are identical to the original row object
for(int i = 0; i < length; i++){
cout << row2[i] << " ";
}
cout << endl;
// Test: Clear
cout << "Test - Clear" << endl;
row2.clear();
for(int i = 0; i < length*2; i++){
for(int i = 0; i < length; i++){
cout << row2[i] << " ";
}
cout << endl;
cout << "Test - Clear: PASS" << endl;
// Tested for memory leaks using valgrind:
// Result: "All heap blocks were freed -- no leaks are possible"
return 0;
}

View File

@@ -1,4 +1,15 @@
#include<iostream>
/**
* @file row.cpp
* @author Trevor Barnes (barnestr@msoe.edu)
* @brief Contains the main functionality for row matrices utilizing arrays of
* double values
* @version 1.0
* @date 2022-03-22
*
* @copyright Copyright (c) 2022
*
*/
#include <stdexcept>
#include "row.h"
using namespace std;
@@ -18,21 +29,17 @@ Row::Row(const Row& from){
// New row matrix gets length from previous matrix
this->length = from.length;
// Create new array in heap with new length
this->row_data - new double[this->length];
this->row_data = new double[this->length];
// Copy all row_data values over to new array
for(int i = 0; i < this->length; i++) {
cout << "before seg fault" << endl;
cout << this->row_data[i] << endl;
this->row_data[i] = from[i];
}
// finish
}
// destructor
Row::~Row(){
cout << "-Row Destructor-" << endl; // TODO: temp
// Check for valid length then free the heap memory
if(this->length > 0 ) {
if(length > 0 ) {
delete[] row_data;
}
}
@@ -63,7 +70,7 @@ Row& Row::operator= (const Row& rhs){
// New row matrix gets length from previous matrix
this->length = rhs.length;
// Create new array in heap with new length
this->row_data - new double[this->length];
this->row_data = new double[this->length];
// Copy all row_data values over to new array
for(int i = 0; i < this->length; i ++) {
this->row_data[i] = rhs.row_data[i];