|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.util.collection |
| 19 | + |
| 20 | +import org.apache.spark.SparkFunSuite |
| 21 | + |
| 22 | +class ImmutableBitSetSuite extends SparkFunSuite { |
| 23 | + |
| 24 | + test("basic get") { |
| 25 | + val bitset = new ImmutableBitSet(100, 0, 9, 1, 10, 90, 96) |
| 26 | + val setBits = Seq(0, 9, 1, 10, 90, 96) |
| 27 | + for (i <- 0 until 100) { |
| 28 | + if (setBits.contains(i)) { |
| 29 | + assert(bitset.get(i)) |
| 30 | + } else { |
| 31 | + assert(!bitset.get(i)) |
| 32 | + } |
| 33 | + } |
| 34 | + assert(bitset.cardinality() === setBits.size) |
| 35 | + } |
| 36 | + |
| 37 | + test("nextSetBit") { |
| 38 | + val bitset = new ImmutableBitSet(100, 0, 9, 1, 10, 90, 96) |
| 39 | + |
| 40 | + assert(bitset.nextSetBit(0) === 0) |
| 41 | + assert(bitset.nextSetBit(1) === 1) |
| 42 | + assert(bitset.nextSetBit(2) === 9) |
| 43 | + assert(bitset.nextSetBit(9) === 9) |
| 44 | + assert(bitset.nextSetBit(10) === 10) |
| 45 | + assert(bitset.nextSetBit(11) === 90) |
| 46 | + assert(bitset.nextSetBit(80) === 90) |
| 47 | + assert(bitset.nextSetBit(91) === 96) |
| 48 | + assert(bitset.nextSetBit(96) === 96) |
| 49 | + assert(bitset.nextSetBit(97) === -1) |
| 50 | + } |
| 51 | + |
| 52 | + test( "xor len(bitsetX) < len(bitsetY)" ) { |
| 53 | + val bitsetX = new ImmutableBitSet(60, 0, 2, 3, 37, 41) |
| 54 | + val bitsetY = new ImmutableBitSet(100, 0, 1, 3, 37, 38, 41, 85) |
| 55 | + |
| 56 | + val bitsetXor = bitsetX ^ bitsetY |
| 57 | + |
| 58 | + assert(bitsetXor.nextSetBit(0) === 1) |
| 59 | + assert(bitsetXor.nextSetBit(1) === 1) |
| 60 | + assert(bitsetXor.nextSetBit(2) === 2) |
| 61 | + assert(bitsetXor.nextSetBit(3) === 38) |
| 62 | + assert(bitsetXor.nextSetBit(38) === 38) |
| 63 | + assert(bitsetXor.nextSetBit(39) === 85) |
| 64 | + assert(bitsetXor.nextSetBit(42) === 85) |
| 65 | + assert(bitsetXor.nextSetBit(85) === 85) |
| 66 | + assert(bitsetXor.nextSetBit(86) === -1) |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + test( "xor len(bitsetX) > len(bitsetY)" ) { |
| 71 | + val bitsetX = new ImmutableBitSet(100, 0, 1, 3, 37, 38, 41, 85) |
| 72 | + val bitsetY = new ImmutableBitSet(60, 0, 2, 3, 37, 41) |
| 73 | + |
| 74 | + val bitsetXor = bitsetX ^ bitsetY |
| 75 | + |
| 76 | + assert(bitsetXor.nextSetBit(0) === 1) |
| 77 | + assert(bitsetXor.nextSetBit(1) === 1) |
| 78 | + assert(bitsetXor.nextSetBit(2) === 2) |
| 79 | + assert(bitsetXor.nextSetBit(3) === 38) |
| 80 | + assert(bitsetXor.nextSetBit(38) === 38) |
| 81 | + assert(bitsetXor.nextSetBit(39) === 85) |
| 82 | + assert(bitsetXor.nextSetBit(42) === 85) |
| 83 | + assert(bitsetXor.nextSetBit(85) === 85) |
| 84 | + assert(bitsetXor.nextSetBit(86) === -1) |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + test( "andNot len(bitsetX) < len(bitsetY)" ) { |
| 89 | + val bitsetX = new ImmutableBitSet(60, 0, 2, 3, 37, 41, 48) |
| 90 | + val bitsetY = new ImmutableBitSet(100, 0, 1, 3, 37, 38, 41, 85) |
| 91 | + |
| 92 | + val bitsetDiff = bitsetX.andNot( bitsetY ) |
| 93 | + |
| 94 | + assert(bitsetDiff.nextSetBit(0) === 2) |
| 95 | + assert(bitsetDiff.nextSetBit(1) === 2) |
| 96 | + assert(bitsetDiff.nextSetBit(2) === 2) |
| 97 | + assert(bitsetDiff.nextSetBit(3) === 48) |
| 98 | + assert(bitsetDiff.nextSetBit(48) === 48) |
| 99 | + assert(bitsetDiff.nextSetBit(49) === -1) |
| 100 | + assert(bitsetDiff.nextSetBit(65) === -1) |
| 101 | + } |
| 102 | + |
| 103 | + test( "andNot len(bitsetX) > len(bitsetY)" ) { |
| 104 | + val bitsetX = new ImmutableBitSet(100, 0, 1, 3, 37, 38, 41, 85) |
| 105 | + val bitsetY = new ImmutableBitSet(60, 0, 2, 3, 37, 41, 48) |
| 106 | + |
| 107 | + val bitsetDiff = bitsetX.andNot( bitsetY ) |
| 108 | + |
| 109 | + assert(bitsetDiff.nextSetBit(0) === 1) |
| 110 | + assert(bitsetDiff.nextSetBit(1) === 1) |
| 111 | + assert(bitsetDiff.nextSetBit(2) === 38) |
| 112 | + assert(bitsetDiff.nextSetBit(3) === 38) |
| 113 | + assert(bitsetDiff.nextSetBit(38) === 38) |
| 114 | + assert(bitsetDiff.nextSetBit(39) === 85) |
| 115 | + assert(bitsetDiff.nextSetBit(85) === 85) |
| 116 | + assert(bitsetDiff.nextSetBit(86) === -1) |
| 117 | + } |
| 118 | + |
| 119 | + test( "immutability" ) { |
| 120 | + val bitset = new ImmutableBitSet(100) |
| 121 | + intercept[UnsupportedOperationException] { |
| 122 | + bitset.set(1) |
| 123 | + } |
| 124 | + intercept[UnsupportedOperationException] { |
| 125 | + bitset.setUntil(10) |
| 126 | + } |
| 127 | + intercept[UnsupportedOperationException] { |
| 128 | + bitset.unset(1) |
| 129 | + } |
| 130 | + intercept[UnsupportedOperationException] { |
| 131 | + bitset.clear() |
| 132 | + } |
| 133 | + intercept[UnsupportedOperationException] { |
| 134 | + bitset.clearUntil(10) |
| 135 | + } |
| 136 | + intercept[UnsupportedOperationException] { |
| 137 | + bitset.union(new ImmutableBitSet(100)) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments