| 1 |
// Copyright 2017 Matt Brubeck. See the COPYRIGHT file at the top-level |
| 2 |
// directory of this distribution and at http://rust-lang.org/COPYRIGHT. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 5 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 7 |
// option. This file may not be copied, modified, or distributed |
| 8 |
// except according to those terms. |
| 9 |
|
| 10 |
use super::*; |
| 11 |
|
| 12 |
use alloc::format; |
| 13 |
|
| 14 |
#[cfg(target_pointer_width = "32")] |
| 15 |
#[test] |
| 16 |
fn test_inline_capacity() { |
| 17 |
assert_eq!(inline_capacity(), 30); |
| 18 |
} |
| 19 |
|
| 20 |
#[cfg(target_pointer_width = "64")] |
| 21 |
#[test] |
| 22 |
fn test_inline_capacity() { |
| 23 |
assert_eq!(inline_capacity(), 62); |
| 24 |
} |
| 25 |
|
| 26 |
#[test] |
| 27 |
fn new() { |
| 28 |
let v = SmallBitVec::new(); |
| 29 |
assert_eq!(v.len(), 0); |
| 30 |
assert_eq!(v.capacity(), inline_capacity()); |
| 31 |
assert!(v.is_empty()); |
| 32 |
assert!(v.is_inline()); |
| 33 |
} |
| 34 |
|
| 35 |
#[test] |
| 36 |
fn with_capacity_inline() { |
| 37 |
for cap in 0..(inline_capacity() + 1) { |
| 38 |
let v = SmallBitVec::with_capacity(cap); |
| 39 |
assert_eq!(v.len(), 0); |
| 40 |
assert_eq!(v.capacity(), inline_capacity()); |
| 41 |
assert!(v.is_inline()); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn with_capacity_heap() { |
| 47 |
let cap = inline_capacity() + 1; |
| 48 |
let v = SmallBitVec::with_capacity(cap); |
| 49 |
assert_eq!(v.len(), 0); |
| 50 |
assert!(v.capacity() > inline_capacity()); |
| 51 |
assert!(v.is_heap()); |
| 52 |
} |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn set_len_inline() { |
| 56 |
let mut v = SmallBitVec::new(); |
| 57 |
for i in 0..(inline_capacity() + 1) { |
| 58 |
unsafe { |
| 59 |
v.set_len(i); |
| 60 |
} |
| 61 |
assert_eq!(v.len(), i); |
| 62 |
} |
| 63 |
for i in (0..(inline_capacity() + 1)).rev() { |
| 64 |
unsafe { |
| 65 |
v.set_len(i); |
| 66 |
} |
| 67 |
assert_eq!(v.len(), i); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn set_len_heap() { |
| 73 |
let mut v = SmallBitVec::with_capacity(500); |
| 74 |
unsafe { |
| 75 |
v.set_len(30); |
| 76 |
} |
| 77 |
assert_eq!(v.len(), 30); |
| 78 |
} |
| 79 |
|
| 80 |
#[test] |
| 81 |
fn push_many() { |
| 82 |
let mut v = SmallBitVec::new(); |
| 83 |
for i in 0..500 { |
| 84 |
v.push(i % 3 == 0); |
| 85 |
} |
| 86 |
assert_eq!(v.len(), 500); |
| 87 |
|
| 88 |
for i in 0..500 { |
| 89 |
assert_eq!(v.get(i).unwrap(), (i % 3 == 0), "{}", i); |
| 90 |
assert_eq!(v[i], v.get(i).unwrap()); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
#[should_panic(expected = "index out of range")] |
| 96 |
fn index_out_of_bounds() { |
| 97 |
let v = SmallBitVec::new(); |
| 98 |
v[0]; |
| 99 |
} |
| 100 |
|
| 101 |
#[test] |
| 102 |
#[should_panic(expected = "index out of range")] |
| 103 |
fn index_out_of_bounds_nonempty() { |
| 104 |
let mut v = SmallBitVec::new(); |
| 105 |
v.push(true); |
| 106 |
v[1 << 32]; |
| 107 |
} |
| 108 |
|
| 109 |
#[test] |
| 110 |
fn get_out_of_bounds() { |
| 111 |
let v = SmallBitVec::new(); |
| 112 |
assert!(v.get(0).is_none()); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
#[should_panic] |
| 117 |
fn set_out_of_bounds() { |
| 118 |
let mut v = SmallBitVec::new(); |
| 119 |
v.set(2, false); |
| 120 |
} |
| 121 |
|
| 122 |
#[test] |
| 123 |
fn all_false() { |
| 124 |
let mut v = SmallBitVec::new(); |
| 125 |
assert!(v.all_false()); |
| 126 |
for _ in 0..100 { |
| 127 |
v.push(false); |
| 128 |
assert!(v.all_false()); |
| 129 |
|
| 130 |
let mut v2 = v.clone(); |
| 131 |
v2.push(true); |
| 132 |
assert!(!v2.all_false()); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn all_true() { |
| 138 |
let mut v = SmallBitVec::new(); |
| 139 |
assert!(v.all_true()); |
| 140 |
for _ in 0..100 { |
| 141 |
v.push(true); |
| 142 |
assert!(v.all_true()); |
| 143 |
|
| 144 |
let mut v2 = v.clone(); |
| 145 |
v2.push(false); |
| 146 |
assert!(!v2.all_true()); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
#[test] |
| 151 |
fn iter() { |
| 152 |
let mut v = SmallBitVec::new(); |
| 153 |
v.push(true); |
| 154 |
v.push(false); |
| 155 |
v.push(false); |
| 156 |
|
| 157 |
let mut i = v.iter(); |
| 158 |
assert_eq!(i.next(), Some(true)); |
| 159 |
assert_eq!(i.next(), Some(false)); |
| 160 |
assert_eq!(i.next(), Some(false)); |
| 161 |
assert_eq!(i.next(), None); |
| 162 |
} |
| 163 |
|
| 164 |
#[test] |
| 165 |
fn into_iter() { |
| 166 |
let mut v = SmallBitVec::new(); |
| 167 |
v.push(true); |
| 168 |
v.push(false); |
| 169 |
v.push(false); |
| 170 |
|
| 171 |
let mut i = v.into_iter(); |
| 172 |
assert_eq!(i.next(), Some(true)); |
| 173 |
assert_eq!(i.next(), Some(false)); |
| 174 |
assert_eq!(i.next(), Some(false)); |
| 175 |
assert_eq!(i.next(), None); |
| 176 |
} |
| 177 |
|
| 178 |
#[test] |
| 179 |
fn iter_back() { |
| 180 |
let mut v = SmallBitVec::new(); |
| 181 |
v.push(true); |
| 182 |
v.push(false); |
| 183 |
v.push(false); |
| 184 |
|
| 185 |
let mut i = v.iter(); |
| 186 |
assert_eq!(i.next_back(), Some(false)); |
| 187 |
assert_eq!(i.next_back(), Some(false)); |
| 188 |
assert_eq!(i.next_back(), Some(true)); |
| 189 |
assert_eq!(i.next(), None); |
| 190 |
} |
| 191 |
|
| 192 |
#[test] |
| 193 |
fn range() { |
| 194 |
let mut v = SmallBitVec::new(); |
| 195 |
v.push(true); |
| 196 |
v.push(false); |
| 197 |
v.push(false); |
| 198 |
v.push(true); |
| 199 |
|
| 200 |
let r = v.range(0..2); |
| 201 |
let mut ri = r.iter(); |
| 202 |
assert_eq!(ri.next(), Some(true)); |
| 203 |
assert_eq!(ri.next(), Some(false)); |
| 204 |
assert_eq!(ri.next(), None); |
| 205 |
assert_eq!(r[0], true); |
| 206 |
} |
| 207 |
|
| 208 |
#[test] |
| 209 |
#[should_panic(expected = "range out of bounds")] |
| 210 |
fn range_oob() { |
| 211 |
let mut v = SmallBitVec::new(); |
| 212 |
v.push(true); |
| 213 |
|
| 214 |
v.range(0..2); |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
#[should_panic(expected = "index out of range")] |
| 219 |
fn range_index_oob() { |
| 220 |
let mut v = SmallBitVec::new(); |
| 221 |
v.push(true); |
| 222 |
v.push(false); |
| 223 |
v.push(true); |
| 224 |
|
| 225 |
let r = v.range(1..2); |
| 226 |
r[1]; |
| 227 |
} |
| 228 |
|
| 229 |
#[test] |
| 230 |
fn debug() { |
| 231 |
let mut v = SmallBitVec::new(); |
| 232 |
v.push(true); |
| 233 |
v.push(false); |
| 234 |
v.push(false); |
| 235 |
|
| 236 |
assert_eq!(format!("{:?}", v), "[1, 0, 0]") |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
fn from_elem() { |
| 241 |
for len in 0..100 { |
| 242 |
let ones = SmallBitVec::from_elem(len, true); |
| 243 |
assert_eq!(ones.len(), len); |
| 244 |
assert!(ones.all_true()); |
| 245 |
|
| 246 |
let zeros = SmallBitVec::from_elem(len, false); |
| 247 |
assert_eq!(zeros.len(), len); |
| 248 |
assert!(zeros.all_false()); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
#[test] |
| 253 |
fn remove() { |
| 254 |
let mut v = SmallBitVec::new(); |
| 255 |
v.push(false); |
| 256 |
v.push(true); |
| 257 |
v.push(false); |
| 258 |
v.push(false); |
| 259 |
v.push(true); |
| 260 |
|
| 261 |
assert_eq!(v.remove(1), true); |
| 262 |
assert_eq!(format!("{:?}", v), "[0, 0, 0, 1]"); |
| 263 |
assert_eq!(v.remove(0), false); |
| 264 |
assert_eq!(format!("{:?}", v), "[0, 0, 1]"); |
| 265 |
v.remove(2); |
| 266 |
assert_eq!(format!("{:?}", v), "[0, 0]"); |
| 267 |
v.remove(1); |
| 268 |
assert_eq!(format!("{:?}", v), "[0]"); |
| 269 |
v.remove(0); |
| 270 |
assert_eq!(format!("{:?}", v), "[]"); |
| 271 |
} |
| 272 |
|
| 273 |
#[test] |
| 274 |
fn remove_big() { |
| 275 |
let mut v = SmallBitVec::from_elem(256, false); |
| 276 |
v.set(100, true); |
| 277 |
v.set(255, true); |
| 278 |
v.remove(0); |
| 279 |
assert_eq!(v.len(), 255); |
| 280 |
assert_eq!(v.get(0).unwrap(), false); |
| 281 |
assert_eq!(v.get(99).unwrap(), true); |
| 282 |
assert_eq!(v.get(100).unwrap(), false); |
| 283 |
assert_eq!(v.get(253).unwrap(), false); |
| 284 |
assert_eq!(v.get(254).unwrap(), true); |
| 285 |
|
| 286 |
v.remove(254); |
| 287 |
assert_eq!(v.len(), 254); |
| 288 |
assert_eq!(v.get(0).unwrap(), false); |
| 289 |
assert_eq!(v.get(99).unwrap(), true); |
| 290 |
assert_eq!(v.get(100).unwrap(), false); |
| 291 |
assert_eq!(v.get(253).unwrap(), false); |
| 292 |
|
| 293 |
v.remove(99); |
| 294 |
assert_eq!(v, SmallBitVec::from_elem(253, false)); |
| 295 |
} |
| 296 |
|
| 297 |
#[test] |
| 298 |
fn eq() { |
| 299 |
assert_eq!(sbvec![], sbvec![]); |
| 300 |
assert_eq!(sbvec![true], sbvec![true]); |
| 301 |
assert_eq!(sbvec![false], sbvec![false]); |
| 302 |
|
| 303 |
assert_ne!(sbvec![], sbvec![false]); |
| 304 |
assert_ne!(sbvec![true], sbvec![]); |
| 305 |
assert_ne!(sbvec![true], sbvec![false]); |
| 306 |
assert_ne!(sbvec![false], sbvec![true]); |
| 307 |
|
| 308 |
assert_eq!(sbvec![true, false], sbvec![true, false]); |
| 309 |
assert_eq!(sbvec![true; 400], sbvec![true; 400]); |
| 310 |
assert_eq!(sbvec![false; 400], sbvec![false; 400]); |
| 311 |
|
| 312 |
assert_ne!(sbvec![true, false], sbvec![true, true]); |
| 313 |
assert_ne!(sbvec![true; 400], sbvec![true; 401]); |
| 314 |
assert_ne!(sbvec![false; 401], sbvec![false; 400]); |
| 315 |
} |
| 316 |
|
| 317 |
#[test] |
| 318 |
fn truncate_inline() { |
| 319 |
let mut v = sbvec![false, true, false, false, true]; |
| 320 |
v.truncate(10); |
| 321 |
assert_eq!(v.len(), 5); |
| 322 |
v.truncate(3); |
| 323 |
assert_eq!(v, sbvec![false, true, false]); |
| 324 |
v.truncate(0); |
| 325 |
assert_eq!(v, sbvec![]); |
| 326 |
} |
| 327 |
|
| 328 |
#[test] |
| 329 |
fn truncate_large() { |
| 330 |
let mut v = SmallBitVec::from_elem(256, false); |
| 331 |
v.set(2, true); |
| 332 |
v.set(100, true); |
| 333 |
v.set(255, true); |
| 334 |
v.truncate(500); |
| 335 |
assert_eq!(v.len(), 256); |
| 336 |
v.truncate(150); |
| 337 |
assert_eq!(v.len(), 150); |
| 338 |
assert_eq!(v.get(0).unwrap(), false); |
| 339 |
assert_eq!(v.get(99).unwrap(), false); |
| 340 |
assert_eq!(v.get(100).unwrap(), true); |
| 341 |
assert_eq!(v.get(101).unwrap(), false); |
| 342 |
assert_eq!(v.get(149).unwrap(), false); |
| 343 |
v.truncate(5); |
| 344 |
assert_eq!(v.len(), 5); |
| 345 |
assert_eq!(v, sbvec![false, false, true, false, false]); |
| 346 |
} |
| 347 |
|
| 348 |
#[test] |
| 349 |
fn resize() { |
| 350 |
let mut v = sbvec![false, true, false, false, true]; |
| 351 |
v.resize(3, false); |
| 352 |
assert_eq!(v, sbvec![false, true, false]); |
| 353 |
v.resize(8, true); |
| 354 |
assert_eq!(v, sbvec![false, true, false, true, true, true, true, true]); |
| 355 |
v.resize(100, false); |
| 356 |
assert_eq!(v.len(), 100); |
| 357 |
assert_eq!(v.get(0).unwrap(), false); |
| 358 |
assert_eq!(v.get(1).unwrap(), true); |
| 359 |
assert_eq!(v.get(2).unwrap(), false); |
| 360 |
assert_eq!(v.get(3).unwrap(), true); |
| 361 |
assert_eq!(v.get(4).unwrap(), true); |
| 362 |
assert_eq!(v.get(7).unwrap(), true); |
| 363 |
assert_eq!(v.get(8).unwrap(), false); |
| 364 |
assert_eq!(v.get(9).unwrap(), false); |
| 365 |
assert_eq!(v.get(98).unwrap(), false); |
| 366 |
assert_eq!(v.get(99).unwrap(), false); |
| 367 |
} |
| 368 |
|
| 369 |
#[test] |
| 370 |
fn hash() { |
| 371 |
extern crate std; |
| 372 |
use core::hash::{Hash, Hasher}; |
| 373 |
|
| 374 |
let v1 = sbvec![true, false, true, false, true, true, true]; |
| 375 |
let mut v2 = v1.clone(); |
| 376 |
v2.reserve(100_000); |
| 377 |
assert_eq!(v1, v2); |
| 378 |
|
| 379 |
let hash = |v: &SmallBitVec| { |
| 380 |
let mut hasher = std::collections::hash_map::DefaultHasher::new(); |
| 381 |
v.hash(&mut hasher); |
| 382 |
hasher.finish() |
| 383 |
}; |
| 384 |
|
| 385 |
assert_eq!(hash(&v1), hash(&v2)); |
| 386 |
|
| 387 |
let mut v3 = v1.clone(); |
| 388 |
v3.push(false); |
| 389 |
assert_ne!(hash(&v1), hash(&v3)); |
| 390 |
|
| 391 |
let mut v4 = v2.clone(); |
| 392 |
v4.push(false); |
| 393 |
assert_ne!(hash(&v2), hash(&v4)); |
| 394 |
|
| 395 |
assert_eq!(v3, v4); |
| 396 |
assert_eq!(hash(&v3), hash(&v4)); |
| 397 |
} |