1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <nel/misc/file.h>
21 #include <nel/misc/path.h>
22 #include <nel/misc/common.h>
24 // Test suite for NLMISC::CFile behavior
25 struct CUTMiscFile
: public Test::Suite
29 TEST_ADD(CUTMiscFile::copyOneBigFile
);
30 TEST_ADD(CUTMiscFile::copyDifferentFileSize
);
31 TEST_ADD(CUTMiscFile::moveOneBigFile
);
32 TEST_ADD(CUTMiscFile::moveDifferentFileSize
);
33 // Add a line here when adding a new test METHOD
42 _SrcFile
= "__copy_file_src.foo";
43 _DstFile
= "__copy_file_dst.foo";
50 void copyFileSize(uint fileSize
)
52 // create a source file (using standard c code)
53 FILE *fp
= NLMISC::nlfopen(_SrcFile
, "wb");
56 for (uint i
=0; i
<fileSize
; ++i
)
58 uint8 c
= uint8(i
& 0xff);
59 nlverify(fwrite(&c
, 1, 1, fp
) == 1);
64 NLMISC::CFile::copyFile(_DstFile
, _SrcFile
, false);
66 // verify the resulting file
67 fp
= NLMISC::nlfopen(_DstFile
, "rb");
68 TEST_ASSERT(fp
!= NULL
);
71 for (uint i
=0; i
<fileSize
; ++i
)
74 size_t nbRead
= fread(&c
, 1,1, fp
);
75 TEST_ASSERT(nbRead
== 1);
78 TEST_ASSERT_MSG(c
== uint8(i
& 0xff), "File content changed during copy");
79 if (c
!= uint8(i
& 0xff))
89 // check for a big file
90 copyFileSize(1024*1024);
93 void copyDifferentFileSize()
95 // check for a series of size
96 for (uint i
=0; i
<10; ++i
)
102 for (uint i
=0; i
<1024; ++i
)
109 void moveFileSize(size_t fileSize
)
111 // remove the destination if any
112 FILE *fp
= NLMISC::nlfopen(_DstFile
, "rb");
116 NLMISC::CFile::deleteFile(_DstFile
);
119 // create a source file (using standard c code)
120 fp
= NLMISC::nlfopen(_SrcFile
, "wb");
121 nlverify(fp
!= NULL
);
123 for (uint i
=0; i
<fileSize
; ++i
)
125 uint8 c
= uint8(i
& 0xff);
126 nlverify(fwrite(&c
, 1, 1, fp
) == 1);
131 NLMISC::CFile::moveFile(_DstFile
, _SrcFile
);
133 // verify the resulting file
134 fp
= NLMISC::nlfopen(_SrcFile
, "rb");
135 TEST_ASSERT_MSG(fp
== NULL
, "The source file is not removed");
139 fp
= NLMISC::nlfopen(_DstFile
, "rb");
140 TEST_ASSERT(fp
!= NULL
);
143 for (uint i
=0; i
<fileSize
; ++i
)
146 size_t nbRead
= fread(&c
, 1,1, fp
);
147 TEST_ASSERT(nbRead
== 1);
150 TEST_ASSERT_MSG(c
== uint8(i
& 0xff), "File content changed during move");
151 if (c
!= uint8(i
& 0xff))
158 void moveOneBigFile()
160 // check for a big file
161 moveFileSize(1024*1024);
164 void moveDifferentFileSize()
166 // check for a series of size
167 for (uint i
=0; i
<10; ++i
)
173 for (uint i
=0; i
<1024; ++i
)