1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file has the unit tests for the IdAllocator class.
7 #include "gpu/command_buffer/common/id_allocator.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 class IdAllocatorTest
: public testing::Test
{
14 virtual void SetUp() {}
15 virtual void TearDown() {}
17 IdAllocator
* id_allocator() { return &id_allocator_
; }
20 IdAllocator id_allocator_
;
23 // Checks basic functionality: AllocateID, FreeID, InUse.
24 TEST_F(IdAllocatorTest
, TestBasic
) {
25 IdAllocator
*allocator
= id_allocator();
26 // Check that resource 1 is not in use
27 EXPECT_FALSE(allocator
->InUse(1));
29 // Allocate an ID, check that it's in use.
30 ResourceId id1
= allocator
->AllocateID();
31 EXPECT_TRUE(allocator
->InUse(id1
));
33 // Allocate another ID, check that it's in use, and different from the first
35 ResourceId id2
= allocator
->AllocateID();
36 EXPECT_TRUE(allocator
->InUse(id2
));
39 // Free one of the IDs, check that it's not in use any more.
40 allocator
->FreeID(id1
);
41 EXPECT_FALSE(allocator
->InUse(id1
));
43 // Frees the other ID, check that it's not in use any more.
44 allocator
->FreeID(id2
);
45 EXPECT_FALSE(allocator
->InUse(id2
));
48 // Checks that the resource IDs are re-used after being freed.
49 TEST_F(IdAllocatorTest
, TestAdvanced
) {
50 IdAllocator
*allocator
= id_allocator();
52 // Allocate the highest possible ID, to make life awkward.
53 allocator
->AllocateIDAtOrAbove(-1);
55 // Allocate a significant number of resources.
56 const unsigned int kNumResources
= 100;
57 ResourceId ids
[kNumResources
];
58 for (unsigned int i
= 0; i
< kNumResources
; ++i
) {
59 ids
[i
] = allocator
->AllocateID();
60 EXPECT_TRUE(allocator
->InUse(ids
[i
]));
63 // Check that a new allocation re-uses the resource we just freed.
64 ResourceId id1
= ids
[kNumResources
/ 2];
65 allocator
->FreeID(id1
);
66 EXPECT_FALSE(allocator
->InUse(id1
));
67 ResourceId id2
= allocator
->AllocateID();
68 EXPECT_TRUE(allocator
->InUse(id2
));
72 // Checks that we can choose our own ids and they won't be reused.
73 TEST_F(IdAllocatorTest
, MarkAsUsed
) {
74 IdAllocator
* allocator
= id_allocator();
75 ResourceId id
= allocator
->AllocateID();
76 allocator
->FreeID(id
);
77 EXPECT_FALSE(allocator
->InUse(id
));
78 EXPECT_TRUE(allocator
->MarkAsUsed(id
));
79 EXPECT_TRUE(allocator
->InUse(id
));
80 ResourceId id2
= allocator
->AllocateID();
82 EXPECT_TRUE(allocator
->MarkAsUsed(id2
+ 1));
83 ResourceId id3
= allocator
->AllocateID();
84 // Checks our algorithm. If the algorithm changes this check should be
86 EXPECT_EQ(id3
, id2
+ 2);
89 // Checks AllocateIdAtOrAbove.
90 TEST_F(IdAllocatorTest
, AllocateIdAtOrAbove
) {
91 const ResourceId kOffset
= 123456;
92 IdAllocator
* allocator
= id_allocator();
93 ResourceId id1
= allocator
->AllocateIDAtOrAbove(kOffset
);
94 EXPECT_EQ(kOffset
, id1
);
95 ResourceId id2
= allocator
->AllocateIDAtOrAbove(kOffset
);
96 EXPECT_GT(id2
, kOffset
);
97 ResourceId id3
= allocator
->AllocateIDAtOrAbove(kOffset
);
98 EXPECT_GT(id3
, kOffset
);
101 // Checks that AllocateIdAtOrAbove wraps around at the maximum 32-bit value.
102 TEST_F(IdAllocatorTest
, AllocateIdAtOrAboveWrapsAround
) {
103 const ResourceId kMaxPossibleOffset
= -1;
104 IdAllocator
* allocator
= id_allocator();
105 ResourceId id1
= allocator
->AllocateIDAtOrAbove(kMaxPossibleOffset
);
106 EXPECT_EQ(kMaxPossibleOffset
, id1
);
107 ResourceId id2
= allocator
->AllocateIDAtOrAbove(kMaxPossibleOffset
);
109 ResourceId id3
= allocator
->AllocateIDAtOrAbove(kMaxPossibleOffset
);
113 TEST_F(IdAllocatorTest
, RedundantFreeIsIgnored
) {
114 IdAllocator
* allocator
= id_allocator();
115 ResourceId id1
= allocator
->AllocateID();
116 allocator
->FreeID(0);
117 allocator
->FreeID(id1
);
118 allocator
->FreeID(id1
);
119 allocator
->FreeID(id1
+ 1);
121 ResourceId id2
= allocator
->AllocateID();
122 ResourceId id3
= allocator
->AllocateID();
124 EXPECT_NE(kInvalidResource
, id2
);
125 EXPECT_NE(kInvalidResource
, id3
);