⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xutest.cpp

📁 eC++编译器源码
💻 CPP
字号:
/* RPC02-14-91 Generates programs to do unit testing of modules. The format is:
   MODULE moduleNameToBeTested
     any variable declarations to be used in the tests.
   [INIT idStringNoBlanks
     test variable initializations
   ]...
   {TEST initIdString testIdStringNoBlanks repeatCountNumber
   CHECK
     Boolean expressions to complete test
   }...

Here's an example:

MODULE String                          //module to test
      unsigned int c,t;                //global variables used in testing
      char s[1000],u[1000];            
INIT    randomStrings                 //the name of an initialization procedure
				      //its called count times as speced in a TEST
				      //all the variables listed are set in each call
	t 5 0  1 2 999 $c3,998        //5 constants; $c indicates random unsigned
				      //between 3 and 998; note no spaces are allowed
	s 1 $St,'a','a'               //generate a random string of length t using chars between two limits
                                      //$i4,22 for scalar int, $r3.5,7.9 for float $l234,900 for long $s'a','z' for char
                                      //$S9,'a','z' for string, $I5,0,9 int array, $C unsigned array, $R float array, $L long array
	u 1 "hello_world_how_are_you"
INIT    nothing
TEST    randomStrings   Length  10
	c = Length(s);
CHECK
	c!=t
TEST    randomStrings   Occurs 10
	c = OccursCS(s, "hello");
CHECK
	c!=last
*/
#include <stdio.h>
#include <cstring.h>

char name[256], s[256], t[256], u[256], 
     v[256], initId[256], testId[256];
unsigned int i, repCount;

  void err(unsigned int i)
  {
    printf("error %i\n", i);
    HALT;
  };

  void getModule()
  {
    unsigned int i;
    if (scanf("%s",s)!=1) err(1);
    if (stricmp(s, "MODULE")!=0) err(2);
    if (scanf("%s",name)!=1) err(3);
  };

  void getVarDecls()
  {
  unsigned int i;
  for ( ;; ) {
    if (scanf("%s",s)!=1) err(8);
    if (stricmp(s, "INIT")==0) break;
    printf("%s ", s);
    if (strchr(s, ';')>=0) printf("\n");
  };
  };

  void getInit()
  {
    unsigned int i,j,k,m;
    while (stricmp(s, "INIT")==0) {
      if (scanf("%s", s)!=1) err(9);
      printf("void %s()\n{\n", s);
      if (scanf("%s", u)!=1) err(10);
      m = 1;
      for ( ;; ) {
	if (stricmp(u, "TEST")==0) break;
	if (stricmp(u, "INIT")==0) break;
	if (scanf("%u", i)!=1) err(11);
	if (i == 0) err(12);
	printf("switch ((dontUseThisName / %u) %% %u) {\n", m,i);
	m = m*i;
	j = 0;
	do {
	  printf("case %u:\n", j);
	  if (scanf("%s", v)!=1) err(12);
	  if (v[0] == '$') {
	    v[0] = ' ';
	    switch (v[1]) {
	    case 'c':
		 v[1] = ' ';
		 printf("%s = Card(dontUseThisR,%s);\n", u, v);
		 break;
	    case 'i':
		 v[1] = ' ';
		 printf("%s = Int(dontUseThisR,%s);\n", u, v);
		 break;
	    case 'l':
		 v[1] = ' ';
		 printf("%s = Long(dontUseThisR,%s);\n", u, v);
		 break;
	    case 'r':
		 v[1] = ' ';
		 printf("%s = Real(dontUseThisR,%s);\n", u, v);
		 break;
	    case 's':
		 v[1] = ' ';
		 printf("%s = Char(dontUseThisR,%s);\n", u, v);
		 break;
	    case 'C':
		 v[1] = ' ';
		 printf("  CardArray(dontUseThisR,%s,%s);\n", u, v);
		 break;
	    case 'I':
		 v[1] = ' ';
		 printf("  IntArray(dontUseThisR,%s,%s);\n", u, v);
		 break;
	    case 'L':
		 v[1] = ' ';
		 printf("  LongArray(dontUseThisR,%s,%s);\n", u, v);
		 break;
	    case 'R':
		 v[1] = ' ';
		 printf("  RealArray(dontUseThisR,%s,%s);\n", u, v);
		 break;
	    case 'S':
		 v[1] = ' ';
		 printf("  String(dontUseThisR,%s,%s);\n", u, v);
		 break;
	    };
	  } else {
	    printf("%s=", u);
	    printf("%s;\n", v);
	  };
	  printf("break;\n");
	  DEC(i); INC(j);
	} while (i > 0);
	m = 1;
	printf("};\n");
	if (scanf("%s", u)!=1) err(13);
      };
      printf("};\n");
      strcpy(s, u);
    }; /*while*/
};

  boolean getTest()
  {
    unsigned int i;
    if (stricmp(s, "TEST")==0) {
      if (scanf("%s", initId)!=1) err(4);
      if (scanf("%s", testId)!=1) err(5);
      if (scanf("%u", repCount)!=1) err(6);
      printf("for (dontUseThisName=0; dontUseThisName<%u; dontUseThisName++) {\n", repCount);
      printf("  %s();\n  ", initId);
      for ( ;; ) {
	if (scanf("%s", s)!=1) err(7);
	if (stricmp(s, "CHECK")==0) return true;
	printf("%s", s);
	if (strchr(s, ';')>=0) printf("\n  ");
      };
    };
    return false;
  };  

  boolean getCheck()
  {
    if (stricmp(s, "CHECK")==0) {
      printf("if (false) {\n");
      for ( ;; ) {
	if (scanf("%s", s)!=1) {
	  printf("  }; /*if*/\n");
	  return true;
	} else if (stricmp(s, "TEST")==0) {
	  printf("  }; /*if*/\n");
	  return true;
	} else {
	  printf("  } else if (%s)\n", s);
	  printf("    printf(\"%s.%s %%u\",dontUseThisName);%n",name,testId);
	}; /*if*/
      }; /*loop*/
    };
    return false;
  };

void main()
{ int i;
  getModule();
  printf("#include <%s.h>\n", name);
  printf("#include <Random.h>\n#include <RandomVars.h>\n#include <stdio.h>\n");
  getVarDecls();
  printf("  unsigned int dontUseThisName;\n");
  printf("  RandomStream dontUseThisR;\n");
  getInit();
  printf("void main()\n{\n");
  printf("InitSeed(dontUseThisR, 77L, 89L);\n");
  for ( ;; ) {
    if (!getTest()) break;
    if (!getCheck()) break;
    printf("} /*for*/;\n");
  };
  printf("};\n");
  if (scanf("%i",i)!=1) HALT;
};

⌨️ 快捷键说明

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