testdriver.cc

来自「CxxTester是一个用于C++代码的通用测试框。它支持测试库系统、有一套方法」· CC 代码 · 共 292 行

CC
292
字号
//----------------------------------------------------------------------////                                                                      //// TestDriver.cpp                                                       ////                                                                      //// this testdriver is a small example for textual regression tests in   //// combination with interactive graphic tests supported by CxxViewer.   ////----------------------------------------------------------------------//#include "CxxTester.h"//----------------------------------------------------------------------//// startup for linux                // startup for windows              ////----------------------------------------------------------------------//int main(int argc, char** argv)     //  BOOL CxxMain::InitInstance() {                                   //  {    return cxxloop(argc,argv);      //      return cxxloop();}                                   //  }//----------------------------------------------------------------------//// the hanoi towers data representation                                 ////----------------------------------------------------------------------//static int theTowers[3][10];static int towSelect = 10;//----------------------------------------------------------------------//// a single hanoimove                                                   ////----------------------------------------------------------------------//void singlemove(int tow[3][10], int from, int to){    int pix, run1 = 0, run2 = 0;    while(tow[from][run1] != 0) run1++;    while(tow[to][run2]   != 0) run2++;    pix = tow[from][run1-1];    if(run2 == 0 || pix < tow[to][run2-1])     {        tow[from][run1-1] = 0;        tow[to][run2] = pix;    }}//----------------------------------------------------------------------//// the hanoi alghorithm with internal testoutput                        ////----------------------------------------------------------------------//void hanoimove(int tow[3][10],int& ctr,int& steps,int top,int from,int to){    if(top > 1) hanoimove(tow,ctr,steps,top-1,from,-to-from);    if(steps > 0)    {        char txt[20];        sprintf(txt,"%c -> %c",'A'+from+1,'A'+to+1);         DOUT["move"][ctr++] = txt;steps--;        singlemove(tow,from+1,to+1);    }    if(top> 1) hanoimove(tow,ctr,steps,top-1,-to-from,to);}//----------------------------------------------------------------------//// display of the Tower graphic                                         ////----------------------------------------------------------------------//void drawTowers(){    ClearBackground(0,0,0);    //    // the heading    //    SetForeground(250,80,120);    DrawLargeString(100,50,"The Towers of Hanoi");    //    // the tower names    //    if(towSelect == -1)    {        SetForeground(255,0,0);    }    else    {        SetForeground(100,0,255);    }    DrawLargeString(85,230, "A");    if(towSelect == 0)    {        SetForeground(255,0,0);    }    else    {        SetForeground(100,0,255);    }    DrawLargeString(185,230,"B");    if(towSelect == 1)    {        SetForeground(255,0,0);    }    else    {        SetForeground(100,0,255);    }    DrawLargeString(285,230,"C");    int tow,run;    //    // the loop across the buildings     //    for(tow = 0; tow < 3; tow++)    {        //        // the loop across the floors        //        for(run = 0; run < 9; run++)        {            int pix = theTowers[tow][run];            //            // the floor elements            //            if(pix != 0)            {                int width = (9 - pix)*4;                SetForeground(150+theTowers[tow][run]*10,0,0);                FillRect(44+tow*100+width,200-run*10,                         136+tow*100-width,210-run*10);            }        }    }	//    // the frames    //    for(tow = 0; tow < 3; tow++)    {        for(int run = 0; run < 9; run++)        {			SetForeground(100+15*run,0,255-15*run);            DrawRect(44+tow*100+4*run,200-run*10,                     136+tow*100-4*run,210-run*10);        }    }}//----------------------------------------------------------------------//// the test entrance for the hanoi regression test                      ////----------------------------------------------------------------------//cxxcall(hanoi){    int ctr = 0;    //    // input parameters are collected    //    int height = DIN["height"];    int steps  = DIN["steps"];    //    // parameter check    //    if(height < 1 || height > 9)    {        DOUT["error"] = "height limits: 1-9";        return;    }    //    // some necessary initializations before the testcall    //    memset(theTowers[0],0,10*sizeof(int));    memset(theTowers[1],0,10*sizeof(int));    memset(theTowers[2],0,10*sizeof(int));    for(int run = 0; run < height; run++)    {        theTowers[0][run] = height - run;     }    //    // the testcall    //    hanoimove(theTowers,ctr,steps,height,-1,1);    drawTowers();}//----------------------------------------------------------------------//// keypress entrance                                                    ////----------------------------------------------------------------------//cxxcall(keypress){    int x   = DIN["x"];    int y   = DIN["y"];    char* k = DIN["key"];    int tow;    //    // which key is pressed     //    if(!strcmp("a",k) || !strcmp("A",k))    {        tow = -1;    }    else if(!strcmp("b",k) || !strcmp("B",k))    {        tow = 0;    }    else if(!strcmp("c",k) || !strcmp("C",k))    {        tow = 1;    }    else    {        //        // draw unchanged tower        //        drawTowers();        return;    }    //    // remember first keypress, execute second one    //    if(towSelect == 10)    {        towSelect = tow;    }    else    {        singlemove(theTowers,towSelect+1,tow+1);        towSelect = 10;    }    drawTowers();}//----------------------------------------------------------------------//// buttonpress entrance                                                 ////----------------------------------------------------------------------//cxxcall(buttonpress){    int x      = DIN["x"];    int y      = DIN["y"];    int button = DIN["button"];    //    // if mouse does not cover tower area: return    //    if(y < 110 || y > 250 || x < 40 || x > 340)    {        //        // draw unchanged tower        //        drawTowers();        return;    }    //    // check which tower is selected    //    int tow;    if(x < 140)    {        tow = -1;    }    else if(x > 240)    {        tow = 1;    }    else    {        tow = 0;    }    //    // remember first click, execute second one    //    if(towSelect == 10)    {       towSelect = tow;    }    else    {        singlemove(theTowers,towSelect+1,tow+1);        towSelect = 10;    }    drawTowers();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?