lab2 submission ready
This commit is contained in:
@@ -1,7 +1,11 @@
|
|||||||
/**
|
/**
|
||||||
* @file main.cpp
|
* @file main.cpp
|
||||||
* @author Trevor Barnes (barnestr@msoe.edu)
|
* @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
|
* @version 1.0
|
||||||
* @date 2022-03-22
|
* @date 2022-03-22
|
||||||
*
|
*
|
||||||
@@ -16,8 +20,11 @@ using namespace std;
|
|||||||
int main() {
|
int main() {
|
||||||
// Test Setup
|
// Test Setup
|
||||||
int length;
|
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;
|
cin >> length;
|
||||||
|
cout << "Enter a double value to test as row data element:" << endl;
|
||||||
|
cin >> testValue;
|
||||||
|
|
||||||
cout << "Test - Constructor / Access Operator" << endl;
|
cout << "Test - Constructor / Access Operator" << endl;
|
||||||
// Test: Constructor
|
// Test: Constructor
|
||||||
@@ -25,49 +32,63 @@ int main() {
|
|||||||
|
|
||||||
// Test: Access Operator (non const)
|
// Test: Access Operator (non const)
|
||||||
try{
|
try{
|
||||||
|
// All elements are accessed
|
||||||
for(int i = 0; i < length; i++){
|
for(int i = 0; i < length; i++){
|
||||||
cout << row1[i] << " ";
|
cout << row1[i] << " ";
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
cout << row1[1] << endl;
|
// First element shown as 0
|
||||||
//row1[1] = 12.18;
|
cout << row1[0] << endl;
|
||||||
cout << row1[1] << endl;
|
// First element is assigned the test value
|
||||||
cout << row1[length+1] << endl; // Might change
|
row1[0] = testValue;
|
||||||
}catch(out_of_range){
|
// 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 << "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
|
// Test: Copy Constructor
|
||||||
const Row row1Copy(row1);
|
const Row row1Copy(row1);
|
||||||
cout << "before seg";
|
|
||||||
// Test: Access Operator (const)
|
// Test: Access Operator (const)
|
||||||
try{
|
try{
|
||||||
for(int i = 0; i < length; i++){
|
for(int i = 0; i < length; i++){
|
||||||
cout << row1Copy[i] << " ";
|
cout << row1Copy[i] << " ";
|
||||||
}
|
}
|
||||||
cout << row1Copy[1];
|
cout << endl;
|
||||||
cout << row1Copy[length+1]; // Might change
|
// First element is the same as the original Row object's
|
||||||
}catch(out_of_range){
|
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 << "Out of Range Exception Successfully Caught" << endl;
|
||||||
}
|
}
|
||||||
cout << "Test - Copy Constructor / Access Operator (const): PASS" << endl;
|
cout << "Test - Copy Constructor / Access Operator (const): PASS" << endl;
|
||||||
|
|
||||||
// Test: Assignment Operator
|
// Test: Assignment Operator
|
||||||
Row row2(length*2);
|
Row row2(length);
|
||||||
|
// Set new row object equal to another row object
|
||||||
row2 = row1;
|
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 << row2[i] << " ";
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
|
|
||||||
// Test: Clear
|
// Test: Clear
|
||||||
|
cout << "Test - Clear" << endl;
|
||||||
row2.clear();
|
row2.clear();
|
||||||
for(int i = 0; i < length*2; i++){
|
for(int i = 0; i < length; i++){
|
||||||
cout << row2[i] << " ";
|
cout << row2[i] << " ";
|
||||||
}
|
}
|
||||||
cout << endl;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
23
Lab2/row.cpp
23
Lab2/row.cpp
@@ -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"
|
#include "row.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -18,21 +29,17 @@ Row::Row(const Row& from){
|
|||||||
// New row matrix gets length from previous matrix
|
// New row matrix gets length from previous matrix
|
||||||
this->length = from.length;
|
this->length = from.length;
|
||||||
// Create new array in heap with new 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
|
// Copy all row_data values over to new array
|
||||||
for(int i = 0; i < this->length; i++) {
|
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];
|
this->row_data[i] = from[i];
|
||||||
}
|
}
|
||||||
// finish
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// destructor
|
// destructor
|
||||||
Row::~Row(){
|
Row::~Row(){
|
||||||
cout << "-Row Destructor-" << endl; // TODO: temp
|
|
||||||
// Check for valid length then free the heap memory
|
// Check for valid length then free the heap memory
|
||||||
if(this->length > 0 ) {
|
if(length > 0 ) {
|
||||||
delete[] row_data;
|
delete[] row_data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,7 +70,7 @@ Row& Row::operator= (const Row& rhs){
|
|||||||
// New row matrix gets length from previous matrix
|
// New row matrix gets length from previous matrix
|
||||||
this->length = rhs.length;
|
this->length = rhs.length;
|
||||||
// Create new array in heap with new 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
|
// Copy all row_data values over to new array
|
||||||
for(int i = 0; i < this->length; i ++) {
|
for(int i = 0; i < this->length; i ++) {
|
||||||
this->row_data[i] = rhs.row_data[i];
|
this->row_data[i] = rhs.row_data[i];
|
||||||
|
|||||||
Reference in New Issue
Block a user