This is the Linux kernel NFS daemon 1.4.7. It is based on linux-nfs 0.4.22. It is tested on Linux/alpha and Linux/x86 running glibc 2.1.1. WARNING: The NFS servers in Linux 2.2 to 2.2.11 are not compatible with other NFS client implemenations. If you plan to use Linux 2.2.x as an NFS server for non-Linux NFS clients, you should apply the patches enlosed here. linux-2.2.7-sunrpc.patch, nfsd-2.2.7-2.lockd.patch, nfsd-2.2.7-3.patch and nfsd-2.2.7-nfsfh.patch are required for Linux 2.2.7 to 2.2.11. For other kernel versions, they have to be applied by hand if they are still needed. For Linux 2.2.7 to 2.2.10, nfsd-2.2.7-1.lock.patch is also required. I made my knfsd package available only because I use it and noone else seems to maintain it. But I don't have much time to really work on it. I will only fix bugs in the NFS utilities and serious kernel NFS bugs which I can duplcate easily. If you have any kernel NFS server problem, please report it to the Linux kernel mailing list. If it can be reproduced with Linux NFS server and client in less than 5 minutes, you can also send me a copy in addition to sending it to the Linux kernel mailing list. I may take a look when I have time. However I will collect kernel NFS related patches. Contributions are more than welcome. The NFS lock only works with lockd. Please make sure the portmapper, portmap, is started before mounting NFS. Changes from knfsd 1.4.6: 1. Fix a typo in knfslock.init. 2. A new kernel patch, nfsd-2.2.7-1.lock.patch, to fix some NFS lock bugs. Changes from knfsd 1.4.5: 1. Rename /var/lib/nfs/xtab.export to /var/lib/nfs/etab. Changes from knfsd 1.4.4: 1. Try to fix mountd performance problem by introducing /var/lib/nfs/xtab.export. That is a. "exportfs" reads from /var/lib/nfs/xtab and writes to /var/lib/nfs/xtab.export. b. "mountd" reads from /var/lib/nfs/xtab.export and writes to /var/lib/nfs/xtab. The idea is "mountd" doesn't have to read /var/lib/nfs/xtab, which is very expensive. Changes from knfsd 1.4.3: 1. nfsd-2.2.7-nfsfh.patch, a new kernel patch for filehandle. 2. nfsd-2.2.7-2.lockd.patch, a new patch for lockd. 3. Misc bug fixes. Changes from knfsd 1.4.2: 1. A mountd patch so that the syslog reports unknown requests, and also reports *what* is being (un)mounted, from Piete Brooks . 2. Fix knfsd.init for restart. 3. Add knfslock.init. 4. knfsd-compat.spec is removed. 5. nfsd-2.2.7-lockd.patch, a patch to start lockd independent of nfs and nfsd. Changes from knfsd 1.4.1: 1. Resolve symlink for umount from Piete.Brooks@cl.cam.ac.uk (Piete Brooks) 2. Fix knfsd.init for statd. Changes from knfsd 1.4: 1. nfsd-2.2.7-3.patch. This is the only patch you need for Linux 2.2.7 to 2.2.10. 2. Remove nfsd-2.2.5-1.patch nfsd-2.2.5-3.patch nfsd-2.2.8-1.patch nfsd-2.2.7-iget.diff nfsd-2.2.5-nfsfh.diff nfsd-2.2.5-file.patch nfsd-2.2.7-quota.patch nfsd-2.2.7-mknod.patch 3. Statd update by Jeff Uphoff . 4. netgroups patch from Peter Breitenlohner . 5. Add option checking to exportfs. Changes from knfsd 1.3.3b: 1. Add linux-2.2.7-sunrpc.patch for a SMP bug in sunrpc. 2. Add --port/-P to nfsd/mountd, by Jeff Johnson . 3. Add nfsd.8, mountd.8 and statd.man, by Olaf Kirch . 4. Update nfsstat.man by Olaf Kirch . 5. Statd fix by Jeff Uphoff . 6. Remove knfsd-nok.patch. Changes from knfsd 1.3.3a: 1. Fix stdin/stdout/stdout handling in mountd. 2. nfsd-2.2.7-mknod.patch. A patch for mknod. 3. nfsd-2.2.7-quota.patch. A patch for quota. Changes from knfsd 1.3.3: 1. Fix hostname matching for wildcard, subnet and netgroup. Changes from knfsd 1.3.2: 1. Modified mountd to allow clients without IP address to hostname map. Changes from knfsd 1.3.1a: 1. nfsd-2.2.5-3.patch. This is the only patch you need for Linux 2.2.5. Changes from knfsd 1.3.1: 1. A patch for knfsd.spec from Markus Linnala . Changes from knfsd 1.3a: 1. nfsd-2.2.8-1.patch. This is the only patch you need for Linux 2.2.8. 2. nfsd-2.2.7-2.patch. This is the only patch you need for Linux 2.2.7. Changes from knfsd 1.3: 1. Adding "--no-nfs-version 3" to mountd in knfsd.init from RedHat 6.0. Changes from knfsd 1.2.2a: 1. Updated knfsd.init from RedHat 6.0. 2. nfsd-2.2.7-1.patch. This is the only patch you need for Linux 2.2.7. 3. Misc updates from RedHat 6.0. Changes from knfsd 1.2.2: 1. Make the default NFS server kernel thread to 8 in the rc script. Changes from knfsd 1.2: 1. Moved knfsd.spec to knfsd-compat.spec. 2. Update knfsd.spec from knfsd-981204-3.src.rpm. 3. Fix the squash_[ug]id parsing in /etc/exports from Anders Hammarquist . 4. nfsd-2.2.5-file.patch to clear the bogus bit for MKDIR and SYMLINK. 5. nfsd-2.2.5-1.patch. A NFS patch based on nfsd-2.2.3-1.patch for Linux 2.2.5. Changes from knfsd 1.1: 1. Remove cache-2.1.131-1.patch linux-2.1.1xx.diff lock-2.1.131.diff lock-2.1.1xx.diff nfsd-2.1.127-5.patch nullproc-2.1.1xx.diff procfs-2.1.127.patch quota-2.1.1xx.diff root-2.1.1xx.diff socket-2.1.1xx.diff sunrpc-2.1.123-1.patch 2. locks-2.2.3.diff. A patch for file lock. 3. nfsd-2.2.3-1.patch. A NFS patch by "G. Allen Morris III" (gam3@acm.org). Changes from knfsd 1.0: 1. Handle broken /var/lib/nfs/rmtab. 2. Handle lower/upper cases in wildcard hostnames in /etc/exports. Changes from knfsd-981204: 1. Modify etc/rc.nfsd to check /var/lib/nfs/rmtab during startup. 2. Add knfsd.spec for RedHat 5. Need nfs-server-2.2beta37-1.1.src.rpm and initscripts-3.78.1-2.src.rpm. 3. Add support for "make install prefix=...". Changes from knfsd-981122: 1. Modify etc/rc.nfsd and etc/rc.nfsfs to handle statd during shutdown. 2. Remove maximum knfsd count checking. 3. Clean up mountd. 4. cache-2.1.131-1.patch from G. Allen Morris III (gam3@acm.org). 5. lock-2.1.131.diff. A nfsd lock patch for Linux 2.1.131. Changes from knfsd-981113: 1. procfs-2.1.127.patch from G. Allen Morris III (gam3@acm.org). 2. Modify etc/rc.nfsd and etc/rc.nfsfs to better handle statd. 3. Fix the sub-mounted directories. Changes from knfsd-981022: 1. Fix buffer overruns from Peter Benie . 2. Fix hostname matching. 3. Correctly handle dupilcations in /etc/exports. 4. Add -F flag to statd. 5. nfsd-2.1.127-5.patch from G. Allen Morris III (gam3@acm.org). Changes from knfsd-981014: 1. lock-2.1.1xx.diff. A nfsd lock patch. 2. nullproc-2.1.1xx.diff. Allow any clients to call the nfsd NULL proc. 3. Add etc/rc.nfsfs to handle statd. 3. Update etc/rc.nfsd to handle statd. 4. nfsd-2.1.125-2.patch from G. Allen Morris III (gam3@acm.org). 5. Fix inet_ntoa usage in statd. Changes from knfsd-981010: 1. Check client aliases when matching for wildcard client hostnames. 2. Fix memory leak in mountd. 3. Fix filename in nfsd-2.1.125-1.patch. Changes from knfsd-980930: 1. nfsd-2.1.125-1.patch from G. Allen Morris III (gam3@acm.org) and me. 2. Fix the hostent bugs in mountd and statd. 3. Remove "kexportfs -au" for "rc.nfsd stop". Changes from knfsd-980925: 1. socket-2.1.1xx.diff for creating socket on NFS client. 2. There is a knsfd root_squash patch for Linux 2.1.1xx, root-2.1.1xx.diff. It is only tested on linux 2.1.123. It also fixes the server side 0711 mode bug. 3. sunrpc-2.1.123-1.patch from Bill Hawes . 4. nfsd-2.1.122-3.patch from G. Allen Morris III (gam3@acm.org). 5. Various buffer overrun changes. 6. Fix mountd to check the duplicated entry in rmtab. 7. Change exportfs to ignore warnings for "-r". 8. Fix showmount -e. Changes from knfsd-980922: 1. nfsd-2.1.121-4.patch from G. Allen Morris III (gam3@acm.org). 2. Make async as default for export. It matches the user space NFS server. Changes from knfsd-980920: 1. Add NFS mount version flags to mountd. Change rc.nfsd to disable NFS V3 for mountd. 2. Fix client hostname. 3. rc.nfsd runs kexportfs with -r instead of -a for restart and reload. Changes from knfsd-980915: 1. There is a knsfd quota patch for Linux 2.1.1xx, quota-2.1.1xx.diff. It is only tested on linux 2.1.122. 2. The submount pathname is removed from the xtab file. 3. rc.nfsd runs kexportfs with -r instead of -a for start. 4. Fix kshowmount -e. 5. Fix hostname matching. 6. Fix compiling on libc 5. Changes from knfsd-980910: 1. nfsd-2.1.121-3.patch from G. Allen Morris III (gam3@acm.org). 2. A new flag, -r, for exportfs. 3. Don't put an entry in xtab if kernel rejects it. 4. Use the official hostname when checking if 2 hostnames are the same. 5. Allow submounts. It is available at ftp://ftp.varesearch.com/pub/support/hjl/knfsd/knfsd-1.4.7.tar.gz ftp://ftp.varesearch.com/pub/support/hjl/knfsd/knfsd-1.4.6-1.4.7.diff.gz ftp://ftp.kernel.org/pub/linux/devel/gcc/knfsd-1.4.7.tar.gz ftp://ftp.kernel.org/pub/linux/devel/gcc/knfsd-1.4.6-1.4.7.diff.gz You have to apply the patch, locks-2.2.3.diff, to the Linux kernel first. It fixes quite some file lock bugs. That patch is against Linux 2.2.3. If your kernel is different, you have to apply it by hand. nfsd-2.2.3-1.patch is a new knfsd patch against linux 2.2.3 from "G. Allen Morris III" (gam3@acm.org). It works for me on x86 and alpha. It is needed for the none-Linux NFS clients. You can get Allen's current patch from http://www.CSUA.Berkeley.EDU/~gam3/knfsd/ nfsd-2.2.7-3.patch is based on nfsd-2.2.3-1.patch for Linux 2.2.7. If you use Linux 2.2.7, you should apply nfsd-2.2.7-3.patch instead of nfsd-2.2.3-1.patch. Please don't use any other patches included here for Linux 2.2.7. For other kernel versions, you may have to apply it by hand. nfsd-2.2.7-2.lockd.patch is also necessary to start lockd independent of nfs and nfsd. To compile, just do # ./configure # make Makefile will try to determine which C library you are using and compile this package accordingly. # make install will install the knfsd binaries. You have to install a knfs start up script by hand. There is a new rc.nfsd in etc. I use it for both the user-space nfsd and the kernel nfsd. There is one RPM spec file, knfs.spec, which is for a Linux system based on Linux 2.2 without support for Linux 2.0. You also need knfsd-1.4.6.tar.gz to create the knfsd RPMs. Thanks. H.J. hjl@lucon.org 08/14/99 >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
require 'test/unit'

ConstTest = 3
class Class
  alias _remove_const remove_const
  public :_remove_const
end

class TestBasicInstructions < Test::Unit::TestCase

  def test_immediates
    assert_equal((1==1), true)
    assert_equal((1==2), false)
    assert_equal [][0], nil
    assert_equal "sym".intern, :sym
    assert_equal "sym".intern, :"sym"
    assert_equal 1234 + 0, 1234
    assert_equal 1234, 1_2_3_4
    assert_equal 41, 0b0101001
    assert_equal 420, 0644
    assert_equal 18, 0x12
    assert_equal 123456789012345678901234567890 + 0,
                 123456789012345678901234567890
    assert_equal 1.234 + 0.0, 1.234
  end

  def test_self
    assert_equal self, self
    assert_equal false, (self == false)   # Qfalse==0 in C
    assert_equal false, (self == nil)
    assert_equal false, (self == 0)
  end

  def test_string
    expected = "str" + "ing"
    assert_equal expected, 'string'
    assert_equal expected, "string"
    assert_equal expected, %q(string)
    assert_equal expected, %Q(string)
    assert_equal expected, %(string)
  end

  def test_dstring
    assert_equal "2", "#{1+1}"
    s = "OK"
    assert_equal "OK", "#{s}"
    assert_equal "OKx", "#{s}x"
    assert_equal "xOK", "x#{s}"
    assert_equal "xOKx", "x#{s}x"
  end

  def test_dsym
    assert_equal :a3c, :"a#{1+2}c"
    s = "sym"
    assert_equal :sym, :"#{s}"
    assert_equal :sym, :"#{"#{"#{s}"}"}"
  end

  def test_xstr
    assert_equal 'hoge', `echo hoge`.chomp
    assert_equal '3', `echo #{1 + 2}`.chomp
    hoge = 'huga'
    assert_equal 'huga', `echo #{hoge}`.chomp
  end

  def test_regexp
    assert_equal /test/, /test/
    assert_equal 'test', /test/.source
    assert_equal 'TEST', /TEST/.source
    assert_equal true, !!(/test/ =~ 'test')
    assert_equal false, !!(/test/ =~ 'does not match')

    re = /test/
    assert_equal re, re
    assert_equal 'test', re.source
    assert_equal true, !!(re =~ 'test')
    assert_equal false, !!(re =~ 'does not match')

    assert_equal /x#{1+1}x/, /x#{1+1}x/
    s = "OK"
    assert_equal /x#{s}x/, /x#{s}x/
    assert_equal true, !!(/x#{s}x/ =~ "xOKx")
    assert_equal false, !!(/x#{s}x/ =~ "does not match")

    s = "OK"
    prev = nil
    3.times do
      assert_equal prev.object_id, (prev ||= /#{s}/o).object_id  if prev
    end
  end

  def test_array
    assert_equal [], []
    assert_equal 0, [].size
    assert_equal [1, 2, 3], [1, 2, 3]
    assert_equal [3, 7, 11], [1+2, 3+4, 5+6]
    assert_equal [[1], [2], [3]], [[1], [2], [3]]

    a = [1, 2, 3]
    assert_equal 1, a[0]
    assert_equal 2, a[1]
    assert_equal 3, a[2]
    assert_nil a[3]

    a = %w( a b c )
    assert_equal 'a', a[0]
    assert_equal 'b', a[1]
    assert_equal 'c', a[2]
    assert_nil a[3]
  end

  def test_hash
    assert_equal({}, {})
    assert_equal({1=>2}, {1=>2})
    assert_equal({1=>2, 3=>4}, {1=>2, 3=>4})
    assert_equal({1=>2, 3=>4}, {3=>4, 1=>2})
    # assert_equal({1=>2, 3=>4}, {1,2, 3,4}) # 1.9 doesn't support
    assert_equal({"key"=>"val"}, {"key"=>"val"})
  end

  def test_range
    assert_equal((1..3), (1..3))
    assert_equal((1...3), (1...3))
    assert_not_equal((1...3), (1..3))
    assert_not_equal((1..3), (1...3))
    assert_equal((1..3), (1..2+1))
    assert_equal((1...3), (1...2+1))
    assert_equal(('a'..'z'), ('a'..'z'))
  end

  def test_not
    assert_equal true, !nil
    assert_equal true, !false
    assert_equal false, !true
    assert_equal false, !3
    assert_equal false, !(1+1)

    assert_equal false, !!nil
    assert_equal false, !!false
    assert_equal true, !!true
    assert_equal true, !!3
    assert_equal true, !!(1+1)

    assert_equal true, (not nil)
    assert_equal true, (not false)
    assert_equal false, (not true)
    assert_equal false, (not 3)
    assert_equal false, (not (1 + 1))

    assert_equal false, (not not nil)
    assert_equal false, (not not false)
    assert_equal true, (not not true)
    assert_equal true, (not not 3)
    assert_equal true, (not not (1+1))
  end

  def test_local_variable
    a = 7
    assert_equal 7, a
    assert_equal a, a
    b = 17
    assert_equal 7, a
    assert_equal 17, b
    assert_equal a, a
    assert_equal b, b
    assert_not_equal a, b
    assert_not_equal b, a
    a = b
    assert_equal 17, a
    assert_equal 17, b
    assert_equal a, a
    assert_equal b, b
    assert_equal a, b
    assert_equal b, a
    c = 28
    assert_equal 17, a
    assert_equal 17, b
    assert_equal 28, c
    assert_equal a, a
    assert_equal b, b
    assert_equal a, b
    assert_equal c, c
    assert_not_equal a, c
    assert_not_equal b, c
    a = b = c
    assert_equal 28, a
    assert_equal 28, b
    assert_equal 28, c
    assert_equal a, a
    assert_equal b, b
    assert_equal a, b
    assert_equal b, a
    assert_equal a, c
    assert_equal c, a
    assert_equal b, c
    assert_equal c, b

    a = 1
    b = 2
    c = 3
    set_lvar_in_another_method
    assert_equal 1, a
    assert_equal 2, b
    assert_equal 3, c
  end

  def set_lvar_in_another_method
    assert_raise(NameError) { a }
    assert_raise(NameError) { b }
    assert_raise(NameError) { c }
    a = "NOT OK"
    b = "NOT OK"
    c = "NOT OK"
  end

  class Const
    $Const = self
    C = 'Const::C'
    def self.c() C end
    def c() C end

    class A
      C = 'Const::A::C'
      def self.c() C end
      def c() C end
      CC = 'Const::A::CC'
      def self.cc() CC end
      def cc() CC end

      class B
        C = 'Const::A::B::C'
        def self.c() C end
        def c() C end
        def self.cc() CC end
        def cc() CC end
      end
    end

    class AA < A
      def self.cc() CC end
      def cc() CC end
    end

    class AAA < AA
      def self.cc() CC end
      def cc() CC end
    end
  end
  C = 0

  def test_const_path
    do_test_const_path
    do_test_const_path
    do_test_const_path
  end

  def do_test_const_path
    assert_equal 0, C
    assert_equal 0, C
    assert_equal 3, ::ConstTest
    assert_equal 3, ::ConstTest
    assert_equal $Const, Const

    assert_equal 'Const::C', Const::C
    assert_equal 'Const::C', Const::C
    assert_equal 'Const::A::C', Const::A::C
    assert_equal 'Const::A::C', Const::A::C
    assert_equal 'Const::A::B::C', Const::A::B::C
    assert_equal 'Const::A::B::C', Const::A::B::C

    Const::A::B._remove_const :C
    assert_equal 'Const::C', Const::C
    assert_equal 'Const::A::C', Const::A::C
    assert_raise(NameError) { Const::A::B::C }

    Const::A._remove_const :C
    assert_equal 'Const::C', Const::C
    assert_raise(NameError) { Const::A::C }
    assert_raise(NameError) { Const::A::B::C }

    Const._remove_const :C
    assert_raise(NameError) { Const::C }
    assert_raise(NameError) { Const::A::C }
    assert_raise(NameError) { Const::A::B::C }

    Const::A.const_set :C, 'Const::A::C'
    assert_raise(NameError) { Const::C }
    assert_equal 'Const::A::C', Const::A::C
    assert_raise(NameError) { Const::A::B::C }

    Const::A::B.const_set :C, 'Const::A::B::C'
    assert_raise(NameError) { Const::C }
    assert_equal 'Const::A::C', Const::A::C
    assert_equal 'Const::A::B::C', Const::A::B::C

    Const.const_set :C, 'Const::C'
    assert_equal 'Const::C', Const::C
    assert_equal 'Const::A::C', Const::A::C
    assert_equal 'Const::A::B::C', Const::A::B::C
  end

  def test_const_cref
    do_test_const_cref
    do_test_const_cref
    do_test_const_cref
  end

  def do_test_const_cref
    assert_equal 'Const::C', Const.new.c
    assert_equal 'Const::A::C', Const::A.new.c
    assert_equal 'Const::A::B::C', Const::A::B.new.c

    assert_equal 'Const::C', Const.c
    assert_equal 'Const::A::C', Const::A.c
    assert_equal 'Const::A::B::C', Const::A::B.c

    Const::A::B._remove_const :C
    assert_equal 'Const::C', Const.c
    assert_equal 'Const::A::C', Const::A.c
    assert_equal 'Const::A::C', Const::A::B.c
    assert_equal 'Const::C', Const.new.c
    assert_equal 'Const::A::C', Const::A.new.c
    assert_equal 'Const::A::C', Const::A::B.new.c

    Const::A._remove_const :C
    assert_equal 'Const::C', Const.c
    assert_equal 'Const::C', Const::A.c
    assert_equal 'Const::C', Const::A::B.c
    assert_equal 'Const::C', Const.new.c
    assert_equal 'Const::C', Const::A.new.c
    assert_equal 'Const::C', Const::A::B.new.c

    Const::A::B.const_set :C, 'Const::A::B::C'
    assert_equal 'Const::C', Const.c
    assert_equal 'Const::C', Const::A.c
    assert_equal 'Const::A::B::C', Const::A::B.c
    assert_equal 'Const::C', Const.new.c
    assert_equal 'Const::C', Const::A.new.c
    assert_equal 'Const::A::B::C', Const::A::B.new.c

    Const::A.const_set :C, 'Const::A::C'
    assert_equal 'Const::C', Const.c
    assert_equal 'Const::A::C', Const::A.c
    assert_equal 'Const::A::B::C', Const::A::B.c
    assert_equal 'Const::C', Const.new.c
    assert_equal 'Const::A::C', Const::A.new.c
    assert_equal 'Const::A::B::C', Const::A::B.new.c
  ensure
    # reset
    Const.const_set :C, 'Const::C' unless Const.const_defined?(:C)
    Const::A.const_set :C, 'Const::A::C' unless Const::A.const_defined?(:C)
    Const::A::B.const_set :C, 'Const::A::B::C' unless Const::A::B.const_defined?(:C)
  end

  def test_const_inherit
    do_test_const_inherit
    do_test_const_inherit
    do_test_const_inherit
  end

  def do_test_const_inherit
    assert_equal 'Const::A::CC',   Const::A.cc
    assert_equal 'Const::A::CC',   Const::AA.cc
    assert_equal 'Const::A::CC',   Const::AAA.cc
    assert_equal 'Const::A::CC',   Const::A.new.cc
    assert_equal 'Const::A::CC',   Const::AA.new.cc
    assert_equal 'Const::A::CC',   Const::AAA.new.cc

    Const::AA.const_set :CC, 'Const::AA::CC'
    assert_equal 'Const::A::CC',   Const::A.cc
    assert_equal 'Const::AA::CC',  Const::AA.cc
    assert_equal 'Const::AA::CC',  Const::AAA.cc
    assert_equal 'Const::A::CC',   Const::A.new.cc
    assert_equal 'Const::AA::CC',  Const::AA.new.cc
    assert_equal 'Const::AA::CC',  Const::AAA.new.cc

    Const::AAA.const_set :CC, 'Const::AAA::CC'
    assert_equal 'Const::A::CC',   Const::A.cc
    assert_equal 'Const::AA::CC',  Const::AA.cc
    assert_equal 'Const::AAA::CC', Const::AAA.cc
    assert_equal 'Const::A::CC',   Const::A.new.cc
    assert_equal 'Const::AA::CC',  Const::AA.new.cc
    assert_equal 'Const::AAA::CC', Const::AAA.new.cc

    Const::AA._remove_const :CC
    assert_equal 'Const::A::CC',   Const::A.cc
    assert_equal 'Const::A::CC',   Const::AA.cc
    assert_equal 'Const::AAA::CC', Const::AAA.cc
    assert_equal 'Const::A::CC',   Const::A.new.cc
    assert_equal 'Const::A::CC',   Const::AA.new.cc
    assert_equal 'Const::AAA::CC', Const::AAA.new.cc

    Const::AAA._remove_const :CC
    assert_equal 'Const::A::CC',   Const::A.cc
    assert_equal 'Const::A::CC',   Const::AA.cc
    assert_equal 'Const::A::CC',   Const::AAA.cc
    assert_equal 'Const::A::CC',   Const::A.new.cc
    assert_equal 'Const::A::CC',   Const::AA.new.cc
    assert_equal 'Const::A::CC',   Const::AAA.new.cc
  end

  def test_global_variable
    $gvar1 = 1
    assert_equal 1, $gvar1
    $gvar1 = 2
    assert_equal 2, $gvar1
    $gvar2 = 77
    assert_equal 2, $gvar1
    assert_equal 77, $gvar2
    $gvar2 = $gvar1
    assert_equal 2, $gvar1
    assert_equal 2, $gvar2
    $gvar1 = 1
    assert_equal 1, $gvar1
    assert_equal 2, $gvar2
    set_gvar_in_another_method
    assert_equal "OK1", $gvar1
    assert_equal "OK2", $gvar2
  end

  def set_gvar_in_another_method
    assert_equal 1, $gvar1
    assert_equal 2, $gvar2
    $gvar1 = "OK1"
    $gvar2 = "OK2"
  end

  class CVarA
    @@cv = 'CVarA@@cv'
    def self.cv() @@cv end
    def self.cv=(v) @@cv = v end
    class << self
      def cv2() @@cv end
    end
    def cv() @@cv end
    def cv=(v) @@cv = v end
  end

  class CVarB < CVarA
    def self.cvB() @@cv end
    def self.cvB=(v) @@cv = v end
    class << self
      def cvB2() @@cv end
    end
    def cvB() @@cv end
    def cvB=(v) @@cv = v end
  end

  def test_class_variable
    assert_equal 'CVarA@@cv',   CVarA.cv
    assert_equal 'CVarA@@cv',   CVarA.cv2
    assert_equal 'CVarA@@cv',   CVarA.new.cv
    CVarA.cv = 'singleton'
    assert_equal 'singleton',   CVarA.cv
    assert_equal 'singleton',   CVarA.cv2
    assert_equal 'singleton',   CVarA.new.cv
    CVarA.new.cv = 'instance'
    assert_equal 'instance',    CVarA.cv
    assert_equal 'instance',    CVarA.cv2
    assert_equal 'instance',    CVarA.new.cv

    CVarA.cv = 'CVarA@@cv'
    CVarB.cv = 'B/singleton'
    assert_equal 'B/singleton', CVarB.cv
    assert_equal 'B/singleton', CVarB.cv2
    assert_equal 'B/singleton', CVarB.new.cv
    assert_equal 'B/singleton', CVarA.cv
    assert_equal 'B/singleton', CVarA.cv2
    assert_equal 'B/singleton', CVarA.new.cv
    CVarB.new.cv = 'B/instance'
    assert_equal 'B/instance',  CVarB.cv
    assert_equal 'B/instance',  CVarB.cv2
    assert_equal 'B/instance',  CVarB.new.cv
    assert_equal 'B/instance',  CVarA.cv
    assert_equal 'B/instance',  CVarA.cv2
    assert_equal 'B/instance',  CVarA.new.cv

    CVarA.cv = 'CVarA@@cv'
    assert_equal('CVarA@@cv', CVarB.cvB)
    assert_equal('CVarA@@cv', CVarB.cvB2)
    assert_equal('CVarA@@cv', CVarB.new.cvB)
    CVarB.cvB = 'B/singleton'
    assert_equal 'B/singleton', CVarB.cvB
    assert_equal 'B/singleton', CVarB.cvB2
    assert_equal 'B/singleton', CVarB.new.cvB
    assert_equal 'B/singleton', CVarA.cv
    assert_equal 'B/singleton', CVarA.cv2
    assert_equal 'B/singleton', CVarA.new.cv
    CVarB.new.cvB = 'B/instance'
    assert_equal 'B/instance',  CVarB.cvB
    assert_equal 'B/instance',  CVarB.cvB2
    assert_equal 'B/instance',  CVarB.new.cvB
    assert_equal 'B/instance',  CVarA.cv
    assert_equal 'B/instance',  CVarA.cv2
    assert_equal 'B/instance',  CVarA.new.cv

    CVarA.cv = 'CVarA@@cv'
    CVarB.cvB = 'CVarB@@cv'
  end

  class OP
    attr_reader :x
    def x=(x)
      @x = x
      :Bug1996
    end
    Bug1996 = '[ruby-dev:39163], [ruby-core:25143]'
    def [](i)
      @x
    end
    def []=(i, x)
      @x = x
      :Bug2050
    end
    Bug2050 = '[ruby-core:25387]'
  end

  def test_opassign2_1
    x = nil
    assert_equal 1, x ||= 1
    assert_equal 1, x
    assert_equal 2, x &&= 2
    assert_equal 2, x
    assert_equal 2, x ||= 3
    assert_equal 2, x
    assert_equal 4, x &&= 4
    assert_equal 4, x
    assert_equal 5, x += 1
    assert_equal 5, x
    assert_equal 4, x -= 1
    assert_equal 4, x
  end

  def test_opassign2_2
    y = OP.new
    y.x = nil
    assert_equal 1, y.x ||= 1, OP::Bug1996
    assert_equal 1, y.x
    assert_equal 2, y.x &&= 2, OP::Bug1996
    assert_equal 2, y.x
    assert_equal 2, y.x ||= 3
    assert_equal 2, y.x
    assert_equal 4, y.x &&= 4, OP::Bug1996
    assert_equal 4, y.x
    assert_equal 5, y.x += 1, OP::Bug1996
    assert_equal 5, y.x
    assert_equal 4, y.x -= 1, OP::Bug1996
    assert_equal 4, y.x
  end

  def test_opassign2_3
    z = OP.new
    z.x = OP.new
    z.x.x = nil
    assert_equal 1, z.x.x ||= 1, OP::Bug1996
    assert_equal 1, z.x.x
    assert_equal 2, z.x.x &&= 2, OP::Bug1996
    assert_equal 2, z.x.x
    assert_equal 2, z.x.x ||= 3
    assert_equal 2, z.x.x
    assert_equal 4, z.x.x &&= 4, OP::Bug1996
    assert_equal 4, z.x.x
    assert_equal 5, z.x.x += 1, OP::Bug1996
    assert_equal 5, z.x.x
    assert_equal 4, z.x.x -= 1, OP::Bug1996
    assert_equal 4, z.x.x
  end

  def test_opassign1_1
    a = []
    a[0] = nil
    assert_equal 1, a[0] ||= 1
    assert_equal 1, a[0]
    assert_equal 2, a[0] &&= 2
    assert_equal 2, a[0]
    assert_equal 2, a[0] ||= 3
    assert_equal 2, a[0]
    assert_equal 4, a[0] &&= 4
    assert_equal 4, a[0]
    assert_equal 5, a[0] += 1
    assert_equal 5, a[0]
    assert_equal 4, a[0] -= 1
    assert_equal 4, a[0]
  end

  def test_opassign1_2
    x = OP.new
    x[0] = nil
    assert_equal 1, x[0] ||= 1, OP::Bug2050
    assert_equal 1, x[0]
    assert_equal 2, x[0] &&= 2, OP::Bug2050
    assert_equal 2, x[0]
    assert_equal 2, x[0] ||= 3, OP::Bug2050
    assert_equal 2, x[0]
    assert_equal 4, x[0] &&= 4, OP::Bug2050
    assert_equal 4, x[0]
    assert_equal 5, x[0] += 1, OP::Bug2050
    assert_equal 5, x[0]
    assert_equal 4, x[0] -= 1, OP::Bug2050
    assert_equal 4, x[0]
  end

  def test_backref
    /re/ =~ 'not match'
    assert_nil $~
    assert_nil $`
    assert_nil $&
    assert_nil $'
    assert_nil $+
    assert_nil $1
    assert_nil $2
    assert_nil $3
    assert_nil $4
    assert_nil $5
    assert_nil $6
    assert_nil $7
    assert_nil $8
    assert_nil $9

    /(a)(b)(c)(d)(e)(f)(g)(h)(i)/ =~ 'xabcdefghiy'
    assert_not_nil $~
    assert_instance_of MatchData, $~
    assert_equal 'abcdefghi', $~[0]
    assert_equal 'a', $~[1]
    assert_equal 'b', $~[2]
    assert_equal 'c', $~[3]
    assert_equal 'd', $~[4]
    assert_equal 'e', $~[5]
    assert_equal 'f', $~[6]
    assert_equal 'g', $~[7]
    assert_equal 'h', $~[8]
    assert_equal 'i', $~[9]
    assert_equal 'x', $`
    assert_equal 'abcdefghi', $&
    assert_equal 'y', $'
    assert_equal 'i', $+
    assert_equal 'a', $1
    assert_equal 'b', $2
    assert_equal 'c', $3
    assert_equal 'd', $4
    assert_equal 'e', $5
    assert_equal 'f', $6
    assert_equal 'g', $7
    assert_equal 'h', $8
    assert_equal 'i', $9

    /re/ =~ 'not match'
    assert_nil $~
    assert_nil $`
    assert_nil $&
    assert_nil $'
    assert_nil $+
    assert_nil $1
    assert_nil $2
    assert_nil $3
    assert_nil $4
    assert_nil $5
    assert_nil $6
    assert_nil $7
    assert_nil $8
    assert_nil $9
  end

  def test_array_splat
    a = []
    assert_equal [], [*a]
    assert_equal [1], [1, *a]
    a = [2]
    assert_equal [2], [*a]
    assert_equal [1, 2], [1, *a]
    a = [2, 3]
    assert_equal [2, 3], [*a]
    assert_equal [1, 2, 3], [1, *a]

    a = nil
    assert_equal [], [*a]
    assert_equal [1], [1, *a]
  end

end