/* Unix SMB/CIFS implementation. Convert a server info struct into the form for PAC and NETLOGON replies Copyright (C) Andrew Bartlett 2004-2011 Copyright (C) Stefan Metzmacher 2005 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "includes.h" #include "librpc/gen_ndr/auth.h" #include "libcli/security/security.h" #include "auth/auth_sam_reply.h" NTSTATUS auth_convert_user_info_dc_sambaseinfo(TALLOC_CTX *mem_ctx, struct auth_user_info_dc *user_info_dc, struct netr_SamBaseInfo **_sam) { NTSTATUS status; struct auth_user_info *info; struct netr_SamBaseInfo *sam = talloc_zero(mem_ctx, struct netr_SamBaseInfo); NT_STATUS_HAVE_NO_MEMORY(sam); if (user_info_dc->num_sids > PRIMARY_USER_SID_INDEX) { status = dom_sid_split_rid(sam, &user_info_dc->sids[PRIMARY_USER_SID_INDEX], &sam->domain_sid, &sam->rid); if (!NT_STATUS_IS_OK(status)) { return status; } } else { return NT_STATUS_INVALID_PARAMETER; } if (user_info_dc->num_sids > PRIMARY_GROUP_SID_INDEX) { status = dom_sid_split_rid(NULL, &user_info_dc->sids[PRIMARY_GROUP_SID_INDEX], NULL, &sam->primary_gid); if (!NT_STATUS_IS_OK(status)) { return status; } } else { /* if we have to encode something like SYSTEM (with no * second SID in the token) then this is the only * choice */ sam->primary_gid = sam->rid; } info = user_info_dc->info; sam->logon_time = info->last_logon; sam->logoff_time = info->last_logoff; sam->kickoff_time = info->acct_expiry; sam->last_password_change = info->last_password_change; sam->allow_password_change = info->allow_password_change; sam->force_password_change = info->force_password_change; sam->account_name.string = info->account_name; sam->full_name.string = info->full_name; sam->logon_script.string = info->logon_script; sam->profile_path.string = info->profile_path; sam->home_directory.string = info->home_directory; sam->home_drive.string = info->home_drive; sam->logon_count = info->logon_count; sam->bad_password_count = info->bad_password_count; sam->groups.count = 0; sam->groups.rids = NULL; if (user_info_dc->num_sids > 2) { size_t i; sam->groups.rids = talloc_array(sam, struct samr_RidWithAttribute, user_info_dc->num_sids); if (sam->groups.rids == NULL) return NT_STATUS_NO_MEMORY; for (i=2; inum_sids; i++) { struct dom_sid *group_sid = &user_info_dc->sids[i]; if (!dom_sid_in_domain(sam->domain_sid, group_sid)) { /* We handle this elsewhere */ continue; } sam->groups.rids[sam->groups.count].rid = group_sid->sub_auths[group_sid->num_auths-1]; sam->groups.rids[sam->groups.count].attributes = SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED; sam->groups.count += 1; } } sam->user_flags = 0; /* w2k3 uses NETLOGON_EXTRA_SIDS | NETLOGON_NTLMV2_ENABLED */ if (!user_info_dc->info->authenticated) { sam->user_flags |= NETLOGON_GUEST; } sam->acct_flags = user_info_dc->info->acct_flags; sam->logon_server.string = user_info_dc->info->logon_server; sam->logon_domain.string = user_info_dc->info->domain_name; sam->sub_auth_status = 0; sam->last_successful_logon = 0; sam->last_failed_logon = 0; sam->failed_logon_count = 0; sam->reserved = 0; ZERO_STRUCT(sam->key); if (user_info_dc->user_session_key.length == sizeof(sam->key.key)) { memcpy(sam->key.key, user_info_dc->user_session_key.data, sizeof(sam->key.key)); } ZERO_STRUCT(sam->LMSessKey); if (user_info_dc->lm_session_key.length == sizeof(sam->LMSessKey.key)) { memcpy(sam->LMSessKey.key, user_info_dc->lm_session_key.data, sizeof(sam->LMSessKey.key)); } *_sam = sam; return NT_STATUS_OK; } /* Note that the validity of the _sam3 structure is only as long as * the user_info_dc it was generated from */ NTSTATUS auth_convert_user_info_dc_saminfo3(TALLOC_CTX *mem_ctx, struct auth_user_info_dc *user_info_dc, struct netr_SamInfo3 **_sam3) { struct netr_SamBaseInfo *sam; struct netr_SamInfo3 *sam3 = talloc_zero(mem_ctx, struct netr_SamInfo3); NTSTATUS status; size_t i; NT_STATUS_HAVE_NO_MEMORY(sam3); status = auth_convert_user_info_dc_sambaseinfo(sam3, user_info_dc, &sam); if (!NT_STATUS_IS_OK(status)) { talloc_free(sam3); return status; } sam3->base = *sam; sam3->sidcount = 0; sam3->sids = NULL; sam3->sids = talloc_array(sam, struct netr_SidAttr, user_info_dc->num_sids); if (sam3->sids == NULL) { TALLOC_FREE(sam3); return NT_STATUS_NO_MEMORY; } /* We don't put the user and group SIDs in there */ for (i=2; inum_sids; i++) { if (dom_sid_in_domain(sam->domain_sid, &user_info_dc->sids[i])) { continue; } sam3->sids[sam3->sidcount].sid = dom_sid_dup(sam3->sids, &user_info_dc->sids[i]); if (sam3->sids[sam3->sidcount].sid == NULL) { TALLOC_FREE(sam3); return NT_STATUS_NO_MEMORY; } sam3->sids[sam3->sidcount].attributes = SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED; sam3->sidcount += 1; } if (sam3->sidcount) { sam3->base.user_flags |= NETLOGON_EXTRA_SIDS; } else { sam3->sids = NULL; } *_sam3 = sam3; return NT_STATUS_OK; } /** * Make a user_info struct from the info3 or similar returned by a domain logon. * * The netr_SamInfo3 is also a key structure in the source3 auth subsystem */ NTSTATUS make_user_info_SamBaseInfo(TALLOC_CTX *mem_ctx, const char *account_name, struct netr_SamBaseInfo *base, bool authenticated, struct auth_user_info **_user_info) { struct auth_user_info *info; info = talloc_zero(mem_ctx, struct auth_user_info); NT_STATUS_HAVE_NO_MEMORY(info); if (base->account_name.string) { info->account_name = talloc_strdup(info, base->account_name.string); } else { info->account_name = talloc_strdup(info, account_name); } NT_STATUS_HAVE_NO_MEMORY(info->account_name); if (base->logon_domain.string) { info->domain_name = talloc_strdup(info, base->logon_domain.string); NT_STATUS_HAVE_NO_MEMORY(info->domain_name); } if (base->full_name.string) { info->full_name = talloc_strdup(info, base->full_name.string); NT_STATUS_HAVE_NO_MEMORY(info->full_name); } if (base->logon_script.string) { info->logon_script = talloc_strdup(info, base->logon_script.string); NT_STATUS_HAVE_NO_MEMORY(info->logon_script); } if (base->profile_path.string) { info->profile_path = talloc_strdup(info, base->profile_path.string); NT_STATUS_HAVE_NO_MEMORY(info->profile_path); } if (base->home_directory.string) { info->home_directory = talloc_strdup(info, base->home_directory.string); NT_STATUS_HAVE_NO_MEMORY(info->home_directory); } if (base->home_drive.string) { info->home_drive = talloc_strdup(info, base->home_drive.string); NT_STATUS_HAVE_NO_MEMORY(info->home_drive); } if (base->logon_server.string) { info->logon_server = talloc_strdup(info, base->logon_server.string); NT_STATUS_HAVE_NO_MEMORY(info->logon_server); } info->last_logon = base->logon_time; info->last_logoff = base->logoff_time; info->acct_expiry = base->kickoff_time; info->last_password_change = base->last_password_change; info->allow_password_change = base->allow_password_change; info->force_password_change = base->force_password_change; info->logon_count = base->logon_count; info->bad_password_count = base->bad_password_count; info->acct_flags = base->acct_flags; /* Only set authenticated if both NETLOGON_GUEST is not set, and authenticated is set */ info->authenticated = (authenticated && (!(base->user_flags & NETLOGON_GUEST))); *_user_info = info; return NT_STATUS_OK; } /** * Make a user_info_dc struct from the info3 returned by a domain logon */ NTSTATUS make_user_info_dc_netlogon_validation(TALLOC_CTX *mem_ctx, const char *account_name, uint16_t validation_level, union netr_Validation *validation, bool authenticated, struct auth_user_info_dc **_user_info_dc) { NTSTATUS status; struct auth_user_info_dc *user_info_dc; struct netr_SamBaseInfo *base = NULL; uint32_t i; switch (validation_level) { case 2: if (!validation || !validation->sam2) { return NT_STATUS_INVALID_PARAMETER; } base = &validation->sam2->base; break; case 3: if (!validation || !validation->sam3) { return NT_STATUS_INVALID_PARAMETER; } base = &validation->sam3->base; break; case 6: if (!validation || !validation->sam6) { return NT_STATUS_INVALID_PARAMETER; } base = &validation->sam6->base; break; default: return NT_STATUS_INVALID_LEVEL; } user_info_dc = talloc(mem_ctx, struct auth_user_info_dc); NT_STATUS_HAVE_NO_MEMORY(user_info_dc); /* Here is where we should check the list of trusted domains, and verify that the SID matches. */ if (!base->domain_sid) { DEBUG(0, ("Cannot operate on a Netlogon Validation without a domain SID")); return NT_STATUS_INVALID_PARAMETER; } /* The IDL layer would be a better place to check this, but to * guard the integer addition below, we double-check */ if (base->groups.count > 65535) { return NT_STATUS_INVALID_PARAMETER; } user_info_dc->num_sids = 2; user_info_dc->sids = talloc_array(user_info_dc, struct dom_sid, user_info_dc->num_sids + base->groups.count); NT_STATUS_HAVE_NO_MEMORY(user_info_dc->sids); user_info_dc->sids[PRIMARY_USER_SID_INDEX] = *base->domain_sid; if (!sid_append_rid(&user_info_dc->sids[PRIMARY_USER_SID_INDEX], base->rid)) { return NT_STATUS_INVALID_PARAMETER; } user_info_dc->sids[PRIMARY_GROUP_SID_INDEX] = *base->domain_sid; if (!sid_append_rid(&user_info_dc->sids[PRIMARY_GROUP_SID_INDEX], base->primary_gid)) { return NT_STATUS_INVALID_PARAMETER; } for (i = 0; i < base->groups.count; i++) { user_info_dc->sids[user_info_dc->num_sids] = *base->domain_sid; if (!sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids], base->groups.rids[i].rid)) { return NT_STATUS_INVALID_PARAMETER; } user_info_dc->num_sids++; } /* Copy 'other' sids. We need to do sid filtering here to prevent possible elevation of privileges. See: http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp */ if (validation_level == 3) { struct dom_sid *dgrps = user_info_dc->sids; size_t sidcount; /* The IDL layer would be a better place to check this, but to * guard the integer addition below, we double-check */ if (validation->sam3->sidcount > 65535) { return NT_STATUS_INVALID_PARAMETER; } sidcount = user_info_dc->num_sids + validation->sam3->sidcount; if (validation->sam3->sidcount > 0) { dgrps = talloc_realloc(user_info_dc, dgrps, struct dom_sid, sidcount); NT_STATUS_HAVE_NO_MEMORY(dgrps); for (i = 0; i < validation->sam3->sidcount; i++) { if (validation->sam3->sids[i].sid) { dgrps[user_info_dc->num_sids] = *validation->sam3->sids[i].sid; user_info_dc->num_sids++; } } } user_info_dc->sids = dgrps; /* Where are the 'global' sids?... */ } status = make_user_info_SamBaseInfo(user_info_dc, account_name, base, authenticated, &user_info_dc->info); if (!NT_STATUS_IS_OK(status)) { return status; } /* ensure we are never given NULL session keys */ if (all_zero(base->key.key, sizeof(base->key.key))) { user_info_dc->user_session_key = data_blob(NULL, 0); } else { user_info_dc->user_session_key = data_blob_talloc(user_info_dc, base->key.key, sizeof(base->key.key)); NT_STATUS_HAVE_NO_MEMORY(user_info_dc->user_session_key.data); } if (all_zero(base->LMSessKey.key, sizeof(base->LMSessKey.key))) { user_info_dc->lm_session_key = data_blob(NULL, 0); } else { user_info_dc->lm_session_key = data_blob_talloc(user_info_dc, base->LMSessKey.key, sizeof(base->LMSessKey.key)); NT_STATUS_HAVE_NO_MEMORY(user_info_dc->lm_session_key.data); } *_user_info_dc = user_info_dc; return NT_STATUS_OK; } /** * Make a user_info_dc struct from the PAC_LOGON_INFO supplied in the krb5 logon */ NTSTATUS make_user_info_dc_pac(TALLOC_CTX *mem_ctx, struct PAC_LOGON_INFO *pac_logon_info, struct auth_user_info_dc **_user_info_dc) { uint32_t i; NTSTATUS nt_status; union netr_Validation validation; struct auth_user_info_dc *user_info_dc; validation.sam3 = &pac_logon_info->info3; nt_status = make_user_info_dc_netlogon_validation(mem_ctx, "", 3, &validation, true, /* This user was authenticated */ &user_info_dc); if (!NT_STATUS_IS_OK(nt_status)) { return nt_status; } if (pac_logon_info->res_groups.count > 0) { size_t sidcount; /* The IDL layer would be a better place to check this, but to * guard the integer addition below, we double-check */ if (pac_logon_info->res_groups.count > 65535) { talloc_free(user_info_dc); return NT_STATUS_INVALID_PARAMETER; } /* Here is where we should check the list of trusted domains, and verify that the SID matches. */ if (!pac_logon_info->res_group_dom_sid) { DEBUG(0, ("Cannot operate on a PAC without a resource domain SID")); return NT_STATUS_INVALID_PARAMETER; } sidcount = user_info_dc->num_sids + pac_logon_info->res_groups.count; user_info_dc->sids = talloc_realloc(user_info_dc, user_info_dc->sids, struct dom_sid, sidcount); if (user_info_dc->sids == NULL) { TALLOC_FREE(user_info_dc); return NT_STATUS_NO_MEMORY; } for (i = 0; pac_logon_info->res_group_dom_sid && i < pac_logon_info->res_groups.count; i++) { user_info_dc->sids[user_info_dc->num_sids] = *pac_logon_info->res_group_dom_sid; if (!sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids], pac_logon_info->res_groups.rids[i].rid)) { return NT_STATUS_INVALID_PARAMETER; } user_info_dc->num_sids++; } } *_user_info_dc = user_info_dc; return NT_STATUS_OK; } 21 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 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
require 'test/unit'

class ComplexSub < Complex; end

class Complex_Test < Test::Unit::TestCase

  def test_compsub
    c = ComplexSub.__send__(:new, 1)
    cc = ComplexSub.__send__(:convert, 1)

    assert_kind_of(Numeric, c)
    assert_kind_of(Numeric, cc)

    if defined?(ComplexSub::Unify)
      assert_instance_of(Fixnum, c)
      assert_instance_of(Fixnum, cc)
    else
      assert_instance_of(ComplexSub, c)
      assert_instance_of(ComplexSub, cc)

      c2 = c + 1
      assert_instance_of(ComplexSub, c2)
      c2 = c - 1
      assert_instance_of(ComplexSub, c2)

      c3 = c - c2
      assert_instance_of(ComplexSub, c3)

      s = Marshal.dump(c)
      c5 = Marshal.load(s)
      assert_equal(c, c5)
      assert_instance_of(ComplexSub, c5)
    end

  end

  def test_eql_p
    c = Complex(0)
    c2 = Complex(0)
    c3 = Complex(1)

    assert_equal(true, c.eql?(c2))
    assert_equal(false, c.eql?(c3))

    if defined?(Complex::Unify)
      assert_equal(true, c.eql?(0))
    else
      assert_equal(false, c.eql?(0))
    end
  end

  def test_hash
    assert_instance_of(Fixnum, Complex(1,2).hash)

    h = {}
    h[Complex(0)] = 0
    h[Complex(0,1)] = 1
    h[Complex(1,0)] = 2
    h[Complex(1,1)] = 3

    assert_equal(4, h.size)
    assert_equal(2, h[Complex(1,0)])

    h[Complex(0,0)] = 9
    assert_equal(4, h.size)
  end

  def test_freeze
    c = Complex(1)
    c.freeze
    unless defined?(Complex::Unify)
      assert_equal(true, c.frozen?)
    end
    assert_instance_of(String, c.to_s)
  end

  def test_new_bang # no unify
    assert_instance_of(Complex, Complex.__send__(:new!, 2,0))
    assert_equal([2,0], Complex.__send__(:new!, 2,0).
		 instance_eval{[real, image]})
    assert_equal([2,4], Complex.__send__(:new!, 2,4).
		 instance_eval{[real, image]})
    assert_equal([-2,4], Complex.__send__(:new!, -2,4).
		 instance_eval{[real, image]})
    assert_equal([2,-4], Complex.__send__(:new!, 2,-4).
		 instance_eval{[real, image]})
    assert_equal([-2,-4], Complex.__send__(:new!, -2,-4).
		 instance_eval{[real, image]})

    assert_equal([2,0], Complex.__send__(:new!, Complex(2)).
		 instance_eval{[real, image]})
    assert_equal([2,3], Complex.__send__(:new!, Complex(2), Complex(3)).
		 instance_eval{[real, image]})
    assert_equal([2,3], Complex.__send__(:new!, 2, Complex(3)).
		 instance_eval{[real, image]})

    assert_equal([1.1,0], Complex.__send__(:new!, 1.1).
		 instance_eval{[real, image]})
    assert_equal([-1.1,0], Complex.__send__(:new!, -1.1).
		 instance_eval{[real, image]})
    assert_equal([1,0], Complex.__send__(:new!, '1').
		 instance_eval{[real, image]})
    assert_equal([0,0], Complex.__send__(:new!, nil).
		 instance_eval{[real, image]})
  end

  def test_new
    if defined?(Complex::Unify)
      assert_instance_of(Fixnum, Complex.__send__(:new, 2,0))
    else
      assert_instance_of(Complex, Complex.__send__(:new, 2,0))
      assert_equal([2,0], Complex.__send__(:new, 2,0). instance_eval{[real, image]})
    end
    assert_equal([2,4], Complex.__send__(:new, 2,4).instance_eval{[real, image]})
    assert_equal([-2,4], Complex.__send__(:new, -2,4).instance_eval{[real, image]})
    assert_equal([2,-4], Complex.__send__(:new, 2,-4).instance_eval{[real, image]})
    assert_equal([-2,-4], Complex.__send__(:new, -2,-4).instance_eval{[real, image]})

    assert_raise(ArgumentError){Complex.__send__(:new, Complex(1,2),2)}
    assert_raise(ArgumentError){Complex.__send__(:new, 2,Complex(1,2))}
    assert_raise(ArgumentError){Complex.__send__(:new, Complex(1,2),Complex(1,2))}

    assert_raise(ArgumentError){Complex.__send__(:new, '1')}
    assert_raise(ArgumentError){Complex.__send__(:new, nil)}
=begin
    assert_raise(ArgumentError){Complex.__send__(:new, Complex(1))}
=end
  end

  def test_conv
    c = Complex(0,0)
    assert_equal(Complex.__send__(:new, 0,0), c)

    c = Complex(2**32, 2**32)
    assert_equal(Complex.__send__(:new, 2**32,2**32), c)
    assert_equal([2**32,2**32], [c.real,c.image])

    c = Complex(-2**32, 2**32)
    assert_equal(Complex.__send__(:new, -2**32,2**32), c)
    assert_equal([-2**32,2**32], [c.real,c.image])

    c = Complex(2**32, -2**32)
    assert_equal(Complex.__send__(:new, 2**32,-2**32), c)
    assert_equal([2**32,-2**32], [c.real,c.image])

    c = Complex(-2**32, -2**32)
    assert_equal(Complex.__send__(:new, -2**32,-2**32), c)
    assert_equal([-2**32,-2**32], [c.real,c.image])

    c = Complex(Complex(1),0)
    assert_equal(Complex.__send__(:new, 1,0), c)

    c = Complex(0,Complex(1))
    assert_equal(Complex.__send__(:new, 0,1), c)

    c = 5.re
    assert_equal(Complex.__send__(:new, 5,0), c)

    c = Complex(1,2).re
    assert_equal(Complex.__send__(:new, 1,2), c)

    c = 5.im
    assert_equal(Complex.__send__(:new, 0,5), c)

    c = Complex(2,0).im
    assert_equal(Complex.__send__(:new, 0,2), c)
    assert_raise(ArgumentError){Complex(1,2).im}

    c = Complex::I
    assert_equal(Complex.__send__(:new, 0,1), c)

    assert_equal(Complex.__send__(:new, 1),Complex(1))
    assert_equal(Complex.__send__(:new, 1),Complex('1'))
    assert_raise(ArgumentError){Complex(nil)}
  end

  def test_attr
    c = Complex(4)

    assert_equal(4, c.real)
    assert_equal(0, c.image)

    c = Complex(4,5)

    assert_equal(4, c.real)
    assert_equal(5, c.image)

    if -0.0.to_s == '-0.0'
      c = Complex(-0.0,-0.0)

      assert_equal('-0.0', c.real.to_s)
      assert_equal('-0.0', c.image.to_s)
    end

    c = Complex.__send__(:new, 4)

    assert_equal(4, c.real)
    assert_equal(0, c.image)
    assert_equal(c.imag, c.image)

    c = Complex.__send__(:new, 4,5)

    assert_equal(4, c.real)
    assert_equal(5, c.image)
    assert_equal(c.imag, c.image)

    if -0.0.to_s == '-0.0'
      c = Complex.__send__(:new, -0.0,-0.0)

      assert_equal('-0.0', c.real.to_s)
      assert_equal('-0.0', c.image.to_s)
      assert_equal(c.imag.to_s, c.image.to_s)
    end

    c = Complex.__send__(:new!, 4)

    assert_equal(4, c.real)
    assert_equal(c.imag, c.image)
    assert_equal(0, c.image)

    c = Complex.__send__(:new!, 4,5)

    assert_equal(4, c.real)
    assert_equal(5, c.image)
    assert_equal(c.imag, c.image)

    c = Complex.__send__(:new!, -0.0,-0.0)

    assert_equal('-0.0', c.real.to_s)
    assert_equal('-0.0', c.image.to_s)
    assert_equal(c.imag.to_s, c.image.to_s)
  end

  def test_attr2
    c = Complex(1)

    if defined?(Complex::Unify)
      assert_equal(true, c.scalar?)
=begin
      assert_equal(true, c.finite?)
      assert_equal(false, c.infinite?)
      assert_equal(false, c.nan?)
      assert_equal(true, c.integer?)
      assert_equal(false, c.float?)
      assert_equal(true, c.rational?)
      assert_equal(true, c.real?)
      assert_equal(false, c.complex?)
      assert_equal(true, c.exact?)
      assert_equal(false, c.inexact?)
=end
    else
      assert_equal(false, c.scalar?)
=begin
      assert_equal(true, c.finite?)
      assert_equal(false, c.infinite?)
      assert_equal(false, c.nan?)
      assert_equal(false, c.integer?)
      assert_equal(false, c.float?)
      assert_equal(false, c.rational?)
      assert_equal(false, c.real?)
      assert_equal(true, c.complex?)
      assert_equal(true, c.exact?)
      assert_equal(false, c.inexact?)
=end
    end

=begin
    assert_equal(0, Complex(0).sign)
    assert_equal(1, Complex(2).sign)
    assert_equal(-1, Complex(-2).sign)
=end

    assert_equal(true, Complex(0).zero?)
    assert_equal(true, Complex(0,0).zero?)
    assert_equal(false, Complex(1,0).zero?)
    assert_equal(false, Complex(0,1).zero?)
    assert_equal(false, Complex(1,1).zero?)

    assert_equal(nil, Complex(0).nonzero?)
    assert_equal(nil, Complex(0,0).nonzero?)
    assert_equal(Complex(1,0), Complex(1,0).nonzero?)
    assert_equal(Complex(0,1), Complex(0,1).nonzero?)
    assert_equal(Complex(1,1), Complex(1,1).nonzero?)
  end

  def test_rect
    assert_equal([1,2], Complex.rectangular(1,2).rectangular)
    assert_equal([1,2], Complex.rect(1,2).rect)
  end

  def test_polar
    assert_equal([1,2], Complex.polar(1,2).polar)
  end

  def test_uplus
    assert_equal(Complex(1), +Complex(1))
    assert_equal(Complex(-1), +Complex(-1))
    assert_equal(Complex(1,1), +Complex(1,1))
    assert_equal(Complex(-1,1), +Complex(-1,1))
    assert_equal(Complex(1,-1), +Complex(1,-1))
    assert_equal(Complex(-1,-1), +Complex(-1,-1))
  end

  def test_negate
    assert_equal(Complex(-1), -Complex(1))
    assert_equal(Complex(1), -Complex(-1))
    assert_equal(Complex(-1,-1), -Complex(1,1))
    assert_equal(Complex(1,-1), -Complex(-1,1))
    assert_equal(Complex(-1,1), -Complex(1,-1))
    assert_equal(Complex(1,1), -Complex(-1,-1))

=begin
    assert_equal(0, Complex(0).negate)
    assert_equal(-2, Complex(2).negate)
    assert_equal(2, Complex(-2).negate)
=end
  end

  def test_add
    c = Complex(1,2)
    c2 = Complex(2,3)

    assert_equal(Complex(3,5), c + c2)

    assert_equal(Complex(3,2), c + 2)
    assert_equal(Complex(3.0,2), c + 2.0)

    if defined?(Rational)
      assert_equal(Complex(Rational(3,1),Rational(2)), c + Rational(2))
      assert_equal(Complex(Rational(5,3),Rational(2)), c + Rational(2,3))
    end
  end

  def test_sub
    c = Complex(1,2)
    c2 = Complex(2,3)

    assert_equal(Complex(-1,-1), c - c2)

    assert_equal(Complex(-1,2), c - 2)
    assert_equal(Complex(-1.0,2), c - 2.0)

    if defined?(Rational)
      assert_equal(Complex(Rational(-1,1),Rational(2)), c - Rational(2))
      assert_equal(Complex(Rational(1,3),Rational(2)), c - Rational(2,3))
    end
  end

  def test_mul
    c = Complex(1,2)
    c2 = Complex(2,3)

    assert_equal(Complex(-4,7), c * c2)

    assert_equal(Complex(2,4), c * 2)
    assert_equal(Complex(2.0,4.0), c * 2.0)

    if defined?(Rational)
      assert_equal(Complex(Rational(2,1),Rational(4)), c * Rational(2))
      assert_equal(Complex(Rational(2,3),Rational(4,3)), c * Rational(2,3))
    end

  end

  def test_div
    c = Complex(1,2)
    c2 = Complex(2,3)

    if defined?(Rational)
      assert_equal(Complex(Rational(8,13),Rational(1,13)), c / c2)
    else
      r = c / c2
      assert_in_delta(0.615, r.real, 0.001)
      assert_in_delta(0.076, r.image, 0.001)
    end

    c = Complex(1.0,2.0)
    c2 = Complex(2.0,3.0)

    r = c / c2
    assert_in_delta(0.615, r.real, 0.001)
    assert_in_delta(0.076, r.image, 0.001)

    c = Complex(1,2)
    c2 = Complex(2,3)

    if defined?(Rational)
      assert_equal(Complex(Rational(1,2),1), c / 2)
    else
      assert_equal(Complex(0.5,1.0), c / 2)
    end
    assert_equal(Complex(0.5,1.0), c / 2.0)

    if defined?(Rational)
      assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2))
      assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3))
    end
  end

  def test_quo
    c = Complex(1,2)
    c2 = Complex(2,3)

    if defined?(Rational)
      assert_equal(Complex(Rational(8,13),Rational(1,13)), c.quo(c2))
    else
      r = c.quo(c2)
      assert_in_delta(0.615, r.real, 0.001)
      assert_in_delta(0.076, r.image, 0.001)
    end

    c = Complex(1.0,2.0)
    c2 = Complex(2.0,3.0)

    r = c.quo(c2)
    assert_in_delta(0.615, r.real, 0.001)
    assert_in_delta(0.076, r.image, 0.001)

    c = Complex(1,2)
    c2 = Complex(2,3)

    if defined?(Rational)
      assert_equal(Complex(Rational(1,2),1), c.quo(2))
    else
      assert_equal(Complex(0.5,1.0), c.quo(2))
    end
    assert_equal(Complex(0.5,1.0), c.quo(2.0))

    if defined?(Rational)
      assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2))
      assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3))
    end
  end

  def test_fdiv
    c = Complex(1,2)
    c2 = Complex(2,3)

    r = c.fdiv(c2)
    assert_in_delta(0.615, r.real, 0.001)
    assert_in_delta(0.076, r.image, 0.001)

    c = Complex(1.0,2.0)
    c2 = Complex(2.0,3.0)

    r = c.fdiv(c2)
    assert_in_delta(0.615, r.real, 0.001)
    assert_in_delta(0.076, r.image, 0.001)

    c = Complex(1,2)
    c2 = Complex(2,3)

    assert_equal(Complex(0.5,1.0), c.fdiv(2))
    assert_equal(Complex(0.5,1.0), c.fdiv(2.0))
  end

  def test_expt
    c = Complex(1,2)
    c2 = Complex(2,3)

    r = c ** c2
    assert_in_delta(-0.015, r.real, 0.001)
    assert_in_delta(-0.179, r.image, 0.001)

    assert_equal(Complex(-3,4), c ** 2)
    if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
      assert_equal(Complex(Rational(-3,25),Rational(-4,25)), c ** -2)
    else
      r = c ** -2
      assert_in_delta(-0.12, r.real, 0.001)
      assert_in_delta(-0.16, r.image, 0.001)
    end
    r = c ** 2.0
    assert_in_delta(-3.0, r.real, 0.001)
    assert_in_delta(4.0, r.image, 0.001)

    r = c ** -2.0
    assert_in_delta(-0.12, r.real, 0.001)
    assert_in_delta(-0.16, r.image, 0.001)

    if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
      assert_equal(Complex(-3,4), c ** Rational(2))
#=begin
      assert_equal(Complex(Rational(-3,25),Rational(-4,25)),
		   c ** Rational(-2)) # why failed?
#=end

      r = c ** Rational(2,3)
      assert_in_delta(1.264, r.real, 0.001)
      assert_in_delta(1.150, r.image, 0.001)

      r = c ** Rational(-2,3)
      assert_in_delta(0.432, r.real, 0.001)
      assert_in_delta(-0.393, r.image, 0.001)
    end
  end

  def test_cmp
    assert_raise(NoMethodError){1 <=> Complex(1,1)}
    assert_raise(NoMethodError){Complex(1,1) <=> 1}
    assert_raise(NoMethodError){Complex(1,1) <=> Complex(1,1)}
  end

  def test_equal
    assert(Complex(1,0) == Complex(1))
    assert(Complex(1,0) == Complex.__send__(:new, 1))
    assert(Complex(1,0) == Complex.__send__(:new, 1,0))
    assert(Complex(1,0) == Complex.__send__(:new!, 1))
    assert(Complex(1,0) == Complex.__send__(:new!, 1,0))

    assert(Complex(-1,0) == Complex(-1))
    assert(Complex(-1,0) == Complex.__send__(:new, -1))
    assert(Complex(-1,0) == Complex.__send__(:new, -1,0))
    assert(Complex(-1,0) == Complex.__send__(:new!, -1))
    assert(Complex(-1,0) == Complex.__send__(:new!, -1,0))

    assert_equal(false, Complex(2,1) == Complex(1))
    assert_equal(true, Complex(2,1) != Complex(1))
    assert_equal(false, Complex(1) == nil)
    assert_equal(false, Complex(1) == '')
  end

  def test_unify
    if defined?(Complex::Unify)
      assert_instance_of(Fixnum, Complex(1,2) + Complex(-1,-2))
      assert_instance_of(Fixnum, Complex(1,2) - Complex(1,2))
      assert_instance_of(Fixnum, Complex(1,2) * 0)
      assert_instance_of(Fixnum, Complex(1,2) / Complex(1,2))
      assert_instance_of(Fixnum, Complex(1,2).div(Complex(1,2)))
      assert_instance_of(Fixnum, Complex(1,2).quo(Complex(1,2)))
#      assert_instance_of(Fixnum, Complex(1,2) ** 0) # mathn's bug
    end
  end

  def test_math
    c = Complex(1,2)

    assert_in_delta(2.236, c.abs, 0.001)
    assert_in_delta(2.236, c.magnitude, 0.001)
    assert_equal(5, c.abs2)

    assert_equal(c.abs, Math.sqrt(c * c.conj))
    assert_equal(c.abs, Math.sqrt(c.real**2 + c.image**2))
    assert_equal(c.abs2, c * c.conj)
    assert_equal(c.abs2, c.real**2 + c.image**2)

    assert_in_delta(1.107, c.arg, 0.001)
    assert_in_delta(1.107, c.angle, 0.001)
    assert_in_delta(1.107, c.phase, 0.001)

    r = c.polar
    assert_in_delta(2.236, r[0], 0.001)
    assert_in_delta(1.107, r[1], 0.001)
    assert_equal(Complex(1,-2), c.conjugate)
    assert_equal(Complex(1,-2), c.conj)
#    assert_equal(Complex(1,-2), ~c)
#    assert_equal(5, c * ~c)

    assert_equal(Complex(1,2), c.numerator)
    assert_equal(1, c.denominator)
  end

  def test_to_s
    c = Complex(1,2)

    assert_instance_of(String, c.to_s)
    assert_equal('1+2i', c.to_s)

    assert_equal('0+2i', Complex(0,2).to_s)
    assert_equal('0-2i', Complex(0,-2).to_s)
    assert_equal('1+2i', Complex(1,2).to_s)
    assert_equal('-1+2i', Complex(-1,2).to_s)
    assert_equal('-1-2i', Complex(-1,-2).to_s)
    assert_equal('1-2i', Complex(1,-2).to_s)
    assert_equal('-1-2i', Complex(-1,-2).to_s)

    assert_equal('0+2.0i', Complex(0,2.0).to_s)
    assert_equal('0-2.0i', Complex(0,-2.0).to_s)
    assert_equal('1.0+2.0i', Complex(1.0,2.0).to_s)
    assert_equal('-1.0+2.0i', Complex(-1.0,2.0).to_s)
    assert_equal('-1.0-2.0i', Complex(-1.0,-2.0).to_s)
    assert_equal('1.0-2.0i', Complex(1.0,-2.0).to_s)
    assert_equal('-1.0-2.0i', Complex(-1.0,-2.0).to_s)

    if defined?(Rational) && !defined?(Complex::Unify) && !Rational.instance_variable_get('@RCS_ID')
      assert_equal('0+2/1i', Complex(0,Rational(2)).to_s)
      assert_equal('0-2/1i', Complex(0,Rational(-2)).to_s)
      assert_equal('1+2/1i', Complex(1,Rational(2)).to_s)
      assert_equal('-1+2/1i', Complex(-1,Rational(2)).to_s)
      assert_equal('-1-2/1i', Complex(-1,Rational(-2)).to_s)
      assert_equal('1-2/1i', Complex(1,Rational(-2)).to_s)
      assert_equal('-1-2/1i', Complex(-1,Rational(-2)).to_s)

      assert_equal('0+2/3i', Complex(0,Rational(2,3)).to_s)
      assert_equal('0-2/3i', Complex(0,Rational(-2,3)).to_s)
      assert_equal('1+2/3i', Complex(1,Rational(2,3)).to_s)
      assert_equal('-1+2/3i', Complex(-1,Rational(2,3)).to_s)
      assert_equal('-1-2/3i', Complex(-1,Rational(-2,3)).to_s)
      assert_equal('1-2/3i', Complex(1,Rational(-2,3)).to_s)
      assert_equal('-1-2/3i', Complex(-1,Rational(-2,3)).to_s)
    end
  end

  def test_inspect
    c = Complex(1,2)

    assert_instance_of(String, c.inspect)
    assert_equal('(1+2i)', c.inspect)
  end

  def test_marshal
    c = Complex(1,2)

    s = Marshal.dump(c)
    c2 = Marshal.load(s)
    assert_equal(c, c2)
    assert_instance_of(Complex, c2)

    if defined?(Rational)
      c = Complex(Rational(1,2),Rational(2,3))

      s = Marshal.dump(c)
      c2 = Marshal.load(s)
      assert_equal(c, c2)
      assert_instance_of(Complex, c2)
    end
  end

  def test_parse
    assert_equal(Complex(0), ''.to_c)
    assert_equal(Complex(0), ' '.to_c)
    assert_equal(Complex(5), "\f\n\r\t\v5\0".to_c)
    assert_equal(Complex(5), '5'.to_c)
    assert_equal(Complex(-5), '-5'.to_c)
    assert_equal(Complex(5,3), '5+3i'.to_c)
    assert_equal(Complex(-5,3), '-5+3i'.to_c)
    assert_equal(Complex(5,-3), '5-3i'.to_c)
    assert_equal(Complex(-5,-3), '-5-3i'.to_c)
    assert_equal(Complex(0,3), '3i'.to_c)
    assert_equal(Complex(0,-3), '-3i'.to_c)
    assert_equal(Complex(5,1), '5+i'.to_c)
    assert_equal(Complex(0,1), 'i'.to_c)
    assert_equal(Complex(0,1), '+i'.to_c)
    assert_equal(Complex(0,-1), '-i'.to_c)

    assert_equal(Complex(5,3), '5+3I'.to_c)
    assert_equal(Complex(5,3), '5+3j'.to_c)
    assert_equal(Complex(5,3), '5+3J'.to_c)
    assert_equal(Complex(0,3), '3I'.to_c)
    assert_equal(Complex(0,3), '3j'.to_c)
    assert_equal(Complex(0,3), '3J'.to_c)
    assert_equal(Complex(0,1), 'I'.to_c)
    assert_equal(Complex(0,1), 'J'.to_c)

    assert_equal(Complex(5.0), '5.0'.to_c)
    assert_equal(Complex(-5.0), '-5.0'.to_c)
    assert_equal(Complex(5.0,3.0), '5.0+3.0i'.to_c)
    assert_equal(Complex(-5.0,3.0), '-5.0+3.0i'.to_c)
    assert_equal(Complex(5.0,-3.0), '5.0-3.0i'.to_c)
    assert_equal(Complex(-5.0,-3.0), '-5.0-3.0i'.to_c)
    assert_equal(Complex(0.0,3.0), '3.0i'.to_c)
    assert_equal(Complex(0.0,-3.0), '-3.0i'.to_c)

    assert_equal(Complex(5.0), '5e0'.to_c)
    assert_equal(Complex(-5.0), '-5e0'.to_c)
    assert_equal(Complex(5.0,3.0), '5e0+3e0i'.to_c)
    assert_equal(Complex(-5.0,3.0), '-5e0+3e0i'.to_c)
    assert_equal(Complex(5.0,-3.0), '5e0-3e0i'.to_c)
    assert_equal(Complex(-5.0,-3.0), '-5e0-3e0i'.to_c)
    assert_equal(Complex(0.0,3.0), '3e0i'.to_c)
    assert_equal(Complex(0.0,-3.0), '-3e0i'.to_c)

    assert_equal(Complex.polar(10,10), '10@10'.to_c)
    assert_equal(Complex.polar(-10,-10), '-10@-10'.to_c)
    assert_equal(Complex.polar(10.5,10.5), '10.5@10.5'.to_c)
    assert_equal(Complex.polar(-10.5,-10.5), '-10.5@-10.5'.to_c)

    assert_equal(Complex(5), Complex('5'))
    assert_equal(Complex(-5), Complex('-5'))
    assert_equal(Complex(5,3), Complex('5+3i'))
    assert_equal(Complex(-5,3), Complex('-5+3i'))
    assert_equal(Complex(5,-3), Complex('5-3i'))
    assert_equal(Complex(-5,-3), Complex('-5-3i'))
    assert_equal(Complex(0,3), Complex('3i'))
    assert_equal(Complex(0,-3), Complex('-3i'))
    assert_equal(Complex(5,1), Complex('5+i'))
    assert_equal(Complex(0,1), Complex('i'))
    assert_equal(Complex(0,1), Complex('+i'))
    assert_equal(Complex(0,-1), Complex('-i'))

    assert_equal(Complex(5,3), Complex('5+3I'))
    assert_equal(Complex(5,3), Complex('5+3j'))
    assert_equal(Complex(5,3), Complex('5+3J'))
    assert_equal(Complex(0,3), Complex('3I'))
    assert_equal(Complex(0,3), Complex('3j'))
    assert_equal(Complex(0,3), Complex('3J'))
    assert_equal(Complex(0,1), Complex('I'))
    assert_equal(Complex(0,1), Complex('J'))

    assert_equal(Complex(5.0), Complex('5.0'))
    assert_equal(Complex(-5.0), Complex('-5.0'))
    assert_equal(Complex(5.0,3.0), Complex('5.0+3.0i'))
    assert_equal(Complex(-5.0,3.0), Complex('-5.0+3.0i'))
    assert_equal(Complex(5.0,-3.0), Complex('5.0-3.0i'))
    assert_equal(Complex(-5.0,-3.0), Complex('-5.0-3.0i'))
    assert_equal(Complex(0.0,3.0), Complex('3.0i'))
    assert_equal(Complex(0.0,-3.0), Complex('-3.0i'))

    assert_equal(Complex(5.0), Complex('5e0'))
    assert_equal(Complex(-5.0), Complex('-5e0'))
    assert_equal(Complex(5.0,3.0), Complex('5e0+3e0i'))
    assert_equal(Complex(-5.0,3.0), Complex('-5e0+3e0i'))
    assert_equal(Complex(5.0,-3.0), Complex('5e0-3e0i'))
    assert_equal(Complex(-5.0,-3.0), Complex('-5e0-3e0i'))
    assert_equal(Complex(0.0,3.0), Complex('3e0i'))
    assert_equal(Complex(0.0,-3.0), Complex('-3e0i'))

    assert_equal(Complex.polar(10,10), Complex('10@10'))
    assert_equal(Complex.polar(-10,-10), Complex('-10@-10'))
    assert_equal(Complex.polar(10.5,10.5), Complex('10.5@10.5'))
    assert_equal(Complex.polar(-10.5,-10.5), Complex('-10.5@-10.5'))

    assert_equal(Complex(0), '_'.to_c)
    assert_equal(Complex(0), '_5'.to_c)
    assert_equal(Complex(5), '5_'.to_c)
    assert_equal(Complex(5), '5x'.to_c)
    assert_equal(Complex(5), '5+_3i'.to_c)
    assert_equal(Complex(5), '5+3_i'.to_c)
    assert_equal(Complex(5,3), '5+3i_'.to_c)
    assert_equal(Complex(5,3), '5+3ix'.to_c)
    assert_raise(ArgumentError){ Complex('')}
    assert_raise(ArgumentError){ Complex('_')}
    assert_raise(ArgumentError){ Complex('_5')}
    assert_raise(ArgumentError){ Complex('5_')}
    assert_raise(ArgumentError){ Complex('5x')}
    assert_raise(ArgumentError){ Complex('5+_3i')}
    assert_raise(ArgumentError){ Complex('5+3_i')}
    assert_raise(ArgumentError){ Complex('5+3i_')}
    assert_raise(ArgumentError){ Complex('5+3ix')}
    assert_raise(ArgumentError){ Complex("5\0")}

    if defined?(Rational) && defined?(''.to_r)
      assert_equal(Complex(Rational(1,5)), '1/5'.to_c)
      assert_equal(Complex(Rational(-1,5)), '-1/5'.to_c)
      assert_equal(Complex(Rational(1,5),3), '1/5+3i'.to_c)
      assert_equal(Complex(Rational(1,5),-3), '1/5-3i'.to_c)
      assert_equal(Complex(Rational(-1,5),3), '-1/5+3i'.to_c)
      assert_equal(Complex(Rational(-1,5),-3), '-1/5-3i'.to_c)
      assert_equal(Complex(Rational(1,5),Rational(3,2)), '1/5+3/2i'.to_c)
      assert_equal(Complex(Rational(1,5),Rational(-3,2)), '1/5-3/2i'.to_c)
      assert_equal(Complex(Rational(-1,5),Rational(3,2)), '-1/5+3/2i'.to_c)
      assert_equal(Complex(Rational(-1,5),Rational(-3,2)), '-1/5-3/2i'.to_c)
      assert_equal(Complex(Rational(1,5),Rational(3,2)), '1/5+3/2i'.to_c)
      assert_equal(Complex(Rational(1,5),Rational(-3,2)), '1/5-3/2i'.to_c)
      assert_equal(Complex(Rational(-1,5),Rational(3,2)), '-1/5+3/2i'.to_c)
      assert_equal(Complex(Rational(-1,5),Rational(-3,2)), '-1/5-3/2i'.to_c)
      assert_equal(Complex.polar(Rational(1,5),Rational(3,2)), Complex('1/5@3/2'))
      assert_equal(Complex.polar(Rational(-1,5),Rational(-3,2)), Complex('-1/5@-3/2'))
    end

  end

  def test_respond
    c = Complex(1,1)
    assert_equal(false, c.respond_to?(:<))
    assert_equal(false, c.respond_to?(:<=))
    assert_equal(false, c.respond_to?(:<=>))
    assert_equal(false, c.respond_to?(:>))
    assert_equal(false, c.respond_to?(:>=))
    assert_equal(false, c.respond_to?(:between?))
#    assert_equal(false, c.respond_to?(:div)) # ?
    assert_equal(false, c.respond_to?(:divmod))
    assert_equal(false, c.respond_to?(:floor))
    assert_equal(false, c.respond_to?(:ceil))
    assert_equal(false, c.respond_to?(:modulo))
    assert_equal(false, c.respond_to?(:round))
    assert_equal(false, c.respond_to?(:step))
    assert_equal(false, c.respond_to?(:tunrcate))

    assert_equal(false, c.respond_to?(:positive?))
    assert_equal(false, c.respond_to?(:negative?))
#    assert_equal(false, c.respond_to?(:sign))

    assert_equal(false, c.respond_to?(:quotient))
    assert_equal(false, c.respond_to?(:quot))
    assert_equal(false, c.respond_to?(:quotrem))

    assert_equal(false, c.respond_to?(:gcd))
    assert_equal(false, c.respond_to?(:lcm))
    assert_equal(false, c.respond_to?(:gcdlcm))
  end

  def test_to_i
    assert_equal(3, Complex(3).to_i)
    assert_equal(3, Integer(Complex(3)))
    assert_raise(RangeError){Complex(3,2).to_i}
    assert_raise(RangeError){Integer(Complex(3,2))}
  end

  def test_to_f
    assert_equal(3.0, Complex(3).to_f)
    assert_equal(3.0, Float(Complex(3)))
    assert_raise(RangeError){Complex(3,2).to_f}
    assert_raise(RangeError){Float(Complex(3,2))}
  end

  def test_to_r
    if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
      assert_equal(Rational(3), Complex(3).to_r)
      assert_equal(Rational(3), Rational(Complex(3)))
      assert_raise(RangeError){Complex(3,2).to_r}
      assert_raise(RangeError){Rational(Complex(3,2))}
    end
  end

  def test_to_c
    c = nil.to_c
    assert_equal([0,0] , [c.real, c.image])

    c = 0.to_c
    assert_equal([0,0] , [c.real, c.image])

    c = 1.to_c
    assert_equal([1,0] , [c.real, c.image])

    c = 1.1.to_c
    assert_equal([1.1, 0], [c.real, c.image])

    if defined?(Rational)
      c = Rational(1,2).to_c
      assert_equal([Rational(1,2), 0], [c.real, c.image])
    end

    c = Complex(1,2).to_c
    assert_equal([1, 2], [c.real, c.image])
  end

  def test_prec
    assert_equal(nil, Complex < Precision)
  end

  def test_supp
    assert_equal(true, 1.scalar?)
    assert_equal(true, 1.1.scalar?)

    assert_equal(1, 1.real)
    assert_equal(0, 1.image)
    assert_equal(0, 1.imag)

    assert_equal(1.1, 1.1.real)
    assert_equal(0, 1.1.image)
    assert_equal(0, 1.1.imag)

    assert_equal(1, 1.magnitude)
    assert_equal(1, -1.magnitude)
    assert_equal(1, 1.0.magnitude)
    assert_equal(1, -1.0.magnitude)

    assert_equal(0, 1.arg)
    assert_equal(0, 1.angle)
    assert_equal(0, 1.phase)

    assert_equal(0, 1.0.arg)
    assert_equal(0, 1.0.angle)
    assert_equal(0, 1.0.phase)

    assert_equal(Math::PI, -1.arg)
    assert_equal(Math::PI, -1.angle)
    assert_equal(Math::PI, -1.phase)

    assert_equal(Math::PI, -1.0.arg)
    assert_equal(Math::PI, -1.0.angle)
    assert_equal(Math::PI, -1.0.phase)

    assert_equal([1,0], 1.rect)
    assert_equal([-1,0], -1.rect)
    assert_equal([1,0], 1.rectangular)
    assert_equal([-1,0], -1.rectangular)

    assert_equal([1.0,0], 1.0.rect)
    assert_equal([-1.0,0], -1.0.rect)
    assert_equal([1.0,0], 1.0.rectangular)
    assert_equal([-1.0,0], -1.0.rectangular)

    assert_equal([1,0], 1.polar)
    assert_equal([1, Math::PI], -1.polar)

    assert_equal([1.0,0], 1.0.polar)
    assert_equal([1.0, Math::PI], -1.0.polar)

    assert_equal(1, 1.conjugate)
    assert_equal(-1, -1.conjugate)
    assert_equal(1, 1.conj)
    assert_equal(-1, -1.conj)

    assert_equal(1.1, 1.1.conjugate)
    assert_equal(-1.1, -1.1.conjugate)
    assert_equal(1.1, 1.1.conj)
    assert_equal(-1.1, -1.1.conj)

    if defined?(Rational)
      assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2))
    else
      assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2))
    end

=begin
    if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
      assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2))
    end
=end

    assert_equal(0.5, 1.fdiv(2))
    assert_equal(5000000000.0, 10000000000.fdiv(2))
    assert_equal(0.5, 1.0.fdiv(2))
    if defined?(Rational)
      assert_equal(0.25, Rational(1,2).fdiv(2))
    end
    assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2))

    unless $".grep(/(\A|\/)complex/).empty?
      assert_equal(Complex(0,2), Math.sqrt(-4.0))
#      assert_equal(true, Math.sqrt(-4.0).inexact?)
      assert_equal(Complex(0,2), Math.sqrt(-4))
#      assert_equal(true, Math.sqrt(-4).exact?)
      if defined?(Rational)
	assert_equal(Complex(0,2), Math.sqrt(Rational(-4)))
#	assert_equal(true, Math.sqrt(Rational(-4)).exact?)
      end

      assert_equal(Complex(0,3), Math.sqrt(-9.0))
#      assert_equal(true, Math.sqrt(-9.0).inexact?)
      assert_equal(Complex(0,3), Math.sqrt(-9))
#      assert_equal(true, Math.sqrt(-9).exact?)
      if defined?(Rational)
	assert_equal(Complex(0,3), Math.sqrt(Rational(-9)))
#	assert_equal(true, Math.sqrt(Rational(-9)).exact?)
      end

      c = Math.sqrt(Complex(1, 2))
      assert_in_delta(1.272, c.real, 0.001)
      assert_in_delta(0.786, c.image, 0.001)

      c = Math.sqrt(-9)
      assert_in_delta(0.0, c.real, 0.001)
      assert_in_delta(3.0, c.image, 0.001)

      c = Math.exp(Complex(1, 2))
      assert_in_delta(-1.131, c.real, 0.001)
      assert_in_delta(2.471, c.image, 0.001)

      c = Math.sin(Complex(1, 2))
      assert_in_delta(3.165, c.real, 0.001)
      assert_in_delta(1.959, c.image, 0.001)

      c = Math.cos(Complex(1, 2))
      assert_in_delta(2.032, c.real, 0.001)
      assert_in_delta(-3.051, c.image, 0.001)

      c = Math.tan(Complex(1, 2))
      assert_in_delta(0.033, c.real, 0.001)
      assert_in_delta(1.014, c.image, 0.001)

      c = Math.sinh(Complex(1, 2))
      assert_in_delta(-0.489, c.real, 0.001)
      assert_in_delta(1.403, c.image, 0.001)

      c = Math.cosh(Complex(1, 2))
      assert_in_delta(-0.642, c.real, 0.001)
      assert_in_delta(1.068, c.image, 0.001)

      c = Math.tanh(Complex(1, 2))
      assert_in_delta(1.166, c.real, 0.001)
      assert_in_delta(-0.243, c.image, 0.001)

      c = Math.log(Complex(1, 2))
      assert_in_delta(0.804, c.real, 0.001)
      assert_in_delta(1.107, c.image, 0.001)

      c = Math.log(Complex(1, 2), Math::E)
      assert_in_delta(0.804, c.real, 0.001)
      assert_in_delta(1.107, c.image, 0.001)

      c = Math.log(-1)
      assert_in_delta(0.0, c.real, 0.001)
      assert_in_delta(Math::PI, c.image, 0.001)

      c = Math.log(8, 2)
      assert_in_delta(3.0, c.real, 0.001)
      assert_in_delta(0.0, c.image, 0.001)

      c = Math.log(-8, -2)
      assert_in_delta(1.092, c.real, 0.001)
      assert_in_delta(-0.420, c.image, 0.001)

      c = Math.log10(Complex(1, 2))
      assert_in_delta(0.349, c.real, 0.001)
      assert_in_delta(0.480, c.image, 0.001)

      c = Math.asin(Complex(1, 2))
      assert_in_delta(0.427, c.real, 0.001)
      assert_in_delta(1.528, c.image, 0.001)

      c = Math.acos(Complex(1, 2))
      assert_in_delta(1.143, c.real, 0.001)
      assert_in_delta(-1.528, c.image, 0.001)

      c = Math.atan(Complex(1, 2))
      assert_in_delta(1.338, c.real, 0.001)
      assert_in_delta(0.402, c.image, 0.001)

      c = Math.atan2(Complex(1, 2), 1)
      assert_in_delta(1.338, c.real, 0.001)
      assert_in_delta(0.402, c.image, 0.001)

      c = Math.asinh(Complex(1, 2))
      assert_in_delta(1.469, c.real, 0.001)
      assert_in_delta(1.063, c.image, 0.001)

      c = Math.acosh(Complex(1, 2))
      assert_in_delta(1.528, c.real, 0.001)
      assert_in_delta(1.143, c.image, 0.001)

      c = Math.atanh(Complex(1, 2))
      assert_in_delta(0.173, c.real, 0.001)
      assert_in_delta(1.178, c.image, 0.001)
    end

  end

  def test_fixed_bug
    if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
      assert_equal(Complex(1), 1 ** Complex(1))
    end
    assert_equal('-1.0-0.0i', Complex(-1.0, -0.0).to_s)
  end

  def test_known_bug
  end

end