1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
|
require "spec"
require "spec/helpers/iterate"
private class DequeTester
# Execute the same actions on an Array and a Deque and compare them at each step.
@deque : Deque(Int32)
@array : Array(Int32)
@i : Int32
@c : Array(Int32) | Deque(Int32) | Nil
def step(&)
@c = @deque
yield
@c = @array
yield
@deque.to_a.should eq(@array)
@i += 1
end
def initialize
@deque = Deque(Int32).new
@array = Array(Int32).new
@i = 1
end
getter i
def c
@c.not_nil!
end
def test(&)
with self yield
end
end
private alias RecursiveDeque = Deque(RecursiveDeque)
# Yields multiple forms of `Deque{}`, `Deque{0}`, `Deque{0, 1}`, `Deque{0, 1, 2}`, ...,
# `Deque{0, 1, 2, ..., max_size - 1}`. All deques have *max_size* as their
# capacity; each deque is yielded *max_size* times with a different start
# position in the internal buffer. Every deque is a fresh instance.
private def each_queue_repr(max_size, &)
(0..max_size).each do |size|
max_size.times do |i|
x = Deque(Int32).new(max_size, 0)
x.rotate!(i)
x.pop(max_size - size)
x.fill &.itself
yield x
end
end
end
describe "Deque" do
describe "implementation" do
it "works the same as array" do
DequeTester.new.test do
step { c.unshift i }
step { c.pop }
step { c.push i }
step { c.shift }
step { c.push i }
step { c.push i }
step { c.push i }
step { c.push i }
step { c.push i }
step { c.push i }
step { c.pop }
step { c.shift }
step { c.push i }
step { c.push i }
step { c.push i }
step { c.push i }
step { c.push i }
step { c.unshift i }
step { c.unshift i }
step { c.unshift i }
step { c.unshift i }
step { c.unshift i }
step { c.unshift i }
step { c.insert(1, i) }
step { c.insert(0, i) }
step { c.insert(17, i) }
step { c.insert(14, i) }
step { c.insert(10, i) }
step { c.insert(10, i) }
end
end
it "works the same as array when inserting at 1/8 size and deleting at 3/4 size" do
DequeTester.new.test do
1000.times do
step { c.insert(c.size // 8, i) }
end
1000.times do
step { c.delete_at(c.size * 3 // 4) }
end
end
end
it "works the same as array when inserting at 3/4 size and deleting at 1/8 size" do
DequeTester.new.test do
1000.times do
step { c.insert(c.size * 3 // 4, i) }
end
1000.times do
step { c.delete_at(c.size // 8) }
end
end
end
end
describe "new" do
it "creates with default value" do
deq = Deque.new(5, 3)
deq.should eq(Deque{3, 3, 3, 3, 3})
end
it "creates with default value in block" do
deq = Deque.new(5) { |i| i * 2 }
deq.should eq(Deque{0, 2, 4, 6, 8})
end
it "creates from an array" do
deq = Deque(Int32).new([1, 2, 3, 4, 5])
deq.should eq(Deque{1, 2, 3, 4, 5})
end
it "raises on negative count" do
expect_raises(ArgumentError, "Negative deque size") do
Deque.new(-1, 3)
end
end
it "raises on negative capacity" do
expect_raises(ArgumentError, "Negative deque capacity") do
Deque(Int32).new(-1)
end
end
end
describe "==" do
it "compares empty" do
Deque(Int32).new.should eq(Deque(Int32).new)
Deque{1}.should_not eq(Deque(Int32).new)
Deque(Int32).new.should_not eq(Deque{1})
end
it "compares elements" do
Deque{1, 2, 3}.should eq(Deque{1, 2, 3})
Deque{1, 2, 3}.should_not eq(Deque{3, 2, 1})
end
it "compares other" do
a = Deque{1, 2, 3}
b = Deque{1, 2, 3}
c = Deque{1, 2, 3, 4}
d = Deque{1, 2, 4}
(a == b).should be_true
(b == c).should be_false
(a == d).should be_false
end
it "compares other types" do
a = Deque{1, 2, 3}
b = Deque{true, false}
c = "other type"
(a == b).should be_false
(b == c).should be_false
(a == c).should be_false
end
end
describe "+" do
it "does +" do
a = Deque{1, 2, 3}
b = Deque{4, 5}
c = a + b
c.size.should eq(5)
0.upto(4) { |i| c[i].should eq(i + 1) }
end
it "does + with different types" do
a = Deque{1, 2, 3}
a += Deque{"hello"}
a.should eq(Deque{1, 2, 3, "hello"})
end
end
describe "[]" do
it "gets on positive index" do
Deque{1, 2, 3}[1].should eq(2)
end
it "gets on negative index" do
Deque{1, 2, 3}[-1].should eq(3)
end
it "gets nilable" do
Deque{1, 2, 3}[2]?.should eq(3)
Deque{1, 2, 3}[3]?.should be_nil
end
end
describe "[]=" do
it "sets on positive index" do
a = Deque{1, 2, 3}
a[1] = 4
a[1].should eq(4)
end
it "sets on negative index" do
a = Deque{1, 2, 3}
a[-1] = 4
a[2].should eq(4)
end
end
it "does clear" do
a = Deque{1, 2, 3}
a.clear
a.should eq(Deque(Int32).new)
end
it "does clone" do
x = {1 => 2}
a = Deque{x}
b = a.clone
b.should eq(a)
a.should_not be(b)
a[0].should_not be(b[0])
end
it "does clone with recursive type" do
deq = Deque(RecursiveDeque).new
deq << deq
clone = deq.clone
clone.should be(clone.first)
end
describe "concat" do
it "concats deque" do
a = Deque{1, 2, 3}
a.concat(Deque{4, 5, 6})
a.should eq(Deque{1, 2, 3, 4, 5, 6})
end
it "concats large deques" do
a = Deque{1, 2, 3}
a.concat((4..1000).to_a)
a.should eq(Deque.new((1..1000).to_a))
end
it "concats enumerable" do
a = Deque{1, 2, 3}
a.concat((4..1000))
a.should eq(Deque.new((1..1000).to_a))
end
it "concats indexable" do
each_queue_repr(16) do |a|
size = a.size
b = Array.new(10, &.+(size))
a.concat(b).should be(a)
a.should eq(Deque.new(size + 10, &.itself))
end
each_queue_repr(16) do |a|
size = a.size
b = Slice.new(10, &.+(size))
a.concat(b).should be(a)
a.should eq(Deque.new(size + 10, &.itself))
end
each_queue_repr(16) do |a|
size = a.size
b = StaticArray(Int32, 10).new(&.+(size))
a.concat(b).should be(a)
a.should eq(Deque.new(size + 10, &.itself))
end
each_queue_repr(16) do |a|
size = a.size
b = Deque.new(10, &.+(size))
a.concat(b).should be(a)
a.should eq(Deque.new(size + 10, &.itself))
end
end
it "concats itself" do
each_queue_repr(16) do |a|
size = a.size
a.concat(a).should be(a)
a.should eq(Deque.new((0...size).to_a * 2))
end
end
end
describe "delete" do
it "deletes many" do
a = Deque{1, 2, 3, 1, 2, 3}
a.delete(2).should be_true
a.should eq(Deque{1, 3, 1, 3})
end
it "delete not found" do
a = Deque{1, 2}
a.delete(4).should be_false
a.should eq(Deque{1, 2})
end
end
describe "delete_at" do
it "deletes positive index" do
a = Deque{1, 2, 3, 4, 5}
a.delete_at(3).should eq(4)
a.should eq(Deque{1, 2, 3, 5})
end
it "deletes negative index" do
a = Deque{1, 2, 3, 4, 5}
a.delete_at(-4).should eq(2)
a.should eq(Deque{1, 3, 4, 5})
end
it "deletes out of bounds" do
a = Deque{1, 2, 3, 4}
expect_raises IndexError do
a.delete_at(4)
end
end
end
it "does dup" do
x = {1 => 2}
a = Deque{x}
b = a.dup
b.should eq(Deque{x})
a.should_not be(b)
a[0].should be(b[0])
b << {3 => 4}
a.should eq(Deque{x})
end
it "does each" do
a = Deque{1, 1, 1}
b = 0
a.each { |i| b += i }.should be_nil
b.should eq(3)
end
it "does each_index" do
a = Deque{1, 1, 1}
b = 0
a.each_index { |i| b += i }.should be_nil
b.should eq(3)
end
describe "empty" do
it "is empty" do
(Deque(Int32).new.empty?).should be_true
end
it "is not empty" do
Deque{1}.empty?.should be_false
end
end
it "does equals? with custom block" do
a = Deque{1, 3, 2}
b = Deque{3, 9, 4}
c = Deque{5, 7, 3}
d = Deque{1, 3, 2, 4}
f = ->(x : Int32, y : Int32) { (x % 2) == (y % 2) }
a.equals?(b, &f).should be_true
a.equals?(c, &f).should be_false
a.equals?(d, &f).should be_false
end
describe "first" do
it "gets first when non empty" do
a = Deque{1, 2, 3}
a.first.should eq(1)
end
it "raises when empty" do
expect_raises Enumerable::EmptyError do
Deque(Int32).new.first
end
end
end
describe "first?" do
it "gets first? when non empty" do
a = Deque{1, 2, 3}
a.first?.should eq(1)
end
it "gives nil when empty" do
Deque(Int32).new.first?.should be_nil
end
end
it "does hash" do
a = Deque{1, 2, Deque{3}}
b = Deque{1, 2, Deque{3}}
a.hash.should eq(b.hash)
end
describe "insert" do
it "inserts with positive index" do
a = Deque{1, 3, 4}
expected = Deque{1, 2, 3, 4}
a.insert(1, 2).should eq(expected)
a.should eq(expected)
end
it "inserts with negative index" do
a = Deque{1, 2, 3}
expected = Deque{1, 2, 3, 4}
a.insert(-1, 4).should eq(expected)
a.should eq(expected)
end
it "inserts with negative index (2)" do
a = Deque{1, 2, 3}
expected = Deque{4, 1, 2, 3}
a.insert(-4, 4).should eq(expected)
a.should eq(expected)
end
it "inserts out of range" do
a = Deque{1, 3, 4}
expect_raises IndexError do
a.insert(4, 1)
end
end
end
describe "inspect" do
it { Deque{1, 2, 3}.inspect.should eq("Deque{1, 2, 3}") }
end
describe "last" do
it "gets last when non empty" do
a = Deque{1, 2, 3}
a.last.should eq(3)
end
it "raises when empty" do
expect_raises IndexError do
Deque(Int32).new.last
end
end
end
describe "size" do
it "has size 0" do
Deque(Int32).new.size.should eq(0)
end
it "has size 2" do
Deque{1, 2}.size.should eq(2)
end
end
describe "pop" do
it "pops when non empty" do
a = Deque{1, 2, 3}
a.pop.should eq(3)
a.should eq(Deque{1, 2})
end
it "raises when empty" do
expect_raises IndexError do
Deque(Int32).new.pop
end
end
it "pops many elements" do
a = Deque{1, 2, 3, 4, 5}
a.pop(3)
a.should eq(Deque{1, 2})
end
it "pops more elements than what is available" do
a = Deque{1, 2, 3, 4, 5}
a.pop(10)
a.should eq(Deque(Int32).new)
end
it "pops negative count raises" do
a = Deque{1, 2}
expect_raises ArgumentError do
a.pop(-1)
end
end
end
describe "push" do
it "adds one element to the deque" do
a = Deque{"a", "b"}
a.push("c")
a.should eq Deque{"a", "b", "c"}
end
it "returns the deque" do
a = Deque{"a", "b"}
a.push("c").should eq Deque{"a", "b", "c"}
end
it "has the << alias" do
a = Deque{"a", "b"}
a << "c"
a.should eq Deque{"a", "b", "c"}
end
end
describe "reject!" do
it "with block" do
a1 = Deque{1, 2, 3, 4, 5}
a2 = a1.reject! &.even?
a2.should eq(Deque{1, 3, 5})
a2.should be(a1)
end
it "with pattern" do
a1 = Deque{1, 2, 3, 4, 5}
a2 = a1.reject!(2..4)
a2.should eq(Deque{1, 5})
a2.should be(a1)
end
end
describe "rotate!" do
it "rotates" do
a = Deque{1, 2, 3, 4, 5}
a.rotate!
a.should eq(Deque{2, 3, 4, 5, 1})
a.rotate!(-2)
a.should eq(Deque{5, 1, 2, 3, 4})
a.rotate!(10)
a.should eq(Deque{5, 1, 2, 3, 4})
end
it "rotates with size=capacity" do
a = Deque{1, 2, 3, 4}
a.rotate!
a.should eq(Deque{2, 3, 4, 1})
a.rotate!(-2)
a.should eq(Deque{4, 1, 2, 3})
a.rotate!(8)
a.should eq(Deque{4, 1, 2, 3})
end
it "rotates with size=0" do
a = Deque(Int32).new
a.rotate!
a.should eq(Deque(Int32).new)
end
it "rotates with size=1" do
a = Deque{1}
a.rotate!
a.should eq(Deque{1})
end
end
describe "select!" do
it "with block" do
a1 = Deque{1, 2, 3, 4, 5}
a2 = a1.select! &.even?
a2.should eq(Deque{2, 4})
a2.should be(a1)
end
it "with pattern" do
a1 = Deque{1, 2, 3, 4, 5}
a2 = a1.select!(2..4)
a2.should eq(Deque{2, 3, 4})
a2.should be(a1)
end
end
describe "shift" do
it "shifts when non empty" do
a = Deque{1, 2, 3}
a.shift.should eq(1)
a.should eq(Deque{2, 3})
end
it "raises when empty" do
expect_raises IndexError do
Deque(Int32).new.shift
end
end
it "shifts many elements" do
a = Deque{1, 2, 3, 4, 5}
a.shift(3)
a.should eq(Deque{4, 5})
end
it "shifts more than what is available" do
a = Deque{1, 2, 3, 4, 5}
a.shift(10)
a.should eq(Deque(Int32).new)
end
it "shifts negative count raises" do
a = Deque{1, 2}
expect_raises ArgumentError do
a.shift(-1)
end
end
end
describe "swap" do
it "swaps" do
a = Deque{1, 2, 3}
a.swap(0, 2)
a.should eq(Deque{3, 2, 1})
end
it "swaps with negative indices" do
a = Deque{1, 2, 3}
a.swap(-3, -1)
a.should eq(Deque{3, 2, 1})
end
it "swaps but raises out of bounds on left" do
a = Deque{1, 2, 3}
expect_raises IndexError do
a.swap(3, 0)
end
end
it "swaps but raises out of bounds on right" do
a = Deque{1, 2, 3}
expect_raises IndexError do
a.swap(0, 3)
end
end
end
describe "to_s" do
it "does to_s" do
Deque{1, 2, 3}.to_s.should eq("Deque{1, 2, 3}")
end
it "does with recursive" do
deq = Deque(RecursiveDeque).new
deq << deq
deq.to_s.should eq("Deque{Deque{...}}")
end
end
it "does unshift" do
a = Deque{2, 3}
expected = Deque{1, 2, 3}
a.unshift(1).should eq(expected)
a.should eq(expected)
end
describe "each iterator" do
it_iterates "#each", [1, 2, 3], Deque{1, 2, 3}.each
it "works while modifying deque" do
a = Deque{1, 2, 3}
count = 0
it = a.each
it.each do
count += 1
a.clear
end
count.should eq(1)
end
end
describe "each_index iterator" do
it_iterates "#each_index", [0, 1, 2], Deque{1, 2, 3}.each_index
it "works while modifying deque" do
a = Deque{1, 2, 3}
count = 0
it = a.each_index
it.each do
count += 1
a.clear
end
count.should eq(1)
end
end
it_iterates "#reverse_each", [3, 2, 1], Deque{1, 2, 3}.reverse_each
it_iterates "#cycle", [1, 2, 3, 1, 2, 3, 1, 2], Deque{1, 2, 3}.cycle, infinite: true
it_iterates "#cycle(limit)", [1, 2, 3, 1, 2, 3], Deque{1, 2, 3}.cycle(2)
end
|