Commit 27b31ef8166361c6a6bdf777400bb43f6d40a3a1
1 parent
87ec65c8
Add mputils module unit test
Showing
4 changed files
with
272 additions
and
76 deletions
test/test_cscript.py
| ... | ... | @@ -337,11 +337,11 @@ class Test(test): |
| 337 | 337 | """ |
| 338 | 338 | Setup cscript test class |
| 339 | 339 | """ |
| 340 | - # Get text class | |
| 341 | - test = cscript_test() | |
| 340 | + # Get test class | |
| 341 | + cls = cscript_test() | |
| 342 | 342 | |
| 343 | 343 | # Recover User parameters |
| 344 | - pars = test.pars() | |
| 344 | + pars = cls.pars() | |
| 345 | 345 | |
| 346 | 346 | # Create a copy of the User parameters |
| 347 | 347 | cpy_pars = pars.copy() |
| ... | ... | @@ -350,7 +350,7 @@ class Test(test): |
| 350 | 350 | pars.clear() |
| 351 | 351 | |
| 352 | 352 | # Return |
| 353 | - return (test, pars, cpy_pars) | |
| 353 | + return (cls, pars, cpy_pars) | |
| 354 | 354 | |
| 355 | 355 | # Test cscript base class |
| 356 | 356 | def _test_cscript(self): |
| ... | ... | @@ -499,91 +499,91 @@ class Test(test): |
| 499 | 499 | # Test get_roi() method for the case that there is no "usepnt" |
| 500 | 500 | # parameter and that there are no valid "ra" and "dec" parameters. |
| 501 | 501 | # In that case an empty ROI should be returned. |
| 502 | - test, pars, cpy_pars = self._setup_cscript() | |
| 502 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 503 | 503 | pars.append(gammalib.GApplicationPar('ra','r','h','NONE','','','')) |
| 504 | 504 | pars.append(gammalib.GApplicationPar('dec','r','h','NONE','','','')) |
| 505 | - roi = test.check_get_roi() | |
| 505 | + roi = cls.check_get_roi() | |
| 506 | 506 | self.test_assert(not roi.is_valid(), 'Check that get_roi() for absent ' |
| 507 | 507 | '"usepnt" and invalid "ra" and "dec" parameters returns ' |
| 508 | 508 | 'an invalid ROI') |
| 509 | - test.pars(cpy_pars) | |
| 509 | + cls.pars(cpy_pars) | |
| 510 | 510 | |
| 511 | 511 | # Test get_roi() method for the case that there is no "usepnt" |
| 512 | 512 | # parameter and that there is no valid "ra" parameter. |
| 513 | 513 | # In that case an empty ROI should be returned. |
| 514 | - test, pars, cpy_pars = self._setup_cscript() | |
| 514 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 515 | 515 | pars.append(gammalib.GApplicationPar('ra','r','h','NONE','','','')) |
| 516 | 516 | pars.append(gammalib.GApplicationPar('dec','r','h','10.0','','','')) |
| 517 | - roi = test.check_get_roi() | |
| 517 | + roi = cls.check_get_roi() | |
| 518 | 518 | self.test_assert(not roi.is_valid(), 'Check that get_roi() for absent ' |
| 519 | 519 | '"usepnt" and an invalid "ra" parameter returns ' |
| 520 | 520 | 'an invalid ROI') |
| 521 | - test.pars(cpy_pars) | |
| 521 | + cls.pars(cpy_pars) | |
| 522 | 522 | |
| 523 | 523 | # Test get_roi() method for the case that there is no "usepnt" |
| 524 | 524 | # parameter and that there is no valid "dec" parameter. |
| 525 | 525 | # In that case an empty ROI should be returned. |
| 526 | - test, pars, cpy_pars = self._setup_cscript() | |
| 526 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 527 | 527 | pars.append(gammalib.GApplicationPar('ra','r','h','10.0','','','')) |
| 528 | 528 | pars.append(gammalib.GApplicationPar('dec','r','h','NONE','','','')) |
| 529 | - roi = test.check_get_roi() | |
| 529 | + roi = cls.check_get_roi() | |
| 530 | 530 | self.test_assert(not roi.is_valid(), 'Check that get_roi() for absent ' |
| 531 | 531 | '"usepnt" and an invalid "dec" parameter returns ' |
| 532 | 532 | 'an invalid ROI') |
| 533 | - test.pars(cpy_pars) | |
| 533 | + cls.pars(cpy_pars) | |
| 534 | 534 | |
| 535 | 535 | # Test get_roi() method for the case that there is no "usepnt" |
| 536 | 536 | # parameter and that there are valid "ra", "dec" and "rad" parameters. |
| 537 | 537 | # In that case a valid ROI should be returned. |
| 538 | - test, pars, cpy_pars = self._setup_cscript() | |
| 538 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 539 | 539 | pars.append(gammalib.GApplicationPar('ra','r','h','10.0','','','')) |
| 540 | 540 | pars.append(gammalib.GApplicationPar('dec','r','h','20.0','','','')) |
| 541 | 541 | pars.append(gammalib.GApplicationPar('rad','r','h','1.0','','','')) |
| 542 | - roi = test.check_get_roi() | |
| 542 | + roi = cls.check_get_roi() | |
| 543 | 543 | self.test_assert(roi.is_valid(), 'Check that get_roi() for absent ' |
| 544 | 544 | '"usepnt" and valid "ra", "dec" and "rad" parameters ' |
| 545 | 545 | 'returns a valid ROI') |
| 546 | 546 | self.test_value(roi.centre().dir().ra_deg(), 10.0, 'Check Right Ascension') |
| 547 | 547 | self.test_value(roi.centre().dir().dec_deg(), 20.0, 'Check Declination') |
| 548 | 548 | self.test_value(roi.radius(), 1.0, 'Check radius') |
| 549 | - test.pars(cpy_pars) | |
| 549 | + cls.pars(cpy_pars) | |
| 550 | 550 | |
| 551 | 551 | # Test get_roi() method for the case that there is a "usepnt" |
| 552 | 552 | # parameter and that there is a valid "rad" parameter. |
| 553 | 553 | # In that case a valid ROI should be returned. |
| 554 | - test, pars, cpy_pars = self._setup_cscript() | |
| 554 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 555 | 555 | pars.append(gammalib.GApplicationPar('usepnt','b','h','yes','','','')) |
| 556 | 556 | pars.append(gammalib.GApplicationPar('rad','r','h','1.0','','','')) |
| 557 | - roi = test.check_get_roi() | |
| 557 | + roi = cls.check_get_roi() | |
| 558 | 558 | self.test_assert(roi.is_valid(), 'Check that get_roi() for present ' |
| 559 | 559 | '"usepnt" and valid "rad" parameter returns a valid ROI') |
| 560 | 560 | self.test_value(roi.centre().dir().ra_deg(), 0.0, 'Check Right Ascension') |
| 561 | 561 | self.test_value(roi.centre().dir().dec_deg(), 0.0, 'Check Declination') |
| 562 | 562 | self.test_value(roi.radius(), 1.0, 'Check radius') |
| 563 | - test.pars(cpy_pars) | |
| 563 | + cls.pars(cpy_pars) | |
| 564 | 564 | |
| 565 | 565 | # Test get_ebounds() method for the case that both "emin" and "emax" |
| 566 | 566 | # parameters are invalid. In that case an empty energy boundary should |
| 567 | 567 | # be returned. |
| 568 | - test, pars, cpy_pars = self._setup_cscript() | |
| 568 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 569 | 569 | pars.append(gammalib.GApplicationPar('emin','r','h','NONE','','','')) |
| 570 | 570 | pars.append(gammalib.GApplicationPar('emax','r','h','NONE','','','')) |
| 571 | - ebounds = test.check_get_ebounds() | |
| 571 | + ebounds = cls.check_get_ebounds() | |
| 572 | 572 | self.test_value(ebounds.size(), 0, 'Check that get_ebounds() for ' |
| 573 | 573 | 'invalid "emin" and "emax" parameters returns empty ' |
| 574 | 574 | 'energy boundaries') |
| 575 | - test.pars(cpy_pars) | |
| 575 | + cls.pars(cpy_pars) | |
| 576 | 576 | |
| 577 | 577 | # Test get_ebounds() method for the case that the "emin" parameter is |
| 578 | 578 | # invalid. In that case an empty energy boundary should be returned. |
| 579 | - test, pars, cpy_pars = self._setup_cscript() | |
| 579 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 580 | 580 | pars.append(gammalib.GApplicationPar('emin','r','h','NONE','','','')) |
| 581 | 581 | pars.append(gammalib.GApplicationPar('emax','r','h','100.0','','','')) |
| 582 | - ebounds = test.check_get_ebounds() | |
| 582 | + ebounds = cls.check_get_ebounds() | |
| 583 | 583 | self.test_value(ebounds.size(), 0, 'Check that get_ebounds() for ' |
| 584 | 584 | 'invalid "emin" parameter returns empty energy ' |
| 585 | 585 | 'boundaries') |
| 586 | - test.pars(cpy_pars) | |
| 586 | + cls.pars(cpy_pars) | |
| 587 | 587 | |
| 588 | 588 | # Test get_ebounds() method for the case that the "emax" parameter is |
| 589 | 589 | # invalid. In that case an empty energy boundary should be returned. |
| ... | ... | @@ -598,125 +598,125 @@ class Test(test): |
| 598 | 598 | |
| 599 | 599 | # Test get_ebounds() method for the case of valid "emin" and "emax" |
| 600 | 600 | # parameters. In that case a single energy boundary should be returned. |
| 601 | - test, pars, cpy_pars = self._setup_cscript() | |
| 601 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 602 | 602 | pars.append(gammalib.GApplicationPar('emin','r','h','1.0','','','')) |
| 603 | 603 | pars.append(gammalib.GApplicationPar('emax','r','h','100.0','','','')) |
| 604 | - ebounds = test.check_get_ebounds() | |
| 604 | + ebounds = cls.check_get_ebounds() | |
| 605 | 605 | self.test_value(ebounds.size(), 1, 'Check that get_ebounds() for ' |
| 606 | 606 | 'valid "emin" and "emax" parameters returns empty one ' |
| 607 | 607 | 'energy boundary') |
| 608 | 608 | self.test_value(ebounds.emin().TeV(), 1.0, 'Check minimum energy') |
| 609 | 609 | self.test_value(ebounds.emax().TeV(), 100.0, 'Check maximum energy') |
| 610 | - test.pars(cpy_pars) | |
| 610 | + cls.pars(cpy_pars) | |
| 611 | 611 | |
| 612 | 612 | # Test get_gti() method for the case that both "tmin" and "tmax" |
| 613 | 613 | # parameters are invalid. In that case an empty Good Time Interval |
| 614 | 614 | # should be returned. |
| 615 | - test, pars, cpy_pars = self._setup_cscript() | |
| 615 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 616 | 616 | pars.append(gammalib.GApplicationPar('tmin','t','h','NONE','','','')) |
| 617 | 617 | pars.append(gammalib.GApplicationPar('tmax','t','h','NONE','','','')) |
| 618 | - gti = test.check_get_gti() | |
| 618 | + gti = cls.check_get_gti() | |
| 619 | 619 | self.test_value(gti.size(), 0, 'Check that get_gti() for invalid "tmin" ' |
| 620 | 620 | 'and "tmax" parameters returns empty GTI') |
| 621 | - test.pars(cpy_pars) | |
| 621 | + cls.pars(cpy_pars) | |
| 622 | 622 | |
| 623 | 623 | # Test get_gti() method for the case that the "tmin" parameter is |
| 624 | 624 | # invalid. In that case an empty Good Time Interval should be returned. |
| 625 | - test, pars, cpy_pars = self._setup_cscript() | |
| 625 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 626 | 626 | pars.append(gammalib.GApplicationPar('tmin','t','h','NONE','','','')) |
| 627 | 627 | pars.append(gammalib.GApplicationPar('tmax','t','h','2005-10-08T14:58:26','','','')) |
| 628 | - gti = test.check_get_gti() | |
| 628 | + gti = cls.check_get_gti() | |
| 629 | 629 | self.test_value(gti.size(), 0, 'Check that get_gti() for an invalid ' |
| 630 | 630 | '"tmin" parameter returns empty GTI') |
| 631 | - test.pars(cpy_pars) | |
| 631 | + cls.pars(cpy_pars) | |
| 632 | 632 | |
| 633 | 633 | # Test get_gti() method for the case that the "tmax" parameter is |
| 634 | 634 | # invalid. In that case an empty Good Time Interval should be returned. |
| 635 | - test, pars, cpy_pars = self._setup_cscript() | |
| 635 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 636 | 636 | pars.append(gammalib.GApplicationPar('tmin','t','h','2005-10-08T14:30:25','','','')) |
| 637 | 637 | pars.append(gammalib.GApplicationPar('tmax','t','h','NONE','','','')) |
| 638 | - gti = test.check_get_gti() | |
| 638 | + gti = cls.check_get_gti() | |
| 639 | 639 | self.test_value(gti.size(), 0, 'Check that get_gti() for an invalid ' |
| 640 | 640 | '"tmax" parameter returns empty GTI') |
| 641 | - test.pars(cpy_pars) | |
| 641 | + cls.pars(cpy_pars) | |
| 642 | 642 | |
| 643 | 643 | # Test get_gti() method for the case that both "tmin" and "tmax" |
| 644 | 644 | # parameters are valid. In that case a valid Good Time Interval should |
| 645 | 645 | # be returned. |
| 646 | - test, pars, cpy_pars = self._setup_cscript() | |
| 646 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 647 | 647 | pars.append(gammalib.GApplicationPar('tmin','t','h','2005-10-08T14:30:25','','','')) |
| 648 | 648 | pars.append(gammalib.GApplicationPar('tmax','t','h','2005-10-08T14:58:26','','','')) |
| 649 | - gti = test.check_get_gti() | |
| 649 | + gti = cls.check_get_gti() | |
| 650 | 650 | self.test_value(gti.size(), 1, 'Check that get_gti() for valid "tmin" ' |
| 651 | 651 | 'and "tmax" parameters returns one GTI') |
| 652 | 652 | self.test_value(gti.tstart().utc(), '2005-10-08T14:30:25', 'Check "tmin"') |
| 653 | 653 | self.test_value(gti.tstop().utc(), '2005-10-08T14:58:26', 'Check "tmax"') |
| 654 | - test.pars(cpy_pars) | |
| 654 | + cls.pars(cpy_pars) | |
| 655 | 655 | |
| 656 | 656 | # Test get_pointing() method |
| 657 | - test, pars, cpy_pars = self._setup_cscript() | |
| 657 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 658 | 658 | pars.append(gammalib.GApplicationPar('ra','r','h','10.0','','','')) |
| 659 | 659 | pars.append(gammalib.GApplicationPar('dec','r','h','20.0','','','')) |
| 660 | - pnt = test.check_get_pointing() | |
| 660 | + pnt = cls.check_get_pointing() | |
| 661 | 661 | self.test_value(pnt.dir().ra_deg(), 10.0, 'Check Right Ascension') |
| 662 | 662 | self.test_value(pnt.dir().dec_deg(), 20.0, 'Check Declination') |
| 663 | - test.pars(cpy_pars) | |
| 663 | + cls.pars(cpy_pars) | |
| 664 | 664 | |
| 665 | 665 | # Test get_skydir() method |
| 666 | - test, pars, cpy_pars = self._setup_cscript() | |
| 666 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 667 | 667 | pars.append(gammalib.GApplicationPar('ra','r','h','10.0','','','')) |
| 668 | 668 | pars.append(gammalib.GApplicationPar('dec','r','h','20.0','','','')) |
| 669 | - dir = test.check_get_skydir() | |
| 669 | + dir = cls.check_get_skydir() | |
| 670 | 670 | self.test_value(dir.ra_deg(), 10.0, 'Check Right Ascension') |
| 671 | 671 | self.test_value(dir.dec_deg(), 20.0, 'Check Declination') |
| 672 | - test.pars(cpy_pars) | |
| 672 | + cls.pars(cpy_pars) | |
| 673 | 673 | |
| 674 | 674 | # Test set_outfile_name() method for "fix" filename, which should |
| 675 | 675 | # return the prefixed basename. |
| 676 | - test, pars, cpy_pars = self._setup_cscript() | |
| 676 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 677 | 677 | pars.append(gammalib.GApplicationPar('prefix','s','h','pre_','','','')) |
| 678 | - filename = test.check_set_outfile_name('fix') | |
| 678 | + filename = cls.check_set_outfile_name('fix') | |
| 679 | 679 | self.test_value(filename, 'pre_fix', 'Check "pre_fix"') |
| 680 | - test.pars(cpy_pars) | |
| 680 | + cls.pars(cpy_pars) | |
| 681 | 681 | |
| 682 | 682 | # Test set_outfile_name() method for "/path/to/fix" filename, which |
| 683 | 683 | # should return the prefixed basename. |
| 684 | - test, pars, cpy_pars = self._setup_cscript() | |
| 684 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 685 | 685 | pars.append(gammalib.GApplicationPar('prefix','s','h','pre_','','','')) |
| 686 | - filename = test.check_set_outfile_name('/path/to/fix') | |
| 686 | + filename = cls.check_set_outfile_name('/path/to/fix') | |
| 687 | 687 | self.test_value(filename, 'pre_fix', 'Check "pre_fix"') |
| 688 | - test.pars(cpy_pars) | |
| 688 | + cls.pars(cpy_pars) | |
| 689 | 689 | |
| 690 | 690 | # Test set_outfile_name() method for "/path/to/fix.gz" filename, which |
| 691 | 691 | # should return the prefixed basename without the ".gz" |
| 692 | - test, pars, cpy_pars = self._setup_cscript() | |
| 692 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 693 | 693 | pars.append(gammalib.GApplicationPar('prefix','s','h','pre_','','','')) |
| 694 | - filename = test.check_set_outfile_name('/path/to/fix.gz') | |
| 694 | + filename = cls.check_set_outfile_name('/path/to/fix.gz') | |
| 695 | 695 | self.test_value(filename, 'pre_fix', 'Check "pre_fix"') |
| 696 | - test.pars(cpy_pars) | |
| 696 | + cls.pars(cpy_pars) | |
| 697 | 697 | |
| 698 | 698 | # Test set_outfile_name() method for empty filename, which should |
| 699 | 699 | # return an empty filename. |
| 700 | - test, pars, cpy_pars = self._setup_cscript() | |
| 700 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 701 | 701 | pars.append(gammalib.GApplicationPar('prefix','s','h','pre_','','','')) |
| 702 | - filename = test.check_set_outfile_name('') | |
| 702 | + filename = cls.check_set_outfile_name('') | |
| 703 | 703 | self.test_value(filename, '', 'Check on empty string') |
| 704 | - test.pars(cpy_pars) | |
| 704 | + cls.pars(cpy_pars) | |
| 705 | 705 | |
| 706 | 706 | # Test is_stacked() method for the case that "enumbins" is zero. In |
| 707 | 707 | # that case the is_stacked() method should return False. |
| 708 | - test, pars, cpy_pars = self._setup_cscript() | |
| 708 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 709 | 709 | pars.append(gammalib.GApplicationPar('emin','r','h','1.0','','','')) |
| 710 | 710 | pars.append(gammalib.GApplicationPar('emax','r','h','100.0','','','')) |
| 711 | 711 | pars.append(gammalib.GApplicationPar('enumbins','i','h','0','','','')) |
| 712 | - self.test_assert(not test.check_is_stacked(), 'Check if is_stacked() ' | |
| 712 | + self.test_assert(not cls.check_is_stacked(), 'Check if is_stacked() ' | |
| 713 | 713 | 'returns False if there are no energy bins') |
| 714 | - test.pars(cpy_pars) | |
| 714 | + cls.pars(cpy_pars) | |
| 715 | 715 | |
| 716 | 716 | # Test is_stacked() method for the case that "enumbins" is larger |
| 717 | 717 | # than zero. In that case the is_stacked() method should return True |
| 718 | 718 | # and query a bunch of parameters. |
| 719 | - test, pars, cpy_pars = self._setup_cscript() | |
| 719 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 720 | 720 | pars.append(gammalib.GApplicationPar('emin','r','h','1.0','','','')) |
| 721 | 721 | pars.append(gammalib.GApplicationPar('emax','r','h','100.0','','','')) |
| 722 | 722 | pars.append(gammalib.GApplicationPar('enumbins','i','h','10','','','')) |
| ... | ... | @@ -727,24 +727,24 @@ class Test(test): |
| 727 | 727 | pars.append(gammalib.GApplicationPar('nxpix','i','h','100','','','')) |
| 728 | 728 | pars.append(gammalib.GApplicationPar('nypix','i','h','100','','','')) |
| 729 | 729 | pars.append(gammalib.GApplicationPar('binsz','r','h','0.02','','','')) |
| 730 | - self.test_assert(test.check_is_stacked(), 'Check if is_stacked() ' | |
| 730 | + self.test_assert(cls.check_is_stacked(), 'Check if is_stacked() ' | |
| 731 | 731 | 'returns True if there are energy bins') |
| 732 | - test.pars(cpy_pars) | |
| 732 | + cls.pars(cpy_pars) | |
| 733 | 733 | |
| 734 | 734 | # Test is_onoff() method for the case that method='NONE', i.e. the |
| 735 | 735 | # method is not 'ONOFF'. In that case the is_onoff() method should |
| 736 | 736 | # return False. |
| 737 | - test, pars, cpy_pars = self._setup_cscript() | |
| 737 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 738 | 738 | pars.append(gammalib.GApplicationPar('method','s','h','NONE','','','')) |
| 739 | - self.test_assert(not test.check_is_onoff(), | |
| 739 | + self.test_assert(not cls.check_is_onoff(), | |
| 740 | 740 | 'Check if is_onoff() returns False if method is not "ONOFF"') |
| 741 | - test.pars(cpy_pars) | |
| 741 | + cls.pars(cpy_pars) | |
| 742 | 742 | |
| 743 | 743 | # Test is_onoff() method for the case that method='ONOFF', |
| 744 | 744 | # srcshape='CIRCLE' and bkgmethod='REFLECTED'. In that case the |
| 745 | 745 | # is_onoff() method should return True and a bunch of application |
| 746 | 746 | # parameters are queried. |
| 747 | - test, pars, cpy_pars = self._setup_cscript() | |
| 747 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 748 | 748 | pars.append(gammalib.GApplicationPar('method','s','h','ONOFF','','','')) |
| 749 | 749 | pars.append(gammalib.GApplicationPar('inexclusion','f','h','NONE','','','')) |
| 750 | 750 | pars.append(gammalib.GApplicationPar('emin','r','h','0.1','','','')) |
| ... | ... | @@ -761,15 +761,15 @@ class Test(test): |
| 761 | 761 | pars.append(gammalib.GApplicationPar('etruemin','r','h','0.1','','','')) |
| 762 | 762 | pars.append(gammalib.GApplicationPar('etruemax','r','h','100.0','','','')) |
| 763 | 763 | pars.append(gammalib.GApplicationPar('etruebins','i','h','10','','','')) |
| 764 | - self.test_assert(test.check_is_onoff(), | |
| 764 | + self.test_assert(cls.check_is_onoff(), | |
| 765 | 765 | 'Check if is_onoff() returns True if method is "ONOFF"') |
| 766 | - test.pars(cpy_pars) | |
| 766 | + cls.pars(cpy_pars) | |
| 767 | 767 | |
| 768 | 768 | # Test is_onoff() method for the case that method='ONOFF', |
| 769 | 769 | # srcshape='CIRCLE' and bkgmethod='REFLECTED'. In that case the |
| 770 | 770 | # is_onoff() method should return True and a bunch of application |
| 771 | 771 | # parameters are queried, except of rad and bkgregmin. |
| 772 | - test, pars, cpy_pars = self._setup_cscript() | |
| 772 | + cls, pars, cpy_pars = self._setup_cscript() | |
| 773 | 773 | pars.append(gammalib.GApplicationPar('method','s','h','ONOFF','','','')) |
| 774 | 774 | pars.append(gammalib.GApplicationPar('inexclusion','f','h','NONE','','','')) |
| 775 | 775 | pars.append(gammalib.GApplicationPar('emin','r','h','0.1','','','')) |
| ... | ... | @@ -784,9 +784,9 @@ class Test(test): |
| 784 | 784 | pars.append(gammalib.GApplicationPar('etruemin','r','h','0.1','','','')) |
| 785 | 785 | pars.append(gammalib.GApplicationPar('etruemax','r','h','100.0','','','')) |
| 786 | 786 | pars.append(gammalib.GApplicationPar('etruebins','i','h','10','','','')) |
| 787 | - self.test_assert(test.check_is_onoff(), | |
| 787 | + self.test_assert(cls.check_is_onoff(), | |
| 788 | 788 | 'Check if is_onoff() returns True if method is "ONOFF"') |
| 789 | - test.pars(cpy_pars) | |
| 789 | + cls.pars(cpy_pars) | |
| 790 | 790 | |
| 791 | 791 | # Test set_response() method |
| 792 | 792 | # TODO: implement | ... | ... |
test/test_mputils.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# This scripts performs unit tests for the mputils module | |
| 4 | +# | |
| 5 | +# Copyright (C) 2018 Juergen Knoedlseder | |
| 6 | +# | |
| 7 | +# This program is free software: you can redistribute it and/or modify | |
| 8 | +# it under the terms of the GNU General Public License as published by | |
| 9 | +# the Free Software Foundation, either version 3 of the License, or | |
| 10 | +# (at your option) any later version. | |
| 11 | +# | |
| 12 | +# This program is distributed in the hope that it will be useful, | |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | +# GNU General Public License for more details. | |
| 16 | +# | |
| 17 | +# You should have received a copy of the GNU General Public License | |
| 18 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 19 | +# | |
| 20 | +# ========================================================================== | |
| 21 | +import gammalib | |
| 22 | +import ctools | |
| 23 | +from cscripts import mputils | |
| 24 | +from testing import test | |
| 25 | + | |
| 26 | + | |
| 27 | +# ================== # | |
| 28 | +# mputils test class # | |
| 29 | +# ================== # | |
| 30 | +class mputils_test(ctools.cscript): | |
| 31 | + """ | |
| 32 | + Test class for the mputils module | |
| 33 | + """ | |
| 34 | + # Constructor | |
| 35 | + def __init__(self, *argv): | |
| 36 | + """ | |
| 37 | + Constructor | |
| 38 | + """ | |
| 39 | + # Initialise application by calling the base class constructor | |
| 40 | + self._init_cscript(self.__class__.__name__, ctools.__version__, argv) | |
| 41 | + | |
| 42 | + # Return | |
| 43 | + return | |
| 44 | + | |
| 45 | + # Bin computation | |
| 46 | + def _compute_bin(self, i): | |
| 47 | + """ | |
| 48 | + Bin computation | |
| 49 | + """ | |
| 50 | + # Return bin index | |
| 51 | + return i | |
| 52 | + | |
| 53 | + | |
| 54 | +# ============================= # | |
| 55 | +# Test class for mputils module # | |
| 56 | +# ============================= # | |
| 57 | +class Test(test): | |
| 58 | + """ | |
| 59 | + Test class for mputils module | |
| 60 | + """ | |
| 61 | + | |
| 62 | + # Constructor | |
| 63 | + def __init__(self): | |
| 64 | + """ | |
| 65 | + Constructor | |
| 66 | + """ | |
| 67 | + # Call base class constructor | |
| 68 | + test.__init__(self) | |
| 69 | + | |
| 70 | + # Create parameter file for mputils_test application | |
| 71 | + pars = gammalib.GApplicationPars() | |
| 72 | + pars.append_standard() | |
| 73 | + pars.save('mputils_test.par') | |
| 74 | + | |
| 75 | + # Return | |
| 76 | + return | |
| 77 | + | |
| 78 | + # Set test functions | |
| 79 | + def set(self): | |
| 80 | + """ | |
| 81 | + Set all test functions | |
| 82 | + """ | |
| 83 | + # Set test name | |
| 84 | + self.name('mputils') | |
| 85 | + | |
| 86 | + # Append tests | |
| 87 | + self.append(self._test_nthreads, 'Test mputils.nthreads() function') | |
| 88 | + self.append(self._test_process, 'Test mputils.process() function') | |
| 89 | + self.append(self._test_mpfunc, 'Test mputils.mpfunc() function') | |
| 90 | + | |
| 91 | + # Return | |
| 92 | + return | |
| 93 | + | |
| 94 | + # Setup mputils test class | |
| 95 | + def _setup_mputils_test(self): | |
| 96 | + """ | |
| 97 | + Setup mputils test class | |
| 98 | + """ | |
| 99 | + # Get test class | |
| 100 | + cls = mputils_test() | |
| 101 | + | |
| 102 | + # Recover User parameters | |
| 103 | + pars = cls.pars() | |
| 104 | + | |
| 105 | + # Create a copy of the User parameters | |
| 106 | + cpy_pars = pars.copy() | |
| 107 | + | |
| 108 | + # Return | |
| 109 | + return (cls, pars, cpy_pars) | |
| 110 | + | |
| 111 | + # Test nthreads() function | |
| 112 | + def _test_nthreads(self): | |
| 113 | + """ | |
| 114 | + Test nthreads() function | |
| 115 | + """ | |
| 116 | + # Set reference number of CPU's | |
| 117 | + ncpus = 1 | |
| 118 | + try: | |
| 119 | + from multiprocessing import cpu_count | |
| 120 | + ncpus = cpu_count() | |
| 121 | + except: | |
| 122 | + pass | |
| 123 | + | |
| 124 | + # Check nthreads=0. In this case the nthreads() function should return | |
| 125 | + # the number of CPU's that are available, provided that the | |
| 126 | + # multiprocessing module is available. If the module is not available | |
| 127 | + # the function returns 1. | |
| 128 | + cls, pars, cpy_pars = self._setup_mputils_test() | |
| 129 | + pars.append(gammalib.GApplicationPar('nthreads','i','h','0','','','')) | |
| 130 | + nthreads = mputils.nthreads(cls) | |
| 131 | + self.test_value(nthreads, ncpus, 'Check nthreads=0') | |
| 132 | + cls.pars(cpy_pars) | |
| 133 | + | |
| 134 | + # Check nthreads=739. In this case the nthreads() function should | |
| 135 | + # return 739, provided that the multiprocessing module is available. | |
| 136 | + # If the module is not available the function returns 1. | |
| 137 | + cls, pars, cpy_pars = self._setup_mputils_test() | |
| 138 | + pars.append(gammalib.GApplicationPar('nthreads','i','h','739','','','')) | |
| 139 | + nthreads = mputils.nthreads(cls) | |
| 140 | + self.test_value(nthreads, 739, 'Check nthreads=739') | |
| 141 | + cls.pars(cpy_pars) | |
| 142 | + | |
| 143 | + # Return | |
| 144 | + return | |
| 145 | + | |
| 146 | + # Test process() function | |
| 147 | + def _test_process(self): | |
| 148 | + """ | |
| 149 | + Test process() function | |
| 150 | + """ | |
| 151 | + # Test only if Pool module is available | |
| 152 | + try: | |
| 153 | + # Import Pool module | |
| 154 | + from multiprocessing import Pool | |
| 155 | + | |
| 156 | + # Get test class | |
| 157 | + cls = mputils_test() | |
| 158 | + | |
| 159 | + # Setup arguments for mpfunc() computation | |
| 160 | + args = [(cls, '_compute_bin', i) for i in range(10)] | |
| 161 | + | |
| 162 | + # Multiprocessing | |
| 163 | + poolresults = mputils.process(2, mputils.mpfunc, args) | |
| 164 | + | |
| 165 | + # Loop over arguments | |
| 166 | + for i in range(10): | |
| 167 | + result = poolresults[i][0] | |
| 168 | + self.test_value(result, i, 'Check process for argument %d' % i) | |
| 169 | + | |
| 170 | + # ... otherwise if Pool is not available then skip this test | |
| 171 | + except: | |
| 172 | + pass | |
| 173 | + | |
| 174 | + # Return | |
| 175 | + return | |
| 176 | + | |
| 177 | + # Test mpfunc() function | |
| 178 | + def _test_mpfunc(self): | |
| 179 | + """ | |
| 180 | + Test mpfunc() function | |
| 181 | + """ | |
| 182 | + # Get test class | |
| 183 | + cls = mputils_test() | |
| 184 | + | |
| 185 | + # Setup arguments for mpfunc() computation | |
| 186 | + args = [(cls, '_compute_bin', i) for i in range(10)] | |
| 187 | + | |
| 188 | + # Loop over arguments | |
| 189 | + for arg in args: | |
| 190 | + result, _ = mputils.mpfunc(arg) | |
| 191 | + self.test_value(result, arg[2], 'Check mpfunc for argument %d' % arg[2]) | |
| 192 | + | |
| 193 | + # Return | |
| 194 | + return | ... | ... |
test/test_obsutils.py
test/test_python_cscripts.py
| ... | ... | @@ -2,7 +2,7 @@ |
| 2 | 2 | # ========================================================================== |
| 3 | 3 | # This scripts performs unit tests for cscripts |
| 4 | 4 | # |
| 5 | -# Copyright (C) 2016-2017 Juergen Knoedlseder | |
| 5 | +# Copyright (C) 2016-2018 Juergen Knoedlseder | |
| 6 | 6 | # |
| 7 | 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | 8 | # it under the terms of the GNU General Public License as published by |
| ... | ... | @@ -52,6 +52,7 @@ import test_cstsmapsplit |
| 52 | 52 | import test_csviscube |
| 53 | 53 | import test_csworkflow |
| 54 | 54 | import test_obsutils |
| 55 | +import test_mputils | |
| 55 | 56 | import test_csiactcopy |
| 56 | 57 | import test_csiactdata |
| 57 | 58 | import test_csiactobs |
| ... | ... | @@ -144,7 +145,8 @@ def test(installed=False, debug=False): |
| 144 | 145 | test_cstsmapsplit.Test(), |
| 145 | 146 | test_csviscube.Test(), |
| 146 | 147 | test_csworkflow.Test(), |
| 147 | - test_obsutils.Test()] | |
| 148 | + test_obsutils.Test(), | |
| 149 | + test_mputils.Test()] | |
| 148 | 150 | |
| 149 | 151 | # Append tests for Python 2.6+ (the IACT cscripts depend on the json |
| 150 | 152 | # module which is only available since Python 2.6+) | ... | ... |