summaryrefslogtreecommitdiff
path: root/src/tests/xb_test_file.cpp
blob: 4614805f767edc5251e1faf21b439a7855faacd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* xb_test_file.cpp

Copyright (c) 1997,2003,2014,2022 Gary A Kunkel

The xb64 software library is covered under the terms of the GPL Version 3, 2007 license.

Email Contact:

    XDB-devel@lists.sourceforge.net
    XDB-users@lists.sourceforge.net

*/

// This program tests the file functions

// usage:   xb_test_file QUITE|NORMAL|VERBOSE


#include "xbase.h"


using namespace xb;


#include "tstfuncs.cpp"


int main( int argCnt, char **av )
{
  int rc = 0;
  int po = 1;          /* print option */
                       /* 0 - QUIET    */
                       /* 1 - NORMAL   */
                       /* 2 - VERBOSE  */

  if( argCnt > 1 ) {
    if( av[1][0] == 'Q' )
      po = 0;
    else if( av[1][0] == 'V' )
      po = 2;
  }
  xbXBase x;

  #ifdef XB_LOGGING_SUPPORT
  x.EnableMsgLogging();
  if( po ){
    std::cout << "Logfile is [" << x.GetLogFqFileName().Str() << "]" << std::endl;
  }
  xbString sMsg;
  sMsg.Sprintf( "Program [%s] initializing...", av[0] );
  x.WriteLogMessage( sMsg );
  #endif

  InitTime();
  xbFile  f( &x );
  xbString sWrkStr;
  xbString sWrkStr2;
  sWrkStr = PROJECT_DATA_DIR;
  f.SetDataDirectory( PROJECT_DATA_DIR );

  #ifdef WIN32
    sWrkStr.SwapChars( '/', '\\' );
  #else
    sWrkStr.SwapChars( '\\', '/' );
  #endif

  rc += TestMethod( po, "Set/GetDataDirectory()", f.GetDataDirectory(), sWrkStr, sWrkStr.Len());

  f.SetFileName( "TestFile.txt" );
  sWrkStr = "TestFile.txt";
  rc += TestMethod( po, "Set/GetFileName()", f.GetFileName(), sWrkStr, sWrkStr.Len());

  f.GetFileType( sWrkStr );
  rc += TestMethod( po, "GetFileType()", sWrkStr, "TXT", 3 );

  #ifdef WIN32
  sWrkStr = "\\my\\directory\\";
  #else
  sWrkStr = "/my/directory/";
  #endif

  f.SetDirectory( sWrkStr );
  rc += TestMethod( po, "Set/GetDirectory()", f.GetDirectory(), sWrkStr, sWrkStr.Len());

  sWrkStr += "TestFile.txt";
  rc += TestMethod( po, "GetFqFileName()", f.GetFqFileName(), sWrkStr, 26 );

  #ifdef WIN32
  sWrkStr  = "\\some\\directory\\myfile.dat";
  sWrkStr2 = "\\some\\directory\\";
  #else
  sWrkStr  = "/some/directory/myfile.dat";
  sWrkStr2 = "/some/directory/";
  #endif

  f.SetFqFileName( sWrkStr );
  rc += TestMethod( po, "GetDirectory()", f.GetDirectory(), sWrkStr2, 16 );
  rc += TestMethod( po, "GetFileName()", f.GetFileName(), "myfile.dat", 10 );

  rc += TestMethod( po, "NameSuffixMissing()", f.NameSuffixMissing( "myfile.dbf", 1 ), 0 );
  rc += TestMethod( po, "NameSuffixMissing()", f.NameSuffixMissing( "myfile", 1 ), 1 );
  rc += TestMethod( po, "NameSuffixMissing()", f.NameSuffixMissing( "MYFILE", 1 ), 2 );

  f.SetDirectory( PROJECT_DATA_DIR );
  f.SetFileName( "xbfile.txt" );

  rc += TestMethod( po, "xbFopen()", f.xbFopen( "w+b", XB_MULTI_USER ), XB_NO_ERROR );

  xbString sTest;
  sTest = "Test Data";
  rc += TestMethod( po, "xbWrite()", f.xbFwrite( sTest.Str(), 9, 1 ), XB_NO_ERROR );
  rc += TestMethod( po, "xbFclose()", f.xbFclose(), XB_NO_ERROR );

  rc += TestMethod( po, "xbFopen()", f.xbFopen( "r+b", XB_MULTI_USER ), XB_NO_ERROR );
  rc += TestMethod( po, "xbFseek()", f.xbFseek( 0, SEEK_SET ), XB_NO_ERROR );
  char buf[10];
  for( int i = 0; i < 10; i++ )
    buf[i] = 0x00;
  rc += TestMethod( po, "xbFread()", f.xbFread( buf, 5, 1 ), XB_NO_ERROR );
  rc += TestMethod( po, "xbFread()", buf, "Test ", 5 );

  rc += TestMethod( po, "xbFclose()", f.xbFclose(), XB_NO_ERROR );
  rc += TestMethod( po, "xbRemove()", f.xbRemove(), XB_NO_ERROR );
  xbInt16 iWork = 100;
  char cBuf[9];
  char *p = cBuf;
  f.ePutInt16( cBuf, iWork );
  rc += TestMethod( po, "Put/GetShort()", f.eGetInt16( p ), 100 );

  xbInt32 lWork = 10101;
  f.ePutInt32( p, lWork );
  rc += TestMethod( po, "Put/GetLong()", f.eGetInt32( p ), 10101 );

  lWork = 2147483647;
  f.ePutInt32( p, lWork );
  rc += TestMethod( po, "Put/GetLong()", f.eGetInt32( p ), 2147483647 );
  rc += TestMethod( po, "Put/GetLong()", (xbInt32) f.eGetUInt32( p ), 2147483647 );

  xbDouble d = 123456.789;
  f.ePutDouble( p, d );
  rc += TestMethod( po, "Put/GetDouble()", f.eGetDouble( p ), 123456.789 );

  xbString sFqnS;
  xbString sFqnT;
  xbFile f2( &x );
  rc += TestMethod( po, "CreateUniqueFileName()", f2.CreateUniqueFileName( PROJECT_DATA_DIR, "dbf", sFqnS ), XB_NO_ERROR );

  rc += TestMethod( po, "FileExists()", f2.FileExists( sFqnS ), xbFalse );
  rc += TestMethod( po, "xbFopen()",  f2.xbFopen( "w+b", sFqnS, XB_SINGLE_USER ), XB_NO_ERROR );
  rc += TestMethod( po, "xbFclose()", f2.xbFclose(), XB_NO_ERROR );
  rc += TestMethod( po, "FileExists()", f2.FileExists( sFqnS ), xbTrue );

  rc += TestMethod( po, "CreateUniqueFileName()", f2.CreateUniqueFileName( PROJECT_DATA_DIR, "dbf", sFqnT ), XB_NO_ERROR );
  rc += TestMethod( po, "xbRename()", f2.xbRename( sFqnS, sFqnT ), XB_NO_ERROR );
  rc += TestMethod( po, "xbRemove()", f.xbRemove( sFqnT ), XB_NO_ERROR );

  xbString sFn;
  rc += TestMethod( po, "GetFileNamePart()", f2.GetFileNamePart( sFqnS , sFn ), XB_NO_ERROR );
  rc += TestMethod( po, "GetFileExtPart()",  f2.GetFileExtPart( sFqnS , sFn ), XB_NO_ERROR );
  rc += TestMethod( po, "GetFileExtPart()",  f2.GetFileDirPart( sFqnS , sFn ), XB_NO_ERROR );

  rc += TestMethod( po, "SetBlockSize()",    f.SetBlockSize( 100 ), XB_INVALID_BLOCK_SIZE );
  rc += TestMethod( po, "SetBlockSize()",    f.SetBlockSize( 512 ), XB_NO_ERROR );
  rc += TestMethod( po, "GetBlockSize()",    (xbInt32) f.GetBlockSize(), 512 );

  char BlockBuf[513];
  memset( BlockBuf, 0x00, 513 );
  rc += TestMethod( po, "xbFopen()", f.xbFopen( "w+b", XB_SINGLE_USER ), XB_NO_ERROR );

  for( int i = 0; i < 512; i++ )
    BlockBuf[i] = 'A';
  rc += TestMethod( po, "WriteBlock()", f.WriteBlock( 0L, 512, BlockBuf ), XB_NO_ERROR );

  for( int i = 0; i < 512; i++ )
    BlockBuf[i] = 'B';
  rc += TestMethod( po, "WriteBlock()", f.WriteBlock( 1L, 512, BlockBuf ), XB_NO_ERROR );

  for( int i = 0; i < 512; i++ )
    BlockBuf[i] = 'C';
  rc += TestMethod( po, "WriteBlock()", f.WriteBlock( 2L, 512, BlockBuf ), XB_NO_ERROR );

  char BlockBuf2[513];
  memset( BlockBuf2, 0x00, 513 );
  rc += TestMethod( po, "ReadBlock()", f.ReadBlock( 2L, 512, BlockBuf2 ), XB_NO_ERROR );

  xbString s1 = BlockBuf;
  xbString s2 = BlockBuf2;

  rc += TestMethod( po, "ReadBlock()", s1, s2, 512 );

  rc += TestMethod( po, "xbTruncate()", f.xbTruncate( 1000 ), XB_NO_ERROR );

  xbUInt64 ullFsize;
  rc += TestMethod( po, "GetFileSize()", f.GetFileSize( ullFsize ), XB_NO_ERROR );
  rc += TestMethod( po, "xbGetFileSize()", (xbInt32) ullFsize, 1000 );
  rc += TestMethod( po, "xbFclose()", f.xbFclose(), XB_NO_ERROR );

  if( po > 0 || rc < 0 )
    fprintf( stdout, "Total Errors = %d\n", rc * -1 );

  #ifdef XB_LOGGING_SUPPORT
  sMsg.Sprintf( "Program [%s] terminating with [%d] errors...", av[0], rc * -1 );
  x.WriteLogMessage( sMsg );
  #endif

  return rc;
}