Debian Codegraph @

tests/utils.h

raw symbols SCIP
1 /***************************************************************************
2 copyright : (C) 2007 by Lukas Lalinsky
3 email : [email protected]
4 ***************************************************************************/
5
6 /***************************************************************************
7 * This library is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License version *
9 * 2.1 as published by the Free Software Foundation. *
10 * *
11 * This library is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the Free Software *
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
19 * 02110-1301 USA *
20 * *
21 * Alternatively, this file is available under the Mozilla Public *
22 * License Version 1.1. You may obtain a copy of the License at *
23 * http://www.mozilla.org/MPL/ *
24 ***************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #ifdef _WIN32
31 #include <windows.h>
32 #else
33 #include <unistd.h>
34 #include <fcntl.h>
35 #ifdef __HAIKU__
36 #include <fcntl.h>
37 #else
38 #include <sys/fcntl.h>
39 #endif
40 #include <sys/stat.h>
41 #endif
42 #include <cstdio>
43 #include <cstdlib>
44 #include <cstring>
45 #include <string>
46 #include <fstream>
47
48 using namespace std;
49
50 inline string testFilePath(const string &filename)
51 {
52 return string(TESTS_DIR "data/") + filename;
53 }
54
55 #define TEST_FILE_PATH_C(f) testFilePath(f).c_str()
56
57 inline string copyFile(const string &filename, const string &ext)
58 {
59 char testFileName[1024];
60
61 #ifdef TESTS_TMPDIR
62 snprintf(testFileName, sizeof(testFileName), "%s/taglib-test%s", TESTS_TMPDIR, ext.c_str());
63 #elif defined _WIN32
64 char tempDir[MAX_PATH + 1];
65 GetTempPathA(sizeof(tempDir), tempDir);
66 wsprintfA(testFileName, "%s\\taglib-test%s", tempDir, ext.c_str());
67 #else
68 snprintf(testFileName, sizeof(testFileName), "%s/taglib-test%s", P_tmpdir, ext.c_str());
69 #endif
70
71 string sourceFileName = testFilePath(filename) + ext;
72 ifstream source(sourceFileName.c_str(), std::ios::binary);
73 ofstream destination(testFileName, std::ios::binary);
74 destination << source.rdbuf();
75 return string(testFileName);
76 }
77
78 inline void deleteFile(const string &filename)
79 {
80 remove(filename.c_str());
81 }
82
83 inline bool fileEqual(const string &filename1, const string &filename2)
84 {
85 char buf1[BUFSIZ];
86 char buf2[BUFSIZ];
87
88 ifstream stream1(filename1.c_str(), ios_base::in | ios_base::binary);
89 ifstream stream2(filename2.c_str(), ios_base::in | ios_base::binary);
90
91 if(!stream1 && !stream2) return true;
92 if(!stream1 || !stream2) return false;
93
94 for(;;)
95 {
96 stream1.read(buf1, BUFSIZ);
97 stream2.read(buf2, BUFSIZ);
98
99 streamsize n1 = stream1.gcount();
100 streamsize n2 = stream2.gcount();
101
102 if(n1 != n2) return false;
103
104 if(n1 == 0) break;
105
106 if(memcmp(buf1, buf2, static_cast<size_t>(n1)) != 0) return false;
107 }
108
109 return stream1.good() == stream2.good();
110 }
111
112 #ifdef TAGLIB_STRING_H
113
114 namespace TagLib {
115
116 inline String longText(size_t length, bool random = false)
117 {
118 const wchar_t chars[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";
119
120 std::wstring text(length, L'X');
121
122 if(random) {
123 for(size_t i = 0; i < length; ++i)
124 text[i] = chars[rand() % 53];
125 }
126
127 return String(text);
128 }
129 } // namespace TagLib
130
131 #endif
132
133 class ScopedFileCopy
134 {
135 public:
136 ScopedFileCopy(const string &filename, const string &ext, bool deleteFile=true) :
137 m_deleteFile(deleteFile),
138 m_filename(copyFile(filename, ext))
139 {
140 }
141
142 ~ScopedFileCopy()
143 {
144 if(m_deleteFile)
145 deleteFile(m_filename);
146 }
147
148 ScopedFileCopy(const ScopedFileCopy &) = delete;
149 ScopedFileCopy &operator=(const ScopedFileCopy &) = delete;
150
151 string fileName() const
152 {
153 return m_filename;
154 }
155
156 private:
157 const bool m_deleteFile;
158 const string m_filename;
159 };