Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 5 Topics covered: Unit testing & Functions Write tests to test the following

ID: 3667297 • Letter: L

Question

Lab 5

Topics covered: Unit testing & Functions

Write tests to test the following functions, which will be implemented in mylib.cpp file. The unit tests will be in mylib_test.h

Write at least 6 unit tests for each function. Be sure to use the built-in sin() and cos() functions in cmath in your unit tests. mySine and myCosine should match the built-in functions for many inputs.

a. int factorial( int ) - Accepts an int value and returns the factorial of that number. If the parameter is negative, return -1.

b. double degreesToRadians( double ) - Accepts a double value as degrees and returns the radian equivalent. If the degrees is positive, return a value between 0 and 2 * PI. If the input is negative, return a value between 0 and -2 * PI.

c. bool isPrime( int ) - Accepts an integer value and returns true if the number is prime. If the value is negative, return false.

d. double mySine( double ) - Accepts a double value in radians and returns the sine of that angle. This must be able to handle any radian input (positive and negative) and give a correct result. Use the taylor-series expansion (Lab 4) for computation until the difference between iterations (expansions) differs less than 0.0001. Note that sin(0.25) == sin(0.25 + 2*PI) == sin(0.25 + 20*PI).

e. double myCosine( double ) - Accepts a double value in radians and returns the cosine of that angle. This must be able to handle any radian input (positive and negative)and give a correct result. Use the taylor-series (Lab 4) expansion for computation until the difference between iterations (expansions) differs less than 0.0001. Note that cos(0.25) == cos(0.25 + 2*PI) == cos(0.25 + 20*PI).

Fill in the above functions in mylib.cpp There will NOT be a main in either mylib.cpp or mylib_test.h.

Be sure you have the 5 functions above implemented, at least 6 unit tests per function, that make test returns …… OK! , and that your unit tests cover all possible code executions in mylib.cpp.

mylib.cpp

// Author: TODO: Fill me in
// Source File: mylib.cpp
// Description: TODO: Fill me in

#include <cmath>

using namespace std;

// TODO: You may implement any helper functions you like, or define
//            or change any constant you like.

const double PI = atan(1.0)*4.0;
const double PRECISION = 0.00001;

// For lab....
double factorial(double n){
   // TODO: Fill me in
   return 0.0;
}

double degreesToRadians(double angle_in_degrees){
   // TODO: Fill me in
   return 0.0;
}

bool isPrime(int number){
   // TODO: Fill me in
   return true;
}

double mySine(double angle_in_radians){
   // TODO: Fill me in
   return 0.0;
}

double myCosine(double angle_in_radians){
   // TODO: Fill me in
   return 0.0;
}

mylib_test.h

#ifndef MYLIB_TEST_H
#define MYLIB_TEST_H

#include <mylib.cpp>

#include <iostream>
#include <cxxtest/TestSuite.h>

using namespace std;

// This requires CxxTest to be installed!
// The current Makefile in this directory assumes cxxtest is a folder one
// level down.


class MyTests : public CxxTest::TestSuite {
public:
void testFact1() {
TS_ASSERT_DELTA(factorial(1), 1.0, 0.01);
}
  
void testFact2() {
TS_ASSERT_DELTA(factorial(5), 120, 0.01);
}
  
// TODO: Fill in more tests!

  
};


#endif

libsandbox.cpp

// Author: Fill me in
// Source File: Fill me in
// Description: Fill me in

#include <iostream>
#include <cmath>
#include "mylib.cpp"

using namespace std;

int main(){
   // TODO: Use this help develop your mylib.cpp functions.
   cout << "Print whatever you like ";
   return 0;
}

Please see Lab 4 details below, and I will post the code which I did for Lab 4.

Objective:

This lab is an introduction to functions. It utilizes the concepts of loop structures, logical and relational operators, C++ library functions, and C++ mathematical operators.

Checklist:

Read over and understand the specification section of this document.

Create a folder for this assignment under your Assignments Hierarchy.

Create and test degreesToRadians(x) function in degreesToRadians.cpp

Create and test factorial(n) function in factorial.cpp

Create a new source code file called taylorSeries.cpp

Copy factorial and degreesToRadians functions from previous steps into taylorSeries.cpp.

Create all function prototypes including factorial and degreesToRadians and place them above int main().

Implement the Taylor Series for mySine(x) function.

Implement the Taylor Series for myCosine(x) function.

Create myCosecant(x) in terms of mySine(x) function above

Create mySecant(x) in terms of myCosine(x) function above

Create myTangent(x) and myCotangent(x) in terms of mySine(x) and myCosine(x)

Display a table of results. See example output.

Submit all three .cpp files created in this laboratory to Mimir

Specification:

Using a loop go from 0 to 360 degrees in steps of 15-degrees and produce a table of Math Library Sine function, Math Library Cosine function, Taylor Series Sine function, Taylor Series Cosine function, Tangent (based on Taylor Series), Cotangent (based on Taylor Series), Cosecant (based on Taylor Series) and Secant (based on Taylor Series) function.

Calculate the Cosine and Sine of this angle using the Taylor Series given below:

Sine(x) = x – x3/3! + x5/5! – x7/7! + x9/9! – x11/11! + ………

Cosine(x) = 1 – x2/2! + x4/4! – x6/6! + x8/8! – x10/10! + ………

The variable x is in radians and not degrees.

Note: See the PI calculator example in the examples directory on blackboard to get you started with series calculations.

Other trigonometric functions can be calculated using:

Tangent(x) = Sine(x)/Cosine(x)

Cotangent(x) = Cosine(x)/Sine(x)

Secant(x) = 1.0/Cosine(x)

Cosecant(x) = 1.0/Sine(x)

You should use a while loop for this series. You will terminate the loop when the difference between previous and current calculated values is equal to or less than 0.00001.

To convert from degrees to radians use:

angle_in_radians = angle_in_degrees * PI/180.0;

You should implement the above conversion as a function called to degreesToRadians(x).

To calculate the factorial of a number use a function called factorial(x). This function should take a double as a parameter and return a double as value. The factorial should be calculated using a “for” loop. Do not use one of the many recursive versions of this function available from the internet.

Note: All function variables and functions return types should be doubles.

Finally, display the values using a in a properly formatted table in landscape format. Each value should show at least 4 digits after the decimal point. The table should print the values in order given above.

Task 1:

Create a new source file called degreesToRadians.cpp to create and test function called degreesToRadians(x). Compile using standard command line. Please use the following function prototype:

double degreesToRadians(double angle_in_radians);

Note: For PI add the following above int main(){

const double PI = atan(1.0)*4.0;

The degreesToRadians function will take double as a parameter and return a double as a value.

You should verify with hand calculations and cin/cout statements in main() that your function is producing the correct results for sample test values in the range of 0 to 2Pi (0 to 360 degrees). Please include these results in the comments section of your .cpp source file.

See sample output below to determine if your program is working properly.

Sample Output:

./degreesToRadians

Degrees to Radians Test Driver

Enter angle in degrees: 45

Angle in Radians: 0.7854

./degreesToRadians

Degrees to Radians Test Driver

Enter angle in degrees: 90

Angle in Radians: 1.5708

./degreesToRadians

Degrees to Radians Test Driver

Enter angle in degrees: 270

Angle in Radians: 4.7124

Task 2:

Create a new source file called factorial.cpp to create and test factorial(x) function. Compile it using the standard command line.

You should verify with hand calculations and cin/cout statements in main() that your function is producing the correct results for test input values in the range of 0 to 10. See sample output for details.

The factorial function will take double as a parameter and return a double value which corresponds to the following prototype:

double factorial(double factorial);

Note: You must use a for loop to implement this function. Please do not copy one of the many recursive functions from Google.

Sample Output

./factorial

Factorial Test Driver

Enter N: 7

Factorial: 5040

./factorial

Factorial Test Driver

Enter N: 10

Factorial: 3628800

./factorial

Factorial Test Driver

Enter N: 13

Factorial: 6227020800

Task 3:

Create a new source file called taylorSeries.cpp so you can create and test a Taylor Series version of the Sine(x) function. Call your function mySine(x) which takes a double as a parameter and returns a double as a result.

You will need to copy the functions from Task 1 and Task 2 to this new source file.
You should compare the result to the system sin(x) function from the math library for values of 0 to 360 (0 to 2PI). They should be within the error (0.0001) indicated above.

Note: Make sure to create a function prototype for mySine() and place it above main()

Task 4:

Update the file called taylorSeries.cpp so you can create and test a Taylor Series version of the Cosine(x) function.

This function should by called myCosine(x) and take one double as a parameter and return a double as a result.
You should compare the result to the system cos function from the math library for values of 0 to 360 (0 to 2PI). They should be within the error (0.0001) indicated above.

Note: Make sure to create a function prototype for myCosine() and place it above main()

Task 5:

Using the same source file add your remaining trigonometric function in terms of your myCosine(x) and mySine(x) function in Task 3 and 4 above. All these functions should take a double as a parameter and return a double as the result.  

Note: Make sure to create a function prototypes for all functions involved and place them above main()

Task 6

Finally, create a table from 0 to 360 degrees in steps of 15 containing the following:

Angle in degrees

Angle in radians

Your Trigonometric functions

The C++ Math library values for sin and cos. These should be grouped together with the mySine and myCosine created above.

Please see the sample output below for output formatting details.

Sample Output:

                         System               System                System

    Degrees Radians Sin                 Sin        Cos       Cos        Tan       Tan     CoTan Secant CoSecant

                 0 0.0000 0.0000 0.0000 1.0000 1.0000 0.0000 0.0000                inf 1.0000          inf

             15 0.2618 0.2588 0.2588 0.9659 0.9659 0.2679 0.2679 3.7321 1.0353   3.8637

             30 0.5236 0.5000 0.5000 0.8660 0.8660 0.5774 0.5774 1.7321 1.1547   2.0000

             45 0.7854 0.7071 0.7071 0.7071 0.7071 1.0000 1.0000 1.0000 1.4142   1.4142

             60 1.0472 0.8660 0.8660 0.5000 0.5000 1.7321 1.7321 0.5774 2.0000   1.1547

             75 1.3090 0.9659 0.9659 0.2588 0.2588 3.7321 3.7321 0.2679 3.8637   1.0353

             90 1.5708 1.0000 1.0000 0.0000 0.0000              inf          inf 0.0000           inf   1.0000

             105 1.8326 0.9659 0.9659 -0.2588 -0.2588 -3.7321 -3.7321 -0.2679 -3.8637   1.0353

             120 2.0944 0.8660 0.8660 -0.5000 -0.5000 -1.7321 -1.7321 -0.5774 -2.0000   1.1547

             135 2.3562 0.7071 0.7071 -0.7071 -0.7071 -1.0000 -1.0000 -1.0000 -1.4142   1.4142

             150 2.6180 0.5000 0.5000 -0.8660 -0.8660 -0.5774 -0.5774 -1.7321 -1.1547   2.0000

             165 2.8798 0.2588 0.2588 -0.9659 -0.9659 -0.2679 -0.2679 -3.7321 -1.0353   3.8637

             180 3.1416 0.0000 0.0000 -1.0000 -1.0000 -0.0000 -0.0000              -inf -1.0000        inf

             195 3.4034 -0.2588 -0.2588 -0.9659 -0.9659 0.2679 0.2679 3.7321 -1.0353 -3.8637

             210 3.6652 -0.5000 -0.5000 -0.8660 -0.8660 0.5774 0.5774 1.7321 -1.1547 -2.0000

             225 3.9270 -0.7071 -0.7071 -0.7071 -0.7071 1.0000 1.0000 1.0000 -1.4142 -1.4142

             240 4.1888 -0.8660 -0.8660 -0.5000 -0.5000 1.7321 1.7321 0.5774 -2.0000 -1.1547

             255 4.4506 -0.9659 -0.9659 -0.2588 -0.2588 3.7321 3.7321 0.2679 -3.8637 -1.0353

             270 4.7124 -1.0000 -1.0000 -0.0000 -0.0000            inf          inf 0.0000           -inf -1.0000

             285 4.9742 -0.9659 -0.9659 0.2588 0.2588 -3.7321 -3.7321 -0.2679 3.8637 -1.0353

             300 5.2360 -0.8660 -0.8660 0.5000 0.5000 -1.7321 -1.7321 -0.5773 2.0000 -1.1547

             315 5.4978 -0.7071 -0.7071 0.7071 0.7071 -1.0000 -1.0000 -1.0000 1.4142 -1.4142

             330 5.7596 -0.5000 -0.5000 0.8660 0.8660 -0.5774 -0.5774 -1.7321 1.1547 -2.0000

             345 6.0214 -0.2588 -0.2588 0.9659 0.9659 -0.2679 -0.2679 -3.7321 1.0353 -3.8637

             360 6.2832 -0.0000 0.0000 1.0000 1.0000 -0.0000 0.0000               inf 1.0000          inf

This is the code I have below of Lab 4. If the code has something wrong, please fix it for me. Thank you very much.

factorial.cpp
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

double factorial(double factorial);

int main()
{
int fact;
cout << setprecision(10);
cout << "Factorial Test Driver" << endl;
cout << "Enter N: ";
cin >> fact;
cout << "Factorial: " << factorial(fact) << endl;
return 0;
}

double factorial(double fact)
{
if(fact == 1 || fact == 0){
return 1;
}
else{
return fact * factorial(fact - 1);
}
}

degreesToRadians.cpp
#include <iostream>
#include <cmath>

using namespace std;

const double PI = atan(1.0)*4.0;
double degreesToRadians(double angle_in_radians);

int main()
{
double angle;
cout << "Degrees to Radians Test Driver" << endl;
cout << "Enter angle in degrees: ";
cin >> angle;
cout << "Angle in Radians: " << degreesToRadians(angle) << endl;
return 0;
}

double degreesToRadians(double angle_in_radians)
{
return angle_in_radians * PI / 180.0;
}

taylorSeries.cpp

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

const double PI = atan(1.0)*4.0;
double degreesToRadians(double angle_in_radians);
double factorial(double factorial);
double mySine(double x);
double myCosine(double x);
double myCosecant(double x);
double mySecant(double x);
double myTangent(double x);

int main()
{
cout << setprecision(4) << fixed;
for(int i = 0; i <= 360; i += 15){
double x = degreesToRadians(i);
cout << i << " " << mySine(x) << " " << myCosine(x) << " " << myTangent(x) << " " << mySecant(x) << myCosecant(x) << endl;
}
return 0;
}

double degreesToRadians(double angle_in_radians)
{
return angle_in_radians * PI / 180.0;
}

double factorial(double fact)
{
if(fact == 1 || fact == 0){
return 1;
}
else{
return fact * factorial(fact - 1);
}
}

double mySine(double x)
{
double sum = 0.0;
for(int i = 0; i < 100; i++){
double top = pow(-1, i) * pow(x, 2 * i + 1);
double bottom = factorial(2 * i + 1);
sum = sum + (top / bottom);
}
return sum;
}

double myCosine(double x)
{
double sum = 0.0;
for(int i = 0; i < 100; i++){
double top = pow(-1, i) * pow(x, 2 * i);
double bottom = factorial(2 * i);
sum = sum + (top / bottom);
}
return sum;
}

double myCosecant(double x)
{
return 1 / mySine(x);
}

Explanation / Answer

#ifndef MYLIB_TEST_H
#define MYLIB_TEST_H
#include <mylib.cpp>
#include <iostream>
#include <cxxtest/TestSuite.h>
#include "mylib.h"
using namespace std;
// This requires CxxTest to be installed!
// The current Makefile in this directory assumes cxxtest is a folder one
// level down.

class MyTests : public CxxTest::TestSuite {
public:
void testFact1() {
TS_ASSERT_DELTA(factorial(1), 1, 0.01);
}
  
void testFact2() {
TS_ASSERT_DELTA(factorial(5), 120, 0.01);
}

void testFact3(){
   TS_ASSERT_DELTA(factorial(0), 1, 0.01);
}

void testFact4(){
   TS_ASSERT_DELTA(factorial(2), 2, 0.01);
}

void testFact5(){
   TS_ASSERT_DELTA(factorial(4), 24, 0.01);
}

void testFact6(){
   TS_ASSERT_DELTA(factorial(3), 6, 0.01);
}

void testFact11() {
TS_ASSERT_DELTA(degreesToRadians(0), 0, 0.01);
}
  
void testFact12() {
TS_ASSERT_DELTA(degreesToRadians(10), 0.1745, 0.01);
}

void testFact13(){
   TS_ASSERT_DELTA(degreesToRadians(100), 1.745, 0.01);
}

void testFact14(){
   TS_ASSERT_DELTA(degreesToRadians(50), 0.872, 0.01);
}

void testFact15(){
   TS_ASSERT_DELTA(degreesToRadians(5), 0.0872, 0.01);
}

void testFact16(){
   TS_ASSERT_DELTA(degreesToRadians(57), 1, 0.01);
}

void testFact21() {
TS_ASSERT(isPrime(2));
}
  
void testFact22() {
TS_ASSERT(isPrime(5));
}

void testFact23(){
   TS_ASSERT(isPrime(7));
}

void testFact24(){
   TS_ASSERT(!isPrime(9));
}

void testFact25(){
   TS_ASSERT(!isPrime(15));
}

void testFact26(){
   TS_ASSERT(isPrime(17));
}

void testFact31() {
TS_ASSERT_DELTA(mySine(10), sin(10), 0.01);
}
  
void testFact32() {
TS_ASSERT_DELTA(mySine(25), sin(25), 0.01);
}

void testFact33(){
   TS_ASSERT_DELTA(mySine(50), sin(50), 0.01);
}

void testFact34(){
   TS_ASSERT_DELTA(mySine(100), sin(100), 0.01);
}

void testFact35(){
   TS_ASSERT_DELTA(mySine(150), sin(150), 0.01);
}

void testFact36(){
   TS_ASSERT_DELTA(mySine(111), sin(111), 0.01);
}
  
void testFact41() {
TS_ASSERT_DELTA(myCosine(10), cos(10), 0.01);
}
  
void testFact42() {
TS_ASSERT_DELTA(myCosine(25), cos(25), 0.01);
}

void testFact43(){
   TS_ASSERT_DELTA(myCosine(50), cos(50), 0.01);
}

void testFact44(){
   TS_ASSERT_DELTA(myCosine(100), cos(105), 0.01);
}

void testFact45(){
   TS_ASSERT_DELTA(myCosine(111), cos(111), 0.01);
}

void testFact46(){
   TS_ASSERT_DELTA(myCosine(150), cos(150), 0.01);
}

// TODO: Fill in more tests!
  
};

#endif