📄 test.cc
字号:
// test.cc Program to load gp.test// W.Langdon cs.ucl.ac.uk $Revision: 1.46 $//Modifications (reverse order)://WBL 15 Apr 97 Ensure use TEST_STATS, tidy to keep alpha cxx quiet//WBL 4 Apr 97 Make compatible with DEC UNIX Alpha C++ Compiler//WBL 12 Dec 95 Save diskspace by removing TEST_STATS from write_stats//WBL 10 Dec 95 BUGFIX fix problme just introduced. Should have removed // hits[0] +=. Net effect hits[num] being given +1 for each // test _instruction_ in sucessful test sub sequence. //WBL 10 Dec 95 Restore proper pareto fitness (in update_seq_score)// HOWEVER still assumes score=0 unless tests passed!//WBL 4 Dec 95 Try new dynamic scoring for test sequences based on crude// estimate of number of progs in population which can pass them//WBL 18 Oct 95 Support random selection of test sequences//WBL 17 Oct 95 Support hurdle//WBL 15 Oct 95 Bugfix update_score if not STORE_FIT but PARETO//WBL 25 Sep 95 Bugfix ~test so tidies up scorep correctly//WBL 22 Sep 95 Replace list.h by prob.h// move test::update_score into it if not PARETO add passed// Add support of scoreq_ and scorneq_// WBL 31 Aug 1995 Allow store_fit to work without stop_on_error// WBL 31 Aug 1995 Add check that stop_on_error is set// WBL 27 Jun 1995 Support multiple test phases// WBL 27 May 1995 Support set_ok// WBL 26 May 1995 Bugfix ensure write_test's use of start_test is constent// with test::test cf 14 May 1995// WBL 22 May 1995 Add not_run// WBL 14 May 1995 Allow start to define random initial data in store// WBL 7 May 1995 Give credit for each scorxx passed to each op only once// Store fractional part of score in a byte// WBL 3 May 1995 Add STORE_FIT// WBL 20 Apr 1995 Add TRACE_RUN// WBL 20 Apr 1995 Add TIMINGS// WBL 8 Apr 1995 Dont copy $ to output when displaying first line of input// this avoids RCS messing up revision text// WBL 15 Feb 1995 Add PREPASS to display_run// WBL 14 Feb 1995 Change way max_passes is set (cf change in test.run.cc)// close input and tidyup on error rather than exit prog// WBL 30 Jan 1995 new file (based on Load() from chrome.cxx version 1.23)#include <assert.h>#include "pch.h"#include "chrome.h"#include "prob.h"#ifdef TIMINGS#include "time.h"#endif#ifdef TRACE_RUN#include "trace.h"#endif#include "test.h"#include "Primitives.h"#include "gp.h"#ifdef STACK_PRIM#include "stack_prim.h"#endifextern test* ThisTest;#ifdef TIMINGSrun_time* ThisTimmer = NULL;#endif#ifdef TRACE_RUNtrace_run* ThisTrace = NULL;#endif//to avoid read/write data in class testint current_phase = 0; int total_cpu = 0; int tests_run = 0; int tests_apparently_run = 0; int hurdle_passes;#ifdef TEST_STATSint tot_seq_passes[NUM_TEST_SEQUENCES];int tot_seq_max_passes[NUM_TEST_SEQUENCES];int tot_seq_max_passed_test[NUM_TEST_SEQUENCES];int tot_seq_run[NUM_TEST_SEQUENCES];#endif//requires MULTREE// Global variablesextern char scratch[100];//**************************************************************************int c;int tp;//can use function in chrome.cxxextern BOOL iscomment(int c,istream& istr);/*BOOL iscomment(int c,istream& istr) { if(c=='%') { do{istr.get();} while((istr.peek()!=EOF)&&(istr.peek()!='\n')); return TRUE; } else return FALSE;}*/#define EATSPACE c=istr.peek();while(isspace(c)||iscomment(c,istr)) {istr.get();c=istr.peek();}#define GETTOK tp=0;c=istr.peek();while(c!=EOF && !isspace(c) && c!=':' && c!='(' && c!=')' &&tp<79) {scratch[tp++]=istr.get(); c=istr.peek();} scratch[tp]='\0'#ifdef debug#define GETTOK tp=0;c=istr.peek();while(c!=EOF && !isspace(c) &&c!=':' && c!='(' && c!=')' &&tp<79) {scratch[tp++]=istr.get(); c=istr.peek();} scratch[tp]='\0';cout<<" `"<<scratch<<"'"<<flush;//debug#endif /*debug*/int get_label(istream& istr, int test){ int i; istr>>i; if ((i<0) || (i>=10)) { cout<<"GPtests: result bad label `"<<i<<"' " <<"in test "<<test<<endl;#ifndef __DECCXX istr.close();#endif /*__DECCXX*/ } return i;}int result(istream& istr, int test){ EATSPACE; if(istr.peek()=='r') istr.get(); //discard 'r' else { cout<<"GPtests: result not labled `r' in test "<<test<<endl;#ifndef __DECCXX istr.close();#endif /*__DECCXX*/ } return get_label( istr, test);}BOOL test::reserved_value(retval data) const{ return ( (data>=reserved_value_min) && (data <= reserved_value_max))? TRUE : FALSE;}BOOL test::get_reserved_value(istream& istr, int test){ return reserved_value_min + get_label(istr, test);}retval test::read_data(istream& istr, int test){ EATSPACE; if(istr.peek()=='d') { istr.get(); //discard 'd' return random_data[get_label(istr, test)]; } else if(istr.peek()=='u') { istr.get(); //discard 'u' return unique_data[get_label(istr, test)]; } else if(istr.peek()=='r') { istr.get(); //discard 'r' return get_reserved_value(istr, test); } else if(isdigit(istr.peek())||(istr.peek()=='-')) { retval r; istr>>r; if(reserved_value(r)) { cout<<"GPtests: reserved value `"<<r<<"' " <<"in test "<<test<<endl;#ifndef __DECCXX istr.close();#endif /*__DECCXX*/ } return r; } else { cout<<"GPtests: data bad labled `"<<char(istr.peek()) <<"' in test "<<test<<endl;#ifndef __DECCXX istr.close();#endif /*__DECCXX*/ }return 999; //keep compiler happy}retval test::Data( retval data, retval dynamic_data[10+1] ) const{ if(reserved_value(data)) return dynamic_data[data - reserved_value_min]; else return data;}void test::write_data( retval data, ostream& fout ) const{ if(reserved_value(data)) fout<<" r"<<data - reserved_value_min; else fout<<" "<<data;}void test::load_random(){int i;for (i=0;i<10;i++) { do {random_data[i] = random_value(seed);} while (reserved_value(random_data[i]));}for (i=0;i<10;i++) { BOOL Unique; do { int j; do {unique_data [i] = random_value(seed);} while (reserved_value(unique_data[i])); Unique = TRUE; for (j=0;j<10&&(Unique==TRUE);j++) { if(unique_data[i]==random_data[j]) Unique = FALSE; }; for (j=0;(j<i)&&(Unique==TRUE);j++) { if(unique_data[i]==unique_data[j]) Unique = FALSE; }; } while ( Unique == FALSE );}}//end load_randomtest::test(Problem* probl, istream& istr): stop_mask(0), rand_test_min_(0), seed(1){ EATSPACE;GETTOK;if (strcmpi(scratch,"GPtests")!=0) { cout<<"Problem with contents of gp.test `"<<scratch<<"'"<<endl;#ifndef __DECCXX istr.close();#endif /*__DECCXX*/}istr >>test_size;tests = new test_struct[test_size+1]; //ignore zeroth entryif (tests == NULL) { cout<<"Problem creating test_struct["<<test_size+1<<"]\n";#ifndef __DECCXX istr.close();#endif /*__DECCXX*/}break_index = new int[test_size+1]; //ignore zeroth entrycout<<"GP tests "<<test_size<<" ";do{int c=istr.get();if(c!='$'){cout<<char(c);};} while((istr.peek()!=EOF)&&(istr.peek()!='\n'));cout<<endl;#ifdef TEST_STATSmemset(tot_seq_passes,0,sizeof(tot_seq_passes));memset(tot_seq_max_passes,0,sizeof(tot_seq_max_passes));memset(tot_seq_max_passed_test,0,sizeof(tot_seq_max_passed_test));memset(tot_seq_run,0,sizeof(tot_seq_run));#endifmemset(Op_Max,0,sizeof(Op_Max));memset(Op_Ok,0,sizeof(Op_Ok));memset(max_passes,0,sizeof(max_passes));memset(tests,0,sizeof(test_struct)*(test_size+1));memset(break_index,0,sizeof(int)*(test_size+1));memset(phase_end_seq,0,sizeof(phase_end_seq));memset(sequence_start,0,sizeof(sequence_start));int max_passes_sofar = 0;int test_sequence = 0;num_phases = 0;load_random();for (int i = 0; i<test_size;) { EATSPACE; GETTOK; if(strcmpi(scratch,"scoreq")==0) { tests[++i].op = scoreq; tests[i].res = 0; tests[i].arg1 = read_data(istr,i); tests[i].arg2 = read_data(istr,i); } else if(strcmpi(scratch,"scoreq_")==0) { tests[++i].op = scoreq_; tests[i].res = 0; tests[i].arg1 = read_data(istr,i); tests[i].arg2 = read_data(istr,i); //score if true } else if(strcmpi(scratch,"scorneq_")==0) { tests[++i].op = scorneq_; tests[i].res = 0; tests[i].arg1 = read_data(istr,i); tests[i].arg2 = read_data(istr,i); //score if false } else if(strcmpi(scratch,"hurdle")==0) { tests[++i].op = hurdle; tests[i].res = 0; tests[i].arg1 = read_data(istr,i); //score required to continue tests[i].arg2 = 0; } else if(strcmpi(scratch,"break")==0) { max_passes_sofar++; tests[++i].op = break_test; if(max_passes_sofar>=MAX_BREAKS) { cout<<"Too many test breaks, test "<<i<<endl;#ifndef __DECCXX istr.close();#endif /*__DECCXX*/ break; } break_index[i]= max_passes_sofar;//nb start at one } else if(strcmpi(scratch,"end_test")==0) { tests[++i].op = end_test; } else if(strcmpi(scratch,"end_phase")==0) { if (num_phases>=NUM_TEST_PHASES) { cout<<"Too many test phases, test "<<i<<endl;#ifndef __DECCXX istr.close();#endif /*__DECCXX*/ break; } max_passes[num_phases] = max_passes_sofar; phase_end_seq[num_phases++] = test_sequence-1; } else if(strcmpi(scratch,"Finished_tests")==0) { test_size = i; if (num_phases>=NUM_TEST_PHASES) { cout<<"Too many test phases, test "<<i<<endl;#ifndef __DECCXX istr.close();#endif /*__DECCXX*/ break; } max_passes[num_phases] = max_passes_sofar; phase_end_seq[num_phases++] = test_sequence-1; cout<<"GPtests warning reduced to " <<test_size<<" test instructions"; break; } else if(strcmpi(scratch,"seed")==0) { istr>>seed; if (seed<=0) { cout<<"GPtests: seed must be positive, test "<<i<<endl;#ifndef __DECCXX istr.close();#endif /*__DECCXX*/ } } else if(strcmpi(scratch,"random")==0) { load_random(); } else if(strcmpi(scratch,"start")==0) { if (test_sequence>=NUM_TEST_SEQUENCES) { cout<<"Too many test sequences, test "<<i<<endl;#ifndef __DECCXX istr.close();#endif /*__DECCXX*/ break; } tests[++i].op = start_test; istr>>tests[i].arg1; sequence_start[test_sequence++] = i; } else if(strcmpi(scratch,"scorep")==0) { //special tree tests[++i].op = scorep; tests[i].res = 0; istr>>tests[i].arg1; if(tests[i].arg1>0) { tests[i].print = new retval[tests[i].arg1]; retval* p = tests[i].print; for(int k=0;k<tests[i].arg1;k++,p++) *p = read_data(istr,i); }; } else if(strcmpi(scratch,"scorneq")==0) { tests[++i].op = scorneq; tests[i].res = 0; istr>>tests[i].arg1; if(tests[i].arg1>0) { tests[i].print = new retval[tests[i].arg1]; retval* p = tests[i].print; for(int k=0;k<tests[i].arg1;k++,p++) *p = read_data(istr,i); }; } else if(strcmpi(scratch,"stop_on")==0) { istr>>stop_mask; } else if(strcmpi(scratch,"rand_test")==0) { istr>>rand_test_min_; istr>>rand_test_max_fails_; } else { BOOL set_ok_flag = FALSE; if(strcmpi(scratch,"set_ok")==0) { set_ok_flag = TRUE; EATSPACE; GETTOK; } BOOL matched = FALSE; for ( int t = 0; t<NUM_OPERATIONS; t++){ if(probl->TreeNameMatch(t,scratch)==0) { matched = TRUE; if(set_ok_flag) { const int offset = i - sequence_start[test_sequence-1]; assert( offset >= 0 && offset < 256 ); Op_Ok[t][test_sequence-1] = offset; } else { tests[++i].op = t; if(tree_return_value[t]) tests[i].res = result(istr,i); else tests[i].res = 10;//hole for junk if(tree_arg_num[t]>=1) tests[i].arg1 = read_data(istr,i); if(tree_arg_num[t]>=2) tests[i].arg2 = read_data(istr,i); }//end else not set_ok break; } }//end for if(matched==FALSE) { cout<<"GPtests syntax error in test "<<i <<" `"<<scratch<<"'\n";#ifndef __DECCXX istr.close();#endif /*__DECCXX*/ tests[++i].op = end_test; //tidyup test_size = i; break; } }}assert(num_phases>0);//Now caclculate maximum possible scoressequence_start[test_sequence] = test_size+1; //makes loops tiderfor(int p=0;p<num_phases;p++) {for(int t=0;t<=phase_end_seq[p];t++) { ok_sub_sequences(sequence_start[t],sequence_start[t+1]-1,Op_Max[p]); }}for(p=0;p<num_phases;p++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -