]> git.sesse.net Git - mlt/blob - src/tests/test_properties/test_properties.cpp
9264b488f2b978169ce9bccfbc4aebf1639c3f0c
[mlt] / src / tests / test_properties / test_properties.cpp
1 /*
2  * Copyright (C) 2013 Dan Dennedy <dan@dennedy.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with consumer library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <QString>
20 #include <QtTest>
21
22 #include <mlt++/Mlt.h>
23 using namespace Mlt;
24
25 class TestProperties: public QObject
26 {
27     Q_OBJECT
28
29 public:
30     TestProperties() {}
31
32 private Q_SLOTS:
33     void InstantiationIsAReference()
34     {
35         Properties p;
36         QCOMPARE(p.ref_count(), 1);
37     }
38
39     void CopyAddsReference()
40     {
41         Properties p;
42         Properties q = p;
43         QCOMPARE(p.ref_count(), 2);
44     }
45
46     void DestructionRemovesReference()
47     {
48         Properties p;
49         Properties* q = new Properties(p);
50         QCOMPARE(p.ref_count(), 2);
51         delete q;
52         QCOMPARE(p.ref_count(), 1);
53     }
54
55     void SetAndGetString()
56     {
57         Properties p;
58         p.set("key", "value");
59         QVERIFY(p.get("key"));
60         QVERIFY(QString(p.get("key")) != QString(""));
61         QCOMPARE(p.get("key"), "value");
62     }
63
64     void SetAndGetInt()
65     {
66         Properties p;
67         int i = 1;
68         p.set("key", i);
69         QCOMPARE(p.get_int("key"), i);
70     }
71
72     void SetAndGetDouble()
73     {
74         Properties p;
75         double d = 1.0;
76         p.set("key", d);
77         QCOMPARE(p.get_double("key"), d);
78     }
79
80     void SetAndGetInt64()
81     {
82         Properties p;
83         int64_t i = 1LL << 32;
84         p.set("key", i);
85         QCOMPARE(p.get_int64("key"), i);
86     }
87
88     void SetAndGetData()
89     {
90         Properties p;
91         const char *value = "value";
92         char* const s = strdup(value);
93         p.set("key", s, strlen(s), free);
94         int size = 0;
95         QCOMPARE((char*) p.get_data("key", size), value);
96         QCOMPARE(size, int(strlen(value)));
97     }
98
99     void IntFromString()
100     {
101         Properties p;
102         const char *s = "-1";
103         int i = -1;
104         p.set("key", i);
105         QCOMPARE(p.get("key"), s);
106         p.set("key", s);
107         QCOMPARE(p.get_int("key"), i);
108     }
109
110     void Int64FromString()
111     {
112         Properties p;
113         const char *s = "-1";
114         int64_t i = -1;
115         p.set("key", i);
116         QCOMPARE(p.get("key"), s);
117         p.set("key", s);
118         QCOMPARE(p.get_int64("key"), i);
119     }
120
121     void DoubleFromString()
122     {
123         Properties p;
124         const char *s = "-1.234567";
125         double d = -1.234567;
126         p.set("key", d);
127         QCOMPARE(p.get("key"), s);
128         p.set("key", s);
129         QCOMPARE(p.get_double("key"), d);
130     }
131
132     void SetNullRemovesProperty()
133     {
134         Properties p;
135         const char *s = NULL;
136         p.set("key", "value");
137         p.set("key", s);
138         QCOMPARE(p.get("key"), s);
139     }
140
141     void SetAndGetHexColor()
142     {
143         Properties p;
144         const char *hexColorString = "0xaabbccdd";
145         int hexColorInt = 0xaabbccdd;
146         p.set("key", hexColorString);
147         QCOMPARE(p.get_int("key"), hexColorInt);
148     }
149
150     void SetAndGetCssColor()
151     {
152         Properties p;
153         const char *cssColorString = "#aabbcc";
154         int cssColorInt = 0xaabbccff;
155         p.set("key", cssColorString);
156         QCOMPARE(p.get_int("key"), cssColorInt);
157
158         const char *cssColorAlphaString = "#00aabbcc";
159         int cssColorAlphaInt = 0xaabbcc00;
160         p.set("key", cssColorAlphaString);
161         QCOMPARE(p.get_int("key"), cssColorAlphaInt);
162     }
163
164     void SetAndGetTimeCode()
165     {
166         Profile profile;
167         Properties p;
168         p.set("_profile", profile.get_profile(), 0);
169         const char *timeString = "11:22:33:04";
170         p.set("key", timeString);
171         QCOMPARE(p.get_int("key"), 1023829);
172         p.set("key", 1023829);
173         QCOMPARE(p.get_time("key", mlt_time_smpte), timeString);
174     }
175
176     void SetAndGetTimeClock()
177     {
178         Profile profile;
179         Properties p;
180         p.set("_profile", profile.get_profile(), 0);
181         const char *timeString = "11:22:33.400";
182         p.set("key", timeString);
183         QCOMPARE(p.get_int("key"), 1023835);
184         p.set("key", 1023835);
185         QCOMPARE(p.get_time("key", mlt_time_clock), timeString);
186     }
187
188     void SetSimpleMathExpression()
189     {
190         Properties p;
191         p.set("key", "@16.0/9.0 *2 +3 -1");
192         QCOMPARE(p.get_int("key"), 5);
193         QCOMPARE(p.get_double("key"), 16.0/9.0 *2 +3 -1);
194     }
195
196     void PassOneProperty()
197     {
198         Properties p[2];
199         const char *s = "value";
200         p[0].set("key", s);
201         QCOMPARE(p[1].get("key"), (void*) 0);
202         p[1].pass_property(p[0], "key");
203         QCOMPARE(p[1].get("key"), s);
204     }
205
206     void PassMultipleByPrefix()
207     {
208         Properties p[2];
209         const char *s = "value";
210         p[0].set("key.one", s);
211         p[0].set("key.two", s);
212         QCOMPARE(p[1].get("key.one"), (void*) 0);
213         QCOMPARE(p[1].get("key.two"), (void*) 0);
214         p[1].pass_values(p[0], "key.");
215         QCOMPARE(p[1].get("one"), s);
216         QCOMPARE(p[1].get("two"), s);
217     }
218
219     void PassMultipleByList()
220     {
221         Properties p[2];
222         const char *s = "value";
223         p[0].set("key.one", s);
224         p[0].set("key.two", s);
225         QCOMPARE(p[1].get("key.one"), (void*) 0);
226         QCOMPARE(p[1].get("key.two"), (void*) 0);
227         p[1].pass_list(p[0], "key.one key.two");
228         QCOMPARE(p[1].get("key.one"), s);
229         QCOMPARE(p[1].get("key.two"), s);
230     }
231
232     void MirrorProperties()
233     {
234         Properties p[2];
235         p[0].mirror(p[1]);
236         p[0].set("key", "value");
237         QCOMPARE(p[1].get("key"), "value");
238     }
239
240     void InheritProperties()
241     {
242         Properties p[2];
243         p[0].set("key", "value");
244         QVERIFY(p[1].get("key") == 0);
245         p[1].inherit(p[0]);
246         QCOMPARE(p[1].get("key"), "value");
247     }
248
249     void ParseString()
250     {
251         Properties p;
252         QCOMPARE(p.get("key"), (void*) 0);
253         p.parse("key=value");
254         QCOMPARE(p.get("key"), "value");
255         p.parse("key=\"new value\"");
256         QCOMPARE(p.get("key"), "new value");
257     }
258
259     void RenameProperty()
260     {
261         Properties p;
262         p.set("key", "value");
263         QVERIFY(p.get("new key") == 0);
264         p.rename("key", "new key");
265         QCOMPARE(p.get("new key"), "value");
266     }
267
268     void SequenceDetected()
269     {
270         Properties p;
271         p.set("1", 1);
272         p.set("2", 2);
273         p.set("3", 3);
274         QVERIFY(p.is_sequence());
275         p.set("four", 4);
276         QVERIFY(!p.is_sequence());
277     }
278
279     void SerializesToYamlTiny()
280     {
281         Properties p[2];
282         p[0].set("key1", "value1");
283         p[0].set("key2", "value2");
284         p[1].set("1", "value3");
285         p[1].set("2", "value4");
286         p[0].set("seq", p[1].get_properties(), 0);
287         QCOMPARE(p[0].serialise_yaml(),
288                 "---\n"
289                 "key1: value1\n"
290                 "key2: value2\n"
291                 "seq:\n"
292                 "  - value3\n"
293                 "  - value4\n"
294                 "...\n");
295     }
296
297     void RadixRespondsToLocale()
298     {
299         Properties p;
300         p.set_lcnumeric("en_US");
301         p.set("key", "0.125");
302         QCOMPARE(p.get_double("key"), double(1) / double(8));
303         p.set_lcnumeric("de_DE");
304         p.set("key", "0,125");
305         QCOMPARE(p.get_double("key"), double(1) / double(8));
306     }
307 };
308
309 QTEST_APPLESS_MAIN(TestProperties)
310
311 #include "test_properties.moc"