Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
1. Solve the coding errors for upvote
make test-stats
g++ -g -std=c++14 -Wall -Werror=return-type -Werror=uninitialized -Wno-sign-compare -o test-
stats stats.o week-data.o test/catch/catch.o test/test-stats.o
Undefined symbols for architecture x86_64:
"stDev(WeekData*, int)", referenced from:
C_A_T_C_H_T_E_S_T_2() in test-stats.o
C_A_T_C_H_T_E_S_T_4() in test-stats.o
"getMean(WeekData*, int)", referenced from:
C_A_T_C_H_T_E_S_T_0() in test-stats.o
C_A_T_C_H_T_E_S_T_4() in test-stats.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test-stats] Error 1
make test-state
g++ -g -std=c++14 -Wall -Werror=return-type -Werror=uninitialized -Wno-sign-compare -c -o
state.o state.cpp
state.cpp:65:12: error: no member named 'stDev' in 'State'; did you mean simply 'stDev'?
return State::stDev(weeks, numberOfDataPoints);
^~~~~~~~~~~~
stDev
./stats.hpp:5:19: note: 'stDev' declared here
static double stDev(WeekData* weeks, int count);
^
1 error generated.
make: *** [state.o] Error 1
make test-morbidity
g++ -g -std=c++14 -Wall -Werror=return-type -Werror=uninitialized -Wno-sign-compare -c -o
state.o state.cpp
state.cpp:65:12: error: no member named 'stDev' in 'State'; did you mean simply 'stDev'?
return State::stDev(weeks, numberOfDataPoints);
^~~~~~~~~~~~
stDev
./stats.hpp:5:19: note: 'stDev' declared here
static double stDev(WeekData* weeks, int count);
3. #include "../state.hpp"
#include "catch/catch.hpp"
TEST_CASE("Testing State class on calculation") {
State bama("Alabama");
CHECK("Alabama" == bama.getName());
bama.addWeek("2020-05-02,2000");
bama.addWeek("2020-05-09,2000");
bama.addWeek("2020-05-16,100");
bama.addWeek("2020-05-23,2000");
bama.addWeek("2020-05-30,2000");
bama.addWeek("2020-06-06,2000");
CHECK(Approx(1683.33).epsilon(0.01) == bama.getMeanDeaths());
CHECK(Approx(708.08).epsilon(0.01) == bama.getStDev());
CHECK(1 == bama.getOutlierCount());
auto outliers = bama.getOutlierInfos();
CHECK("2020-05-16 - total deaths: 100" == outliers.at(0));
}
TEST_CASE("Testing State class on rule of three") {
State bama("Alabama");
CHECK("Alabama" == bama.getName());
State bama1(bama);
CHECK("Alabama" == bama1.getName());
State bama2;
bama2 = bama;
4. CHECK("Alabama" == bama2.getName());
}
test-stats.cpp
#include "catch/catch.hpp"
#include "../stats.hpp"
#include "../week-data.hpp"
WeekData data[] = {
WeekData("1776-07-04,1"), // Just some static test data.
WeekData("1776-07-11,2"), // The dates are not important
WeekData("1776-07-18,3"), // since we are just trying to test the
WeekData("1776-07-25,5"), // statistical calculations
WeekData("1776-08-02,6")
};
TEST_CASE("Testing Mean")
{
// Comparing doubles, can use Approx() to allow some epsilon difference
CHECK(Approx(3.4) == getMean(data, 5));
}
TEST_CASE("Testing Stdev")
{
CHECK(Approx(1.85).epsilon(0.01) == stDev(data, 5));
}
TEST_CASE("Testing empty list")
{
// let's call the getMean of no numbers: 0
CHECK(Approx(0) == getMean({}, 0));
CHECK(Approx(0) == stDev({}, 0));
5. }
// Compile & run:
// make clean test
morbidity.cpp
#include "morbidity.hpp"
#include "state.hpp"
#include
#include
Morbidity::Morbidity() : numberOfStates(0), states(nullptr) {
}
Morbidity::~Morbidity() {
for (int i = 0; i < numberOfStates; i++) {
delete states[i];
}
delete[] states;
}
bool Morbidity::load(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
return false;
}
6. std::string line;
std::string stateName;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string state, weekData;
std::getline(iss, state, ',');
std::getline(iss, weekData);
if (state != stateName) {
stateName = state;
State* newState = new State(stateName);
if (numberOfStates < 65) {
if (states == nullptr) {
states = new State*[65];
}
states[numberOfStates] = newState;
numberOfStates++;
}
}
try {
int deaths = std::stoi(weekData);
if (states != nullptr) {
states[numberOfStates - 1]->addWeek(weekData);
}
} catch (const std::invalid_argument& e) {
7. }
}
file.close();
return true;
}
double Morbidity::getMean(const std::string& stateName) const {
for (int i = 0; i < numberOfStates; i++) {
if (states[i]->getName() == stateName) {
return states[i]->getMeanDeaths();
}
}
return -1.0;
}
int Morbidity::getStateOutlierCount(const std::string& stateName) const {
for (int i = 0; i < numberOfStates; i++) {
if (states[i]->getName() == stateName) {
return states[i]->getOutlierCount();
}
}
return 0;
}
std::vector Morbidity::getStateOutliers(const std::string& stateName) const {
for (int i = 0; i < numberOfStates; i++) {
if (states[i]->getName() == stateName) {
return states[i]->getOutlierInfos();