Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 675 lines (593 sloc) 20.606 kb
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
#! /usr/local/bin/ruby -w

require 'RMagick'
require 'test/unit'
require 'test/unit/ui/console/testrunner'

class ImageList1_UT < Test::Unit::TestCase

    def setup
        @list = Magick::ImageList.new(*FILES[0..9])
        @list2 = Magick::ImageList.new # intersection is 5..9
        @list2 << @list[5]
        @list2 << @list[6]
        @list2 << @list[7]
        @list2 << @list[8]
        @list2 << @list[9]
    end

    def test_delay
        assert_nothing_raised { @list.delay }
        assert_equal(0, @list.delay)
        assert_nothing_raised { @list.delay = 20 }
        assert_equal(20, @list.delay)
        assert_raise(ArgumentError) { @list.delay = 'x' }
    end

    def test_ticks_per_second
        assert_nothing_raised { @list.ticks_per_second }
        assert_equal(100, @list.ticks_per_second)
        assert_nothing_raised { @list.ticks_per_second = 1000 }
        assert_equal(1000, @list.ticks_per_second)
        assert_raise(ArgumentError) { @list.ticks_per_second = 'x' }
    end

    def test_iterations
        assert_nothing_raised { @list.iterations }
        assert_equal(1, @list.iterations)
        assert_nothing_raised { @list.iterations = 20 }
        assert_equal(20, @list.iterations)
        assert_raise(ArgumentError) { @list.iterations = 'x' }
    end

    def test_length
        assert_nothing_raised { @list.length }
        assert_equal(10, @list.length)
        assert_raise(NoMethodError) { @list.length = 2 }
    end

    def test_scene
        assert_nothing_raised { @list.scene }
        assert_equal(9, @list.scene)
        assert_nothing_raised { @list.scene = 0 }
        assert_equal(0, @list.scene)
        assert_nothing_raised { @list.scene = 1 }
        assert_equal(1, @list.scene)
        assert_raise(IndexError) { @list.scene = -1 }
        assert_raise(IndexError) { @list.scene = 1000 }
        assert_raise(IndexError) { @list.scene = nil }

        # allow nil on empty list
        empty_list = Magick::ImageList.new
        assert_nothing_raised { empty_list.scene = nil }
    end

    def test_undef_array_methods
        assert_raise(NoMethodError) { @list.assoc }
        assert_raise(NoMethodError) { @list.flatten }
        assert_raise(NoMethodError) { @list.flatten! }
        assert_raise(NoMethodError) { @list.join }
        assert_raise(NoMethodError) { @list.pack }
        assert_raise(NoMethodError) { @list.rassoc }
    end

    def test_aref
        assert_nothing_raised { @list[0] }
        assert_instance_of(Magick::Image, @list[0])
        assert_instance_of(Magick::Image, @list[-1])
        assert_instance_of(Magick::ImageList, @list[0,1])
        assert_instance_of(Magick::ImageList, @list[0..2])
        assert_nil(@list[20])
    end

    def test_aset
        img = @list[0].copy
        assert_nothing_raised do
            rv = @list[0] = img
            assert_same(img, rv)
            assert_same(img, @list[0])
            assert_equal(0, @list.scene)
        end

        # replace 2 images with 1
        assert_nothing_raised do
            rv = @list[1,2] = img
            assert_same(img, rv)
            assert_equal(9, @list.length)
            assert_same(img, @list[1])
            assert_equal(1, @list.scene)
        end

        # replace 1 image with 2
        assert_nothing_raised do
            ary = [img, img]
            rv = @list[3,1] = ary
            assert_same(ary, rv)
            assert_equal(10, @list.length)
            assert_same(img, @list[3])
            assert_same(img, @list[4])
            assert_equal(4, @list.scene)
        end

        assert_nothing_raised do
            rv = @list[5..6] = img
            assert_same(img, rv)
            assert_equal(9, @list.length)
            assert_equal(6, @list.scene)
        end

        assert_nothing_raised do
            ary = [img, img]
            rv = @list[7..8] = ary
            assert_same(ary, rv)
            assert_equal(9, @list.length)
            assert_same(img, @list[7])
            assert_same(img, @list[8])
            assert_equal(8, @list.scene)
        end

        assert_nothing_raised do
            rv = @list[-1] = img
            assert_same(img, rv)
            assert_equal(9, @list.length)
            assert_same(img, @list[8])
            assert_equal(8, @list.scene)
        end

        assert_raise(ArgumentError) { @list[0] = 1 }
        assert_raise(ArgumentError) { @list[0,1] = [1,2] }
        assert_raise(ArgumentError) { @list[2..3] = 'x' }
    end

    def test_and
        @list.scene = 7
        cur = @list.cur_image
        assert_nothing_raised do
            res = @list & @list2
            assert_instance_of(Magick::ImageList, res)
            assert_equal(5, res.length)
            assert_equal(2, res.scene)
            assert_same(cur, res.cur_image)
        end

        # current scene not in the result, set result scene to last image in result
        @list.scene = 2
        assert_nothing_raised do
            res = @list & @list2
            assert_instance_of(Magick::ImageList, res)
            assert_equal(4, res.scene)
        end

        assert_raise(ArgumentError) { @list & 2 }
    end

    def test_at # There is no Magick::ImageList#at since the super does just fine
        assert_nothing_raised do
            cur = @list.cur_image
            img = @list.at(7)
            assert_same(img, @list[7])
            assert_same(cur, @list.cur_image)
            img = @list.at(10)
            assert_nil(img)
            assert_same(cur, @list.cur_image)
            img = @list.at(-1)
            assert_same(img, @list[9])
            assert_same(cur, @list.cur_image)
        end
    end

    def test_star
        @list.scene = 7
        cur = @list.cur_image
        assert_nothing_raised do
            res = @list * 2
            assert_instance_of(Magick::ImageList, res)
            assert_equal(20, res.length)
            assert_same(cur, res.cur_image)
        end

        assert_raise(ArgumentError) { @list * 'x' }
    end

    def test_plus
        @list.scene = 7
        cur = @list.cur_image
        assert_nothing_raised do
            res = @list + @list2
            assert_instance_of(Magick::ImageList, res)
            assert_equal(15, res.length)
            assert_same(cur, res.cur_image)
        end

        assert_raise(ArgumentError) { @list + [2] }
    end

    def test_minus
        @list.scene = 0
        cur = @list.cur_image
        assert_nothing_raised do
            res = @list - @list2
            assert_instance_of(Magick::ImageList, res)
            assert_equal(5, res.length)
            assert_same(cur, res.cur_image)
        end

        # current scene not in result - set result scene to last image in result
        @list.scene = 7
        cur = @list.cur_image
        assert_nothing_raised do
            res = @list - @list2
            assert_instance_of(Magick::ImageList, res)
            assert_equal(5, res.length)
            assert_equal(4, res.scene)
        end
    end

    def test_catenate
        assert_nothing_raised do
            @list2.each { |img| @list << img }
            assert_equal(15, @list.length)
            assert_equal(14, @list.scene)
        end

        assert_raise(ArgumentError) { @list << 2 }
        assert_raise(ArgumentError) { @list << [2] }
    end

    def test_or
        assert_nothing_raised do
            @list.scene = 7
            # The or of these two lists should be the same as @list
            res = @list | @list2
            assert_instance_of(Magick::ImageList, res)
            assert_equal(res, @list)
        end

        # Try or'ing disjoint lists
        temp_list = Magick::ImageList.new(*FILES[10..14])
        res = @list | temp_list
        assert_instance_of(Magick::ImageList, res)
        assert_equal(15, res.length)
        assert_equal(7, res.scene)

        assert_raise(ArgumentError) { @list | 2 }
        assert_raise(ArgumentError) { @list | [2] }
    end

    def test_clear
        assert_nothing_raised { @list.clear }
        assert_instance_of(Magick::ImageList, @list)
        assert_equal(0, @list.length)
        assert_nil(@list.scene)
    end

    def test_collect
        assert_nothing_raised do
            cur = @list.cur_image
            res = @list.collect { |img| img }
            assert_instance_of(Magick::ImageList, res)
            assert_same(cur, res.cur_image)
        end
        assert_nothing_raised do
            cur = @list.cur_image
            @list.collect! { |img| img }
            assert_instance_of(Magick::ImageList, @list)
            assert_same(cur, @list.cur_image)
        end
    end

    def test_compact
        assert_nothing_raised do
            res = @list.compact
            assert_equal(res, @list)
        end
        assert_nothing_raised do
            res = @list
            @list.compact!
            assert_instance_of(Magick::ImageList, @list)
            assert_equal(res, @list)
            assert_same(res, @list)
        end
    end

    def test_concat
        assert_nothing_raised do
            res = @list.concat(@list2)
            assert_instance_of(Magick::ImageList, res)
            assert_equal(15, res.length)
            assert_same(res[14], res.cur_image)
        end
        assert_raise(ArgumentError) { res = @list.concat(2) }
        assert_raise(ArgumentError) { res = @list.concat([2]) }
    end

    def test_delete
        assert_nothing_raised do
            cur = @list.cur_image
            img = @list[7]
            assert_same(img, @list.delete(img))
            assert_equal(9, @list.length)
            assert_same(cur, @list.cur_image)

            # Try deleting the current image.
            assert_same(cur, @list.delete(cur))
            assert_same(@list[-1], @list.cur_image)

            assert_raise(ArgumentError) { @list.delete(2) }
            assert_raise(ArgumentError) { @list.delete([2]) }

            # Try deleting something that isn't in the list.
            # Should return the value of the block.
            assert_nothing_raised do
                img = Magick::Image.read(FILES[10]).first
                res = @list.delete(img) { 1 }
                assert_equal(1, res)
            end
        end
    end

    def test_delete_at
        @list.scene = 7
        cur = @list.cur_image
        assert_nothing_raised { @list.delete_at(9) }
        assert_same(cur, @list.cur_image)
        assert_nothing_raised { @list.delete_at(7) }
        assert_same(@list[-1], @list.cur_image)
    end

    def test_delete_if
        @list.scene = 7
        cur = @list.cur_image
        assert_nothing_raised do
            @list.delete_if { |img| img.filename =~ /5/ }
            assert_instance_of(Magick::ImageList, @list)
            assert_equal(9, @list.length)
            assert_same(cur, @list.cur_image)
        end

        # Delete the current image
        assert_nothing_raised do
            @list.delete_if { |img| img.filename =~ /7/ }
            assert_instance_of(Magick::ImageList, @list)
            assert_equal(8, @list.length)
            assert_same(@list[-1], @list.cur_image)
        end
    end

    def test_fill
        list = @list.copy
        img = list[0].copy
        assert_nothing_raised do
            assert_instance_of(Magick::ImageList, list.fill(img))
        end
        list.each {|el| assert_same(el, img) }

        list = @list.copy
        list.fill(img, 0, 3)
        0.upto(2) {|i| assert_same(img, list[i]) }

        list = @list.copy
        list.fill(img, 4..7)
        4.upto(7) {|i| assert_same(img, list[i]) }

        list = @list.copy
        list.fill { |i| list[i] = img }
        list.each {|el| assert_same(el, img) }

        list = @list.copy
        list.fill(0, 3) { |i| list[i] = img }
        0.upto(2) {|i| assert_same(img, list[i]) }

        assert_raise(ArgumentError) { list.fill('x', 0) }
    end

    def find_all
        assert_nothing_raised do
            res = @list.select { |img| img.filename =~ /Button_2/ }
            assert_instance_of(Magick::ImageList, res)
            assert_equal(1, res.length)
            assert_same(res[0], @list[2])
        end
    end

    def test_insert
        assert_nothing_raised do
            @list.scene = 7
            cur = @list.cur_image
            assert_instance_of(Magick::ImageList, @list.insert(1, @list[2]))
            assert_same(cur, @list.cur_image)
            @list.insert(1, @list[2], @list[3], @list[4])
            assert_same(cur, @list.cur_image)
        end

        assert_raise(ArgumentError) { @list.insert(0) }
        assert_raise(ArgumentError) { @list.insert(0, 'x') }
        assert_raise(ArgumentError) { @list.insert(0, 'x', 'y') }
    end

    def test___map__
        img = @list[0]
        assert_nothing_raised do
            @list.__map__ { |x| img }
        end
        assert_instance_of(Magick::ImageList, @list)
        assert_raise(ArgumentError) { @list.__map__ { 2 } }
    end

    def test_map!
        img = @list[0]
        assert_nothing_raised do
            @list.map! { img }
        end
        assert_instance_of(Magick::ImageList, @list)
        assert_raise(ArgumentError) { @list.map! { 2 } }
    end

    def test_pop
        @list.scene = 8
        cur = @list.cur_image
        last = @list[-1]
        assert_nothing_raised do
            assert_same(last, @list.pop)
            assert_same(cur, @list.cur_image)
        end

        assert_same(cur, @list.pop)
        assert_same(@list[-1], @list.cur_image)
    end

    def test_push
        list = @list
        img1 = @list[0]
        img2 = @list[1]
        assert_nothing_raised { @list.push(img1, img2) }
        assert_same(list, @list) # push returns self
        assert_same(img2, @list.cur_image)
    end

    def test_reject
        @list.scene = 7
        cur = @list.cur_image
        list = @list
        assert_nothing_raised do
            res = @list.reject {|img| img.filename =~ /Button_9/ }
            assert_equal(9, res.length)
            assert_instance_of(Magick::ImageList, res)
            assert_same(cur, res.cur_image)
        end
        assert_same(list, @list)
        assert_same(cur, @list.cur_image)

        # Omit current image from result list - result cur_image s/b last image
        res = @list.reject {|img| img.filename =~ /Button_7/}
        assert_equal(9, res.length)
        assert_same(res[-1], res.cur_image)
        assert_same(cur, @list.cur_image)
    end

    def test_reject!
        @list.scene = 7
        cur = @list.cur_image
        assert_nothing_raised do
            @list.reject! { |img| img.filename =~ /5/ }
            assert_instance_of(Magick::ImageList, @list)
            assert_equal(9, @list.length)
            assert_same(cur, @list.cur_image)
        end

        # Delete the current image
        assert_nothing_raised do
            @list.reject! { |img| img.filename =~ /7/ }
            assert_instance_of(Magick::ImageList, @list)
            assert_equal(8, @list.length)
            assert_same(@list[-1], @list.cur_image)
        end

        # returns nil if no changes are made
        assert_nil(@list.reject! { false })
    end

    def test_replace1
        # Replace with empty list
        assert_nothing_raised do
            res = @list.replace([])
            assert_same(res, @list)
            assert_equal(0, @list.length)
            assert_nil(@list.scene)
        end

        # Replace empty list with non-empty list
        temp = Magick::ImageList.new
        assert_nothing_raised do
            temp.replace(@list2)
            assert_equal(5, temp.length)
            assert_equal(4, temp.scene)
        end

        # Try to replace with illegal values
        assert_raise(ArgumentError) { @list.replace([1, 2, 3]) }
    end

    def test_replace2
        # Replace with shorter list
        assert_nothing_raised do
            @list.scene = 7
            cur = @list.cur_image
            res = @list.replace(@list2)
            assert_same(res, @list)
            assert_equal(5, @list.length)
            assert_equal(2, @list.scene)
            assert_same(cur, @list.cur_image)
        end
    end

    def test_replace3
        # Replace with longer list
        assert_nothing_raised do
            @list2.scene = 2
            cur = @list2.cur_image
            res = @list2.replace(@list)
            assert_same(res, @list2)
            assert_equal(10, @list2.length)
            assert_equal(7, @list2.scene)
            assert_same(cur, @list2.cur_image)
        end
    end

    def test_reverse
        list = nil
        cur = @list.cur_image
        assert_nothing_raised { list = @list.reverse }
        assert_equal(list.length, @list.length)
        assert_same(cur, @list.cur_image)
    end

    def test_reverse!
        list = @list
        cur = @list.cur_image
        assert_nothing_raised { @list.reverse! }
        assert_same(list, @list)
        assert_same(cur, @list.cur_image)
    end

    def test_select
        assert_nothing_raised do
            res = @list.select { |img| img.filename =~ /Button_2/ }
            assert_instance_of(Magick::ImageList, res)
            assert_equal(1, res.length)
            assert_same(res[0], @list[2])
        end
    end

    def test_shift
        assert_nothing_raised do
            @list.scene = 0
            res = @list[0]
            img = @list.shift
            assert_same(res, img)
            assert_equal(8, @list.scene)
        end
        res = @list[0]
        img = @list.shift
        assert_same(res, img)
        assert_equal(7, @list.scene)
    end

    def test_slice
        assert_nothing_raised { @list.slice(0) }
        assert_nothing_raised { @list.slice(-1) }
        assert_nothing_raised { @list.slice(0,1) }
        assert_nothing_raised { @list.slice(0..2) }
        assert_nothing_raised { @list.slice(20) }
    end

    def test_slice!
        @list.scene = 7
        assert_nothing_raised do
            img0 = @list[0]
            img = @list.slice!(0)
            assert_same(img0, img)
            assert_equal(9, @list.length)
            assert_equal(6, @list.scene)
        end
        cur = @list.cur_image
        img = @list.slice!(6)
        assert_same(cur, img)
        assert_equal(8, @list.length)
        assert_equal(7, @list.scene)
        assert_nothing_raised { @list.slice!(-1) }
        assert_nothing_raised { @list.slice!(0,1) }
        assert_nothing_raised { @list.slice!(0..2) }
        assert_nothing_raised { @list.slice!(20) }
    end

    def test_uniq
        assert_nothing_raised { @list.uniq }
        assert_instance_of(Magick::ImageList, @list.uniq)
        @list[1] = @list[0]
        @list.scene = 7
        list = @list.uniq
        assert_equal(9, list.length)
        assert_equal(6, list.scene)
        assert_equal(7, @list.scene)
        @list[6] = @list[7]
        list = @list.uniq
        assert_equal(8, list.length)
        assert_equal(5, list.scene)
        assert_equal(6, @list.scene)
    end

    def test_uniq!
        assert_nothing_raised do
            assert_nil(@list.uniq!)
        end
        @list[1] = @list[0]
        @list.scene = 7
        cur = @list.cur_image
        list = @list
        @list.uniq!
        assert_same(list, @list)
        assert_same(cur, @list.cur_image)
        assert_equal(6, @list.scene)
        @list[5] = @list[6]
        @list.uniq!
        assert_same(cur, @list.cur_image)
        assert_equal(5, @list.scene)
    end

    def test_unshift
        img = @list[9]
        @list.scene = 7
        @list.unshift(img)
        assert_equal(0, @list.scene)
        assert_raise(ArgumentError) { @list.unshift(2) }
        assert_raise(ArgumentError) { @list.unshift([1,2]) }
    end

    def test_spaceship
        list2 = @list.copy
        assert_equal(@list.scene, list2.scene)
        assert_equal(@list, list2)
        list2.scene = 0
        assert_not_equal(@list, list2)
        list2 = @list.copy
        list2[9] = list2[0]
        assert_not_equal(@list, list2)
        list2 = @list.copy
        list2 << @list[9]
        assert_not_equal(@list, list2)

        assert_raise(TypeError) { @list <=> 2 }
        list = Magick::ImageList.new
        list2 = Magick::ImageList.new
        assert_raise(TypeError) { list2 <=> @list }
        assert_raise(TypeError) { @list <=> list2 }
        assert_nothing_raised(TypeError) { list <=> list2 }
    end

end

if __FILE__ == $0
IMAGES_DIR = '../doc/ex/images'
FILES = Dir[IMAGES_DIR+'/Button_*.gif']
Test::Unit::UI::Console::TestRunner.run(ImageList1_UT)
end
Something went wrong with that request. Please try again.