summaryrefslogtreecommitdiff
path: root/tests/test-resource.cpp
blob: afccc166402d71a40f44c13cc9b646ed7eb06cd4 (plain)
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
#include "test-resource.h"
#include <QSet>

using namespace ResourcePolicy;

TestResource::TestResource()
{
}

TestResource::~TestResource()
{
}

void TestResource::init()
{
    resource = new Resource();
}

void TestResource::cleanup()
{
    delete resource;
}

void TestResource::testType_data()
{
    QTest::addColumn<ResourceType>("type");
    QTest::addColumn<ResourceType>("expected");

    QTest::newRow("AudioPlayback") << AudioPlaybackResource << AudioPlaybackResource;
    QTest::newRow("VideoPlayback") << VideoPlaybackResource << VideoPlaybackResource;
    QTest::newRow("AudioRecorder") << AudioRecorderResource << AudioRecorderResource;
    QTest::newRow("VideoRecorder") << VideoRecorderResource << VideoRecorderResource;
    QTest::newRow("Invalid") << InvalidResource << InvalidResource;
}

void TestResource::testType()
{
    QFETCH(ResourceType, type);
    QFETCH(ResourceType, expected);
    bool setWasSuccessfull = resource->setType(type);
    ResourceType result = resource->type();

    if(expected == InvalidResource) {
	QEXPECT_FAIL("", "Expecting to fail since type == InvalidResource", Continue);
    }
    QVERIFY(setWasSuccessfull);
    QCOMPARE(result, expected);
}

void TestResource::testOptional_data()
{
    QTest::addColumn<bool>("optional");
    QTest::addColumn<bool>("expected");
    
    QTest::newRow("Resource is optional") << true << true;
    QTest::newRow("Resource is not optional") << false << false;
}

void TestResource::testOptional()
{
    QFETCH(bool, optional);
    QFETCH(bool, expected);

    resource->setType(AudioPlaybackResource);
    resource->setOptional(optional);
    bool result = resource->isOptional();

    QCOMPARE(result, expected);
}

void TestResource::testShared_data()
{
    QTest::addColumn<bool>("optional");
    QTest::addColumn<bool>("expected");
    
    QTest::newRow("Resource is shared") << true << true;
    QTest::newRow("Resource is not hhared") << false << false;
}

void TestResource::testShared()
{
    QFETCH(bool, optional);
    QFETCH(bool, expected);

    resource->setType(AudioPlaybackResource);
    resource->setShared(optional);
    bool result = resource->isShared();

    QCOMPARE(result, expected);
}

void TestResource::testIdentifier_data()
{
    QTest::addColumn<quint32>("identifier");
    QTest::addColumn<quint32>("expected");
    
    QTest::newRow("Set identifier") << (quint32)this << (quint32)this;
    QTest::newRow("Set to 0") << 0U << 0U;
}

void TestResource::testIdentifier()
{
    QFETCH(quint32, identifier);
    QFETCH(quint32, expected);

    resource->setType(AudioPlaybackResource);
    resource->setId(identifier);
    quint32 result = resource->id();

    QCOMPARE(result, expected);
}

void TestResource::testCopy()
{
    Resource copy;
    resource->setType(AudioPlaybackResource);
    resource->setOptional();

    copy = *resource;

    QCOMPARE(copy.id(), resource->id());
    QCOMPARE(copy.isOptional(), resource->isOptional());
    QCOMPARE(copy.isShared(), resource->isShared());
    QCOMPARE(copy.type(), resource->type());
}

void TestResource::testCopyConstructor()
{
    resource->setType(AudioPlaybackResource);
    resource->setOptional();
    Resource copy(*resource);

    QCOMPARE(copy.id(), resource->id());
    QCOMPARE(copy.isOptional(), resource->isOptional());
    QCOMPARE(copy.isShared(), resource->isShared());
    QCOMPARE(copy.type(), resource->type());
}

void TestResource::testEqualsOperator()
{
    Resource copy;
    resource->setType(AudioPlaybackResource);
    resource->setOptional();

    copy = *resource;

    QVERIFY(copy == *resource);
}

void TestResource::testHashFunctionWithCopies()
{
    resource->setType(AudioPlaybackResource);
    resource->setOptional();
    resource->setShared();
    Resource copy(*resource);

    uint originalHash = qHash(*resource);
    uint copyHash = qHash(copy);
    
    QCOMPARE(originalHash, copyHash);
}

void TestResource::testHashFunctionWithDifferentResources()
{
    resource->setType(AudioPlaybackResource);
    resource->setOptional();
    resource->setShared();
    Resource copy(*resource);
    copy.setType(VideoPlaybackResource);

    uint originalHash = qHash(*resource);
    uint copyHash = qHash(copy);
    
    QEXPECT_FAIL("", "Expecting to fail since they are different resources", Continue);
    QCOMPARE(originalHash, copyHash);
}

void TestResource::testHashFunctionWithDifferentOptionality()
{
    resource->setType(AudioPlaybackResource);
    resource->setOptional();
    resource->setShared();
    Resource copy(*resource);
    copy.setOptional(false);

    uint originalHash = qHash(*resource);
    uint copyHash = qHash(copy);
    
    QEXPECT_FAIL("", "Expecting to fail since one isOptional, the other isn't", Continue);
    QCOMPARE(originalHash, copyHash);
}

void TestResource::testHashFunctionWithDifferentSharedness()
{
    resource->setType(AudioPlaybackResource);
    resource->setOptional();
    resource->setShared();
    Resource copy(*resource);
    copy.setShared(false);

    uint originalHash = qHash(*resource);
    uint copyHash = qHash(copy);
    
    QEXPECT_FAIL("", "Expecting to fail since one isShared , the other isn't", Continue);
    QCOMPARE(originalHash, copyHash);
}

void TestResource::testHashFunctionWithIdentical()
{
    resource->setType(AudioPlaybackResource);
    resource->setOptional();
    resource->setShared();
    Resource copy;
    copy.setOptional();
    copy.setShared();
    copy.setType(AudioPlaybackResource);

    uint originalHash = qHash(*resource);
    uint copyHash = qHash(copy);
    
    QCOMPARE(originalHash, copyHash);
}

void TestResource::testQSetOfResource()
{
    Resource a,b,c;
    a.setType(AudioPlaybackResource);
    b.setType(VideoPlaybackResource);
    c.setType(AudioRecorderResource);
    
    QSet<Resource> set;
    set.insert(a);
    set.insert(b);
    set.insert(c);

    QVERIFY(set.contains(a));
    QVERIFY(set.contains(b));
    QVERIFY(set.contains(c));
}

QTEST_MAIN(TestResource)