Advertisement
serikov

unit tests

Jun 23rd, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.30 KB | None | 0 0
  1. // сompile string g++ ya.cpp -lboost_regex
  2. // run example on linux: ./a.out "23:25" dms q as demo input
  3. // for run unit test please compile with --test key (argv[4])
  4.  
  5. #include <boost/regex.hpp>
  6. #include <cstring>
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. class Time {
  11.   public:
  12.     bool Parse(string time) {
  13.                // cout<<time<<"\n";
  14.                hours = 0;
  15.                minutes = 0;
  16.                boost::regex xRegEx1("([0-1][0-9]):([0-5][0-9])\\s([AP]M)");
  17.                boost::regex xRegEx2("([0-2][0-9]):([0-5][0-9])");
  18.                boost::smatch xResults;
  19.                if ((boost::regex_match(time, xResults, xRegEx1))||(boost::regex_match(time, xResults, xRegEx2)))    {
  20.                     //cout<<"xResults[0] is "<<xResults[0]<<"\n";
  21.                     //cout<<"xResults[1] is "<<xResults[1]<<"\n";
  22.                     //cout<<"xResults[2] is "<<xResults[2]<<"\n";
  23.                     //cout<<"xResults[3] is "<<xResults[3]<<"\n";
  24.                 hours = strtol(xResults[1].str().c_str(), NULL, 10);
  25.                 minutes = strtol(xResults[2].str().c_str(), NULL, 10);
  26.                 if (strcmp(xResults[3].str().c_str(), "PM")==0)  hours = hours + 12;
  27.                    return true;
  28.              } else {
  29.                 cout<<"Incorrect time format or bad regular expression\n";
  30.                 return false;
  31.                 }
  32. }
  33.  
  34.     int GetHours () {
  35.         return hours;
  36.     }  
  37.  
  38.     int GetMinutes() {
  39.         return minutes;
  40.     }  
  41.    
  42.     private:
  43.      int hours;
  44.      int minutes;
  45. };
  46.  
  47.  
  48. class AnalogClock {
  49.    public:
  50.       AnalogClock(Time* atime) {
  51.       t = *atime;
  52.       }
  53.  
  54.       float GetAngle() {
  55.          angle = 0;
  56.          int hours = t.GetHours();
  57.          if (hours>12) hours = hours - 12;
  58.          int minutes = t.GetMinutes();
  59.          if (minutes==0) angle = abs(hours*30-minutes*6);
  60.          else angle = abs((hours+1/minutes)*30-minutes*6);
  61.          if (angle>180) angle=abs(180-angle);
  62.          return angle;
  63.         }
  64.  
  65.     private:
  66.         float angle;
  67.         Time t;
  68. };
  69.  
  70.  
  71. class QuartzClock {
  72.    public:
  73.       QuartzClock(Time* qtime) {
  74.       t = *qtime;
  75.     }
  76.  
  77.       float GetAngle() {
  78.         angle = 0;
  79.         int hours = t.GetHours();
  80.         if (hours>12) hours = hours - 12;
  81.         int minutes = t.GetMinutes();
  82.         angle = abs(hours*30-minutes*6); if (angle>180) angle=abs(180-angle);
  83.         return angle;
  84.         }
  85.  
  86.     private:
  87.         float angle;
  88.         Time t;
  89. };
  90.  
  91.  
  92. string convert_angle(float angle, const char* tformat) {
  93.         char angle1 [50];
  94.         if (strcmp(tformat,"deg")==0) {
  95.             sprintf (angle1, "%f", angle);
  96.             }
  97.         else if (strcmp(tformat,"rad")==0) {
  98.             angle=angle*(M_PI)/180;
  99.             sprintf (angle1, "%f", angle);
  100.             }
  101.         else if (strcmp(tformat,"dms")==0) {
  102.             int hrs = int(angle+0.5);
  103.             //cout<<"hrs is: "<<hrs<<"\n";
  104.             //cout<<"floor(angle): "<<floor(angle)<<"\n";
  105.             int minst = (angle-floor(angle))*60;
  106.             //cout<<"minst is: "<<minst<<"\n";
  107.             int mins = int(minst+0.5);
  108.             //cout<<"mins is: "<<mins<<"\n";
  109.             int secst = (minst-floor(minst))*60;
  110.             //cout<<"secst is: "<<secst<<"\n";
  111.             int secs = int(secst+0.5);
  112.             //cout<<"secs is: "<<secs<<"\n";
  113.             sprintf (angle1, "%d.%d'%d\"", hrs, mins, secs);           
  114.         }
  115.         else {
  116.         cout<<"Angle format is incorrect. Exit\n";
  117.         }
  118.     return string(angle1);
  119.     }
  120.  
  121. ////////////////// start of unit test functions
  122.  
  123. void TestTimeParse() {
  124.         Time time;
  125.         assert(time.Parse("10:30 AM"));
  126.         assert(time.GetHours() == 10);
  127.         assert(time.GetMinutes() == 30);
  128.         assert(time.Parse("09:45 PM"));
  129.         assert(time.GetHours() == 21);
  130.         assert(time.GetMinutes() == 45);
  131.         assert(time.Parse("23:32"));
  132.         assert(time.GetHours() == 23);
  133.         assert(time.GetMinutes() == 32);
  134.         }
  135.  
  136.  
  137. void AnalogClockTest() {
  138.         Time atime;
  139.         atime.Parse("10:30 AM");
  140.         AnalogClock(Time* atime);
  141.         assert(atime.GetAngle() == 135);
  142.         atime.Parse("22:22");
  143.         AnalogClock(Time* atime);
  144.         assert(atime.GetAngle() == 179);
  145.         }
  146.  
  147.  
  148. void QuartzClockTest() {
  149.         Time atime;
  150.         atime.Parse("10:30 AM");
  151.         QuartzClock(Time* atime);
  152.         assert(atime.GetAngle() == 120);
  153.         atime.Parse("22:22");
  154.         QuartzClock(Time* atime);
  155.         assert(atime.GetAngle() == 168);
  156.         }
  157.  
  158.  
  159. void AngleConvertTest() {
  160.         assert(convert_angle(179, "deg")== 179);
  161.         assert(convert_angle(168., "deg")== 179);
  162.         assert(convert_angle(168, "rad")== 2.9326);     // точность?
  163.         assert(convert_angle(168, "dms")== "168.0'0\"");
  164.         }
  165.  
  166. ///////////////////////// end of unit test functions
  167.  
  168.  
  169.  
  170. int main(int argc, char *argv[])
  171. {
  172. ///////////////////// unit tests
  173.     if (strcmp(argv[4],"--test")==0)  {
  174.     TestTimeParse();
  175.     AnalogClockTest();
  176.     QuartzClockTest();
  177.     AngleConvertTest();
  178.     }
  179. ///////////////////// end of unit tests
  180.    
  181. cout<<"argv[1] is "<<argv[1]<<"\n";
  182. cout<<"argv[2] is "<<argv[2]<<"\n";
  183. cout<<"argv[3] is "<<argv[3]<<"\n";
  184. Time t;
  185. t.Parse(argv[1]);
  186. //cout<<t.Parse(argv[1])<<"\n";
  187. float angle;
  188. if (!argv[3]) {
  189.     cout<<"You are not specify clock format. By default, it will be quartz\n";
  190.     QuartzClock c(&t);
  191.     angle = c.GetAngle();
  192.     }
  193. else {
  194.     if (strcmp(argv[3],"q")==0)  {
  195.         QuartzClock c(&t);
  196.         angle = c.GetAngle();
  197.         }
  198.     else if (strcmp(argv[3],"a")==0)  {
  199.         AnalogClock c(&t);
  200.         angle = c.GetAngle();
  201.         }
  202.     else {
  203.     cout<<"Clock format is incorrect. By default, it will be quartz\n";
  204.     QuartzClock c(&t);
  205.     angle = c.GetAngle();
  206.     }
  207. }
  208. cout<<"Natural angle is: "<<angle<<"\n";
  209. string angle1 = convert_angle(angle, argv[2]);
  210. //cout<<"Time is "<<t.GetHours()<<" hours and "<<t.GetMinutes()<<" minutes.\n";
  211. cout<<"Converted angle is: "<<angle1<<"\n";
  212. }
  213.  
  214.  
  215.  
  216. // нужна об
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement