File: CacheDisk.cpp

package info (click to toggle)
libopenshot 0.2.7%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,720 kB
  • sloc: cpp: 25,009; python: 93; makefile: 18; ruby: 5; sh: 2
file content (260 lines) | stat: -rw-r--r-- 6,725 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
 * @file
 * @brief Unit tests for openshot::Cache
 * @author Jonathan Thomas <jonathan@openshot.org>
 *
 * @ref License
 */

/* LICENSE
 *
 * Copyright (c) 2008-2019 OpenShot Studios, LLC
 * <http://www.openshotstudios.com/>. This file is part of
 * OpenShot Library (libopenshot), an open-source project dedicated to
 * delivering high quality video editing and animation solutions to the
 * world. For more information visit <http://www.openshot.org/>.
 *
 * OpenShot Library (libopenshot) is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * OpenShot Library (libopenshot) is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
 */

#include <memory>
#include <QDir>

#include <catch2/catch.hpp>

#include "CacheDisk.h"
#include "Json.h"

using namespace openshot;

TEST_CASE( "constructor", "[libopenshot][cachedisk]" )
{
	QDir temp_path = QDir::tempPath() + QString("/constructor/");

	// Create cache object
	CacheDisk c(temp_path.path().toStdString(), "PPM", 1.0, 0.25);

	for (int i = 0; i < 20; i++)
	{
		// Add blank frame to the cache
		auto f = std::make_shared<openshot::Frame>();
		f->number = i;
		c.Add(f);
	}

	CHECK(c.Count() == 20); // Cache should have all frames, with no limit
	CHECK(c.GetMaxBytes() == 0); // Max frames should default to 0

	// Clean up
	c.Clear();
	temp_path.removeRecursively();
}

TEST_CASE( "MaxBytes constructor", "[libopenshot][cachedisk]" )
{
	QDir temp_path = QDir::tempPath() + QString("/maxbytes-constructor/");

	// Create cache object
	CacheDisk c(temp_path.path().toStdString(), "PPM", 1.0, 0.25, 20 * 1024);

	CHECK(c.GetMaxBytes() == 20 * 1024);

	for (int i = 0; i < 20; i++)
	{
		// Add blank frame to the cache
		auto f = std::make_shared<openshot::Frame>();
		f->number = i;
		c.Add(f);
	}

	CHECK(c.Count() == 20);
	CHECK(c.GetMaxBytes() == 20 * 1024);

	// Clean up
	c.Clear();
	temp_path.removeRecursively();
}

TEST_CASE( "SetMaxBytes", "[libopenshot][cachedisk]" )
{
	QDir temp_path = QDir::tempPath() + QString("/set_max_bytes/");

	// Create cache object
	CacheDisk c(temp_path.path().toStdString(), "PPM", 1.0, 0.25);

	// Add frames to disk cache
	for (int i = 0; i < 20; i++)
	{
		// Add blank frame to the cache
		auto f = std::make_shared<openshot::Frame>();
		f->number = i;
		// Add some picture data
		f->AddColor(1280, 720, "Blue");
		f->ResizeAudio(2, 500, 44100, LAYOUT_STEREO);
		f->AddAudioSilence(500);
		c.Add(f);
	}

	CHECK(c.GetMaxBytes() == 0); // Cache defaults max frames to -1, unlimited frames

	// Set max frames
	c.SetMaxBytes(8 * 1024);
	CHECK(c.GetMaxBytes() == 8 * 1024);

	// Set max frames
	c.SetMaxBytes(4 * 1024);
	CHECK(c.GetMaxBytes() == 4 * 1024);

	// Read frames from disk cache
	auto f = c.GetFrame(5);
	CHECK(f->GetWidth() == 320);
	CHECK(f->GetHeight() == 180);
	CHECK(f->GetAudioChannelsCount() == 2);
	CHECK(f->GetAudioSamplesCount() == 500);
	CHECK(f->ChannelsLayout() == LAYOUT_STEREO);
	CHECK(f->SampleRate() == 44100);

	// Check count of cache
	CHECK(c.Count() == 20);

	// Clear cache
	c.Clear();

	// Check count of cache
	CHECK(c.Count() == 0);

	// Delete cache directory
	temp_path.removeRecursively();
}

TEST_CASE( "freshen frames", "[libopensoht][cachedisk]" )
{
	QDir temp_path = QDir::tempPath() + QString("/freshen-frames/");

	// Create cache object
	CacheDisk c(temp_path.path().toStdString(), "PPM", 1.0, 0.25);

	auto f1 = std::make_shared<Frame>(1, 1280, 1024, "#FRIST!");

	c.Add(f1);

	for(int i = 2; i <= 20; i++)
	{
		// Add blank frame to the cache
		auto f = std::make_shared<openshot::Frame>();
		f->number = i;
		// Add some picture data
		f->AddColor(1280, 720, "Blue");
		f->ResizeAudio(2, 500, 44100, LAYOUT_STEREO);
		f->AddAudioSilence(500);
		c.Add(f);
	}

	CHECK(c.Count() == 20);

	// Capture current size of cache
	auto start_bytes = c.GetBytes();

	// Re-add existing frame a few times
	for (int x = 0; x < 5; x++)
	{
		c.Add(f1);
	}

	// Check that size hasn't changed
	CHECK(c.Count() == 20);
	CHECK(c.GetBytes() == start_bytes);

	// Clean up
	c.Clear();
	temp_path.removeRecursively();
}

TEST_CASE( "multiple remove", "[libopenshot][cachedisk]" )
{
	QDir temp_path = QDir::tempPath() + QString("/multiple-remove/");
	CacheDisk c(temp_path.path().toStdString(), "PPM", 1.0, 0.25);

	// Add frames to disk cache
	for (int i = 1; i <= 20; i++)
	{
		// Add blank frame to the cache
		auto f = std::make_shared<openshot::Frame>();
		f->number = i;
		// Add some picture data
		f->AddColor(1280, 720, "Blue");
		f->ResizeAudio(2, 500, 44100, LAYOUT_STEREO);
		f->AddAudioSilence(500);
		c.Add(f);
	}

	CHECK(c.Count() == 20);

	// Remove a single frame
	c.Remove(5);
	CHECK(c.Count() == 19);

	// Remove a range of frames
	c.Remove(4, 20);
	CHECK(c.Count() == 3);

	// Remove the rest
	c.Remove(1, 3);
	CHECK(c.Count() == 0);

	// Delete cache directory
	temp_path.removeRecursively();
}

TEST_CASE( "JSON", "[libopenshot][cachedisk]" )
{
	QDir temp_path = QDir::tempPath() + QString("/cache_json/");

	// Create cache object
	CacheDisk c(temp_path.path().toStdString(), "PPM", 1.0, 0.25);

	// Add some frames (out of order)
	auto f3 = std::make_shared<Frame>(3, 1280, 720, "Blue", 500, 2);
	c.Add(f3);
	CHECK((int)c.JsonValue()["ranges"].size() == 1);
	CHECK(c.JsonValue()["version"].asString() == "1");

	// Add some frames (out of order)
	auto f1 = std::make_shared<Frame>(1, 1280, 720, "Blue", 500, 2);
	c.Add(f1);
	CHECK((int)c.JsonValue()["ranges"].size() == 2);
	CHECK(c.JsonValue()["version"].asString() == "2");

	// Add some frames (out of order)
	auto f2 = std::make_shared<Frame>(2, 1280, 720, "Blue", 500, 2);
	c.Add(f2);
	CHECK((int)c.JsonValue()["ranges"].size() == 1);
	CHECK(c.JsonValue()["version"].asString() == "3");

	// Add some frames (out of order)
	auto f5 = std::make_shared<Frame>(5, 1280, 720, "Blue", 500, 2);
	c.Add(f5);
	CHECK((int)c.JsonValue()["ranges"].size() == 2);
	CHECK(c.JsonValue()["version"].asString() == "4");

	// Add some frames (out of order)
	auto f4 = std::make_shared<Frame>(4, 1280, 720, "Blue", 500, 2);
	c.Add(f4);
	CHECK((int)c.JsonValue()["ranges"].size() == 1);
	CHECK(c.JsonValue()["version"].asString() == "5");

	// Delete cache directory
	c.Clear();
	temp_path.removeRecursively();
}