Enable adaptive step size for upload and download limits
[qBittorrent.git] / test / testutilsio.cpp
blob2e2b970ed288b4055cae96a2dc2e95f24301604b
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2023 Mike Tzou (Chocobo1)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include <QtSystemDetection>
30 #include <QObject>
31 #include <QTest>
33 #include "base/global.h"
34 #include "base/path.h"
35 #include "base/utils/io.h"
37 class TestUtilsIO final : public QObject
39 Q_OBJECT
40 Q_DISABLE_COPY_MOVE(TestUtilsIO)
42 public:
43 TestUtilsIO() = default;
45 private slots:
46 void testReadFile() const
48 const Path testFolder = Path(QString::fromUtf8(__FILE__)).parentPath() / Path(u"testdata"_s);
50 const Path size10File = testFolder / Path(u"size10.txt"_s);
51 const QByteArray size10Data = QByteArrayLiteral("123456789\n");
54 const auto readResult = Utils::IO::readFile(size10File, 0);
55 QCOMPARE(readResult.has_value(), false);
56 QCOMPARE(readResult.error().status, Utils::IO::ReadError::ExceedSize);
57 QCOMPARE(readResult.error().message.isEmpty(), false);
60 const auto readResult = Utils::IO::readFile(size10File, 9);
61 QCOMPARE(readResult.has_value(), false);
62 QCOMPARE(readResult.error().status, Utils::IO::ReadError::ExceedSize);
63 QCOMPARE(readResult.error().message.isEmpty(), false);
66 const auto readResult = Utils::IO::readFile(size10File, 10);
67 QCOMPARE(readResult.has_value(), true);
68 QCOMPARE(readResult.value(), size10Data);
71 const auto readResult = Utils::IO::readFile(size10File, 11);
72 QCOMPARE(readResult.has_value(), true);
73 QCOMPARE(readResult.value(), size10Data);
76 const auto readResult = Utils::IO::readFile(size10File, -1);
77 QCOMPARE(readResult.has_value(), true);
78 QCOMPARE(readResult.value(), size10Data);
82 const Path crlfFile = testFolder / Path(u"crlf.txt"_s);
83 const auto readResult = Utils::IO::readFile(crlfFile, -1, QIODevice::Text);
84 QCOMPARE(readResult.has_value(), true);
85 QCOMPARE(readResult.value(), "\n\n");
89 const Path nonExistFile = testFolder / Path(u".non_existent_file_1234"_s);
90 const auto readResult = Utils::IO::readFile(nonExistFile, 1);
91 QCOMPARE(readResult.has_value(), false);
92 QCOMPARE(readResult.error().status, Utils::IO::ReadError::NotExist);
93 QCOMPARE(readResult.error().message.isEmpty(), false);
96 #ifdef Q_OS_UNIX
98 const auto readResult = Utils::IO::readFile(Path(u"/dev/null"_s), 10);
99 QCOMPARE(readResult.has_value(), true);
100 QCOMPARE(readResult.value().length(), 0);
103 const auto readResult = Utils::IO::readFile(Path(u"/dev/null"_s), -1);
104 QCOMPARE(readResult.has_value(), true);
105 QCOMPARE(readResult.value().length(), 0);
109 const auto readResult = Utils::IO::readFile(Path(u"/dev/zero"_s), 10);
110 QCOMPARE(readResult.has_value(), true);
111 QCOMPARE(readResult.value().length(), 0);
114 const auto readResult = Utils::IO::readFile(Path(u"/dev/zero"_s), -1);
115 QCOMPARE(readResult.has_value(), true);
116 QCOMPARE(readResult.value().length(), 0);
118 #endif
122 QTEST_APPLESS_MAIN(TestUtilsIO)
123 #include "testutilsio.moc"