 }
 
 BitsetTests.test("Word.capacity") {
-  expectEqual(_Bitset.Word.capacity, UInt.bitWidth)
+  expectEqual(_UnsafeBitset.Word.capacity, UInt.bitWidth)
 }
 
 BitsetTests.test("Word.uncheckedContains(_:)") {
-  typealias Word = _Bitset.Word
+  typealias Word = _UnsafeBitset.Word
   for i in 0 ..< Word.capacity {
     expectEqual(Word(0).uncheckedContains(i), false, "i=\(i)")
     expectEqual(Word(1).uncheckedContains(i), i == 0, "i=\(i)")

  expectEqual(Array(Word(UInt.max)), Array(0..<UInt.bitWidth))
}

let capacities = [
  0, 1, 2, 3, 7, 8, 9, 31, 32, 33, 48, 63, 64, 65, 127, 128, 129, 1024, 10000
]

let primes: Set<Int> = [
  2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
  73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
  157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233,
  239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
  331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
  421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499
]

BitsetTests.test("_Bitset:InitializedToZero")
.forEach(in: capacities) { capacity in
  let bitset = _Bitset(capacity: capacity)
  expectEqual(bitset.count, 0)
  for bit in 0 ..< capacity {
    expectFalse(bitset.uncheckedContains(bit))
  }
}

BitsetTests.test("_Bitset:Allocation")
.forEach(in: capacities) { capacity in
  let bitset = _Bitset(capacity: capacity)
  expectEqual(bitset.storage != nil, capacity > _Bitset.Word.capacity)
}

BitsetTests.test("_Bitset.uncheckedContains(_:)")
.forEach(in: capacities) { capacity in
  var bitset = _Bitset(capacity: capacity)
  for p in primes where p < capacity {
    expectFalse(bitset.uncheckedContains(p), "\(p)")
    _ = bitset.uncheckedInsert(p)
    expectTrue(bitset.uncheckedContains(p), "\(p)")
  }
  for i in 0 ..< capacity {
    expectEqual(
      bitset.uncheckedContains(i),
      primes.contains(i),
      "\(i)")
  }
}

BitsetTests.test("_Bitset.uncheckedInsert(_:)")
.forEach(in: capacities) { capacity in
  var bitset = _Bitset(capacity: capacity)
  for p in primes where p < capacity {
    let inserted = bitset.uncheckedInsert(p)
    expectTrue(inserted, "\(p)")
  }
  for p in primes where p < capacity {
    let inserted = bitset.uncheckedInsert(p)
    expectFalse(inserted, "\(p)")
  }
  for i in 0 ..< capacity {
    expectEqual(
      bitset.uncheckedContains(i),
      primes.contains(i),
      "\(i)")
  }
}

BitsetTests.test("_Bitset.uncheckedInsert(_:):COW")
.forEach(in: capacities) { capacity in
  for bit in primes where bit < capacity {
    var bitset = _Bitset(capacity: capacity)
    let copy = bitset

    bitset.uncheckedInsert(bit)
    expectTrue(bitset.uncheckedContains(bit), "\(bit)")
    expectFalse(copy.uncheckedContains(bit), "\(bit)")
  }
}

BitsetTests.test("_Bitset.uncheckedRemove(_:)")
.forEach(in: capacities) { capacity in
  var bitset = _Bitset(capacity: capacity)
  for i in 0 ..< capacity {
    bitset.uncheckedInsert(i)
  }
  for bit in primes where bit < capacity {
    expectTrue(bitset.uncheckedRemove(bit), "\(bit)")
  }
  for bit in primes where bit < capacity {
    expectFalse(bitset.uncheckedRemove(bit), "\(bit)")
  }
  for i in 0 ..< capacity {
    expectEqual(
      bitset.uncheckedContains(i),
      !primes.contains(i),
      "\(i)")
  }
}

BitsetTests.test("_Bitset.uncheckedRemove(_:):COW")
.forEach(in: capacities) { capacity in
  var bitset = _Bitset(capacity: capacity)
  for bit in primes where bit < capacity {
    bitset.uncheckedInsert(bit)
  }
  for bit in primes where bit < capacity {
    var copy = bitset
    expectTrue(copy.uncheckedRemove(bit), "\(bit)")
    expectFalse(copy.uncheckedContains(bit), "\(bit)")
    expectTrue(bitset.uncheckedContains(bit), "\(bit)")
  }
}

BitsetTests.test("_Bitset.count")
.forEach(in: capacities) { capacity in
  var bitset = _Bitset(capacity: capacity)
  var c = 0
  for bit in primes where bit < capacity {
    bitset.uncheckedInsert(bit)
    c += 1
  }
  expectEqual(c, bitset.count)
}

BitsetTests.test("_Bitset.makeIterator()")
.forEach(in: capacities) { capacity in
  var bitset = _Bitset(capacity: capacity)
  let bits = primes.filter { $0 < capacity }
  for b in bits {
    bitset.uncheckedInsert(b)
  }
  expectEqual(Array(bitset), bits.sorted())
}

runAllTests()

}

BitsetTests.test("Word.makeIterator()") {
  typealias Word = _UnsafeBitset.Word
  expectEqual(Array(Word(0)), [])
  expectEqual(Array(Word(1)), [0])
  expectEqual(Array(Word(2)), [1])
