SmallVector and SmallPtrSet allocations now power-of-two aligned.
[oota-llvm.git] / unittests / ADT / SmallPtrSetTest.cpp
1 //===- llvm/unittest/ADT/SmallPtrSetTest.cpp ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // SmallPtrSet unit tests.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "gtest/gtest.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16
17 using namespace llvm;
18
19 // SmallPtrSet swapping test.
20 TEST(SmallPtrSetTest, GrowthTest) {
21   int i;
22   int buf[8];
23   for(i=0; i<8; ++i) buf[i]=0;
24
25
26   SmallPtrSet<int *, 4> s;
27   typedef SmallPtrSet<int *, 4>::iterator iter;
28   
29   s.insert(&buf[0]);
30   s.insert(&buf[1]);
31   s.insert(&buf[2]);
32   s.insert(&buf[3]);
33   EXPECT_EQ(4U, s.size());
34
35   i = 0;
36   for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
37       (**I)++;
38   EXPECT_EQ(4, i);
39   for(i=0; i<8; ++i)
40       EXPECT_EQ(i<4?1:0,buf[i]);
41
42   s.insert(&buf[4]);
43   s.insert(&buf[5]);
44   s.insert(&buf[6]);
45   s.insert(&buf[7]);
46
47   i = 0;
48   for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
49       (**I)++;
50   EXPECT_EQ(8, i);
51   s.erase(&buf[4]);
52   s.erase(&buf[5]);
53   s.erase(&buf[6]);
54   s.erase(&buf[7]);
55   EXPECT_EQ(4U, s.size());
56
57   i = 0;
58   for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
59       (**I)++;
60   EXPECT_EQ(4, i);
61   for(i=0; i<8; ++i)
62       EXPECT_EQ(i<4?3:1,buf[i]);
63
64   s.clear();
65   for(i=0; i<8; ++i) buf[i]=0;
66   for(i=0; i<128; ++i) s.insert(&buf[i%8]); // test repeated entires
67   EXPECT_EQ(8U, s.size());
68   for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
69       (**I)++;
70   for(i=0; i<8; ++i)
71       EXPECT_EQ(1,buf[i]);
72 }
73
74
75 TEST(SmallPtrSetTest, SwapTest) {
76   int buf[10];
77
78   SmallPtrSet<int *, 2> a;
79   SmallPtrSet<int *, 2> b;
80
81   a.insert(&buf[0]);
82   a.insert(&buf[1]);
83   b.insert(&buf[2]);
84
85   std::swap(a, b);
86
87   EXPECT_EQ(1U, a.size());
88   EXPECT_EQ(2U, b.size());
89   EXPECT_TRUE(a.count(&buf[2]));
90   EXPECT_TRUE(b.count(&buf[0]));
91   EXPECT_TRUE(b.count(&buf[1]));
92
93   b.insert(&buf[3]);
94   std::swap(a, b);
95
96   EXPECT_EQ(3U, a.size());
97   EXPECT_EQ(1U, b.size());
98   EXPECT_TRUE(a.count(&buf[0]));
99   EXPECT_TRUE(a.count(&buf[1]));
100   EXPECT_TRUE(a.count(&buf[3]));
101   EXPECT_TRUE(b.count(&buf[2]));
102
103   std::swap(a, b);
104
105   EXPECT_EQ(1U, a.size());
106   EXPECT_EQ(3U, b.size());
107   EXPECT_TRUE(a.count(&buf[2]));
108   EXPECT_TRUE(b.count(&buf[0]));
109   EXPECT_TRUE(b.count(&buf[1]));
110   EXPECT_TRUE(b.count(&buf[3]));
111
112   a.insert(&buf[4]);
113   a.insert(&buf[5]);
114   a.insert(&buf[6]);
115
116   std::swap(b, a);
117
118   EXPECT_EQ(3U, a.size());
119   EXPECT_EQ(4U, b.size());
120   EXPECT_TRUE(b.count(&buf[2]));
121   EXPECT_TRUE(b.count(&buf[4]));
122   EXPECT_TRUE(b.count(&buf[5]));
123   EXPECT_TRUE(b.count(&buf[6]));
124   EXPECT_TRUE(a.count(&buf[0]));
125   EXPECT_TRUE(a.count(&buf[1]));
126   EXPECT_TRUE(a.count(&buf[3]));
127 }