home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / xgc / tests.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-08  |  12.5 KB  |  561 lines

  1. /*
  2. ** $XConsortium: tests.c,v 1.20 91/06/08 18:57:07 rws Exp $
  3. **
  4. */
  5.  
  6. #include <X11/Intrinsic.h>
  7. #include <X11/StringDefs.h>
  8. #include <X11/Xaw/AsciiText.h>
  9. #include <X11/Xos.h>
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include "xgc.h"
  13. #ifdef SVR4
  14. #define SYSV
  15. #endif
  16. #ifndef SYSV
  17. #include <sys/timeb.h>
  18. #include <sys/resource.h>
  19. #endif
  20.  
  21. #ifndef PI
  22. #define PI 3.14159265
  23. #endif
  24.  
  25. #ifdef SYSV
  26. #define random lrand48
  27. #endif
  28.  
  29. #ifndef sgi
  30. extern long random();
  31. #endif
  32.  
  33. extern XStuff X;
  34. extern Widget result;
  35.  
  36. extern void print_if_recording();
  37. void show_result();
  38.  
  39. /* timer(flag)
  40. ** -----------
  41. ** When called with StartTimer, starts the stopwatch and returns nothing.
  42. ** When called with EndTimer, stops the stopwatch and returns the time
  43. ** in microseconds since it started.
  44. **
  45. ** Uses rusage() so we can subtract the time used by the system and user
  46. ** from our timer, and just concentrate on the time used in the X calls.
  47. */
  48.  
  49. static long
  50. timer(flag)
  51.      int flag;
  52. {
  53. #ifndef SYSV
  54.   static struct timeval starttime;  /* starting time for gettimeofday() */
  55.   struct timeval endtime;           /* ending time for gettimeofday() */
  56.   static struct rusage startusage;  /* starting time for getrusage() */
  57.   struct rusage endusage;           /* ending time for getrusage() */
  58.   struct timezone tz;               /* to make gettimeofday() happy */
  59.  
  60.   long elapsedtime;                 /* how long since we started the timer */
  61.  
  62.   switch (flag) {
  63.     case StartTimer:                       /* store initial values */
  64.       gettimeofday(&starttime,&tz);       
  65.       getrusage(RUSAGE_SELF,&startusage);
  66.       return((long) NULL);
  67.     case EndTimer:
  68.       gettimeofday(&endtime,&tz);          /* store final values */
  69.       getrusage(RUSAGE_SELF,&endusage);
  70.  
  71.   /* all the following line does is use the formula 
  72.      elapsed time = ending time - starting time, but there are three 
  73.      different timers and two different units of time, ack... */
  74.  
  75.       elapsedtime = (long) ((long)
  76.     ((endtime.tv_sec - endusage.ru_utime.tv_sec - endusage.ru_stime.tv_sec
  77.      - starttime.tv_sec + startusage.ru_utime.tv_sec
  78.      + startusage.ru_stime.tv_sec)) * 1000000) + (long)
  79.       ((endtime.tv_usec - endusage.ru_utime.tv_usec - endusage.ru_stime.tv_usec
  80.      - starttime.tv_usec + startusage.ru_utime.tv_usec
  81.      + startusage.ru_stime.tv_usec));
  82.       return(elapsedtime);                
  83.  
  84.     default:                              
  85.       fprintf(stderr,"Invalid flag in timer()\n");
  86.       return((long) NULL);
  87.     }
  88. #else
  89.   static long starttime;
  90.   
  91.   switch (flag) {
  92.     case StartTimer:
  93.       time(&starttime);
  94.       return((long) NULL);
  95.     case EndTimer:
  96.       return( (time(NULL) - starttime) * 1000000);
  97.     default:
  98.       fprintf(stderr,"Invalid flag in timer()\n");
  99.       return((long) NULL);
  100.     }
  101. #endif
  102. }
  103.  
  104.  
  105. void
  106. copyarea_test()
  107. {
  108.   int num_copies = 200;
  109.   int i;
  110.   long totaltime;
  111.   char buf[80];
  112.  
  113.   num_copies *= X.percent;
  114.  
  115.   XSetFillStyle(X.dpy,X.miscgc,FillTiled);
  116.   XFillRectangle(X.dpy,X.win,X.miscgc,0,0,400,400);
  117.  
  118.   XSync(X.dpy,0);
  119.   timer(StartTimer);
  120.   for (i=0;i<num_copies;++i)
  121.     XCopyArea(X.dpy,X.win,X.win,X.gc,i,200-i,
  122.           200,200,200-i,i);
  123.   XSync(X.dpy,0);
  124.   totaltime = timer(EndTimer);
  125.  
  126.   sprintf(buf,"%.2f seconds.",(double)totaltime/1000000.);
  127.   show_result(buf);
  128. }
  129.  
  130. void
  131. copyplane_test()
  132. {
  133.   int num_copies = 200;
  134.   int i;
  135.   long totaltime;
  136.   char buf[80];
  137.  
  138.   if(!X.gcv.plane_mask || (X.gcv.plane_mask & (X.gcv.plane_mask - 1))) {
  139.     show_result("exactly one bit in plane mask must be set for this test");
  140.     return;
  141.   }
  142.  
  143.  
  144.   num_copies *= X.percent;
  145.  
  146.   XSetPlaneMask(X.dpy, X.gc, ~0L);
  147.   XSetFillStyle(X.dpy,X.miscgc,FillTiled);
  148.   XFillRectangle(X.dpy,X.win,X.miscgc,0,0,400,400);
  149.  
  150.   XSync(X.dpy,0);
  151.   timer(StartTimer);
  152.   for (i=0;i<num_copies;++i)
  153.     XCopyPlane(X.dpy,X.win,X.win,X.gc,i,200-i,
  154.           200,200,200-i,i,X.gcv.plane_mask);
  155.   XSync(X.dpy,0);
  156.   totaltime = timer(EndTimer);
  157.   XSetPlaneMask(X.dpy, X.gc, X.gcv.plane_mask);
  158.  
  159.   sprintf(buf,"%.2f seconds.",(double)totaltime/1000000.);
  160.   show_result(buf);
  161. }
  162.  
  163. void
  164. circle_line_test(num_vertices,radius)
  165.      int num_vertices,radius;
  166. {
  167.   double theta, delta;
  168.   int length, centerx, centery, i;
  169.   int relative_angle;
  170.   long totaltime;
  171.   char buf[80];
  172.   XPoint *coord;
  173.  
  174.   relative_angle = num_vertices*5/12+1;
  175.   delta = (double) relative_angle / (double) num_vertices * 2 * PI;
  176.   centerx = centery = 200;
  177.  
  178.   coord = (XPoint *) malloc (sizeof(XPoint)*(num_vertices+1));
  179.  
  180.   length = (int) (2 * radius * (float) atan(delta/2.));
  181.  
  182.   for (i=0;i<=num_vertices;++i) {
  183.     theta = (double) i * delta;
  184.     coord[i].x = centerx + (int) (radius * cos(theta));
  185.     coord[i].y = centery + (int) (radius * sin(theta));
  186.   }
  187.  
  188.   XSync(X.dpy,0);
  189.   timer(StartTimer);
  190.   XDrawLines(X.dpy,X.win,X.gc,coord,num_vertices+1,CoordModeOrigin);
  191.   XSync(X.dpy,0);
  192.   totaltime = timer(EndTimer);
  193.  
  194.   sprintf(buf,"%d lines of length %d in %.3f seconds.",num_vertices,
  195.       length,(double)totaltime/1000000.);
  196.   show_result(buf);
  197.  
  198.   free(coord);
  199. }
  200.  
  201.  
  202.  
  203. void
  204. polyline_test()
  205. {
  206.   circle_line_test((int)(601*X.percent),190);
  207. }
  208.  
  209. void
  210. polysegment_test()
  211. {
  212.   XSegment *segments;
  213.   int num_segments = 600;
  214.   long totaltime;
  215.   char buf[80];
  216.   int i;
  217.  
  218.   num_segments *= X.percent;
  219.  
  220.   segments = (XSegment *) malloc(sizeof(XSegment) * num_segments);
  221.  
  222.   segments[0].x1 = random()%400; segments[0].y1 = random()%400;
  223.   segments[0].x2 = random()%400; segments[0].y2 = random()%400;
  224.  
  225.   for(i=1;i<num_segments;++i) {
  226.     segments[i].x1 = (segments[i-1].x1-segments[i-1].y2+400+i)%400;
  227.     segments[i].y1 = (segments[i-1].y1+segments[i-1].x2+i)%400;
  228.     segments[i].x2 = (segments[i-1].x1-segments[i-1].y1+400+i)%400;
  229.     segments[i].y2 = (segments[i-1].x2+segments[i-1].y2+i)%400;
  230.   }
  231.  
  232.   XSync(X.dpy,0);
  233.   start_timer();
  234.   XDrawSegments(X.dpy,X.win,X.gc,segments,num_segments);
  235.   XSync(X.dpy,0);
  236.   totaltime = end_timer();
  237.   
  238.   sprintf(buf,"%d segments in %.3f seconds.",num_segments,
  239.       (double)totaltime/1000000.);
  240.   show_result(buf);
  241.  
  242.   free(segments);
  243. }
  244.  
  245. void
  246. polypoint_test()
  247. {
  248.   XPoint *points;
  249.   int num_points = 100000;
  250.   long totaltime;
  251.   char buf[80];
  252.   int i;
  253.   
  254.   num_points *= X.percent;
  255.  
  256.   points = (XPoint *) malloc(sizeof(XPoint) * num_points);
  257.  
  258.   points[0].x = random()%400; points[0].y = random()%400;
  259.   points[1].x = random()%400; points[1].y = random()%400;
  260.  
  261.   for (i=2;i<num_points;++i) {
  262.     points[i].x = (points[i-1].x+points[i-2].y+i*3/200)%400;
  263.     points[i].y = (points[i-1].y+points[i-2].x+i*5/200)%400;
  264.   }
  265.  
  266.   XSync(X.dpy,0);
  267.   start_timer();
  268.   XDrawPoints(X.dpy,X.win,X.gc,points,num_points,CoordModeOrigin);
  269.   XSync(X.dpy,0);
  270.   totaltime = end_timer();
  271.  
  272.   sprintf(buf,"%d points in %.3f seconds.",num_points,
  273.       (double)totaltime/1000000.);
  274.   show_result(buf);
  275.  
  276.   free(points);
  277. }
  278.  
  279. void
  280. genericrectangle_test(fill)
  281.      Boolean fill;
  282. {
  283.   XRectangle *rects;
  284.   int num_rects = 200;
  285.   int perimeter = 0, area = 0;
  286.   int i;
  287.   long totaltime;
  288.   char buf[80];
  289.  
  290.   num_rects *= X.percent;
  291.  
  292.   rects = (XRectangle *) malloc(sizeof(XRectangle) * num_rects);
  293.  
  294.   for (i=0;i<num_rects;++i) {
  295.     rects[i].x = rects[i].y = 200 - i;
  296.     rects[i].width = rects[i].height = 2 * i;
  297.     perimeter += rects[i].width * 2 + rects[i].height * 2;
  298.     area += rects[i].width * rects[i].height;
  299.   }
  300.  
  301.   XSync(X.dpy,0);
  302.   start_timer();
  303.   if (fill) XFillRectangles(X.dpy,X.win,X.gc,rects,num_rects);
  304.   else XDrawRectangles(X.dpy,X.win,X.gc,rects,num_rects);
  305.   XSync(X.dpy,0);
  306.   totaltime = end_timer();
  307.  
  308.   if (fill)
  309.     sprintf(buf,"%d pixels in %.2f seconds.",area,(double)totaltime/1000000.);
  310.   else
  311.     sprintf(buf,"Total line length %d in %.3f seconds.",perimeter,
  312.         (double)totaltime/1000000.);
  313.   show_result(buf);
  314.  
  315.   free(rects);
  316. }
  317.  
  318. void
  319. polyrectangle_test()
  320. {
  321.   genericrectangle_test(FALSE);
  322. }
  323.  
  324. void
  325. polyfillrectangle_test()
  326. {
  327.   genericrectangle_test(TRUE);
  328. }
  329.  
  330. /*****************************/
  331.  
  332. void
  333. fillpolygon_test()
  334. {
  335.   int i;
  336.   int points_per_side = 40;
  337.   int spacing;
  338.   XPoint *points;
  339.   XPoint polypoints[3];
  340.  
  341.   points = (XPoint *) malloc (sizeof(XPoint) * points_per_side * 4);
  342.   spacing = 400 / points_per_side;
  343.  
  344.   for (i = 0; i < points_per_side; ++i) {
  345.     points[i].x = i * spacing;
  346.     points[i].y = 0;
  347.  
  348.     points[i + points_per_side].x = 400;
  349.     points[i + points_per_side].y = i * spacing;
  350.  
  351.     points[i + 2 * points_per_side].x = 400 - i * spacing;
  352.     points[i + 2 * points_per_side].y = 400;
  353.  
  354.     points[i + 3 * points_per_side].x = 0;
  355.     points[i + 3 * points_per_side].y = 400 - i * spacing;
  356.   }
  357.  
  358.   for (i = 0; i < 2 * points_per_side; i += 2) {
  359.     polypoints[0].x = points[i].x;
  360.     polypoints[0].y = points[i].y;
  361.  
  362.     polypoints[1].x = points[i + 2 * points_per_side].x;
  363.     polypoints[1].y = points[i + 2 * points_per_side].y;
  364.  
  365.     polypoints[2].x = points[i + 2 * points_per_side + 1].x;
  366.     polypoints[2].y = points[i + 2 * points_per_side + 1].y;
  367.  
  368.     XFillPolygon (X.dpy, X.win, X.gc, polypoints, 3, Convex, CoordModeOrigin);
  369.  
  370.     free(points);
  371.   }
  372. }
  373.  
  374. /*****************************/
  375.  
  376. void
  377. genericarc_test(fill)
  378.      Boolean fill;
  379. {
  380.   XArc *arcs;
  381.   int num_arcs = 180;
  382.   int i;
  383.   long totaltime;
  384.   char buf[80];
  385.  
  386.   num_arcs *= X.percent;
  387.  
  388.   arcs = (XArc *) malloc(sizeof(XArc) * num_arcs);
  389.  
  390.   for (i=0;i<num_arcs;++i) {
  391.     arcs[i].x = i;
  392.     arcs[i].y = i;
  393.     arcs[i].width = i;
  394.     arcs[i].height = i;
  395.     arcs[i].angle1 = i * 128;
  396.     arcs[i].angle2 = i * 128;
  397.   }
  398.  
  399.   XSync(X.dpy,0);
  400.   start_timer();
  401.   if (fill) XFillArcs(X.dpy,X.win,X.gc,arcs,num_arcs);
  402.   else XDrawArcs(X.dpy,X.win,X.gc,arcs,num_arcs);
  403.   XSync(X.dpy,0);
  404.   totaltime = end_timer();
  405.  
  406.   sprintf(buf,"An uncounted number of pixels in %.3f seconds.",
  407.       (double)totaltime/1000000.);
  408.   show_result(buf);
  409.  
  410.   free(arcs);
  411. }
  412.  
  413. void
  414. polyarc_test()
  415. {
  416.   genericarc_test(FALSE);
  417. }
  418.  
  419. void
  420. polyfillarc_test()
  421. {
  422.   genericarc_test(TRUE);
  423. }
  424.  
  425. void
  426. polytext8_test()
  427. {
  428.   int num_strings = 200;
  429.   static char string[] = "pack my box with five dozen liquor jugs";
  430.   int i;
  431.   long totaltime;
  432.   char buf[80];
  433.  
  434.   num_strings *= X.percent;
  435.  
  436.   XSync(X.dpy,0);
  437.   start_timer();
  438.   for (i=0;i<num_strings;++i) {
  439.     XDrawString(X.dpy,X.win,X.gc,(i%2 ? i : num_strings - i),i,
  440.         string,sizeof(string));
  441.   }
  442.   XSync(X.dpy,0);
  443.   totaltime = end_timer();
  444.  
  445.   sprintf(buf,"%d strings in %.2f seconds.",num_strings,
  446.       (double) totaltime/1000000.);
  447.   show_result(buf);
  448. }
  449.  
  450. void
  451. imagetext8_test()
  452. {
  453.   int num_strings = 200;
  454.   static char string[] = "pack my box with five dozen liquor jugs";
  455.   int i;
  456.   long totaltime;
  457.   char buf[80];
  458.  
  459.   num_strings *= X.percent;
  460.  
  461.   XSync(X.dpy,0);
  462.   start_timer();
  463.   for (i=0;i<num_strings;++i) {
  464.     XDrawImageString(X.dpy,X.win,X.gc,(i%2 ? i : num_strings - i),i,
  465.         string,sizeof(string));
  466.   }
  467.   XSync(X.dpy,0);
  468.   totaltime = end_timer();
  469.  
  470.   sprintf(buf,"%d strings in %.2f seconds.",num_strings,
  471.       (double) totaltime/1000000.);
  472.   show_result(buf);
  473. }
  474.  
  475. void
  476. putimage_test()
  477. {
  478.   int num_copies = 200;
  479.   int i;
  480.   long totaltime;
  481.   char buf[80];
  482.  
  483.   num_copies *= X.percent;
  484.  
  485.   XSetFillStyle(X.dpy,X.miscgc,FillTiled);
  486.   XFillRectangle(X.dpy,X.win,X.miscgc,0,0,400,400);
  487.  
  488.   X.image = XGetImage(X.dpy,X.win,0,0,200,200,~0,XYPixmap);
  489.  
  490.   XSync(X.dpy,0);
  491.   timer(StartTimer);
  492.   for (i=0;i<num_copies;++i)
  493.     XPutImage(X.dpy,X.win,X.gc,X.image,0,0,i,i,200,200);
  494.   XSync(X.dpy,0);
  495.   totaltime = timer(EndTimer);
  496.  
  497.   sprintf(buf,"%.2f seconds.",(double)totaltime/1000000.);
  498.   show_result(buf);
  499. }
  500.  
  501.  
  502. /*****************************/
  503. /*****************************/
  504.  
  505. void
  506. run_test()
  507. {
  508.   XClearWindow(X.dpy,X.win);
  509.  
  510.   print_if_recording("run\n");
  511.   
  512.   switch (X.test) {
  513.     case CopyArea:      copyarea_test();           break;
  514.     case CopyPlane:     copyplane_test();          break;
  515.     case PolyPoint:     polypoint_test();          break;
  516.     case PolyLine:      polyline_test();           break;
  517.     case PolySegment:   polysegment_test();        break;
  518.     case PolyRectangle: polyrectangle_test();      break;
  519.     case PolyArc:       polyarc_test();            break;
  520.     case FillPolygon:   fillpolygon_test();        break;
  521.     case PolyFillRect:  polyfillrectangle_test();  break;
  522.     case PolyFillArc:   polyfillarc_test();        break;
  523.     case PolyText8:     polytext8_test();          break;
  524.     case ImageText8:    imagetext8_test();         break;
  525.     case PutImage:      putimage_test();           break;
  526.     default: fprintf(stderr,"That test doesn't exist yet.\n");
  527.     }
  528. }
  529.  
  530. /*****************************/
  531.  
  532. /* set_text(w,string)
  533. ** ------------------
  534. ** Sets the text in a read-only text widget to the specified string.
  535. */
  536.  
  537. void
  538. set_text(w,string)
  539.      Widget w;
  540.      char *string;
  541. {
  542.   static Arg args[1];
  543.  
  544.   XtSetArg(args[0], XtNstring, string);
  545.   XtSetValues(w, args, (Cardinal) 1 );
  546. }
  547.  
  548. void
  549. show_result(string)
  550.      char *string;
  551. {
  552.   char buf[80];
  553.  
  554.   set_text(result,string);
  555.  
  556.   strcpy(buf,"# ");
  557.   strcat(buf,string);
  558.   strcat(buf,"\n");
  559.   print_if_recording(buf);
  560. }
  561.