1// Copyright 2012 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
29
30#include "v8.h"
31
32#include "data-flow.h"
33#include "cctest.h"
34
35using namespace v8::internal;
36
37TEST(BitVector) {
38  v8::internal::V8::Initialize(NULL);
39  Zone zone(Isolate::Current());
40  {
41    BitVector v(15, &zone);
42    v.Add(1);
43    CHECK(v.Contains(1));
44    v.Remove(0);
45    CHECK(!v.Contains(0));
46    v.Add(0);
47    v.Add(1);
48    BitVector w(15, &zone);
49    w.Add(1);
50    v.Intersect(w);
51    CHECK(!v.Contains(0));
52    CHECK(v.Contains(1));
53  }
54
55  {
56    BitVector v(64, &zone);
57    v.Add(27);
58    v.Add(30);
59    v.Add(31);
60    v.Add(33);
61    BitVector::Iterator iter(&v);
62    CHECK_EQ(27, iter.Current());
63    iter.Advance();
64    CHECK_EQ(30, iter.Current());
65    iter.Advance();
66    CHECK_EQ(31, iter.Current());
67    iter.Advance();
68    CHECK_EQ(33, iter.Current());
69    iter.Advance();
70    CHECK(iter.Done());
71  }
72
73  {
74    BitVector v(15, &zone);
75    v.Add(0);
76    BitVector w(15, &zone);
77    w.Add(1);
78    v.Union(w);
79    CHECK(v.Contains(0));
80    CHECK(v.Contains(1));
81  }
82
83  {
84    BitVector v(15, &zone);
85    v.Add(0);
86    BitVector w(15, &zone);
87    w = v;
88    CHECK(w.Contains(0));
89    w.Add(1);
90    BitVector u(w, &zone);
91    CHECK(u.Contains(0));
92    CHECK(u.Contains(1));
93    v.Union(w);
94    CHECK(v.Contains(0));
95    CHECK(v.Contains(1));
96  }
97
98  {
99    BitVector v(35, &zone);
100    v.Add(0);
101    BitVector w(35, &zone);
102    w.Add(33);
103    v.Union(w);
104    CHECK(v.Contains(0));
105    CHECK(v.Contains(33));
106  }
107
108  {
109    BitVector v(35, &zone);
110    v.Add(32);
111    v.Add(33);
112    BitVector w(35, &zone);
113    w.Add(33);
114    v.Intersect(w);
115    CHECK(!v.Contains(32));
116    CHECK(v.Contains(33));
117    BitVector r(35, &zone);
118    r.CopyFrom(v);
119    CHECK(!r.Contains(32));
120    CHECK(r.Contains(33));
121  }
122}
123