Debian Codegraph @

tests/test_file.cpp

raw symbols SCIP
1 /***************************************************************************
2 copyright : (C) 2015 by Tsuda Kageyu
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 #include "tfile.h"
27 #include "plainfile.h"
28 #include <cppunit/extensions/HelperMacros.h>
29 #include "utils.h"
30
31 using namespace TagLib;
32
33 class TestFile : public CppUnit::TestFixture
34 {
35 CPPUNIT_TEST_SUITE(TestFile);
36 CPPUNIT_TEST(testFindInSmallFile);
37 CPPUNIT_TEST(testRFindInSmallFile);
38 CPPUNIT_TEST(testSeek);
39 CPPUNIT_TEST(testTruncate);
40 CPPUNIT_TEST_SUITE_END();
41
42 public:
43
44 void testFindInSmallFile()
45 {
46 ScopedFileCopy copy("empty", ".ogg");
47 std::string name = copy.fileName();
48 {
49 PlainFile file(name.c_str());
50 file.seek(0);
51 file.writeBlock(ByteVector("0123456239", 10));
52 file.truncate(10);
53 }
54 {
55 PlainFile file(name.c_str());
56 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(10), file.length());
57
58 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(2), file.find(ByteVector("23", 2)));
59 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(2), file.find(ByteVector("23", 2), 2));
60 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(7), file.find(ByteVector("23", 2), 3));
61
62 file.seek(0);
63 const ByteVector v = file.readBlock(file.length());
64 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(10), v.size());
65
66 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(v.find("23")), file.find("23"));
67 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(v.find("23", 2)), file.find("23", 2));
68 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(v.find("23", 3)), file.find("23", 3));
69 }
70 }
71
72 void testRFindInSmallFile()
73 {
74 ScopedFileCopy copy("empty", ".ogg");
75 std::string name = copy.fileName();
76 {
77 PlainFile file(name.c_str());
78 file.seek(0);
79 file.writeBlock(ByteVector("0123456239", 10));
80 file.truncate(10);
81 }
82 {
83 PlainFile file(name.c_str());
84 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(10), file.length());
85
86 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(7), file.rfind(ByteVector("23", 2)));
87 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(7), file.rfind(ByteVector("23", 2), 7));
88 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(2), file.rfind(ByteVector("23", 2), 6));
89
90 file.seek(0);
91 const ByteVector v = file.readBlock(file.length());
92 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(10), v.size());
93
94 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(v.rfind("23")), file.rfind("23"));
95 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(v.rfind("23", 7)), file.rfind("23", 7));
96 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(v.rfind("23", 6)), file.rfind("23", 6));
97 }
98 }
99
100 void testSeek()
101 {
102 ScopedFileCopy copy("empty", ".ogg");
103 std::string name = copy.fileName();
104
105 PlainFile f(name.c_str());
106 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(0), f.tell());
107 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(4328), f.length());
108
109 f.seek(100, File::Beginning);
110 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(100), f.tell());
111 f.seek(100, File::Current);
112 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(200), f.tell());
113 f.seek(-300, File::Current);
114 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(200), f.tell());
115
116 f.seek(-100, File::End);
117 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(4228), f.tell());
118 f.seek(-100, File::Current);
119 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(4128), f.tell());
120 f.seek(300, File::Current);
121 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(4428), f.tell());
122 }
123
124 void testTruncate()
125 {
126 ScopedFileCopy copy("empty", ".ogg");
127 std::string name = copy.fileName();
128
129 {
130 PlainFile f(name.c_str());
131 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(4328), f.length());
132
133 f.truncate(2000);
134 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(2000), f.length());
135 }
136 {
137 PlainFile f(name.c_str());
138 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(2000), f.length());
139 }
140 }
141
142 };
143
144 CPPUNIT_TEST_SUITE_REGISTRATION(TestFile);