1 // Copyright (c) 2013 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.
4
5 #include "ui/gfx/sequential_id_generator.h"
6
7 #include "base/logging.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace ui {
11
12 typedef testing::Test SequentialIDGeneratorTest;
13
TEST(SequentialIDGeneratorTest,AddRemove)14 TEST(SequentialIDGeneratorTest, AddRemove) {
15 const uint32 kMinID = 2;
16 SequentialIDGenerator generator(kMinID);
17
18 EXPECT_EQ(2U, generator.GetGeneratedID(45));
19 EXPECT_EQ(3U, generator.GetGeneratedID(23));
20 EXPECT_EQ(2U, generator.GetGeneratedID(45));
21 EXPECT_TRUE(generator.HasGeneratedIDFor(45));
22 EXPECT_TRUE(generator.HasGeneratedIDFor(23));
23
24 generator.ReleaseGeneratedID(2);
25 EXPECT_FALSE(generator.HasGeneratedIDFor(45));
26 EXPECT_TRUE(generator.HasGeneratedIDFor(23));
27 EXPECT_EQ(3U, generator.GetGeneratedID(23));
28
29 EXPECT_FALSE(generator.HasGeneratedIDFor(1));
30 EXPECT_EQ(2U, generator.GetGeneratedID(1));
31 EXPECT_TRUE(generator.HasGeneratedIDFor(1));
32
33 generator.ReleaseGeneratedID(3);
34 EXPECT_EQ(3U, generator.GetGeneratedID(45));
35 EXPECT_TRUE(generator.HasGeneratedIDFor(45));
36
37 generator.ReleaseNumber(45);
38 EXPECT_FALSE(generator.HasGeneratedIDFor(45));
39 }
40
TEST(SequentialIDGeneratorTest,RemoveMultipleNumbers)41 TEST(SequentialIDGeneratorTest, RemoveMultipleNumbers) {
42 const uint32 kMinID = 4;
43 SequentialIDGenerator generator(kMinID);
44
45 EXPECT_EQ(4U, generator.GetGeneratedID(45));
46 EXPECT_EQ(5U, generator.GetGeneratedID(55));
47 EXPECT_EQ(6U, generator.GetGeneratedID(15));
48
49 generator.ReleaseNumber(45);
50 EXPECT_FALSE(generator.HasGeneratedIDFor(45));
51 generator.ReleaseNumber(15);
52 EXPECT_FALSE(generator.HasGeneratedIDFor(15));
53
54 EXPECT_EQ(5U, generator.GetGeneratedID(55));
55 EXPECT_EQ(4U, generator.GetGeneratedID(12));
56
57 generator.ReleaseNumber(12);
58 generator.ReleaseNumber(55);
59 EXPECT_EQ(4U, generator.GetGeneratedID(0));
60 }
61
62 } // namespace ui
63