File: FileManagerTest.cpp

package info (click to toggle)
clutils 20031216-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,348 kB
  • ctags: 540
  • sloc: sh: 7,191; cpp: 4,128; makefile: 192
file content (385 lines) | stat: -rw-r--r-- 9,611 bytes parent folder | download
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385

// Copyright (c) 2000-2003 Clifton Labs, Inc.  
// All rights reserved.

// Clifton Labs MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
// SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  Clifton Labs SHALL NOT BE
// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, RESULT
// OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.

// By using or copying this Software, Licensee agrees to abide by the
// intellectual property laws, and all other applicable laws of the U.S.,
// and the terms of this license.

// Authors: Dale E. Martin   dmartin@cliftonlabs.com

#include "FileManagerTest.h"
#include <clutils/Debug.h>
#include <clutils/FileManager.h>
#include <clutils/StringUtilities.h>
#include <fstream>
#include <stdio.h>
#include <assert.h>

using std::endl;
using std::ofstream;
using std::cerr;

FileManagerTest *
FileManagerTest::instance(){
  static FileManagerTest *myFileManagerTest = new FileManagerTest();
  
  return myFileManagerTest;
}

int 
FileManagerTest::instanceTest() const {
  int retval = 0;

  clutils::debug << "Starting instance test - ";

  const FileManager *fm1 = FileManager::instance();
  const FileManager *fm2 = FileManager::instance();

  if( fm1 == 0 || fm2 == 0 ){
    cerr << "Got a null instance for a FileManager!" << endl;
    retval = -1;
    goto end;
  }

  if( fm1 != fm2 ){
    cerr << "Got a two separate instances for FileManagers!" << endl;
    retval = -1;
    goto end;
  }

 end:
  if( retval == 0 ){
    clutils::debug << "passed." << endl;
  }

  return retval;
}

int 
FileManagerTest::buildDirectory( const string &dirName ) const {
  int retval = 0;

  clutils::debug << "Starting directory test - ";

  // First we want to make sure that the dir doesn't exist.
  const FileManager *fm = FileManager::instance();
  if( fm->checkFileStatus( dirName ) == FileManager::OK ){
    // Uh oh, the test dir exists.  For the moment we'll complain and exit.
    cerr << "The directory " << dirName << " exists, currently I can't handle this." << endl;
    exit( -1 );
  }

  // Good, it didn't exist.  We need to create it.
  if( fm->makeDirectory( dirName ) != FileManager::OK ){
    cerr << "Got an error making directory " << dirName << endl;
    perror( "Here's why" );
    
    retval = -1;
    goto end;
  }

  if( fm->checkFileStatus( dirName, FileManager::DIRECTORY ) != FileManager::OK ){
    cerr << "Thought I built a directory, but can't find it!" << endl;
    retval = -1;
    goto end;
  }

 end:
  if( retval == 0 ){
    clutils::debug << "passed." << endl;
  }
  return retval;
}

const string
FileManagerTest::buildFileName( const string &dirName, 
				const string &fileSuffix,
				const int fileNum ) const {
  const FileManager *fm = FileManager::instance();

  const string fileName = dirName + fm->getDirectorySeparator() + 
    "testFile" + intToString(fileNum) + fileSuffix;

  return fileName;
}

int
FileManagerTest::buildFiles( const string &dirName, 
			     const string &fileSuffix,
			     const int numFiles ) const {

  clutils::debug << "Starting file creation tests -";

  int retval = 0;

  // Let's build some files.
  for( int i = 0; i < numFiles; i++ ){
    const string fileName = buildFileName( dirName, fileSuffix, i );
    ofstream outfile( fileName.c_str() );
    if( !outfile.good() ){
      cerr << "Couldn't open file " << fileName << endl;
      perror( "Here's why" );
      goto end;
    }
    outfile << "Here's some data!" << endl;
    outfile.close();
  }
  
 end:
  if( retval == 0 ){
    clutils::debug << "passed." << endl;
  }
  return retval;
}

int
FileManagerTest::checkFiles( const string &dirName, 
			     const string &fileSuffix,
			     const int numFiles ) const {

  clutils::debug << "Starting file counting test - ";

  int retval = 0;

  const FileManager *fm = FileManager::instance();
  
  // We expect that there are numCount files in the directory
  const vector<string> *fileList = fm->getAllFiles( fileSuffix + "$", dirName );
  if( fileList == 0 ){
    cerr << "Got a null file list after creating files!" << endl;
    retval = -1;
    goto end;
  }

  if( (int)fileList->size() != numFiles ){
    cerr << "Only found " << fileList->size() << " files in " << dirName
	 << " and was expecting " << numFiles << endl;
    retval = -1;
    goto end;
  }

  // Now we'll create a new file with a different suffix and try again:
  { // New scope to keep compiler happy
    const string fileName = dirName + 
      fm->getDirectorySeparator() + "extrafile" + fileSuffix + "1";

    ofstream newFile( fileName.c_str() );
    newFile << "Here's something to put in here" << endl;
    newFile.close();
    const vector<string> *newFileList = fm->getAllFiles( fileSuffix + "$", dirName );
    if( (int)newFileList->size() != numFiles ){
      cerr << "Oops, a file with a different suffix got counted!" << endl;
      retval = -1;
      goto end;
    }
  }

 end:
  if( retval == 0 ){
    clutils::debug << "passed." << endl;
  }
  return retval;
}

int
FileManagerTest::removeFile( const string &dirName,
			     const string &fileSuffix ) const {
  int retval = 0;
  clutils::debug << "Starting file removal test - ";

  const string fileName = buildFileName( dirName, fileSuffix, 1 );
  const FileManager *fm = FileManager::instance();
  assert( fm->checkFileStatus( fileName, FileManager::REGULAR_FILE ) == FileManager::OK );

  if( fm->unlink( fileName ) != FileManager::OK ){
    perror( "Error" );
    retval = -1;
    goto end;
  }
  
  if( fm->checkFileStatus( fileName ) != FileManager::NOT_FOUND ){
    cerr << "Removed file " << fileName << " but FileManager:checkFileStatus gives "
	 << "erroneous status" << endl;
    retval = -1;
    goto end;
  }

 end:
  if( retval == 0 ){
    clutils::debug << "passed." << endl;
  }

  return retval;
}

int
FileManagerTest::removeFiles( const string &dirName,
			      const string &fileSuffix ) const {
  int retval = 0;
  clutils::debug << "Starting file(s) removal test - ";

  const string fileName = buildFileName( dirName, fileSuffix, 1 );
  const FileManager *fm = FileManager::instance();
  const vector<string> *fileList = fm->getAllFiles( fileSuffix + ".*$", dirName );
  for( vector<string>::const_iterator i = fileList->begin();
       i != fileList->end();
       i++ ){
    const string fileName = (*i);
    assert( fm->checkFileStatus( fileName, FileManager::REGULAR_FILE ) == FileManager::OK );
    if( fm->unlink( fileName ) != FileManager::OK ){
      perror( "Error" );
      retval = -1;
      goto end;
    }
    if( fm->checkFileStatus( fileName ) != FileManager::NOT_FOUND ){
      cerr << "Removed file " << fileName << " but FileManager:checkFileStatus gives "
	   << "erroneous status" << endl;
      retval = -1;
      goto end;
    }
  }

 end:
  if( retval == 0 ){
    clutils::debug << "passed." << endl;
  }

  return retval;
}

int
FileManagerTest::removeDirectory( const string &directoryName ) const {

  clutils::debug << "Starting remove directory test -";

  int retval = 0;
  const FileManager *fm = FileManager::instance();
  assert( fm->checkFileStatus( directoryName, FileManager::DIRECTORY ) ==
	  FileManager::OK );
  if( fm->removeDirectory( directoryName ) != FileManager::OK ){
    perror( "Error" );
    retval = -1;
    goto end;
  }

  if( fm->checkFileStatus( directoryName, FileManager::DIRECTORY ) !=
      FileManager::NOT_FOUND ){
    cerr << "Removed directory " << directoryName << " but FileManager:checkFileStatus gives "
	 << "erroneous status" << endl;
    retval = -1;
    goto end;
  }

 end:
  if( retval == 0 ){
    clutils::debug << "passed" << endl;
  }
  
  return retval;
}

int
FileManagerTest::baseName(){
  clutils::debug << "Starting basename test - ";

  int retval = 0;
  const FileManager *fm = FileManager::instance();
  const string path1 = "/long/path/to/file.c";
  const string path2 = "secondfile.c";

  if( fm->baseName( path1 ) != "file.c" ){
    retval = -1;
    goto end;
  }

  if( fm->baseName( path2 ) != "secondfile.c" ){
    retval = -1;
    goto end;
  }

 end:
  if( retval == 0 ){
    clutils::debug << "passed" << endl;
  }
  else{
    clutils::debug << "failed" << endl;
  }
  return retval;
}



int 
FileManagerTest::regressionTest(){
  int retval = 0;
  const string dirName = "testDir";
  int numFiles = 100;
  const string fileSuffix = ".txt";

  clutils::enableDebug();

  retval = instanceTest();
  if( retval != 0 ){
    goto end;
  }

  // This will build the test dir.
  retval = buildDirectory( dirName );
  if( retval != 0 ){
    goto end;
  }

  // This will write the files into it.
  retval = buildFiles( dirName, fileSuffix, numFiles );
  if( retval != 0 ){
    goto end;
  }

  // Now we'll count the files 
  retval = checkFiles( dirName, fileSuffix, numFiles );
  if( retval != 0 ){
    goto end;
  }

  // Now we'll remove a file
  retval = removeFile( dirName, fileSuffix );
  if( retval != 0 ){
    goto end;
  }

  // Now we'll recount the directory having removed a file.
  numFiles--;
  retval = checkFiles( dirName, fileSuffix, numFiles );
  if( retval != 0 ){
    goto end;
  }

  // Now we'll kill the rest of the files in that subdir
  retval = removeFiles( dirName, fileSuffix );
  if( retval != 0 ){
    goto end;
  }

  // Now let's kill the directory
  retval = removeDirectory( dirName );
  if( retval != 0 ){
    goto end;
  }

  // Basename test
  retval = baseName();
  if( retval != 0 ){
    goto end;
  }

 end:
  return retval;
}