Topics covered: Unit testing & Functions Write tests to test the following funct
ID: 3667412 • Letter: T
Question
Topics covered: Unit testing & Functions
Write tests to test the following functions, which will be implemented in mylib.cppfile. 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() andcos() 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 eithermylib.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;
}
Explanation / Answer
// Author: TODO: Fill me in
// Source File: mylib.cpp
// Description: TODO: Fill me in
#include<iostream.h>
#include <ctype>
// 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
double f,i;
f=1.0;
if(n <0.0)
return (-1.0);
else
for(i=1.0;i<=n;i++)
{
f=f*i;
}
return(f);
}
double degreesToRadians(double angle_in_degrees){
// TODO: Fill me in
if(angle_in_degrees >0)
{
return( (angle_in_degrees *2*PI ) / 180 );
}
else
{
return( ( angle_in_degrees* (-2) * PI ) / 180 );
}
}
bool isPrime(int number){
// TODO: Fill me in
int flag=0,i;
for(i=2;i<number/2;i++)
{
if(number%i==0)
{
flag=1; break;
}
}
if(flag==0)
return(true);
else
return(false);
}
double mySine(double angle_in_radians){
// TODO: Fill me in
int term,j;
double value=0.0;
int no_of_terms=2;
for ( term = 1, j = 2;term < no_of_terms*2;term += 2, j++ )
{
value += ( double ) pow( -1.0, j ) * pow(angle_in_radians, term ) / factorial( term );
}
return value;
}
double myCosine(double angle_in_radians){
// TODO: Fill me in
int term, j;
int no_of_terms=2;
double value = 1.0, ang_rad = 0.0;
for ( term = 2, j = 1;term <= no_of_terms;term += 2, j++ )
{
value += ( double ) pow( -1.0, j ) * pow(angle_in_radians, term ) / factorial(term );
}
return value;
}
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!
void testFact3() {
TS_ASSERT_DELTA(factorial(-1), -1, 0.01);
}
void testFact4()
{
TS_ASSERT_DELTA(factorial(-5), -1, 0.01);
}
void testFact5()
{
TS_ASSERT_DELTA(factorial(8),40320, 0.01);
}
void testFact6()
{
TS_ASSERT_DELTA(factorial(12), 479001600, 0.01);
}
void testd2r1()
{
TS_ASSERT_DELTA(degreesToRadians(90), 1.5707,0.0001);
}
void testd2r2()
{
TS_ASSERT_DELTA(degreesToRadians(-90), -1.5707,0.0001);
}
void testd2r3()
{
TS_ASSERT_DELTA(degreesToRadians(80), 1.3962,0.0001);
}
void testd2r4()
{
TS_ASSERT_DELTA(degreesToRadians(-120), 1.39626 ,0.0001);
}
void testd2r5()
{
TS_ASSERT_DELTA(degreesToRadians(110), 1.9198 ,0.0001);
}
void testd2r6()
{
TS_ASSERT_DELTA(degreesToRadians(60),0 1.04719 ,0.0001);
}
void testprime1()
{
TS_ASSERT_DELTA(isPrime(10),0,0.01);
}
void testprime2()
{
TS_ASSERT_DELTA(isPrime(9),0,0.01);
}
void testprime3()
{
TS_ASSERT_DELTA(isPrime(17),1,0.01);
}
void testprime4()
{
TS_ASSERT_DELTA(isPrime(25),0,0.01);
}
void testprime5()
{
TS_ASSERT_DELTA(isPrime(190),0,0.01);
}
void testprime6()
{
TS_ASSERT_DELTA(isPrime(109),1,0.01);
}
void testmsine1()
{
TS_ASSERT_DELTA(mySine(90), 0.8939,0.0001);
}
void testmsine2()
{
TS_ASSERT_DELTA(mySine(60),-0.3048 ,0.0001);
}
void testmsine3()
{
TS_ASSERT_DELTA(mySine(65),-0.8268 ,0.0001);
}
void testmsine4()
{
TS_ASSERT_DELTA(mySine(59),0.6367 ,0.0001);
}
void testmsine5()
{
TS_ASSERT_DELTA(mySine(-59),0.6367 ,0.0001);
}
void testmsine6()
{
TS_ASSERT_DELTA(mySine(85),-0.1760 ,0.0001);
}
void testmcsine1()
{
TS_ASSERT_DELTA(myCosine(90), -0.4480,0.0001);
}
void testmcsine2()
{
TS_ASSERT_DELTA(myCosine(60),-0.9524 ,0.0001);
}
void testmcsine3()
{
TS_ASSERT_DELTA(myCosine(65),-0.5624 ,0.0001);
}
void testmcsine4()
{
TS_ASSERT_DELTA(myCosine(59),-0.7710 ,0.0001);
}
void testmcsine5()
{
TS_ASSERT_DELTA(myCosine(67),-0.5177 ,0.0001);
}
void testmcsine6()
{
TS_ASSERT_DELTA(myCosine(85),-0.9843 ,0.0001);
}
};
#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;
}