blob: 6b9fbc7ee266a45c02289b60273ddff999e5bdbd [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Block6ded16b2010-05-10 14:33:55 +01002// 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030#include "src/v8.h"
Steve Block6ded16b2010-05-10 14:33:55 +010031
Emily Bernierd0a1eb72015-03-24 16:35:39 -040032#include "src/bit-vector.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033#include "test/cctest/cctest.h"
Steve Block6ded16b2010-05-10 14:33:55 +010034
35using namespace v8::internal;
36
37TEST(BitVector) {
Ben Murdochda12d292016-06-02 14:46:10 +010038 v8::base::AccountingAllocator allocator;
39 Zone zone(&allocator);
Steve Block6ded16b2010-05-10 14:33:55 +010040 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 BitVector v(15, &zone);
Steve Block6ded16b2010-05-10 14:33:55 +010042 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);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 BitVector w(15, &zone);
Steve Block6ded16b2010-05-10 14:33:55 +010049 w.Add(1);
50 v.Intersect(w);
51 CHECK(!v.Contains(0));
52 CHECK(v.Contains(1));
53 }
54
55 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056 BitVector v(64, &zone);
Ben Murdochb0fe1622011-05-05 13:52:32 +010057 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 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 BitVector v(15, &zone);
Steve Block6ded16b2010-05-10 14:33:55 +010075 v.Add(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076 BitVector w(15, &zone);
Steve Block6ded16b2010-05-10 14:33:55 +010077 w.Add(1);
78 v.Union(w);
79 CHECK(v.Contains(0));
80 CHECK(v.Contains(1));
81 }
82
83 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084 BitVector v(15, &zone);
Steve Block6ded16b2010-05-10 14:33:55 +010085 v.Add(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 BitVector w(15, &zone);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040087 w.CopyFrom(v);
Steve Block6ded16b2010-05-10 14:33:55 +010088 CHECK(w.Contains(0));
89 w.Add(1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090 BitVector u(w, &zone);
Steve Block6ded16b2010-05-10 14:33:55 +010091 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 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099 BitVector v(35, &zone);
Steve Block6ded16b2010-05-10 14:33:55 +0100100 v.Add(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 BitVector w(35, &zone);
Steve Block6ded16b2010-05-10 14:33:55 +0100102 w.Add(33);
103 v.Union(w);
104 CHECK(v.Contains(0));
105 CHECK(v.Contains(33));
106 }
107
108 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 BitVector v(35, &zone);
Steve Block6ded16b2010-05-10 14:33:55 +0100110 v.Add(32);
111 v.Add(33);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 BitVector w(35, &zone);
Steve Block6ded16b2010-05-10 14:33:55 +0100113 w.Add(33);
114 v.Intersect(w);
115 CHECK(!v.Contains(32));
116 CHECK(v.Contains(33));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117 BitVector r(35, &zone);
Steve Block6ded16b2010-05-10 14:33:55 +0100118 r.CopyFrom(v);
119 CHECK(!r.Contains(32));
120 CHECK(r.Contains(33));
121 }
122}