Posted By

DrPepper on 09/01/11


Tagged


Versions (?)

validations for assignment 1


 / Published in: C++
 

  1. // FILE: statexam.cpp
  2.  
  3. // This program calls five test functions to test the statisitician class.
  4. // Maximum number of points from this program is 200.
  5.  
  6. #include <iostream>
  7. #include <cstdlib>
  8. #include <cmath>
  9. #include <string.h> // Provides memcpy function
  10. #include "stats.h"
  11. using namespace CISP430_A1;
  12. using namespace std;
  13.  
  14. const int SCORE0=0,SCORE1=100, SCORE2=25;
  15.  
  16. bool close(double a, double b)
  17. {
  18. const double EPSILON = 1e-5;
  19. return (fabs(a-b) < EPSILON);
  20. }
  21.  
  22. int test1( )
  23. {
  24. // Test program for basic statistician functions.
  25. // Returns 90 if everything goes okay; otherwise returns 0.
  26.  
  27. statistician s, t;
  28. int i;
  29. double r = 0;
  30.  
  31. if (s.length( ) || t.length( )) return 0;
  32. if (s.sum( ) || t.sum( )) return 0;
  33.  
  34. for (i = 1; i <= 10000; i++)
  35. {
  36. s.next(i);
  37. r += i;
  38. };
  39.  
  40. if (t.length( ) || t.sum( )) return 0;
  41. if (s.length( ) != 10000) return 0;
  42. if (!close(s.sum( ), r)) return 0;
  43. if (!close(s.mean( ), r/10000)) return 0;
  44.  
  45. // Reset and then retest everything
  46. s.reset( );
  47. t.reset( );
  48. r = 0;
  49.  
  50. if (s.length( ) || t.length( )) return 0;
  51. if (s.sum( ) || t.sum( )) return 0;
  52.  
  53. for (i = 1; i <= 10000; i++)
  54. {
  55. s.next(i);
  56. r += i;
  57. };
  58.  
  59. if (t.length( ) || t.sum( )) return 0;
  60. if (s.length( ) != 10000) return 0;
  61. if (!close(s.sum( ), r)) return 0;
  62. if (!close(s.mean( ), r/10000)) return 0;
  63.  
  64. return SCORE1;
  65. }
  66.  
  67. int test2( )
  68. {
  69. // Test program for minimum/maximum statistician functions.
  70. // Returns 15 if everything goes okay; otherwise returns 0.
  71.  
  72. statistician s, t, u;
  73. double r = 1000;
  74. char n[15] = "10000000000000";
  75.  
  76. if (s.length( ) || t.length( )) return 0;
  77. if (s.sum( ) || t.sum( )) return 0;
  78.  
  79. memcpy(&r, n, sizeof(double));
  80. r = 1/r;
  81. s.next(r);
  82. if ((s.minimum( ) != r) || (s.maximum( ) != r)) return 0;
  83. r *= -1;
  84. t.next(r);
  85. if ((t.minimum( ) != r) || (t.maximum( ) != r)) return 0;
  86.  
  87. u.next(100); u.next(-1); u.next(101); u.next(3);
  88. if ((u.minimum( ) != -1) || (u.maximum( ) != 101)) return 0;
  89.  
  90. return SCORE2;
  91. }
  92.  
  93. int test3( )
  94. {
  95. // Test program for + operator of the statistician
  96. // Returns 15 if everything goes okay; otherwise returns 0.
  97.  
  98. statistician s, t, u, v;
  99.  
  100. if (s.length( ) || t.length( )) return 0;
  101. if (s.sum( ) || t.sum( )) return 0;
  102.  
  103. t.next(5);
  104. u.next(0); u.next(10); u.next(10); u.next(20);
  105.  
  106. v = s + s;
  107. if (v.length( ) || v.sum( )) return 0;
  108. v = s + u;
  109. if (!(u == v)) return 0;
  110. v = t + s;
  111. if (!(t == v)) return 0;
  112. v = t + u;
  113. if (v.length( ) != 5) return 0;
  114. if (!close(v.sum( ), 45)) return 0;
  115. if (v.minimum( ) != 0) return 0;
  116. if (v.maximum( ) != 20) return 0;
  117. if (!close(v.mean( ), 45.0/5)) return 0;
  118. v = v + t;
  119. if (v.length( ) != 6) return 0;
  120. if (!close(v.sum( ), 50)) return 0;
  121. if (v.minimum( ) != 0) return 0;
  122. if (v.maximum( ) != 20) return 0;
  123. if (!close(v.mean( ), 50.0/6)) return 0;
  124. return SCORE2;
  125. }
  126.  
  127. int test4( )
  128. {
  129. // Test program for * operator of the statistician
  130. // Returns 15 if everything goes okay; otherwise returns 0.
  131.  
  132. statistician s, t, u;
  133.  
  134. if (s.length( ) || t.length( )) return 0;
  135. if (s.sum( ) || t.sum( )) return 0;
  136.  
  137. u.next(0); u.next(10); u.next(10); u.next(20);
  138.  
  139. s = 2*u;
  140. if (s.length( ) != 4) return 0;
  141. if (!close(s.sum( ), 80)) return 0;
  142. if (s.minimum( ) != 0) return 0;
  143. if (s.maximum( ) != 40) return 0;
  144. if (!close(s.mean( ), 80.0/4)) return 0;
  145.  
  146. s = -2*u;
  147. if (s.length( ) != 4) return 0;
  148. if (!close(s.sum( ), -80)) return 0;
  149. if (s.minimum( ) != -40) return 0;
  150. if (s.maximum( ) != 0) return 0;
  151. if (!close(s.mean( ), -80.0/4)) return 0;
  152.  
  153. s = 0*u;
  154. if (s.length( ) != 4) return 0;
  155. if (!close(s.sum( ), 0)) return 0;
  156. if (s.minimum( ) != 0) return 0;
  157. if (s.maximum( ) != 0) return 0;
  158. if (!close(s.mean( ), 0)) return 0;
  159.  
  160. s = 10 * t;
  161. if (s.length( ) != 0) return 0;
  162. if (s.sum( ) != 0) return 0;
  163.  
  164. return SCORE2;
  165. }
  166.  
  167. int test5( )
  168. {
  169. // Test program for == operator of the statistician.
  170. // Returns 15 if everything goes okay; otherwise returns 0.
  171.  
  172. statistician s, t, u, v, w, x;
  173.  
  174. if (s.length( ) || t.length( )) return 0;
  175. if (s.sum( ) || t.sum( )) return 0;
  176.  
  177. t.next(10);
  178. u.next(0); u.next(10); u.next(10); u.next(20);
  179. v.next(5); v.next(0); v.next(20); v.next(15);
  180. w.next(0);
  181. x.next(0); x.next(0);
  182.  
  183. if (!(s == s)) return 0;
  184. if (s == t) return 0;
  185. if (t == s) return 0;
  186. if (u == t) return 0;
  187. if (!(u == v)) return 0;
  188. if (w == x) return 0;
  189.  
  190. return SCORE2;
  191. }
  192.  
  193. int main( )
  194. {
  195. int value = 0;
  196. int result;
  197.  
  198. cerr << "Running statistician tests:" << endl;
  199.  
  200. cerr << "TEST 1:" << endl;
  201. cerr << "Testing next, reset, length, sum, and mean (100 points).\n";
  202. result = test1( );
  203. value += result;
  204. if (result > 0) cerr << "Test 1 passed." << endl << endl;
  205. else cerr << "Test 1 failed." << endl << endl;
  206.  
  207. cerr << "\nTEST 2:" << endl;
  208. cerr << "Testing minimum and maximum member functions (25 points).\n";
  209. result = test2( );
  210. value += result;
  211. if (result > 0) cerr << "Test 2 passed." << endl << endl;
  212. else cerr << "Test 2 failed." << endl << endl;
  213.  
  214. cerr << "\nTEST 3:" << endl;
  215. cerr << "Testing the + operator (25 points).\n";
  216. result = test3( );
  217. value += result;
  218. if (result > 0) cerr << "Test 3 passed." << endl << endl;
  219. else cerr << "Test 3 failed." << endl << endl;
  220.  
  221. cerr << "\nTEST 4:" << endl;
  222. cerr << "Testing the * operator (25 points).\n";
  223. result = test4( );
  224. value += result;
  225. if (result > 0) cerr << "Test 4 passed." << endl << endl;
  226. else cerr << "Test 4 failed." << endl << endl;
  227.  
  228. cerr << "\nTEST 5:" << endl;
  229. cerr << "Testing the == operator (25 points).\n";
  230. result = test5( );
  231. value += result;
  232. if (result > 0) cerr << "Test 5 passed." << endl << endl;
  233. else cerr << "Test 5 failed." << endl << endl;
  234.  
  235. cerr << "If you submit the statistician to me now, this part of the\n";
  236. cerr << "grade will be " << value << " points out of 200.\n";
  237. system("PAUSE");
  238.  
  239. return EXIT_SUCCESS;
  240.  
  241. }

Report this snippet  

You need to login to post a comment.