📄 tst_res.c
字号:
/* * If tst_res() was called with a file, append file contents to the * end of last printed result. */ if ( File != NULL ) cat_file(File); File = NULL;} /* tst_print() *//* * check_env() - Check the value of the environment variable TOUTPUT and * set the global variable T_mode. The TOUTPUT environment * variable should be set to "VERBOSE", "CONDENSE", * "NOPASS", or "DISCARD". If TOUTPUT does not exist or * is not set to a valid value, the default is "VERBOSE". */static voidcheck_env(){ static int first_time = 1; char *value; /* value of TOUTPUT environment variable */#if DEBUG printf("IN check_env\n"); fflush(stdout);#endif if ( !first_time ) return; first_time = 0; if ( (value = getenv(TOUTPUT)) == NULL ) { /* TOUTPUT not defined, use default */ T_mode = VERBOSE; } else if ( strcmp(value, TOUT_CONDENSE_S) == 0 ) { T_mode = CONDENSE; } else if ( strcmp(value, TOUT_NOPASS_S) == 0 ) { T_mode = NOPASS; } else if ( strcmp(value, TOUT_DISCARD_S) == 0 ) { T_mode = DISCARD; } else { /* default */ T_mode = VERBOSE; } return;} /* check_env() *//* * tst_exit() - Call exit() with the value T_exitval, set up by * tst_res(). T_exitval has a bit set for most of the * result types that were seen (including TPASS, TFAIL, * TBROK, TWARN, TCONF). Also, print the last result (if * necessary) before exiting. */voidtst_exit(){#if DEBUG printf("IN tst_exit\n"); fflush(stdout); fflush(stdout);#endif /* * Call tst_flush() flush any ouput in the buffer or the last * result not printed because of CONDENSE mode. */ tst_flush(); /* * Mask out TRETR, TINFO, and TCONF results from the exit status. */ exit(T_exitval & ~(TRETR | TINFO | TCONF));} /* tst_exit() *//* * tst_environ() - Preserve the tst_res() output location, despite any * changes to stdout. */inttst_environ(){ FILE *fdopen(); if ( (T_out = fdopen(dup(fileno(stdout)), "w")) == NULL ) return(-1); else return(0);} /* tst_environ() *//* * tst_brk() - Fail or break current test case, and break the remaining * tests cases. */voidtst_brk(int ttype, char *fname, void (*func)(), const char *arg_fmt, ...){ char tmesg[USERMESG]; /* expanded message */#if DEBUG printf("IN tst_brk\n"); fflush(stdout); fflush(stdout);#endif /* * Expand the arg_fmt string into tmesg, if necessary. */ EXPAND_VAR_ARGS(arg_fmt, tmesg); /* * Only FAIL, BROK, CONF, and RETR are supported by tst_brk(). */ if ( ttype != TFAIL && ttype != TBROK && ttype != TCONF && ttype != TRETR ) { sprintf(Warn_mesg, "tst_brk(): Invalid Type: %d. Using TBROK", ttype); tst_print(TCID, 0, 1, TWARN, Warn_mesg); ttype = TBROK; } /* * Print the first result, if necessary. */ if ( Tst_count < TST_TOTAL ) tst_res(ttype, fname, tmesg); /* * Determine the number of results left to report. */ Tst_range = TST_TOTAL - Tst_count; /* * Print the rest of the results, if necessary. */ if ( Tst_range > 0 ) { if ( ttype == TCONF ) tst_res(ttype, NULL, "Remaining cases not appropriate for configuration"); else if ( ttype == TRETR ) tst_res(ttype, NULL, "Remaining cases retired"); else tst_res(TBROK, NULL, "Remaining cases broken"); } else { Tst_range = 1; Expand_varargs = TRUE; } /* if ( Tst_range > 0 ) */ /* * If no cleanup function was specified, just return to the caller. * Otherwise call the specified function. If specified function * returns, call tst_exit(). */ if ( func != NULL ) { (*func)(); tst_exit(); } return;} /* tst_brk() *//* * tst_brkloop() - Fail or break current test case, and break the * remaining test cases within test case loop. */voidtst_brkloop(int ttype, char *fname, void (*func)(), const char *arg_fmt, ...){ char tmesg[USERMESG]; /* expanded message */#if DEBUG printf("IN tst_brkloop\n"); fflush(stdout); fflush(stdout);#endif /* * Expand the arg_fmt string into tmesg. */ EXPAND_VAR_ARGS(arg_fmt, tmesg); /* * Verify that Tst_lpstart & Tst_lptotal are valid. */ if ( Tst_lpstart < 0 || Tst_lptotal < 0 ) { tst_print(TCID, 0, 1, TWARN, "tst_brkloop(): Tst_lpstart & Tst_lptotal must both be assigned non-negative values"); Expand_varargs = TRUE; return; } /* * Only FAIL, BROK, CONF, and RETR are supported by tst_brkloop(). */ if ( ttype != TFAIL && ttype != TBROK && ttype != TCONF && ttype != TRETR ) { sprintf(Warn_mesg, "tst_brkloop(): Invalid Type: %d. Using TBROK", ttype); tst_print(TCID, 0, 1, TWARN, Warn_mesg); ttype = TBROK; } /* * Print the first result, if necessary. */ if ( Tst_count < Tst_lpstart + Tst_lptotal ) tst_res(ttype, fname, tmesg); /* * Determine the number of results left to report. */ Tst_range = Tst_lptotal + Tst_lpstart - Tst_count; /* * Print the rest of the results, if necessary. */ if ( Tst_range > 0 ) { if ( ttype == TCONF ) tst_res(ttype, NULL, "Remaining cases in loop not appropriate for configuration"); else if ( ttype == TRETR ) tst_res(ttype, NULL, "Remaining cases in loop retired"); else tst_res(TBROK, NULL, "Remaining cases in loop broken"); } else { Tst_range = 1; Expand_varargs = TRUE; } /* if ( Tst_range > 0 ) */ /* * If a cleanup function was specified, call it. */ if ( func != NULL ) (*func)();} /* tst_brkloop() *//* * tst_resm() - Interface to tst_res(), with no filename. */voidtst_resm(int ttype, const char *arg_fmt, ...){ char tmesg[USERMESG]; /* expanded message */#if DEBUG printf("IN tst_resm\n"); fflush(stdout); fflush(stdout);#endif /* * Expand the arg_fmt string into tmesg. */ EXPAND_VAR_ARGS(arg_fmt, tmesg); /* * Call tst_res with a null filename argument. */ tst_res(ttype, NULL, tmesg);} /* tst_resm() *//* * tst_brkm() - Interface to tst_brk(), with no filename. */voidtst_brkm(int ttype, void (*func)(), const char *arg_fmt, ...){ char tmesg[USERMESG]; /* expanded message */#if DEBUG printf("IN tst_brkm\n"); fflush(stdout); fflush(stdout);#endif /* * Expand the arg_fmt string into tmesg. */ EXPAND_VAR_ARGS(arg_fmt, tmesg); /* * Call tst_brk with a null filename argument. */ tst_brk(ttype, NULL, func, tmesg);} /* tst_brkm() *//* * tst_brkloopm() - Interface to tst_brkloop(), with no filename. */voidtst_brkloopm(int ttype, void (*func)(), const char *arg_fmt, ...){ char tmesg[USERMESG]; /* expanded message */#if DEBUG printf("IN tst_brkloopm\n"); fflush(stdout);#endif /* * Expand the arg_fmt string into tmesg. */ EXPAND_VAR_ARGS(arg_fmt, tmesg); /* * Call tst_brkloop with a null filename argument. */ tst_brkloop(ttype, NULL, func, tmesg);} /* tst_brkloopm() *//* * cat_file() - Print the contents of a file to standard out. */static voidcat_file(char *filename){ FILE *fp; /* file pointer */ int b_read; /* number of bytes read with read() */ int b_written; /* number of bytes written with write() */ char buffer[BUFSIZ]; /* read/write buffer; BUFSIZ defined in */ /* stdio.h */#if DEBUG printf("IN cat_file\n"); fflush(stdout);#endif /* * Open the file for reading. */ if ( (fp = fopen(filename, "r")) == NULL ) { sprintf(Warn_mesg, "tst_res(): fopen(%s, \"r\") failed; errno = %d: %s", filename, errno, strerror(errno)); tst_print(TCID, 0, 1, TWARN, Warn_mesg); return; } /* if ( fopen(filename, "r") == -1 ) */ /* * While something to read, continue to read blocks. * From fread(3) man page: * If an error occurs, or the end-of-file is reached, the return * value is zero. */ errno = 0; while ( (b_read = fread((void *)buffer, 1, BUFSIZ, fp)) != (size_t)0 ) { /* * Write what was read to the result output stream. */ if ( (b_written = fwrite((void *)buffer, 1, b_read, T_out)) != b_read ) { sprintf(Warn_mesg, "tst_res(): While trying to cat \"%s\", fwrite() wrote only %d of %d bytes", filename, b_written, b_read); tst_print(TCID, 0, 1, TWARN, Warn_mesg); break; } /* if ( b_written != b_read ) */ } /* while ( fread() != 0 ) */ /* * Check for an fread() error. */ if ( !feof(fp) ) { sprintf(Warn_mesg, "tst_res(): While trying to cat \"%s\", fread() failed, errno = %d: %s", filename, errno, strerror(errno)); tst_print(TCID, 0, 1, TWARN, Warn_mesg); } /* if ( !feof() ) */ /* * Close the file. */ if ( fclose(fp) == EOF ) { sprintf(Warn_mesg, "tst_res(): While trying to cat \"%s\", fclose() failed, errno = %d: %s", filename, errno, strerror(errno)); tst_print(TCID, 0, 1, TWARN, Warn_mesg); } /* if ( fclose(fp) == EOF ) */} /* cat_file() */#ifdef UNIT_TEST/**************************************************************************** * Unit test code: Takes input from stdin and can make the following * calls: tst_res(), tst_resm(), tst_brk(), tst_brkm(), * tst_flush_buf(), tst_exit(). ****************************************************************************/int TST_TOTAL = 10;char *TCID = "TESTTCID";#define RES "tst_res.c UNIT TEST message; ttype = %d; contents of \"%s\":"#define RESM "tst_res.c UNIT TEST message; ttype = %d"main(){ int ttype; int range; char *chrptr; char chr; char fname[MAXMESG]; printf("UNIT TEST of tst_res.c. Options for ttype:\n\ -1 : call tst_exit()\n\ -2 : call tst_flush()\n\ -3 : call tst_brk()\n\ -4 : call tst_brkloop()\n\ -5 : call tst_res() with a range\n\ 0 : call tst_res(TPASS, ...)\n\ 1 : call tst_res(TFAIL, ...)\n\ 2 : call tst_res(TBROK, ...)\n\ 4 : call tst_res(TWARN, ...)\n\ 8 : call tst_res(TRETR, ...)\n\ 16 : call tst_res(TINFO, ...)\n\ 32 : call tst_res(TCONF, ...)\n\n"); while ( 1 ) { printf("Enter ttype (-5,-4,-3,-2,-1,0,1,2,4,8,16,32): "); (void) scanf("%d%c", &ttype, &chr); switch ( ttype ) { case -1: tst_exit(); break; case -2: tst_flush(); break; case -3: printf("Enter the current type (1=FAIL, 2=BROK, 8=RETR, 32=CONF): "); (void) scanf("%d%c", &ttype, &chr); printf("Enter file name (<cr> for none): "); gets(fname); if ( strcmp(fname, "") == 0 ) tst_brkm(ttype, tst_exit, RESM, ttype); else tst_brk(ttype, fname, tst_exit, RES, ttype, fname); break; case -4: printf("Enter the size of the loop: "); (void) scanf("%d%c", &range, &chr); Tst_lpstart = Tst_count; Tst_lptotal = range; printf("Enter the current type (1=FAIL, 2=BROK, 8=RETR, 32=CONF): "); (void) scanf("%d%c", &ttype, &chr); printf("Enter file name (<cr> for none): "); gets(fname); if ( strcmp(fname, "") == 0 ) tst_brkloopm(ttype, NULL, RESM, ttype); else tst_brkloop(ttype, fname, NULL, RES, ttype, fname); break; case -5: printf("Enter the size of the range: "); (void) scanf("%d%c", &Tst_range, &chr); printf("Enter the current type (0,1,2,4,8,16,32): "); (void) scanf("%d%c", &ttype, &chr); /* fall through to tst_res() call */ default: printf("Enter file name (<cr> for none): "); gets(fname); if ( strcmp(fname, "") == 0 ) tst_resm(ttype, RESM, ttype); else tst_res(ttype, fname, RES, ttype, fname); break; } /* switch() */ } /* while() */}#endif /* UNIT_TEST */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -