diff --git a/Lab2/lab2 b/Lab2/lab2 deleted file mode 100755 index 014852c..0000000 Binary files a/Lab2/lab2 and /dev/null differ diff --git a/Lab2/main.cpp b/Lab2/main.cpp index 31b21ef..87c6dda 100644 --- a/Lab2/main.cpp +++ b/Lab2/main.cpp @@ -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; } diff --git a/Lab2/row.cpp b/Lab2/row.cpp index f3c5fd2..8042ca2 100644 --- a/Lab2/row.cpp +++ b/Lab2/row.cpp @@ -1,4 +1,15 @@ -#include +/** + * @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 #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];