-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathTestUidAllocator.cpp
More file actions
69 lines (55 loc) · 1.69 KB
/
Copy pathTestUidAllocator.cpp
File metadata and controls
69 lines (55 loc) · 1.69 KB
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
/*
* TestUidAllocator.cpp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#include "TestUidAllocator.h"
#include "UidAllocator.h"
using namespace mmp;
void TestUidAllocator::allocateIsSequential()
{
UidAllocator allocator;
QCOMPARE(allocator.allocate(), uid(1));
QCOMPARE(allocator.allocate(), uid(2));
QCOMPARE(allocator.allocate(), uid(3));
QVERIFY(allocator.exists(1));
QVERIFY(allocator.exists(2));
QVERIFY(allocator.exists(3));
QVERIFY(!allocator.exists(4));
QCOMPARE(int(allocator.list().size()), 3);
}
void TestUidAllocator::freeReusesLowestId()
{
UidAllocator allocator;
allocator.allocate(); // 1
allocator.allocate(); // 2
allocator.allocate(); // 3
QVERIFY(allocator.free(2));
QVERIFY(!allocator.exists(2));
// The next allocation should reuse the lowest free id (2).
QCOMPARE(allocator.allocate(), uid(2));
QVERIFY(allocator.exists(2));
}
void TestUidAllocator::reserve()
{
UidAllocator allocator;
// Reserving an unused id succeeds and marks it as existing.
QVERIFY(allocator.reserve(42));
QVERIFY(allocator.exists(42));
// Reserving an already-reserved id fails.
QVERIFY(!allocator.reserve(42));
// A subsequent allocate must skip the reserved id.
QCOMPARE(allocator.allocate(), uid(1));
}
void TestUidAllocator::freeUnknownReturnsFalse()
{
UidAllocator allocator;
QVERIFY(!allocator.free(99));
allocator.allocate(); // 1
QVERIFY(!allocator.free(99));
QVERIFY(allocator.free(1));
QVERIFY(!allocator.free(1)); // already freed
}