source: trunk/test/fileutil_test.cc @ 865

Last change on this file since 865 was 865, checked in by Peter, 16 years ago

changing URL to http://trac.thep.lu.se/trac/yat

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1// $Id: fileutil_test.cc 865 2007-09-10 19:41:04Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen
5
6  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "yat/utility/FileUtil.h"
25#include "yat/utility/Exception.h"
26
27#include <fstream>
28#include <iostream>
29#include <string>
30
31int main(const int argc,const char* argv[])
32
33{ 
34  using namespace theplu::yat;
35
36  std::ostream* error;
37  if (argc>1 && argv[1]==std::string("-v"))
38    error = &std::cerr;
39  else {
40    error = new std::ofstream("/dev/null");
41    if (argc>1)
42      std::cout << "fileio_test -v : for printing extra information\n";
43  }
44  *error << "testing FileUtil ... " << std::endl;
45
46  bool ok = true;
47
48  // checking permissions for current directory
49  try {
50    *error << "FileUtil Test 1" << std::endl;
51    bool testval=true;
52    utility::FileUtil file(".");
53    if (testval=file.exists())
54      *error << "\tfile exists: " << file.path() << std::endl;
55    ok&=testval;
56    if (!(testval=file.permissions("rwx")))
57      *error << "\trwx permissions on " << file.path() << std::endl;
58    ok&=!testval;
59    if (!(testval=file.permissions("rx")))
60      *error << "\trx permissions on " << file.path() << std::endl;
61    ok&=!testval;
62  }
63  catch (utility::IO_error e) {
64    ok=false;
65    *error << e.what() << std::endl;
66  }
67
68  // checking permissions on non-existent file with ./ in path
69  try {
70    *error << "FileUtil Test 2" << std::endl;
71    bool testval=true;
72    // filename below should be unique
73    utility::FileUtil file("./fileio_test.sdf34DSF");
74    *error << "\tpath: " << std::endl;
75    if (!(testval=file.exists()))
76      *error << "\tfile does not exist: " << file.path() << std::endl;
77    ok&=!testval;
78    if (!(testval=file.permissions("r")))
79      *error << "\tr permission on " << file.path() << std::endl;
80    ok&=!testval;
81    if (!(testval=file.permissions("rwx")))
82      *error << "\trwx permissions on " << file.path() << std::endl;
83    ok&=!testval;
84    if (!(testval=file.permissions("w")))
85      *error << "\tw permission on " << file.path() << std::endl;
86    ok&=!testval; // 'w' on non-existent file ok if directory writeable
87  }
88  catch (utility::IO_error e) {
89    ok=false;
90    *error << e.what() << std::endl;
91  }
92
93  // checking permissions on non-existent file without ./ in path
94  try {
95    *error << "FileUtil Test 3" << std::endl;
96    bool testval=true;
97    // filename below should be unique
98    utility::FileUtil file("fileio_test.sdf34DSF");
99    if (!(testval=file.exists()))
100      *error << "\tfile does not exist: " << file.path() << std::endl;
101    ok&=!testval;
102    if (!(testval=file.permissions("r")))
103      *error << "\tr permission on " << file.path() << std::endl;
104    ok&=!testval;
105    if (!(testval=file.permissions("rwx")))
106      *error << "\trwx permissions on " << file.path() << std::endl;
107    ok&=!testval;
108    if (!(testval=file.permissions("w")))
109      *error << "\tw permission on " << file.path() << std::endl;
110    ok&=!testval; // 'w' on non-existent file ok if directory writeable
111  }
112  catch (utility::IO_error e) {
113    ok=false;
114    *error << e.what() << std::endl;
115  }
116
117  // checking permissions on non-existent file with non-existent
118  // directories in path
119  try {
120    *error << "FileUtil Test 4" << std::endl;
121    bool testval=true;
122     // intermediate dir must be unique!
123    utility::FileUtil file("./fileio_test.sdf34DSF/file");
124    if (!(testval=file.exists()))
125      *error << "\tfile does not exist: " << file.path() << std::endl;
126    ok&=!testval;
127    if ((testval=file.permissions("r")))
128      *error << "\tr permission failed on " << file.path() << std::endl;
129    ok&=testval;
130    if (testval=file.permissions("rwx"))
131      *error << "\trwx permissions failed on " << file.path() << std::endl;
132    ok&=testval;
133    if (testval=file.permissions("w"))
134      *error << "\tw permission failed on " << file.path() << std::endl;
135    ok&=testval;
136  }
137  catch (utility::IO_error e) {
138    ok=false;
139    *error << e.what() << std::endl;
140  }
141
142  *error << (ok ? "OK" : "Failed") << std::endl;
143  if (error!=&std::cerr)
144    delete error;
145
146  return (ok ? 0 : -1);
147}
Note: See TracBrowser for help on using the repository browser.