Question

1. Write CppUnitLite tests to verify correct behavior for all the exercises. Using C++ 2. Please...

1. Write CppUnitLite tests to verify correct behavior for all the exercises. Using C++

2. Please show all outputs.

Write functions to add one day, another function to add one month, and yet another function to add one year to a Date struct.

struct Date
{
    int year;
    int month;
    int day;
};

Pass Dates by reference when appropriate (i.e., Date& or const Date&). For example, the following function returns by value a new Date instance with one day added to the passed in date.

Date addOneDay(const Date& date);

Create a C++ header file named write.h which contains function prototypes for three functions named write. Write the implementations for each write function in a file named write.cpp. Each write function takes two arguments. The first argument is always std::ostream& os. The second arguments are an int, a float, and a std::string respectively. Each write function should stream its second argument to the passed in std::ostream. Write cppunitlite unit tests that pass a std::stringstream as the first argument to each function and verify its operation. Write non unit test code that calls each write function and passes std::cout as the first argument (the cout tests are written outside the unit test framework because verification can't easily be automated). Here's the prototype for the first write overload:

void write(std::ostream& os, int value);

Notice that both std::stringstream and std::cout may be passed as the first argument. Both inherit from std::ostream and thus may be used where ever a std::ostream& is used. This is our first use of inheritance in C++. We'll do much more with inheritance as the course progresses.

Write a lambda function which makes the following TEST pass:

TEST(lambdaTestProblem, lambdas)
{
    auto values = { 2, 4, 6, 8, 10, 12 };
    auto sum = 0;

    std::for_each(values.begin(), values.end(), /*define lambda function here*/);

    CHECK_EQUAL(42, sum);
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

dateTest.cpp

// Assignment 3-1
// Write functions to add one day, another function to add one month, and yet another
// function to add one year to a Date struct. Write a function that gives the day of
// the week for a given Date. Write a function that returns the Date of the first Monday
// following a given Date. Return the date by value. Pass Dates by reference when
// appropriate (i.e., Date& or const Date&).

#include "TestHarness.h"
#include <iostream>
#include <string>

struct Date
{
    int year;
    int month;
    int day;
};

std::string days[] = {
   "Sunday",
   "Monday",
   "Tuesday",
   "Wednesday",
   "Thursday",
   "Friday",
   "Saturday"
};

int dow(const Date& date){
   int y = date.year;
   int m = date.month;
   int d = date.day;
   static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
   y -=m < 3;
   return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

std::string getDayOfWeek(int dayIndex){
   // get the day of the week by name
   return days[dayIndex];
}

// required for following two functions
static char daytab[2][13] = {
   {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
   {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

/* day_of_year: set day of year from month & day */
int day_of_year(int year, int month, int day)
{
   int i, leap;
   leap = (year%4 == 0 && year%100 != 0) || year%400 == 0;
   for (i = 1; i < month; i++)
       day += daytab[leap][i];
   return day;
}

/* month_day: set month, day from day of year */
void month_day(int year, int yearday, int *pmonth, int *pday)
{
   int i, leap;
   leap = (year%4 == 0 && year%100 != 0) || year%400 == 0;
   for (i = 1; yearday > daytab[leap][i]; i++)
       yearday -= daytab[leap][i];
   *pmonth = i; //sets month via pointer
   *pday = yearday; //sets day via pointer
}

void addDays(Date& date, int days){
   int doy = day_of_year(date.year, date.month, date.day) + days;
   int* pmonth = &date.month; //Address of month member or same address as reference
   int* pday = &date.day;
   month_day(date.year, doy, pmonth, pday);
}

void addDay(Date& date){
   addDays(date, 1);
}

void addMonth(Date& date){
   // broad assumption that adding a month is 30 days
   // could add a switch statement using dayTab and switch statement,
   // but that would require 'adding a Februrary' or similar which feels bizarre
   addDays(date, 30);
}

void addYear(Date& date){
   date.year++;
}

Date getNextMonday(Date date){
   // keep adding a day
   // until the day equals monday
   // then return that day
   while (dow(date) != 1){
       date.day++;
   }
  
   return date;
}

void printDate(const Date& date){
   //output the date in a readable format
   std::cout << std::string(20, '*') << std::endl;
   std::cout << "\nmonth : " << date.month << std::endl;
   std::cout << "day : " << date.day << std::endl;
   std::cout << "year : " << date.year << std::endl;
   std::cout << "day of week : " << getDayOfWeek(dow(date)) << std::endl;
   std::cout << std::string(20, '*') << std::endl;
}


TEST(dateTest, Date){
   //validation based on http://www.timeanddate.com/date/weekday.html  
   Date birthday;
   birthday.day = 7;
   birthday.month = 11;
   birthday.year = 1984;
  
   Date dday;
   dday.day = 6;
   dday.month = 6;
   dday.year = 1944;
  
   Date apolloEleven;
   apolloEleven.day = 20;
   apolloEleven.month = 7;
   apolloEleven.year = 1969;
  
   // begin addDay check  
   addDay(birthday);
   CHECK(birthday.day == 8 && birthday.month == 11 && birthday.year == 1984);
   CHECK(getDayOfWeek(dow(birthday)) == "Thursday");
  
   addDay(dday);
   CHECK(dday.day == 7 && dday.month == 6 && dday.year == 1944);
   CHECK(getDayOfWeek(dow(dday)) == "Wednesday");
  
   addDay(apolloEleven);
   CHECK(apolloEleven.day == 21 && apolloEleven.month == 7 && apolloEleven.year == 1969);
   CHECK(getDayOfWeek(dow(apolloEleven)) == "Monday");  
   // end addDay check
  
   // begin addMonth check
   addMonth(birthday);
   CHECK(birthday.day == 8 && birthday.month == 12 && birthday.year == 1984);
   CHECK(getDayOfWeek(dow(birthday)) == "Saturday");
  
   addMonth(dday);
   CHECK(dday.day == 7 && dday.month == 7 && dday.year == 1944);
   CHECK(getDayOfWeek(dow(dday)) == "Friday");
  
   addMonth(apolloEleven);
   CHECK(apolloEleven.day == 20 && apolloEleven.month == 8 && apolloEleven.year == 1969);
   CHECK(getDayOfWeek(dow(apolloEleven)) == "Wednesday");
   // end addMonth check
  
   // begin addYear check
   addYear(birthday);
   CHECK(birthday.day == 8 && birthday.month == 12 && birthday.year == 1985);
   CHECK(getDayOfWeek(dow(birthday)) == "Sunday");
  
   addYear(dday);
   CHECK(dday.day == 7 && dday.month == 7 && dday.year == 1945);
   CHECK(getDayOfWeek(dow(dday)) == "Saturday");
  
   addYear(apolloEleven);
   CHECK(apolloEleven.day == 20 && apolloEleven.month == 8 && apolloEleven.year == 1970);
   CHECK(getDayOfWeek(dow(apolloEleven)) == "Thursday");
   // end addYear check

   // begin getNextMonday check
   Date nmBirthday = getNextMonday(birthday);
   CHECK(nmBirthday.day == 9 && nmBirthday.month == 12 && nmBirthday.year == 1985);
   CHECK(getDayOfWeek(dow(nmBirthday)) == "Monday");
  
   Date nmDDay = getNextMonday(dday);
   CHECK(nmDDay.day == 9 && nmDDay.month == 7 && nmDDay.year == 1945);
   CHECK(getDayOfWeek(dow(nmDDay)) == "Monday");
  
   Date nmApolloEleven = getNextMonday(apolloEleven);
   CHECK(nmApolloEleven.day == 24 && nmApolloEleven.month == 8 && nmApolloEleven.year == 1970);
   CHECK(getDayOfWeek(dow(nmApolloEleven)) == "Monday");
   // end getNextMonday check
}


write.cpp

#include <iostream>
#include <string>
#include <sstream>
#include "write.h"

void write(std::ostream& os, int value){
   os << value;
}

void write(std::ostream& os, float value){
   os << value;
}

void write(std::ostream& os, std::string value){
   os << value;
}

void empty(std::stringstream& ss){
   ss.clear();
   ss.str(std::string());
}

write.h

//write.h
#include <string>
#pragma once

void write(std::ostream& os, int i);
void write(std::ostream& os, float f);
void write(std::ostream& os, std::string);
void empty(std::stringstream&);

writeTest.cpp

// Assignment 3-2
// Create a C++ header file named write.h which contains function prototypes for three functions
// named write. Write the implementations for each write function in a file named write.cpp. Each
// write function takes two arguments. The first argument is always std::ostream& os. The second
// arguments are an int, a float, and a std::string respectively. Each write function should stream
// its second argument to the passed in std::ostream. Write unit tests that pass a std::stringstream
// as the first argument to each function and verify its operation. Write additional code that calls
// each write function and passes std::cout as the first argument. Here's the prototype for the first
// write overload:
// void write(std::ostream& os, int value);
// Notice that both std::stringstream and std::cout may be passed as the first argument. Both inherit
// from std::ostream and thus may be used where ever a std::ostream& is used. This is our first use
// of inheritance in C++. We'll do much more with inheritance as the course progresses.

#include "TestHarness.h"
#include "write.h"
#include <iostream>

TEST(writeSSTest, write){
   int iValue = 4;
   float fValue = 4.4;
   std::string sValue = "four";
  
   std::stringstream ss;
  
   write(ss, iValue);
   CHECK_EQUAL(ss.str(), "4");
   empty(ss);
  
   write(ss, fValue);
   CHECK_EQUAL(ss.str(), "4.4");
   empty(ss);
  
   write(ss, sValue);
   CHECK_EQUAL(ss.str(), "four");
   empty(ss);
}

TEST(writeCoutTest, write){
   std::cout << "std::cout test follows: " << std::endl;
  
   int iValue = 5;
   float fValue = 5.5;
   std::string sValue = "five";
  
   write(std::cout, iValue);
   if( !std::cout){
       CHECK_FAIL("could not stream value");
   }
  
   std::cout << std::endl;
  
   write(std::cout, fValue);
   if( !std::cout){
       CHECK_FAIL("could not stream value");
   }
   std::cout << std::endl;
  
   write(std::cout, sValue);
   if( !std::cout){
       CHECK_FAIL("could not stream value");
   }
  
   std::cout << std::endl;
   std::cout << "std::cout test complete" << std::endl;
}

Add a comment
Know the answer?
Add Answer to:
1. Write CppUnitLite tests to verify correct behavior for all the exercises. Using C++ 2. Please...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • I was wondering how to do the following. This will all be in a header file...

    I was wondering how to do the following. This will all be in a header file to be called in a .cpp file. They all have to be template functions because the .cpp file will test a few different values for each function. This should be done in C++ ====== Write a variadic template for a function named problem5 that joins an arbitrary number of strings together. This function will serve as the terminating case for two strings and your...

  • Hi guys! I need help for the Data Structure class i need to provide implementation of...

    Hi guys! I need help for the Data Structure class i need to provide implementation of the following methods: Destructor Add Subtract Multiply Derive (extra credit ) Evaluate (extra credit ) ------------------------------------------------------- This is Main file cpp file #include "polynomial.h" #include <iostream> #include <sstream> using std::cout; using std::cin; using std::endl; using std::stringstream; int main(int argc, char* argv[]){    stringstream buffer1;    buffer1.str(        "3 -1 2 0 -2.5"    );    Polynomial p(3);    p.Read(buffer1);    cout << p.ToString()...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • Please use C++ and add comments to make it easier to read. Do the following: 1)...

    Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...

  • Please answer all the questions thank you 1) (Classes – 20 Points) Consider the following class...

    Please answer all the questions thank you 1) (Classes – 20 Points) Consider the following class declaration for Time to complete the questions below: DO NOT WRITE MORE THAN ASKED FOR. class Time private: int hours; int minutes; public: Time(); Time (int , int m = 0); void addMin(int m); void addHr(int h); void reset(int h = 0, int m = 0); Time operator+(const Time & t) const; Time operator-(const Time & t) const; Time operator*(double n) const; friend Time...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT