update test program.

svn path=/trunk/; revision=3783
This commit is contained in:
Robert Dickenson 2002-11-24 19:12:30 +00:00
parent 425bbb1543
commit 11e87876dc
3 changed files with 353 additions and 105 deletions

View file

@ -28,37 +28,27 @@
#ifdef UNICODE
#define _tfopen _wfopen
#define _tunlink _wunlink
#define _TEOF WEOF
#define _gettchar getwchar
#define _puttchar putwchar
#define _THEX_FORMAT _T("0x%04x ")
#define TEST_B1_FILE_SIZE 85
#define TEST_B2_FILE_SIZE 332
#define TEST_B3_FILE_SIZE 83
#define TEST_B4_FILE_SIZE 324
#else /*UNICODE*/
#define _tfopen fopen
#define _tunlink _unlink
#define _TEOF EOF
#define _gettchar getchar
#define _puttchar putchar
#define _THEX_FORMAT "0x%02x "
#define TEST_B1_FILE_SIZE 170
#define TEST_B2_FILE_SIZE 162
#define TEST_B3_FILE_SIZE 166
#define TEST_B4_FILE_SIZE 162
#endif /*UNICODE*/
#define TEST_BUFFER_SIZE 200
#define TEST_FILE_LINES 4
extern BOOL verbose_flagged;
extern BOOL status_flagged;
extern TCHAR test_buffer[TEST_BUFFER_SIZE];
@ -72,80 +62,144 @@ static TCHAR nix_data[] = _T("line1: this is a bunch of readable text.\n")\
_T("line3: followed up with some numerals 1234567890\n")\
_T("line4: done.\n");
#ifdef UNICODE
#define TEST_B1_FILE_SIZE ((((sizeof(dos_data)/2)-1)+TEST_FILE_LINES)/2) // (166+4)/2=85
#define TEST_B2_FILE_SIZE (((sizeof(dos_data)/2)-1)*2) // (166*2) =332
#define TEST_B3_FILE_SIZE ((((sizeof(nix_data)/2)-1)+TEST_FILE_LINES)/2) // (162+4)/2=83
#define TEST_B4_FILE_SIZE (((sizeof(nix_data)/2)-1)*2) // (162*2) =324
#else /*UNICODE*/
#define TEST_B1_FILE_SIZE (sizeof(dos_data)-1+TEST_FILE_LINES) // (166+4)=170
#define TEST_B2_FILE_SIZE (sizeof(dos_data)-1-TEST_FILE_LINES) // (166-4)=162
#define TEST_B3_FILE_SIZE (sizeof(nix_data)-1+TEST_FILE_LINES) // (162+4)=166
#define TEST_B4_FILE_SIZE (sizeof(nix_data)-1) // (162) =162
#endif /*UNICODE*/
static BOOL create_output_file(TCHAR* file_name, TCHAR* file_mode, TCHAR* file_data)
{
BOOL result = FALSE;
FILE *file = _tfopen(file_name, file_mode);
if (file == NULL) {
_tprintf(_T("ERROR: Can't open file \"%s\" for writing\n"), file_name);
return FALSE;
}
if (_fputts(file_data, file) != _TEOF) {
result = TRUE;
if (file != NULL) {
#ifndef _NO_NEW_DEPENDS_
if (_fputts(file_data, file) != _TEOF) {
result = TRUE;
} else {
_tprintf(_T("ERROR: failed to write data to file \"%s\"\n"), file_name);
_tprintf(_T("ERROR: ferror returned %d\n"), ferror(file));
}
#endif
fclose(file);
} else {
_tprintf(_T("ERROR: failed to write to file \"%s\"\n"), file_name);
_tprintf(_T("ERROR: failed to open/create file \"%s\" for output\n"), file_name);
_tprintf(_T("ERROR: ferror returned %d\n"), ferror(file));
}
fclose(file);
return result;
}
static BOOL verify_output_file(TCHAR* file_name, TCHAR* file_mode, TCHAR* file_data)
{
int error_code;
int offset = 0;
int line_num = 0;
BOOL result = FALSE;
BOOL error_flagged = FALSE;
FILE* file = _tfopen(file_name, file_mode);
if (file == NULL) {
_tprintf(_T("ERROR: Can't open file \"%s\" for reading\n"), file_name);
_tprintf(_T("ERROR: (%s) Can't open file for reading\n"), file_name);
_tprintf(_T("ERROR: ferror returned %d\n"), ferror(file));
return FALSE;
} else if (verbose_flagged) {
_tprintf(_T("STATUS: (%s) opened file for reading\n"), file_name);
}
#ifndef _NO_NEW_DEPENDS_
while (_fgetts(test_buffer, TEST_BUFFER_SIZE, file)) {
int length = _tcslen(test_buffer);
_tprintf(_T("STATUS: Verifying %d bytes read from line %d\n"), length, ++line_num);
++line_num;
if (verbose_flagged) {
_tprintf(_T("STATUS: Verifying %d bytes read from line %d\n"), length, line_num);
}
if (_tcsncmp(test_buffer, file_data+offset, length) == 0) {
result = TRUE;
} else {
_tprintf(_T("WARNING: failed to verify data from file \"%s\"\n"), file_name);
if (verbose_flagged) {
int i;
_tprintf(_T("WARNING: (%s) failed to verify file\n"), file_name);
//_tprintf(_T("expected: \"%s\"\n"), file_data+offset);
//_tprintf(_T(" found: \"%s\"\n"), test_buffer);
_tprintf(_T("expected: "));
for (i = 0; i < length, i < 10; i++) {
_tprintf(_T("0x%02x "), file_data+offset+i);
}
_tprintf(_T("\n found: "));
for (i = 0; i < length, i < 10; i++) {
_tprintf(_T("0x%02x "), test_buffer+i);
}
_tprintf(_T("\n"));
} else {
error_flagged = TRUE;
}
}
offset += length;
}
if (!line_num) {
_tprintf(_T("ERROR: failed to read from file \"%s\"\n"), file_name);
error_code = ferror(file);
if (error_code) {
_tprintf(_T("ERROR: (%s) ferror returned %d after reading\n"), file_name, error_code);
perror("Read error");
}
if (!line_num) {
_tprintf(_T("ERROR: (%s) failed to read from file\n"), file_name);
}
if (error_flagged == TRUE) {
_tprintf(_T("ERROR: (%s) failed to verify file\n"), file_name);
result = FALSE;
}
#endif
fclose(file);
return result;
}
static int test_A(TCHAR* file_name, TCHAR* write_mode, TCHAR* read_mode, TCHAR* file_data)
static int create_test_file(TCHAR* file_name, TCHAR* write_mode, TCHAR* read_mode, TCHAR* file_data)
{
_tprintf(_T("STATUS: Attempting to create output file %s\n"), file_name);
if (status_flagged) {
_tprintf(_T("STATUS: Attempting to create output file %s\n"), file_name);
}
if (create_output_file(file_name, write_mode, file_data)) {
_tprintf(_T("STATUS: Attempting to verify output file %s\n"), file_name);
if (status_flagged) {
_tprintf(_T("STATUS: Attempting to verify output file %s\n"), file_name);
}
if (verify_output_file(file_name, read_mode, file_data)) {
_tprintf(_T("SUCCESS: %s verified ok\n"), file_name);
if (status_flagged) {
_tprintf(_T("SUCCESS: %s verified ok\n"), file_name);
}
} else {
_tprintf(_T("ERROR: Can't verify output file %s\n"), file_name);
//_tprintf(_T("ERROR: failed to verify file %s\n"), file_name);
return 2;
}
} else {
_tprintf(_T("ERROR: Can't create output file %s\n"), file_name);
_tprintf(_T("ERROR: failed to create file %s\n"), file_name);
return 1;
}
return 0;
}
static int test_B(TCHAR* file_name, TCHAR* file_mode, int expected)
static int check_file_size(TCHAR* file_name, TCHAR* file_mode, int expected)
{
int count = 0;
FILE* file;
TCHAR ch;
int error_code;
_tprintf(_T("STATUS: checking %s in %s mode\n"), file_name, _tcschr(file_mode, _T('b')) ? _T("binary") : _T("text"));
if (status_flagged) {
//_tprintf(_T("STATUS: (%s) checking for %d bytes in %s mode\n"), file_name, expected, _tcschr(file_mode, _T('b')) ? _T("binary") : _T("text"));
_tprintf(_T("STATUS: (%s) checking for %d bytes with mode %s\n"), file_name, expected, file_mode);
}
file = _tfopen(file_name, file_mode);
if (file == NULL) {
_tprintf(_T("ERROR: Can't open file \"%s\" for reading\n"), file_name);
_tprintf(_T("ERROR: (%s) failed to open file for reading\n"), file_name);
return 1;
}
while ((ch = _fgettc(file)) != _TEOF) {
@ -154,47 +208,94 @@ static int test_B(TCHAR* file_name, TCHAR* file_mode, int expected)
}
++count;
}
error_code = ferror(file);
if (error_code) {
_tprintf(_T("ERROR: (%s) ferror returned %d after reading\n"), file_name, error_code);
perror("Read error");
}
if (verbose_flagged) {
_puttchar(_T('\n'));
// _puttc(_T('\n'), stdout);
}
fclose(file);
if (count == expected) {
_tprintf(_T("PASSED: read %d bytes from %s as expected\n"), count, file_name);
if (status_flagged) {
_tprintf(_T("PASSED: (%s) read %d bytes\n"), file_name, count);
}
} else {
_tprintf(_T("ERROR: read %d bytes from %s, expected %d\n"), count, file_name, expected);
_tprintf(_T("FAILED: (%s) read %d bytes but expected %d using mode \"%s\"\n"), file_name, count, expected, file_mode);
}
return count;
return (count == expected) ? 0 : -1;
}
static int test_C(void)
static int test_console_io(void)
{
#ifndef _NO_NEW_DEPENDS_
TCHAR buffer[81];
TCHAR ch;
int i;
int i, j;
_tprintf(_T("Enter a line: "));
for (i = 0; (i < 80) && ((ch = _gettchar()) != _TEOF) && (ch != _T('\n')); i++) {
//printf("Enter a line for echoing:\n");
_tprintf(_T("Enter a line for echoing:\n"));
//for (i = 0; (i < 80) && ((ch = _gettchar()) != _TEOF) && (ch != _T('\n')); i++) {
for (i = 0; (i < 80) && ((ch = _gettc(stdin)) != _TEOF) && (ch != _T('\n')); i++) {
buffer[i] = (TCHAR)ch;
}
buffer[i] = _T('\0');
for (j = 0; j < i; j++) {
_puttc(buffer[j], stdout);
}
_puttc(_T('\n'), stdout);
_tprintf(_T("%s\n"), buffer);
#endif
return 0;
}
static int test_D(void)
static int test_console_getchar(void)
{
int result = 0;
#ifndef _NO_NEW_DEPENDS_
TCHAR ch;
while ((ch = _gettchar()) != _TEOF) {
//printf("Enter lines for dumping or <ctrl-z><nl> to finish:\n");
_tprintf(_T("Enter lines for dumping or <ctrl-z><nl> to finish:\n"));
//while ((ch = _gettchar()) != _TEOF) {
while ((ch = _gettc(stdin)) != _TEOF) {
_tprintf(_THEX_FORMAT, ch);
//printf("0x%04x ", ch);
}
#endif
return result;
}
static int clean_files(void)
static int test_console_putch(void)
{
int result = 0;
//TCHAR ch;
_putch('1');
_putch('@');
_putch('3');
_putch(':');
_putch('\n');
_putch('a');
_putch('B');
_putch('c');
_putch(':');
_putch('\n');
return result;
}
static int test_unlink_files(void)
{
int result = 0;
//printf("sizeof dos_data: %d\n", sizeof(dos_data));
//printf("sizeof nix_data: %d\n", sizeof(nix_data));
result |= _tunlink(_T("binary.dos"));
result |= _tunlink(_T("binary.nix"));
@ -203,6 +304,26 @@ static int clean_files(void)
return result;
}
static int test_text_fileio(TCHAR* file_name, TCHAR* file_data, int tsize, int bsize)
{
int result = 0;
result = create_test_file(file_name, _T("w"), _T("r"), file_data);
result = check_file_size(file_name, _T("r"), tsize);
result = check_file_size(file_name, _T("rb"), bsize);
return result;
}
static int test_binary_fileio(TCHAR* file_name, TCHAR* file_data, int tsize, int bsize)
{
int result = 0;
result = create_test_file(file_name, _T("wb"), _T("rb"), file_data);
result = check_file_size(file_name, _T("r"), tsize);
result = check_file_size(file_name, _T("rb"), bsize);
return result;
}
static int test_files(int test_num, char* type)
{
int result = 0;
@ -211,43 +332,28 @@ static int test_files(int test_num, char* type)
switch (test_num) {
case 1:
result = test_A(_T("text.dos"), _T("w"), _T("r"), dos_data);
result = test_text_fileio(_T("text.dos"), dos_data, 166, TEST_B1_FILE_SIZE);
break;
case 2:
result = test_A(_T("binary.dos"), _T("wb"), _T("rb"), dos_data);
result = test_binary_fileio(_T("binary.dos"), dos_data, TEST_B2_FILE_SIZE, 166);
break;
case 3:
result = test_A(_T("text.nix"), _T("w"), _T("r"), nix_data);
result = test_text_fileio(_T("text.nix"), nix_data, 162, TEST_B3_FILE_SIZE);
break;
case 4:
result = test_A(_T("binary.nix"), _T("wb"), _T("rb"), nix_data);
result = test_binary_fileio(_T("binary.nix"), nix_data, TEST_B4_FILE_SIZE, 162);
break;
case 5:
result = test_B(_T("text.dos"), _T("r"), 166);
result = test_B(_T("text.dos"), _T("rb"), TEST_B1_FILE_SIZE);
result = test_console_io();
break;
case 6:
result = test_B(_T("binary.dos"), _T("r"), TEST_B2_FILE_SIZE);
result = test_B(_T("binary.dos"), _T("rb"), 166);
result = test_console_getchar();
break;
case 7:
result = test_B(_T("text.nix"), _T("r"), 162);
result = test_B(_T("text.nix"), _T("rb"), TEST_B3_FILE_SIZE);
break;
case 8:
result = test_B(_T("binary.nix"), _T("r"), TEST_B4_FILE_SIZE);
result = test_B(_T("binary.nix"), _T("rb"), 162);
break;
case 9:
result = test_C();
break;
case 0:
result = test_D();
result = test_console_putch();
break;
case -1:
result = clean_files();
result = test_unlink_files();
break;
default:
_tprintf(_T("no test number selected\n"));
@ -255,3 +361,62 @@ static int test_files(int test_num, char* type)
}
return result;
}
#if 1
#else
static int test_files(int test_num, char* type)
{
int result = 0;
printf("performing test: %d (%s)\n", test_num, type);
switch (test_num) {
case 1:
result = create_test_file(_T("text.dos"), _T("w"), _T("r"), dos_data);
break;
case 2:
result = create_test_file(_T("binary.dos"), _T("wb"), _T("rb"), dos_data);
break;
case 3:
result = create_test_file(_T("text.nix"), _T("w"), _T("r"), nix_data);
break;
case 4:
result = create_test_file(_T("binary.nix"), _T("wb"), _T("rb"), nix_data);
break;
case 5:
result = check_file_size(_T("text.dos"), _T("r"), 166);
result = check_file_size(_T("text.dos"), _T("rb"), TEST_B1_FILE_SIZE);
break;
case 6:
result = check_file_size(_T("binary.dos"), _T("r"), TEST_B2_FILE_SIZE);
result = check_file_size(_T("binary.dos"), _T("rb"), 166);
break;
case 7:
result = check_file_size(_T("text.nix"), _T("r"), 162);
result = check_file_size(_T("text.nix"), _T("rb"), TEST_B3_FILE_SIZE);
break;
case 8:
result = check_file_size(_T("binary.nix"), _T("r"), TEST_B4_FILE_SIZE);
result = check_file_size(_T("binary.nix"), _T("rb"), 162);
break;
case 9:
result = test_console_io();
break;
case 0:
result = test_console_getchar();
break;
case -1:
result = test_unlink_files();
break;
default:
_tprintf(_T("no test number selected\n"));
break;
}
return result;
}
#endif

View file

@ -25,71 +25,153 @@
#include <tchar.h>
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#define VERSION "1.00"
#ifdef UNICODE
#define TARGET "UNICODE"
#else
#define TARGET "MBCS"
#endif
#define VERSION 1
#define TEST_BUFFER_SIZE 200
TCHAR test_buffer[TEST_BUFFER_SIZE];
BOOL verbose_flagged = 0;
#ifdef UNICODE
#define TARGET "UNICODE"
w_char_t test_buffer[TEST_BUFFER_SIZE];
#else
#define TARGET "MBCS"
char test_buffer[TEST_BUFFER_SIZE*2];
#endif
BOOL verbose_flagged = 0;
BOOL status_flagged = 0;
int usage(char* argv0)
{
printf("USAGE:\n");
printf("\t%s clean - delete test output files\n", argv0);
printf("\t%s test_number [unicode][ansi]\n", argv0);
printf("USAGE: %s test_id [unicode]|[ansi] [clean]|[status][verbose]\n", argv0);
printf("\tWhere test_id is one of:\n");
printf("\t0 - (default) regression mode, run tests 1-4 displaying failures only\n");
printf("\t1 - Write DOS style eol data to file in text mode (text.dos)\n");
printf("\t2 - Write NIX style eol data to file in binary mode (binary.dos)\n");
printf("\t3 - Write DOS style eol data to file in text mode (text.nix)\n");
printf("\t4 - Write NIX style eol data to file in binary mode (binary.nix)\n");
printf("\t5 - Echo console line input\n");
printf("\t6 - Dump console line input in hex format\n");
printf("\t7 - The source code is your friend\n");
printf("\t[unicode] - perform tests using UNICODE versions of library functions\n");
printf("\t[ansi] - perform tests using ANSI versions of library functions\n");
printf("\t If neither unicode or ansi is specified build default is used\n");
printf("\t[clean] - delete all temporary test output files\n");
printf("\t[status] - enable extra status display while running\n");
printf("\t[verbose] - enable verbose output when running\n");
return 0;
}
int __cdecl main(int argc, char* argv[])
{
int test_num = 0;
int version = 0;
int result = 0;
int i = 0;
printf("%s test application - build %s (%s)\n", argv[0], VERSION, TARGET);
printf("%s test application - build %03d (default: %s)\n", argv[0], VERSION, TARGET);
if (argc < 2) {
return usage(argv[0]);
}
if (argc > 1) {
if (strstr(argv[1], "clean") || strstr(argv[1], "clean")) {
#ifdef UNICODE
return test_unicode_files(-1);
#else
return test_ansi_files(-1);
#endif
}
test_num = atoi(argv[1]);
}
if (argc > 2) {
if (argc > 3) {
for (i = 1; i < argc; i++) {
if (strstr(argv[i], "ansi") || strstr(argv[i], "ANSI")) {
version = 1;
} else if (strstr(argv[i], "unicode") || strstr(argv[i], "UNICODE")) {
version = 2;
} else if (strstr(argv[i], "clean") || strstr(argv[i], "CLEAN")) {
test_num = -1;
} else if (strstr(argv[i], "verbose") || strstr(argv[i], "VERBOSE")) {
verbose_flagged = 1;
}
if (strstr(argv[2], "ansi") || strstr(argv[2], "ANSI")) {
result = test_ansi_files(test_num);
} else if (strstr(argv[2], "unicode") || strstr(argv[2], "UNICODE")) {
result = test_unicode_files(test_num);
} else if (strstr(argv[i], "status") || strstr(argv[i], "STATUS")) {
status_flagged = 1;
} else {
result = test_ansi_files(test_num);
result = test_unicode_files(test_num);
test_num = atoi(argv[1]);
//if (test_num < 0
}
}
#if 1
for (i = test_num; i <= test_num; i++) {
if (!test_num) {
test_num = 4;
i = 1;
}
switch (version) {
case 1:
result = test_ansi_files(i);
break;
case 2:
result = test_unicode_files(i);
break;
default:
result = test_ansi_files(i);
result = test_unicode_files(i);
break;
}
}
#else
if (test_num == 0) {
for (i = 1; i <= 4; i++) {
result = test_ansi_files(i);
result = test_unicode_files(i);
}
} else {
#ifdef UNICODE
result = test_unicode_files(test_num);
#else
result = test_ansi_files(test_num);
#endif
switch (version) {
case 1:
//result = test_ansi_files(test_num);
while (i <= test_num) {
result = test_ansi_files(i);
}
break;
case 2:
//result = test_unicode_files(test_num);
while (i <= test_num) {
result = test_unicode_files(i);
}
break;
default:
while (i <= test_num) {
result = test_ansi_files(i);
result = test_unicode_files(i);
++i;
}
break;
}
}
#endif
printf("finished\n");
return result;
}
#ifndef __GNUC__
char* args[] = { "fileio.exe", "0", "ansi", "verbose"};
char* args1[] = { "fileio.exe", "1", "unicode" };
char* args2[] = { "fileio.exe", "2", "unicode" };
char* args3[] = { "fileio.exe", "3", "unicode" };
char* args4[] = { "fileio.exe", "4", "unicode" };
int __cdecl mainCRTStartup(void)
{
main(2, args);
#if 0
//#if 1
main(2, args1);
main(2, args2);
main(2, args3);
main(2, args4);
//#else
main(3, args1);
main(3, args2);
main(3, args3);
main(3, args4);
//#endif
#endif
return 0;
}
#endif /*__GNUC__*/

View file

@ -11,6 +11,7 @@ TARGET_APPTYPE = console
TARGET_NAME = fileio
#TARGET_CFLAGS = -DDBG -DUNICODE -D_UNICODE
TARGET_CFLAGS = -DDBG
TARGET_SDKLIBS = ntdll.a kernel32.a