Annotation of loncom/test/lonplottest.pl, revision 1.1
1.1 ! matthew 1: #!/usr/bin/perl -w
! 2: # The LearningOnline Network with CAPA
! 3: # Dynamic plot testing routines
! 4: #
! 5: # $Id$
! 6: #
! 7: # Copyright Michigan State University Board of Trustees
! 8: #
! 9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
! 10: #
! 11: # LON-CAPA is free software; you can redistribute it and/or modify
! 12: # it under the terms of the GNU General Public License as published by
! 13: # the Free Software Foundation; either version 2 of the License, or
! 14: # (at your option) any later version.
! 15: #
! 16: # LON-CAPA is distributed in the hope that it will be useful,
! 17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
! 18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 19: # GNU General Public License for more details.
! 20: #
! 21: # You should have received a copy of the GNU General Public License
! 22: # along with LON-CAPA; if not, write to the Free Software
! 23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
! 24: #
! 25: # /home/httpd/html/adm/gpl.txt
! 26: #
! 27: # http://www.lon-capa.org/
! 28: #
! 29: # 12/30 Matthew
! 30: use strict;
! 31:
! 32: ##
! 33: ## The goal here is to be able to take the metadata from lonplot.pm
! 34: ## verbatim, and add to it.
! 35: ##
! 36:
! 37: ###################################################################
! 38: ## ##
! 39: ## Tests used in checking the validitity of input ##
! 40: ## ##
! 41: ###################################################################
! 42: my $int_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
! 43: my $real_test =
! 44: sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
! 45: my $color_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-f]{6}$/};
! 46: my $onoff_test = sub {$_[0]=~/^(on|off)$/};
! 47: my $key_pos_test = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
! 48: my $sml_test = sub {$_[0]=~/^(small|medium|large)$/};
! 49: my $linestyle_test = sub {$_[0]=~/^(lines|linespoints|dots|points|steps)$/};
! 50: my $words_test = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w\(\)]+ ?)+$/};
! 51:
! 52: ###################################################################
! 53: ## ##
! 54: ## Attribute metadata ##
! 55: ## ##
! 56: ###################################################################
! 57:
! 58: my %plot_defaults =
! 59: (
! 60: height => {
! 61: default => 200,
! 62: test => $int_test,
! 63: description => 'vertical size of image (pixels)',
! 64: edit_type => 'entry'
! 65: },
! 66: width => {
! 67: default => 200,
! 68: test => $int_test,
! 69: description => 'horizontal size of image (pixels)',
! 70: edit_type => 'entry'
! 71: },
! 72: bgcolor => {
! 73: default => 'xffffff',
! 74: test => $color_test,
! 75: description => 'background color of image (xffffff)',
! 76: edit_type => 'entry'
! 77: },
! 78: fgcolor => {
! 79: default => 'x000000',
! 80: test => $color_test,
! 81: description => 'foreground color of image (x000000)',
! 82: edit_type => 'entry'
! 83: },
! 84: transparent => {
! 85: default => 'off',
! 86: test => $onoff_test,
! 87: description => '',
! 88: edit_type => 'on_off'
! 89: },
! 90: grid => {
! 91: default => 'off',
! 92: test => $onoff_test,
! 93: description => '',
! 94: edit_type => 'on_off'
! 95: },
! 96: border => {
! 97: default => 'on',
! 98: test => $onoff_test,
! 99: description => '',
! 100: edit_type => 'on_off'
! 101: },
! 102: font => {
! 103: default => 'medium',
! 104: test => $sml_test,
! 105: description => 'Size of font to use',
! 106: edit_type => 'choice',
! 107: choices => ['small','medium','large']
! 108: },
! 109: align => {
! 110: default => 'left',
! 111: test => sub {$_[0]=~/^(left|right|center)$/},
! 112: description => 'alignment for image in html',
! 113: edit_type => 'choice',
! 114: choices => ['left','right','center']
! 115: }
! 116: );
! 117: my %key_defaults =
! 118: (
! 119: title => {
! 120: default => '',
! 121: test => $words_test,
! 122: description => 'Title of key',
! 123: edit_type => 'entry'
! 124: },
! 125: box => {
! 126: default => 'off',
! 127: test => $onoff_test,
! 128: description => 'Draw a box around the key?',
! 129: edit_type => 'on_off'
! 130: },
! 131: pos => {
! 132: default => 'top right',
! 133: test => $key_pos_test,
! 134: description => 'position of the key on the plot',
! 135: edit_type => 'choice',
! 136: choices => ['top left','top right','bottom left','bottom right',
! 137: 'outside','below']
! 138: }
! 139: );
! 140: my %label_defaults =
! 141: (
! 142: xpos => {
! 143: default => 0,
! 144: test => $real_test,
! 145: description => 'x position of label (graph coordinates)',
! 146: edit_type => 'entry'
! 147: },
! 148: ypos => {
! 149: default => 0,
! 150: test => $real_test,
! 151: description => 'y position of label (graph coordinates)',
! 152: edit_type => 'entry'
! 153: },
! 154: justify => {
! 155: default => 'left',
! 156: test => sub {$_[0]=~/^(left|right|center)$/},
! 157: description => 'justification of the label text on the plot',
! 158: edit_type => 'choice',
! 159: choices => ['left','right','center']
! 160: }
! 161: );
! 162: my %axis_defaults =
! 163: (
! 164: color => {
! 165: default => 'x000000',
! 166: test => $color_test,
! 167: description => 'color of axes (x000000)',
! 168: edit_type => 'entry'
! 169: },
! 170: xmin => {
! 171: default => '-10.0',
! 172: test => $real_test,
! 173: description => 'minimum x-value shown in plot',
! 174: edit_type => 'entry'
! 175: },
! 176: xmax => {
! 177: default => ' 10.0',
! 178: test => $real_test,
! 179: description => 'maximum x-value shown in plot',
! 180: edit_type => 'entry'
! 181: },
! 182: ymin => {
! 183: default => '-10.0',
! 184: test => $real_test,
! 185: description => 'minimum y-value shown in plot',
! 186: edit_type => 'entry'
! 187: },
! 188: ymax => {
! 189: default => ' 10.0',
! 190: test => $real_test,
! 191: description => 'maximum y-value shown in plot',
! 192: edit_type => 'entry'
! 193: }
! 194: );
! 195: my %curve_defaults =
! 196: (
! 197: color => {
! 198: default => 'x000000',
! 199: test => $color_test,
! 200: description => 'color of curve (x000000)',
! 201: edit_type => 'entry'
! 202: },
! 203: name => {
! 204: default => '',
! 205: test => $words_test,
! 206: description => 'name of curve to appear in key',
! 207: edit_type => 'entry'
! 208: },
! 209: linestyle => {
! 210: default => 'lines',
! 211: test => $linestyle_test,
! 212: description => 'Style of the axis lines',
! 213: edit_type => 'choice',
! 214: choices => ['lines','linespoints','dots','points','steps']
! 215: }
! 216: );
! 217:
! 218: ##############################################################
! 219: ## ##
! 220: ## Values to use in the tests ##
! 221: ## ##
! 222: ##############################################################
! 223:
! 224: my $long_text = 'Reader review of Bil Keane\'s "Daddy\'s Cap Is On Backwards": Although he will probably forever be denied the Nobel Prize because of the radio broadcasts he made during the late war on behalf of the government in Rome, Bil Keane is certainly one writer who has nothing to prove. Having already taken his place among the company of Homer, Dante, Shakespeare, and Dostoyevsky, with the publication of "Daddy\'s Cap Is On Backwards" Bil Keane now emerges as the master of them all. The storyline is deceptively simple: after Thel dies in a freak accident, Daddy abandons PJ, Jeffie, and Little Billy to take Dolly on a meandering automobile tour across America -- culminating in the loss of Dolly, and the emergence, too late, of Daddy\'s ability to love. But God, as Keane has long demonstrated in his other works, is in the details, and in the intricate and masterfully coordinated layer upon layer of innuendo and hidden meanings. The title itself, on its face, refers only to Dolly\'s innocent, even endearing, observation that her father, unlike all the other men in her neighborhood, lacks a prepuce. But the true significance of Daddy\'s "cap" is slowly revealed, chapter by chapter, and even at the end of the book one is left wondering whether other layers of meaning remain, beyond the reader\'s grasp. The turning point of the narrative is the episode where Jeffy sells his soul to Mephistopheles for power and knowledge, yet this can be fully understood only in contrast to the many events that procede and follow it -- such as the haunting scene where little Billy carries his father out of the burning city on his shoulders, or the passage where PJ, now the viceroy of Egypt, reveals himself to his brothers as the boy whom they sold into servitude years before. Nothing can compare, however, to the episode where Jeffy hurls his harpoon at the great white whale, it fails to meet its mark, and is reclaimed by the Rhine-maidens as it descends into the waters, while flames from the untended hilltop fire engulf the island paradise, tossing a firebrand onto the raft where little Billy and the runaway slave are [remainder of review missing]';
! 225:
! 226: my @color_test = ('x','xa','xaaaa','xaaaaaa','yaaaa','xabcdef',undef,'',
! 227: 'xdeadbeef','xgggggg','\nset output /etc/passwdfile');
! 228:
! 229: my @on_off_test = ('on','off','1','0','no','yes',undef,'',
! 230: '\nset output /etc/passwdfile');
! 231:
! 232: my %plot_values =
! 233: (
! 234: height => ['300','10','-10','0'],
! 235: width => ['200','100000','-10','0'],
! 236: bgcolor => ['xffffff',@color_test],
! 237: fgcolor => ['x000000',@color_test],
! 238: transparent => ['off',@on_off_test],
! 239: grid => [@on_off_test],
! 240: border => [@on_off_test],
! 241: font => ['small','medium','large','huge','\nset output \/etc\/passwdfile'],
! 242: align => ['left','right','center','wobble','watson-crick']
! 243: );
! 244:
! 245: my %key_values =
! 246: (
! 247: title => ['key title','',undef,'\n set output /etc/passwdfile\n'],
! 248: box => [@on_off_test],
! 249: pos => ['top left','top right','bottom left','bottom right','outside','below','below outside top left','suprise me']
! 250: );
! 251:
! 252: my %label_values =
! 253: (
! 254: xpos => ['1.3',undef,'-1000000.0','no','\nset output /etc/passwdfile'],
! 255: ypos => ['2.7',undef,'3.141592','no','\nset output /etc/passwdfile'],
! 256: justify => ['center','left','right','no','yes','3.5','\nset output /etc/passwdfile'],
! 257: text => ['label text','\nset output /etc/passwdfile',undef,$long_text]
! 258: );
! 259:
! 260: my %axis_values =
! 261: (
! 262: color => ['x000000',@color_test],
! 263: xmin => ['-5.0','6.0','inf',''],
! 264: xmax => [' 5.0','-6.0','inf',''],
! 265: ymin => ['-4.0','6.0','inf',''],
! 266: ymax => [' 4.0','-6.0','inf',''],
! 267: );
! 268:
! 269: my %curve_values =
! 270: (
! 271: color => ['xff0000',@color_test],
! 272: name => ['curve1'],
! 273: linestyle => ['linespoints']
! 274: );
! 275:
! 276: my @function_values = ('sin(x)','f(x)=cos(x)','e^x','ln(t)','\nset output /etc/passwdfile\n','x^x');
! 277:
! 278: my @data_values =
! 279: (
! 280: [
! 281: [ -3.0,-2.0,-1.0, 0.0, 1.0, 2.0, 3.0 ],
! 282: [ -3.0, 2.0,-1.0, 3.3, 1.0, 2.0,-3.0 ]
! 283: ]
! 284: );
! 285:
! 286: my @title_values = ('title 1','\nset output /etc/passwdfile',undef,
! 287: $long_text);
! 288:
! 289: my @xlabel_values = ('xlabel 1','\nset output /etc/passwdfile',undef,
! 290: $long_text);
! 291:
! 292: my @ylabel_values = ('ylabel 1','\nset output /etc/passwdfile',undef,
! 293: $long_text);
! 294:
! 295: ##############################################################
! 296: ## ##
! 297: ## Put it all together ##
! 298: ## ##
! 299: ##############################################################
! 300:
! 301: # run through plot variations
! 302: if ( (! -e 'plottest') && (! -d 'plottest')) {
! 303: system 'mkdir plottest';
! 304: }
! 305:
! 306: &write_tests(\%plot_values,'plot','plot_test_p_%02d_%02d',
! 307: q/$plot->{$key}/);
! 308: &write_tests(\%key_values,'key','plot_test_k_%02d_%02d',
! 309: q/$plot->{'key'}->{$key}/);
! 310: &write_tests(\%label_values,'label','plot_test_l_%02d_%02d',
! 311: q/$plot->{'labels'}->[0]->{$key}/);
! 312: &write_tests(\%axis_values,'axis','plot_test_a_%02d_%02d',
! 313: q/$plot->{'axis'}->{$key}/);
! 314: &write_tests(\@title_values,'title','plot_test_t_%02d',
! 315: q/$plot->{'title'}/);
! 316: &write_tests(\@xlabel_values,'xlabel','plot_test_xl_%02d',
! 317: q/$plot->{'xlabel'}/);
! 318: &write_tests(\@ylabel_values,'ylabel','plot_test_yl_%02d',
! 319: q/$plot->{'ylabel'}/);
! 320:
! 321: sub write_tests {
! 322: my $tmp = shift;
! 323: my $dir = shift;
! 324: system("mkdir plottest/$dir") if (! -d $dir);
! 325: #
! 326: my $filenameTemplate = shift;
! 327: my $place = shift;
! 328: if (ref($tmp) eq 'HASH') {
! 329: my %valuesHash = %{$tmp};
! 330: my @Keys = keys(%valuesHash);
! 331: for (my $i = 0; $i<=$#Keys; $i++) {
! 332: my $key = $Keys[$i];
! 333: for (my $j = 0; $j<=$#{$valuesHash{$key}}; $j++) {
! 334: my $plot = &set_defaults(); # hash reference
! 335: eval($place.'=$valuesHash{$key}[$j]');
! 336: my $filename = sprintf("plottest/$dir/$filenameTemplate.problem",$i,$j);
! 337: open FILE,">$filename" || die "Unable to open $filename\n";
! 338: print FILE "<problem>\n".&insert_plot($plot) ."\n</problem>\n";
! 339: close FILE;
! 340: }
! 341: }
! 342: } elsif (ref($tmp) eq 'ARRAY') {
! 343: my @valuesArray = @{$tmp};
! 344: for (my $i = 0; $i<=$#valuesArray; $i++) {
! 345: my $key = $valuesArray[$i];
! 346: my $plot = &set_defaults(); # hash reference
! 347: eval($place.'=$key');
! 348: my $filename = sprintf("plottest/$dir/$filenameTemplate.problem",$i);
! 349: open FILE,">$filename" || die "Unable to open $filename\n";
! 350: print FILE "<problem>\n".&insert_plot($plot) ."\n</problem>\n";
! 351: close FILE;
! 352: }
! 353: }
! 354: }
! 355:
! 356: ##############################################################
! 357: # Set up the default values by taking the first item from each
! 358: # xxx_values->{'attr'} array.
! 359: ##############################################################
! 360: sub set_defaults{
! 361: my $attr;
! 362: my $plot;
! 363: foreach $attr (keys (%plot_values)) {
! 364: $plot->{$attr} = $plot_values{$attr}[0];
! 365: }
! 366: foreach $attr (keys (%key_values)) {
! 367: $plot->{'key'}->{$attr} = $key_values{$attr}[0];
! 368: }
! 369: foreach $attr (keys (%axis_values)) {
! 370: $plot->{'axis'}->{$attr} = $axis_values{$attr}[0];
! 371: }
! 372: foreach $attr (keys (%label_values)) {
! 373: $plot->{'labels'}->[0]->{$attr} = $label_values{$attr}->[0];
! 374: }
! 375: foreach $attr (keys (%curve_values)) {
! 376: $plot->{'curves'}->{$attr} = $curve_values{$attr}[0];
! 377: }
! 378: $plot->{'curves'}->{'data'}[0] = $data_values[0]->[0];
! 379: $plot->{'curves'}->{'data'}[1] = $data_values[0]->[1];
! 380: $plot->{'title'} = $title_values[0];
! 381: $plot->{'xlabel'} = $xlabel_values[0];
! 382: $plot->{'ylabel'} = $ylabel_values[0];
! 383: return $plot;
! 384: }
! 385:
! 386: ##############################################################
! 387: ## ##
! 388: ## Routines to output the plot information ##
! 389: ## ##
! 390: ##############################################################
! 391: sub insert_plot {
! 392: my $plot = shift;
! 393: my $result;
! 394: # plot attributes
! 395: $result .= '<plot ';
! 396: foreach my $attr (keys(%plot_defaults)) {
! 397: $result .= " $attr=\"$plot->{$attr}\"\n";
! 398: }
! 399: $result .= ">\n";
! 400: # Add the components
! 401: $result.=&insert_title ($plot->{'title'}) if (exists($plot->{'title'}));
! 402: $result.=&insert_xlabel($plot->{'xlabel'}) if (exists($plot->{'xlabel'}));
! 403: $result.=&insert_ylabel($plot->{'ylabel'}) if (exists($plot->{'ylabel'}));
! 404: $result.=&insert_key($plot->{'key'}) if (exists($plot->{'key'}));
! 405: $result.=&insert_axis($plot->{'axis'}) if (exists($plot->{'axis'}));
! 406: if (exists($plot->{'labels'})) {
! 407: foreach my $label (@{$plot->{'labels'}}) {
! 408: $result .= &insert_label($label) ;
! 409: }
! 410: }
! 411: if (exists($plot->{'curves'})) {
! 412: # foreach my $curve (@{$plot->{'curves'}} ) {
! 413: $result .= &insert_curve($plot->{'curves'});
! 414: # }
! 415: }
! 416: # close up the <plot>
! 417: $result .= "</plot>\n";
! 418: return $result;
! 419: }
! 420:
! 421: sub insert_title { return " <title>$_[0]</title>\n" if (defined($_[0])); }
! 422: sub insert_xlabel { return " <xlabel>$_[0]</xlabel>\n" if (defined($_[0])); }
! 423: sub insert_ylabel { return " <ylabel>$_[0]</ylabel>\n" if (defined($_[0])); }
! 424:
! 425: sub insert_key {
! 426: my $key = shift;
! 427: my $result;
! 428: $result .= " <key\n";
! 429: foreach my $attr (keys(%key_defaults)) {
! 430: $result .= " $attr=\"$key->{$attr}\"\n";
! 431: }
! 432: $result .= " />\n";
! 433: return $result;
! 434: }
! 435:
! 436: sub insert_axis{
! 437: my $axis = shift;
! 438: my $result;
! 439: $result .= " <axis \n";
! 440: foreach my $attr (keys(%axis_defaults)) {
! 441: $result .= " $attr=\"$axis->{$attr}\"\n";
! 442: }
! 443: $result .= " />\n";
! 444: return $result;
! 445: }
! 446:
! 447: sub insert_label {
! 448: my $label = shift;
! 449: my $result;
! 450: $result .= " <label \n";
! 451: foreach my $attr (keys(%label_defaults)) {
! 452: next if ($attr eq 'text');
! 453: $result .= " $attr=\"$label->{$attr}\"\n";
! 454: }
! 455: $result .= " >$label->{'text'}</label>\n";
! 456: return $result;
! 457: }
! 458:
! 459: sub insert_curve {
! 460: my $curve = shift;
! 461: my $result;
! 462: $result .= ' <curve ';
! 463: foreach my $attr (keys(%curve_defaults)) {
! 464: $result .= " $attr=\"$curve->{$attr}\"\n";
! 465: }
! 466: $result .= " >\n";
! 467: if (exists($curve->{'function'})) {
! 468: $result .= &insert_function($curve->{'function'});
! 469: } else {
! 470: $result .= &insert_data($curve->{'data'});
! 471: }
! 472: $result .= " </curve>\n";
! 473: }
! 474:
! 475: sub insert_function {
! 476: my $function = shift;
! 477: my $result;
! 478: $result .= "<function>$function->{'text'}</function>\n";
! 479: return $result;
! 480: }
! 481:
! 482: sub insert_data {
! 483: my $dataArray = shift;
! 484: my $result;
! 485: my $tmp = $";
! 486: $" = ',';
! 487: foreach my $data (@$dataArray) {
! 488: $result .= " <data>@$data</data>\n";
! 489: }
! 490: $"=$tmp;
! 491: return $result;
! 492: }
! 493:
! 494: ##----------------------------------------------------------------------
! 495: 1;
! 496: __END__
! 497:
! 498:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>