Fix crash
[ryzomcore.git] / nel / tools / nel_unit_test / ut_misc_file.h
blob486320269e5bdd2cf24234af0fdc6710b88886cb
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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/>.
17 #ifndef UT_MISC_FILE
18 #define UT_MISC_FILE
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
27 CUTMiscFile()
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
36 private:
37 string _SrcFile;
38 string _DstFile;
40 void setup()
42 _SrcFile = "__copy_file_src.foo";
43 _DstFile = "__copy_file_dst.foo";
46 void tear_down()
50 void copyFileSize(uint fileSize)
52 // create a source file (using standard c code)
53 FILE *fp = NLMISC::nlfopen(_SrcFile, "wb");
54 nlverify(fp != NULL);
56 for (uint i=0; i<fileSize; ++i)
58 uint8 c = uint8(i & 0xff);
59 nlverify(fwrite(&c, 1, 1, fp) == 1);
61 fclose(fp);
62 fp = NULL;
64 NLMISC::CFile::copyFile(_DstFile, _SrcFile, false);
66 // verify the resulting file
67 fp = NLMISC::nlfopen(_DstFile, "rb");
68 TEST_ASSERT(fp != NULL);
69 if (fp)
71 for (uint i=0; i<fileSize; ++i)
73 uint8 c;
74 size_t nbRead = fread(&c, 1,1, fp);
75 TEST_ASSERT(nbRead == 1);
76 if (nbRead != 1)
77 break;
78 TEST_ASSERT_MSG(c == uint8(i & 0xff), "File content changed during copy");
79 if (c != uint8(i & 0xff))
80 break;
82 fclose(fp);
87 void copyOneBigFile()
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)
98 copyFileSize(i);
101 srand(1234);
102 for (uint i=0; i<1024; ++i)
104 i += rand()%10;
105 copyFileSize(i);
109 void moveFileSize(size_t fileSize)
111 // remove the destination if any
112 FILE *fp = NLMISC::nlfopen(_DstFile, "rb");
113 if (fp != NULL)
115 fclose(fp);
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);
128 fclose(fp);
129 fp = NULL;
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");
136 if (fp)
137 fclose(fp);
139 fp = NLMISC::nlfopen(_DstFile, "rb");
140 TEST_ASSERT(fp != NULL);
141 if (fp)
143 for (uint i=0; i<fileSize; ++i)
145 uint8 c;
146 size_t nbRead = fread(&c, 1,1, fp);
147 TEST_ASSERT(nbRead == 1);
148 if (nbRead != 1)
149 break;
150 TEST_ASSERT_MSG(c == uint8(i & 0xff), "File content changed during move");
151 if (c != uint8(i & 0xff))
152 break;
154 fclose(fp);
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)
169 moveFileSize(i);
172 srand(1234);
173 for (uint i=0; i<1024; ++i)
175 i += rand()%10;
176 moveFileSize(i);
182 #endif