summaryrefslogtreecommitdiff
path: root/storage/ndb/src/common/util/filetest/FileUnitTest.cpp
blob: bf5fa1fd60f9df949111ccb3c88da25f2b531739 (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
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
/* Copyright (c) 2003-2005 MySQL AB
   Use is subject to license terms

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA */

#include "FileUnitTest.hpp"
#include <File.hpp>

#include <NdbOut.hpp>

typedef bool (*TESTFUNC)(const char*);

typedef const char TESTNAME;
typedef struct 
{
  const char* name;
  TESTFUNC test;
}Tests;

static Tests testCases[] = { {"Create/Write", &FileUnitTest::testWrite}, 
                            {"Read", &FileUnitTest::testRead},
                            {"Exists", &FileUnitTest::testExists},
                            {"File Size", &FileUnitTest::testSize}, 
                            {"Rename", &FileUnitTest::testRename},
                            {"Remove", &FileUnitTest::testRemove} };

static int testFailed = 0;
                          
int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    ndbout << "Usage: filetest <filename>" << endl;
    return 0;
  }
  const char* fileName = argv[1];

  int testCount = (sizeof(testCases) / sizeof(Tests)); 
  ndbout << "Starting " << testCount << " tests..." << endl;
  for (int i = 0; i < testCount; i++)
  {
    ndbout << "-- " << " Test " << i + 1 
         << " [" << testCases[i].name << "] --" << endl;
    if (testCases[i].test(fileName))
    {
      ndbout << "-- Passed --" << endl;
    }    
    else
    {
      ndbout << "-- Failed -- " << endl;
    }
    
  }
  ndbout << endl << "-- " << testCount - testFailed << " passed, " 
       << testFailed << " failed --" << endl;
  return 0;
}


bool 
FileUnitTest::testWrite(const char* aFileName)
{
  bool rc = true;
  File f;
  if (f.open(aFileName, "w"))
  {  
   	f.writeChar("ABABABABABAB ABBABAB ABBABA ABAB JKH KJHA JHHAHAH...");
   	f.writeChar("12129791242 1298371923 912738912 378129837128371128132...\n");    
    f.close();    
  }
  else
  {
    error("testWrite failed: ");
    rc = false;
  }
  return rc;
}

bool 
FileUnitTest::testRead(const char* aFileName)
{
  bool rc = true;
  // Read file
  File f;
  if (f.open(aFileName, "r"))
  {
    long size = f.size();
    ndbout << "File size = " << size << endl;
    ndbout << "Allocating buf of " << size << " bytes" << endl;
    char* buf = new char[size];
    buf[size - 1] = '\0';
    int r = 0;
    while ((r = f.readChar(buf, r, size)) > 0)
    {       
      ndbout << "Read(" << r << "):" << buf << endl;
    }   
    f.close(); 
    delete buf;
  }
  else
  {
    error("readTest failed: ");
    rc = false;
  }  
  return rc;
}

bool 
FileUnitTest::testExists(const char* aFileName)
{
  bool rc = true;
  if (File::exists(aFileName))
  {
    if (File::exists("ThisFileShouldnotbe.txt"))
    {
      rc = false;
      error("testExists failed, the file should NOT be found.");
    }
  }
  else
  {
    rc = false;
    error("testExists failed, the file should exist.");    
  }
  
  return rc;
}


bool 
FileUnitTest::testSize(const char* aFileName)
{
  bool rc = true;
  File f;
  if (f.open(aFileName, "r"))
  {
    long size = f.size();
    if (size <= 0)
    {
      rc = false;
      error("testSize failed, size is <= 0");
    }
    ndbout << "File size = " << size << endl;
  }
  else
  {
    rc = false;
    error("testSize failed, could no open file.");
  }
  f.close();
  return rc;
}

bool 
FileUnitTest::testRename(const char* aFileName)
{
  bool rc = true;
  if (File::rename(aFileName, "filetest_new.txt"))
  {
    if (!File::exists("filetest_new.txt"))
    {
      rc = false;
      error("testRename failed, new file does not exists.");
    }
    else
    {
      ndbout << "Renamed " << aFileName << " to filetest_new.txt" << endl;
    }        
  }
  else
  {
    rc = false;
    error("testRename failed, unable to rename file.");
  }
  
  return rc;
}

bool 
FileUnitTest::testRemove(const char* aFileName)
{
  bool rc = true;
  File f;
  if (f.open("filetest_new.txt", "r"))
  {
    if (!f.remove())
    {
      rc = false;
      error("testRemove failed, could not remove file.");
    }
    else
    {
      if (File::exists("filetest_new"))
      {
        rc = false;
        error("testRemove failed, file was not removed, it still exists.");
      }      
    }         
  } // (f.open("filetest_new", "r"))
  else
  {
    rc = false;
    error("testRemove failed, could not read the file.");
  }
  
 return rc;   
}

void  
FileUnitTest::error(const char* msg)
{
  testFailed++;
  ndbout << "Test failed: " << msg << endl;  
  perror("Errno msg");
}


FileUnitTest::FileUnitTest()
{
  
}

FileUnitTest::~FileUnitTest()
{

}