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 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
|
require "./spec_helper"
require "spec/helpers/iterate"
require "big"
struct RangeSpecIntWrapper
include Comparable(self)
getter value : Int32
def initialize(@value)
end
def succ
RangeSpecIntWrapper.new(@value + 1)
end
def <=>(other)
value <=> other.value
end
def self.additive_identity
RangeSpecIntWrapper.new(0)
end
def +(other : RangeSpecIntWrapper)
RangeSpecIntWrapper.new(value + other.value)
end
end
private def range_endless_each
(2..).each do |x|
return x
end
end
private def range_beginless_reverse_each
(..2).reverse_each do |x|
return x
end
end
describe "Range" do
it "initialized with new method" do
Range.new(1, 10).should eq(1..10)
Range.new(1, 10, false).should eq(1..10)
Range.new(1, 10, true).should eq(1...10)
end
it "gets basic properties" do
r = 1..5
r.begin.should eq(1)
r.end.should eq(5)
r.excludes_end?.should be_false
r = 1...5
r.begin.should eq(1)
r.end.should eq(5)
r.excludes_end?.should be_true
end
it "#==" do
((1..1) == (1..1)).should be_true
((1...1) == (1..1)).should be_false
((1...1) == (1...1)).should be_true
((1..1) == (1...1)).should be_false
((1..nil) == (1..nil)).should be_true
(1..1).should eq Range(Int32?, Int32?).new(1, 1)
((1..1) == Range(Int32?, Int32?).new(1, 1)).should be_true
((1.0..1.0) == (1..1)).should be_true
end
it "includes?" do
(1..5).includes?(1).should be_true
(1..5).includes?(5).should be_true
(1...5).includes?(1).should be_true
(1...5).includes?(5).should be_false
end
it "does to_s" do
(1...5).to_s.should eq("1...5")
(1..5).to_s.should eq("1..5")
(1..nil).to_s.should eq("1..")
(nil..3).to_s.should eq("..3")
(nil..nil).to_s.should eq("..")
end
it "does inspect" do
(1...5).inspect.should eq("1...5")
end
it "is empty with .. and begin > end" do
(1..0).to_a.should be_empty
end
it "is empty with ... and begin > end" do
(1...0).to_a.should be_empty
end
it "is not empty with .. and begin == end" do
(1..1).to_a.should eq([1])
end
it "is not empty with ... and begin.succ == end" do
(1...2).to_a.should eq([1])
end
describe "sum" do
it "called with no block is specialized for performance" do
(1..3).sum.should eq 6
(1...3).sum.should eq 3
(1..3).sum(4).should eq 10
(3..1).sum(4).should eq 4
(1..11).step(2).sum.should eq 36
(1...11).step(2).sum.should eq 25
end
it "called with no block is specialized for performance (BigInt)" do
(BigInt.new("1")..BigInt.new("1 000 000 000")).sum.should eq BigInt.new("500 000 000 500 000 000")
(BigInt.new("1")..BigInt.new("1 000 000 000")).step(2).sum.should eq BigInt.new("250 000 000 000 000 000")
end
it "is equivalent to Enumerable#sum" do
(1..3).sum { |x| x * 2 }.should eq 12
(1..3).step(2).sum { |x| x * 2 }.should eq 8
(RangeSpecIntWrapper.new(1)..RangeSpecIntWrapper.new(3)).sum.should eq RangeSpecIntWrapper.new(6)
(RangeSpecIntWrapper.new(1)..RangeSpecIntWrapper.new(3)).step(2).sum.should eq RangeSpecIntWrapper.new(4)
end
end
describe "bsearch" do
it "Int" do
ary = [3, 4, 7, 9, 12]
(0...ary.size).bsearch { |i| ary[i] >= 2 }.should eq 0
(0...ary.size).bsearch { |i| ary[i] >= 4 }.should eq 1
(0...ary.size).bsearch { |i| ary[i] >= 6 }.should eq 2
(0...ary.size).bsearch { |i| ary[i] >= 8 }.should eq 3
(0...ary.size).bsearch { |i| ary[i] >= 10 }.should eq 4
(0...ary.size).bsearch { |i| ary[i] >= 100 }.should eq nil
(0...ary.size).bsearch { |i| true }.should eq 0
(0...ary.size).bsearch { |i| false }.should eq nil
(0...ary.size).bsearch { |i| ary[i] >= 10 ? 1 : nil }.should eq 4
ary = [0, 100, 100, 100, 200]
(0...ary.size).bsearch { |i| ary[i] >= 100 }.should eq 1
(0_i8..10_i8).bsearch { |x| x >= 10 }.should eq 10_i8
(0_i8...10_i8).bsearch { |x| x >= 10 }.should eq nil
(-10_i8...10_i8).bsearch { |x| x >= -5 }.should eq -5_i8
(0_u8..10_u8).bsearch { |x| x >= 10 }.should eq 10_u8
(0_u8...10_u8).bsearch { |x| x >= 10 }.should eq nil
(0_u32..10_u32).bsearch { |x| x >= 10 }.should eq 10_u32
(0_u32...10_u32).bsearch { |x| x >= 10 }.should eq nil
end
it "BigInt" do
(BigInt.new("-10")...BigInt.new("10")).bsearch { |x| x >= -5 }.should eq BigInt.new("-5")
end
it "Float" do
inf = Float64::INFINITY
(0.0...100.0).bsearch { |x| x > 0 && Math.log(x / 10) >= 0 }.not_nil!.should be_close(10.0, 0.0001)
(0.0...inf).bsearch { |x| x > 0 && Math.log(x / 10) >= 0 }.not_nil!.should be_close(10.0, 0.0001)
(-inf..100.0).bsearch { |x| x >= 0 || Math.log(-x / 10) < 0 }.not_nil!.should be_close(-10.0, 0.0001)
(-inf..inf).bsearch { |x| x > 0 && Math.log(x / 10) >= 0 }.not_nil!.should be_close(10.0, 0.0001)
(-inf..5).bsearch { |x| x > 0 && Math.log(x / 10) >= 0 }.should be_nil
(-inf..10).bsearch { |x| x > 0 && Math.log(x / 10) >= 0 }.not_nil!.should be_close(10.0, 0.0001)
(inf...10).bsearch { |x| x > 0 && Math.log(x / 10) >= 0 }.should be_nil
(-inf..inf).bsearch { false }.should be_nil
(-inf..inf).bsearch { true }.should eq -inf
(0..inf).bsearch { |x| x == inf }.should eq inf
(0...inf).bsearch { |x| x == inf }.should be_nil
v = (0.0..1.0).bsearch { |x| x > 0 }.not_nil!
v.should be_close(0, 0.0001)
v.should be > 0
(-1.0..0.0).bsearch { |x| x >= 0 }.should eq 0.0
(-1.0...0.0).bsearch { |x| x >= 0 }.should be_nil
(0.0..inf).bsearch { |x| Math.log(x) >= 0 }.not_nil!.should be_close(1.0, 0.0001)
(0.0..10).bsearch { |x| x >= 3.5 }.not_nil!.should be_close(3.5, 0.0001)
(0..10.0).bsearch { |x| x >= 3.5 }.not_nil!.should be_close(3.5, 0.0001)
(0_f32..5_f32).bsearch { |x| x >= 5_f32 }.not_nil!.should be_close(5_f32, 0.0001_f32)
(0_f32...5_f32).bsearch { |x| x >= 5_f32 }.should be_nil
(0_f32..5.0).bsearch { |x| x >= 5.0 }.not_nil!.should be_close(5.0, 0.0001)
(0..5.0_f32).bsearch { |x| x >= 5.0 }.not_nil!.should be_close(5.0, 0.0001)
inf32 = Float32::INFINITY
(0..inf32).bsearch { |x| x == inf32 }.should eq inf32
(0_f32..inf).bsearch { |x| x == inf }.should eq inf
(0.0..inf32).bsearch { |x| x == inf32 }.should eq inf32
(0_f32...5_f32).bsearch { |x| x >= 5_f32 }.should be_nil
end
end
describe "#each" do
it "gives correct values with inclusive range" do
range = -1..3
arr = [] of Int32
range.each { |x| arr << x }
arr.should eq([-1, 0, 1, 2, 3])
end
it "gives correct values with exclusive range" do
range = 'a'...'c'
arr = [] of Char
range.each { |x| arr << x }
arr.should eq(['a', 'b'])
end
it "is empty with empty inclusive range" do
range = 0..-1
any = false
range.each { any = true }
any.should eq(false)
end
it "endless" do
range = (3..nil)
ary = [] of Int32
range.each do |x|
ary << x
break if ary.size == 5
end
ary.should eq([3, 4, 5, 6, 7])
end
it "raises on beginless" do
expect_raises(ArgumentError, "Can't each beginless range") do
(..4).each { }
end
typeof((..4).each { |x| break x }).should eq Nil
expect_raises(ArgumentError, "Can't each beginless range") do
(nil.as(Int32?)..4).each { }
end
typeof((nil.as(Int32?)..4).each { |x| break x }).should eq Int32?
end
it "doesn't have Nil as a type for endless each" do
typeof(range_endless_each).should eq(Int32)
end
it "doesn't have Nil as a type for beginless each" do
typeof(range_beginless_reverse_each).should eq(Int32)
end
end
describe "#reverse_each" do
it "gives correct values with inclusive range" do
range = 'a'..'c'
arr = [] of Char
range.reverse_each { |x| arr << x }
arr.should eq(['c', 'b', 'a'])
end
it "gives correct values with exclusive range" do
range = -1...3
arr = [] of Int32
range.reverse_each { |x| arr << x }
arr.should eq([2, 1, 0, -1])
end
it "is empty with empty inclusive range" do
range = 0..-1
any = false
range.reverse_each { any = true }
any.should eq(false)
end
it "raises on endless range" do
expect_raises(ArgumentError, "Can't reverse_each endless range") do
(3..).reverse_each { }
end
expect_raises(ArgumentError, "Can't reverse_each endless range") do
(3..nil.as(Int32?)).reverse_each { }
end
end
it "iterators on beginless range" do
range = nil..2
arr = [] of Int32
range.reverse_each do |x|
arr << x
break if arr.size == 5
end
arr.should eq([2, 1, 0, -1, -2])
end
end
describe "#each iterator" do
it "does next with inclusive range" do
a = 1..3
iter = a.each
iter.next.should eq(1)
iter.next.should eq(2)
iter.next.should eq(3)
iter.next.should be_a(Iterator::Stop)
end
it "does next with exclusive range" do
r = 1...3
iter = r.each
iter.next.should eq(1)
iter.next.should eq(2)
iter.next.should be_a(Iterator::Stop)
end
it "does with endless range" do
r = (3..nil)
iter = r.each
iter.next.should eq(3)
iter.next.should eq(4)
end
it "raises on beginless range" do
expect_raises(ArgumentError, "Can't each beginless range") do
(..3).each
end
expect_raises(ArgumentError, "Can't each beginless range") do
(nil.as(Int32?)..3).each
end
end
it "cycles" do
(1..3).cycle.first(8).join.should eq("12312312")
end
it "is empty with .. and begin > end" do
(1..0).each.to_a.should be_empty
end
it "is empty with ... and begin > end" do
(1...0).each.to_a.should be_empty
end
it "is not empty with .. and begin == end" do
(1..1).each.to_a.should eq([1])
end
it "is not empty with ... and begin.succ == end" do
(1...2).each.to_a.should eq([1])
end
end
describe "#reverse_each iterator" do
it "does next with inclusive range" do
a = 1..3
iter = a.reverse_each
iter.next.should eq(3)
iter.next.should eq(2)
iter.next.should eq(1)
iter.next.should be_a(Iterator::Stop)
end
it "does next with exclusive range" do
r = 1...3
iter = r.reverse_each
iter.next.should eq(2)
iter.next.should eq(1)
iter.next.should be_a(Iterator::Stop)
end
it "does next with beginless range" do
r = nil...3
iter = r.reverse_each
iter.next.should eq(2)
iter.next.should eq(1)
iter.next.should eq(0)
iter.next.should eq(-1)
end
it "reverse cycles" do
(1..3).reverse_each.cycle.first(8).join.should eq("32132132")
end
it "is empty with .. and begin > end" do
(1..0).reverse_each.to_a.should be_empty
end
it "is empty with ... and begin > end" do
(1...0).reverse_each.to_a.should be_empty
end
it "is not empty with .. and begin == end" do
(1..1).reverse_each.to_a.should eq([1])
end
it "is not empty with ... and begin.succ == end" do
(1...2).reverse_each.to_a.should eq([1])
end
it "raises on endless range" do
expect_raises(ArgumentError, "Can't reverse_each endless range") do
(1..).reverse_each
end
expect_raises(ArgumentError, "Can't reverse_each endless range") do
(1..nil.as(Int32?)).reverse_each
end
end
end
describe "#sample" do
it "raises on open range" do
expect_raises(ArgumentError, "Can't sample an open range") do
(1..).sample
end
expect_raises(ArgumentError, "Can't sample an open range") do
(1..nil.as(Int32?)).sample
end
expect_raises(ArgumentError, "Can't sample an open range") do
(..1).sample
end
expect_raises(ArgumentError, "Can't sample an open range") do
(nil.as(Int32?)..1).sample
end
expect_raises(ArgumentError, "Can't sample an open range") do
(..).sample
end
expect_raises(ArgumentError, "Can't sample an open range") do
(nil.as(Int32?)..nil.as(Int32?)).sample
end
end
it "samples a float range as a distribution" do
r = (1.2..3.4)
x = r.sample
r.should contain(x)
r.sample(Random.new(1)).should be_close(2.9317256017544837, 1e-12)
end
it "samples a range with nilable types" do
r = ((true ? 1 : nil)..(true ? 4 : nil))
x = r.sample
r.should contain(x)
((true ? 1 : nil)...(true ? 2 : nil)).sample.should eq(1)
r = ((true ? 1.2 : nil)..(true ? 3.4 : nil))
x = r.sample
r.should contain(x)
end
it "samples with n = 0" do
(1..3).sample(0).empty?.should be_true
end
context "for an integer range" do
it "samples an inclusive range without n" do
value = (1..3).sample
(1 <= value <= 3).should be_true
end
it "samples an exclusive range without n" do
value = (1...3).sample
(1 <= value <= 2).should be_true
end
it "samples an inclusive range with n = 1" do
values = (1..3).sample(1)
values.size.should eq(1)
(1 <= values.first <= 3).should be_true
end
it "samples an exclusive range with n = 1" do
values = (1...3).sample(1)
values.size.should eq(1)
(1 <= values.first <= 2).should be_true
end
it "samples an inclusive range with n > 1" do
values = (1..10).sample(5)
values.size.should eq(5)
values.uniq.size.should eq(5)
values.all? { |value| 1 <= value <= 10 }.should be_true
end
it "samples an exclusive range with n > 1" do
values = (1...10).sample(5)
values.size.should eq(5)
values.uniq.size.should eq(5)
values.all? { |value| 1 <= value <= 9 }.should be_true
end
it "samples an inclusive range with n > 16" do
values = (1..1000).sample(100)
values.size.should eq(100)
values.uniq.size.should eq(100)
values.all? { |value| 1 <= value <= 1000 }.should be_true
end
it "samples an inclusive range with n equal to or bigger than the available values" do
values = (1..10).sample(20)
values.size.should eq(10)
values.uniq.size.should eq(10)
values.all? { |value| 1 <= value <= 10 }.should be_true
end
it "raises on invalid range without n" do
expect_raises ArgumentError do
(1..0).sample
end
end
it "raises on invalid range with n = 0" do
expect_raises ArgumentError do
(1..0).sample(0)
end
end
it "raises on invalid range with n = 1" do
expect_raises ArgumentError do
(1..0).sample(1)
end
end
it "raises on invalid range with n > 1" do
expect_raises ArgumentError do
(1..0).sample(10)
end
end
it "raises on exclusive range that would underflow" do
expect_raises ArgumentError do
(1_u8...0_u8).sample(10)
end
end
end
context "for a float range" do
it "samples an inclusive range without n" do
value = (1.0..2.0).sample
(1.0 <= value <= 2.0).should be_true
end
it "samples an exclusive range without n" do
value = (1.0...2.0).sample
(1.0 <= value < 2.0).should be_true
end
it "samples an inclusive range with n = 1" do
values = (1.0..2.0).sample(1)
values.size.should eq(1)
(1.0 <= values.first <= 2.0).should be_true
end
it "samples an exclusive range with n = 1" do
values = (1.0..2.0).sample(1)
values.size.should eq(1)
(1.0 <= values.first < 2.0).should be_true
end
it "samples an inclusive range with n > 1" do
values = (1.0..2.0).sample(10)
values.size.should eq(10)
values.all? { |value| 1.0 <= value <= 2.0 }.should be_true
end
it "samples an exclusive range with n > 1" do
values = (1.0...2.0).sample(10)
values.size.should eq(10)
values.all? { |value| 1.0 <= value < 2.0 }.should be_true
end
it "samples an inclusive range with n >= 1 and begin == end" do
values = (1.0..1.0).sample(3)
values.size.should eq(1)
values.first.should eq(1.0)
end
it "samples an inclusive range with n > 16" do
values = (1.0..2.0).sample(100)
values.size.should eq(100)
values.all? { |value| 1.0 <= value <= 2.0 }.should be_true
end
it "raises on invalid range with n = 0" do
expect_raises ArgumentError do
(1.0..0.0).sample(0)
end
end
it "raises on invalid range with n = 1" do
expect_raises ArgumentError do
(1.0..0.0).sample(1)
end
end
it "raises on invalid range with n > 1" do
expect_raises ArgumentError do
(1.0..0.0).sample(10)
end
end
end
end
describe "#step" do
it_iterates "inclusive default", [1, 2, 3, 4, 5], (1..5).step
it_iterates "inclusive step", [1, 3, 5], (1..5).step(2)
it_iterates "inclusive step over", [1, 3, 5], (1..6).step(2)
it_iterates "exclusive default", [1, 2, 3, 4], (1...5).step
it_iterates "exclusive step", [1, 3], (1...5).step(2)
it_iterates "exclusive step over", [1, 3, 5], (1...6).step(2)
it_iterates "endless range", [1, 3, 5, 7, 9], (1...nil).step(2), infinite: true
it "raises on beginless range" do
expect_raises(ArgumentError, "Can't step beginless range") do
(nil..3).step(2) { }
end
end
it_iterates "begin > end inclusive", [] of Int32, (1..0).step(1)
it_iterates "begin > end exclusive", [] of Int32, (1...0).step(1)
it_iterates "begin == end inclusive", [1], (1..1).step(1)
it_iterates "begin == end exclusive", [] of Int32, (1...1).step(1)
it_iterates "begin.succ == end inclusive", [1, 2] of Int32, (1..2).step(1)
it_iterates "begin.succ == end exclusive", [1] of Int32, (1...2).step(1)
it_iterates "Float step", [1.0, 1.5, 2.0, 2.5, 3.0], (1..3).step(by: 0.5)
it_iterates "Time::Span step", [1.minutes, 2.minutes, 3.minutes], (1.minutes..3.minutes).step(by: 1.minutes)
describe "with #succ type" do
range_basic = RangeSpecIntWrapper.new(1)..RangeSpecIntWrapper.new(5)
it_iterates "basic", [1, 2, 3, 4, 5].map(&->RangeSpecIntWrapper.new(Int32)), range_basic.step
it_iterates "basic by", [1, 3, 5].map(&->RangeSpecIntWrapper.new(Int32)), range_basic.step(by: 2)
it_iterates "missing end by", [1, 4].map(&->RangeSpecIntWrapper.new(Int32)), range_basic.step(by: 3)
it_iterates "at definition range",
[Int32::MAX - 2, Int32::MAX - 1, Int32::MAX].map(&->RangeSpecIntWrapper.new(Int32)),
(RangeSpecIntWrapper.new(Int32::MAX - 2)..RangeSpecIntWrapper.new(Int32::MAX)).step
it_iterates "at definition range by",
[RangeSpecIntWrapper.new(Int32::MAX - 2), RangeSpecIntWrapper.new(Int32::MAX)],
(RangeSpecIntWrapper.new(Int32::MAX - 2)..RangeSpecIntWrapper.new(Int32::MAX)).step(by: 2)
it_iterates "at definition range missing by",
[RangeSpecIntWrapper.new(Int32::MAX - 1)],
(RangeSpecIntWrapper.new(Int32::MAX - 1)..RangeSpecIntWrapper.new(Int32::MAX)).step(by: 2)
it_iterates "at definition range by",
[RangeSpecIntWrapper.new(Int32::MAX - 3), RangeSpecIntWrapper.new(Int32::MAX - 1)],
(RangeSpecIntWrapper.new(Int32::MAX - 3)..RangeSpecIntWrapper.new(Int32::MAX - 1)).step(by: 2)
it_iterates "at definition range missing by",
[RangeSpecIntWrapper.new(Int32::MAX - 2)],
(RangeSpecIntWrapper.new(Int32::MAX - 2)..RangeSpecIntWrapper.new(Int32::MAX - 1)).step(by: 2)
end
end
describe "map" do
it "optimizes for int range" do
(5..12).map(&.itself).should eq([5, 6, 7, 8, 9, 10, 11, 12])
(5...12).map(&.itself).should eq([5, 6, 7, 8, 9, 10, 11])
(5..4).map(&.itself).size.should eq(0)
end
it "works for other types" do
('a'..'c').map(&.itself).should eq(['a', 'b', 'c'])
end
end
describe "#size" do
it "optimizes for int range" do
(5..12).size.should eq(8)
(5...12).size.should eq(7)
(5..4).size.should eq(0)
end
it "works for other types" do
('a'..'c').size.should eq(3)
end
it "raises on beginless range" do
expect_raises(ArgumentError, "Can't calculate size of an open range") do
(..3).size
end
expect_raises(ArgumentError, "Can't calculate size of an open range") do
(nil.as(Int32?)..3).size
end
end
it "raises on endless range" do
expect_raises(ArgumentError, "Can't calculate size of an open range") do
(3..).size
end
expect_raises(ArgumentError, "Can't calculate size of an open range") do
(3..nil.as(Int32?)).size
end
end
end
it "clones" do
range = [1]..[2]
clone = range.clone
clone.should eq(range)
clone.begin.should_not be(range.begin)
clone.end.should_not be(range.end)
end
describe "===" do
it "inclusive" do
((1..2) === 0).should be_false
((1..2) === 1).should be_true
((1..2) === 2).should be_true
((1..2) === 3).should be_false
end
it "exclusive" do
((1...2) === 0).should be_false
((1...2) === 1).should be_true
((1...2) === 2).should be_false
end
it "endless" do
((1...nil) === 0).should be_false
((1...nil) === 1).should be_true
((1...nil) === 2).should be_true
((1..nil) === 2).should be_true
end
it "beginless" do
((nil..3) === -1).should be_true
((nil..3) === 3).should be_true
((nil..3) === 4).should be_false
((nil...3) === 2).should be_true
((nil...3) === 3).should be_false
end
it "no limits" do
((nil..nil) === 1).should be_true
end
end
end
|