repo update
This commit is contained in:
188
Lab2/main.cpp
188
Lab2/main.cpp
@@ -1,94 +1,94 @@
|
||||
/**
|
||||
* @file main.cpp
|
||||
* @author Trevor Barnes (barnestr@msoe.edu)
|
||||
* @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
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
*/
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "row.h"
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
// Test Setup
|
||||
int length;
|
||||
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
|
||||
Row row1(length);
|
||||
|
||||
// Test: Access Operator (non const)
|
||||
try{
|
||||
// All elements are accessed
|
||||
for(int i = 0; i < length; i++){
|
||||
cout << row1[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
// 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 / Access Operator: PASS" << endl;
|
||||
|
||||
cout << "Test - Copy Constructor / Access Operator (const)" << endl;
|
||||
// Test: Copy Constructor
|
||||
const Row row1Copy(row1);
|
||||
// Test: Access Operator (const)
|
||||
try{
|
||||
for(int i = 0; i < length; i++){
|
||||
cout << row1Copy[i] << " ";
|
||||
}
|
||||
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);
|
||||
// Set new row object equal to another row object
|
||||
row2 = row1;
|
||||
// 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; 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;
|
||||
}
|
||||
/**
|
||||
* @file main.cpp
|
||||
* @author Trevor Barnes (barnestr@msoe.edu)
|
||||
* @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
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
*/
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "row.h"
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
// Test Setup
|
||||
int length;
|
||||
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
|
||||
Row row1(length);
|
||||
|
||||
// Test: Access Operator (non const)
|
||||
try{
|
||||
// All elements are accessed
|
||||
for(int i = 0; i < length; i++){
|
||||
cout << row1[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
// 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 / Access Operator: PASS" << endl;
|
||||
|
||||
cout << "Test - Copy Constructor / Access Operator (const)" << endl;
|
||||
// Test: Copy Constructor
|
||||
const Row row1Copy(row1);
|
||||
// Test: Access Operator (const)
|
||||
try{
|
||||
for(int i = 0; i < length; i++){
|
||||
cout << row1Copy[i] << " ";
|
||||
}
|
||||
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);
|
||||
// Set new row object equal to another row object
|
||||
row2 = row1;
|
||||
// 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; 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;
|
||||
}
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
# lab2 Makefile
|
||||
|
||||
CC = g++
|
||||
CFLAGS = -c -MMD -g
|
||||
LFLAGS =
|
||||
# Change w/ every new project
|
||||
SOURCES = main.cpp row.cpp
|
||||
OBJECTS = $(SOURCES:.cpp=.o)
|
||||
# Change w/ every new project
|
||||
EXECUTABLE = Lab2
|
||||
|
||||
all: $(EXECUTABLE) $(SOURCES)
|
||||
|
||||
$(EXECUTABLE): $(OBJECTS)
|
||||
$(CC) $(LFLAGS) -o $@ $(OBJECTS)
|
||||
|
||||
-include *.d
|
||||
|
||||
%.o:%.cpp
|
||||
$(CC) $(CFLAGS) $<
|
||||
|
||||
clean:
|
||||
rm -f $(EXECUTABLE)
|
||||
rm -f $(OBJECTS)
|
||||
# lab2 Makefile
|
||||
|
||||
CC = g++
|
||||
CFLAGS = -c -MMD -g
|
||||
LFLAGS =
|
||||
# Change w/ every new project
|
||||
SOURCES = main.cpp row.cpp
|
||||
OBJECTS = $(SOURCES:.cpp=.o)
|
||||
# Change w/ every new project
|
||||
EXECUTABLE = Lab2
|
||||
|
||||
all: $(EXECUTABLE) $(SOURCES)
|
||||
|
||||
$(EXECUTABLE): $(OBJECTS)
|
||||
$(CC) $(LFLAGS) -o $@ $(OBJECTS)
|
||||
|
||||
-include *.d
|
||||
|
||||
%.o:%.cpp
|
||||
$(CC) $(CFLAGS) $<
|
||||
|
||||
clean:
|
||||
rm -f $(EXECUTABLE)
|
||||
rm -f $(OBJECTS)
|
||||
rm -f *.d
|
||||
102
Lab2/row.h
102
Lab2/row.h
@@ -1,51 +1,51 @@
|
||||
#ifndef row_h
|
||||
#define row_h
|
||||
class Row{
|
||||
public:
|
||||
/* Parameterized constructor
|
||||
* Takes in length and creates a row matrix with values cleared
|
||||
* to zero
|
||||
*/
|
||||
Row(unsigned int length);
|
||||
|
||||
/* Copy constructor
|
||||
* Create a new row matrix with the same size and values as the
|
||||
* from matrix
|
||||
*/
|
||||
Row(const Row& from);
|
||||
|
||||
/* Destructor
|
||||
* Correctly delete any heap memory
|
||||
*/
|
||||
~Row();
|
||||
|
||||
/* Access operator (const version)
|
||||
* Allow access to row matrix data
|
||||
* Should return an exception if column is too large
|
||||
*/
|
||||
double operator[](unsigned int column) const;
|
||||
|
||||
/* Access operator (non const version)
|
||||
* Allow access to row matrix data
|
||||
* Should return an exception if column is too large
|
||||
*/
|
||||
double& operator[] (unsigned int column);
|
||||
|
||||
/* Assignment operator
|
||||
* 1. Check if two sides are the same object
|
||||
* 2. Delete the current row matrix
|
||||
* 3. Create a new row matrix with the same size and values as
|
||||
* the rhs matrix
|
||||
*/
|
||||
Row& operator= (const Row& rhs);
|
||||
|
||||
/* Clear all data values to zero
|
||||
*/
|
||||
void clear();
|
||||
private:
|
||||
// Row matrix data
|
||||
double * row_data;
|
||||
// Size of row matrix
|
||||
unsigned int length;
|
||||
};
|
||||
#endif
|
||||
#ifndef row_h
|
||||
#define row_h
|
||||
class Row{
|
||||
public:
|
||||
/* Parameterized constructor
|
||||
* Takes in length and creates a row matrix with values cleared
|
||||
* to zero
|
||||
*/
|
||||
Row(unsigned int length);
|
||||
|
||||
/* Copy constructor
|
||||
* Create a new row matrix with the same size and values as the
|
||||
* from matrix
|
||||
*/
|
||||
Row(const Row& from);
|
||||
|
||||
/* Destructor
|
||||
* Correctly delete any heap memory
|
||||
*/
|
||||
~Row();
|
||||
|
||||
/* Access operator (const version)
|
||||
* Allow access to row matrix data
|
||||
* Should return an exception if column is too large
|
||||
*/
|
||||
double operator[](unsigned int column) const;
|
||||
|
||||
/* Access operator (non const version)
|
||||
* Allow access to row matrix data
|
||||
* Should return an exception if column is too large
|
||||
*/
|
||||
double& operator[] (unsigned int column);
|
||||
|
||||
/* Assignment operator
|
||||
* 1. Check if two sides are the same object
|
||||
* 2. Delete the current row matrix
|
||||
* 3. Create a new row matrix with the same size and values as
|
||||
* the rhs matrix
|
||||
*/
|
||||
Row& operator= (const Row& rhs);
|
||||
|
||||
/* Clear all data values to zero
|
||||
*/
|
||||
void clear();
|
||||
private:
|
||||
// Row matrix data
|
||||
double * row_data;
|
||||
// Size of row matrix
|
||||
unsigned int length;
|
||||
};
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user