source: trunk/test/fileutil_test.cc @ 1248

Last change on this file since 1248 was 1248, checked in by Peter, 14 years ago

fixes tests - ticket:223

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1// $Id: fileutil_test.cc 1248 2008-03-19 23:07:30Z 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/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 "Suite.h"
25
26#include "yat/utility/FileUtil.h"
27#include "yat/utility/Exception.h"
28
29#include <fstream>
30#include <iostream>
31#include <string>
32
33int main(int argc,char* argv[])
34{ 
35  using namespace theplu::yat;
36  test::Suite suite(argc, argv);
37  suite.err() << "testing FileUtil ... " << std::endl;
38
39  // checking permissions for current directory
40  try {
41    suite.err() << "FileUtil Test 1" << std::endl;
42    bool testval=true;
43    utility::FileUtil file(".");
44    if (testval=file.exists())
45      suite.err() << "\tfile exists: " << file.path() << std::endl;
46    suite.add(testval);
47    if (!(testval=file.permissions("rwx")))
48      suite.err() << "\trwx permissions on " << file.path() << std::endl;
49    suite.add(!testval);
50    if (!(testval=file.permissions("rx")))
51      suite.err() << "\trx permissions on " << file.path() << std::endl;
52    suite.add(!testval);
53    if (!(testval=file.permissions("d")))
54      suite.err() << "\td permissions on " << file.path() << std::endl;
55    suite.add(!testval);
56  }
57  catch (utility::IO_error e) {
58    suite.add(false);
59    suite.err() << e.what() << std::endl;
60  }
61
62  // checking permissions on non-existent file with ./ in path
63  try {
64    suite.err() << "FileUtil Test 2" << std::endl;
65    bool testval=true;
66    // filename below should be unique
67    utility::FileUtil file("./fileio_test.sdf34DSF");
68    suite.err() << "\tpath: " << std::endl;
69    if (!(testval=file.exists()))
70      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
71    suite.add(!testval);
72    if (!(testval=file.permissions("r")))
73      suite.err() << "\tr permission on " << file.path() << std::endl;
74    suite.add(!testval);
75    if (!(testval=file.permissions("rwx")))
76      suite.err() << "\trwx permissions on " << file.path() << std::endl;
77    suite.add(!testval);
78    if (!(testval=file.permissions("w")))
79      suite.err() << "\tw permission on " << file.path() << std::endl;
80    suite.add(!testval); // 'w' on non-existent file ok if directory writeable
81    if (!(testval=file.permissions("d")))
82      suite.err() << "\td permission on " << file.path() << std::endl;
83    suite.add(!testval); 
84  }
85  catch (utility::IO_error e) {
86    suite.add(false);
87    suite.err() << e.what() << std::endl;
88  }
89
90  // checking permissions on non-existent file without ./ in path
91  try {
92    suite.err() << "FileUtil Test 3" << std::endl;
93    bool testval=true;
94    // filename below should be unique
95    utility::FileUtil file("fileio_test.sdf34DSF");
96    if (!(testval=file.exists()))
97      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
98    suite.add(!testval);
99    if (!(testval=file.permissions("r")))
100      suite.err() << "\tr permission on " << file.path() << std::endl;
101    suite.add(!testval);
102    if (!(testval=file.permissions("rwx")))
103      suite.err() << "\trwx permissions on " << file.path() << std::endl;
104    suite.add(!testval);
105    if (!(testval=file.permissions("w")))
106      suite.err() << "\tw permission on " << file.path() << std::endl;
107    suite.add(!testval); // 'w' on non-existent file ok if directory writeable
108    if (!(testval=file.permissions("d")))
109      suite.err() << "\td permission on " << file.path() << std::endl;
110    suite.add(!testval); 
111  }
112  catch (utility::IO_error e) {
113    suite.add(false);
114    suite.err() << e.what() << std::endl;
115  }
116
117  // checking permissions on non-existent file with non-existent
118  // directories in path
119  try {
120    suite.err() << "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      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
126    suite.add(!testval);
127    if ((testval=file.permissions("r")))
128      suite.err() << "\tr permission failed on " << file.path() << std::endl;
129    suite.add(testval);
130    if (testval=file.permissions("rwx"))
131      suite.err() << "\trwx permissions failed on " << file.path() << std::endl;
132    suite.add(testval);
133    if (testval=file.permissions("w"))
134      suite.err() << "\tw permission failed on " << file.path() << std::endl;
135    suite.add(testval);
136    if (testval=file.permissions("d"))
137      suite.err() << "\td permission failed on " << file.path() << std::endl;
138    suite.add(testval); 
139  }
140  catch (utility::IO_error e) {
141    suite.add(false);
142    suite.err() << e.what() << std::endl;
143  }
144
145  // checking permissions on existent file after non-existence file
146  // has been checked
147  try {
148    suite.err() << "FileUtil Test 5" << std::endl;
149    bool testval=true;
150     // intermediate dir must be unique!
151    utility::FileUtil file("./fileio_test.sdf34DSF/file");
152    if (!(testval=file.exists()))
153      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
154    suite.add(!testval);
155    utility::FileUtil file2("fileutil_test.cc");
156    if (!(testval=!file2.exists()))
157      suite.err() << "\tfile does exist: " << file2.path() << std::endl;
158    suite.add(!testval);
159  }
160  catch (utility::IO_error e) {
161    suite.add(false);
162    suite.err() << e.what() << std::endl;
163  }
164
165  try {
166    suite.err() << "FileUtil Test 6" << std::endl;
167    utility::FileUtil file("fileutil_test.cc");
168    file.permissions("rxwa"); // should throw
169    suite.add(false); 
170  }
171  catch (std::invalid_argument& e) {
172    suite.err() << "Expected exception thrown with what: " << e.what() << std::endl;
173  }
174
175  return suite.return_value();
176}
Note: See TracBrowser for help on using the repository browser.