Ignore:
Timestamp:
Mar 5, 2009, 8:29:28 AM (14 years ago)
Author:
rwilson
Message:

More testing for ensure_numeric()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/numpy/anuga/utilities/test_numerical_tools.py

    r6441 r6456  
    9797        assert A is B   #Same object
    9898
    99 # THIS FAILS!  ASSUMED TYPE IS num.int!?
    100 #        # check default num.array type, which is supposed to be num.float
    101 #        A = num.array((1,2,3,4))
    102 #        assert isinstance(A, num.ndarray)
    103 #        assert A.dtype.char == 'd', \
    104 #                "Expected dtype='d', got '%s'" % A.dtype.char
     99        # check default num.array type, which is supposed to be num.int32
     100        A = num.array((1,2,3,4))
     101        assert isinstance(A, num.ndarray)
     102        msg = "Expected dtype.char='l', got '%s'" % A.dtype.char
     103        assert A.dtype.char == 'l', msg
    105104
    106105        A = num.array([1,2,3,4])
     
    116115        B = ensure_numeric(A, num.float)
    117116        assert num.alltrue(A == B)
    118 #        print 'A=%s' % str(A)
    119 #        print 'B=%s, B.shape=%s' % (str(B), str(B.shape))
    120117
    121118        B = ensure_numeric(A, num.int)       
    122119        assert num.alltrue(A == B)
    123120
    124         # try to simulate getting (x,0) shape
    125         data_points = [[ 413634. ],]
    126         array_data_points = ensure_numeric(data_points)
    127         #if not (0,) == array_data_points.shape:
    128         #    assert len(array_data_points.shape) == 2
    129         #    assert array_data_points.shape[1] == 2
    130 
     121#        # try to simulate getting (x,0) shape
     122#        data_points = [[ 413634. ],]
     123#        array_data_points = ensure_numeric(data_points)
     124#        if not (0,) == array_data_points.shape:
     125#            assert len(array_data_points.shape) == 2
     126#            assert array_data_points.shape[1] == 2
     127
     128        # strings input should raise exception
     129        self.failUnlessRaises(Exception, ensure_numeric(['abc',]))
     130        self.failUnlessRaises(Exception, ensure_numeric(('abc',)))
     131        self.failUnlessRaises(Exception, ensure_numeric(num.array(('abc',))))
    131132
    132133    def NO_test_ensure_numeric_char(self):
     
    468469    ##
    469470    # @brief Test to see if ensure_numeric() behaves as we expect.
    470     # @note Under Numeric ensure_numeric() *always* returned a copy.
     471    # @note Under Numeric ensure_numeric() *always* returned a copy (bug).
    471472    #       Under numpy it copies only when it has to.
    472473    def test_ensure_numeric_copy(self):
     
    524525        #####
    525526        # Make 'points' a numeric array of int coordinates.
    526         # Should be changed by ensure_numeric().
     527        # Should be changed by ensure_numeric(, num.float).
    527528        #####
    528529        points = num.array([[1,2], [3,4], [5,6]], num.int)
     
    551552        msg = 'ensure_numeric() should return the original input'
    552553        self.failUnless(points_new_id == points_id, msg)
     554
     555        # should never change it's input parameter
     556        msg = "ensure_numeric() changed it's input parameter"
     557        self.failUnless(points_id == id(points), msg)
     558
     559        #####
     560        # Make 'points' a numeric array of float32 coordinates.
     561        # Should NOT be changed by ensure_numeric(, num.float32).
     562        #####
     563        points = num.array([[1.,2.], [3.,4.], [5.,6.]], num.float32)
     564        points_id = id(points)
     565
     566        points_new = ensure_numeric(points, num.float32)
     567        points_new_id = id(points_new)
     568
     569        msg = 'ensure_numeric() should return the original input'
     570        self.failUnless(points_new_id == points_id, msg)
     571
     572        # should never change it's input parameter
     573        msg = "ensure_numeric() changed it's input parameter"
     574        self.failUnless(points_id == id(points), msg)
     575
     576        #####
     577        # Make 'points' a numeric array of float32 coordinates.
     578        # Should be changed by ensure_numeric(, num.float64).
     579        #####
     580        points = num.array([[1.,2.], [3.,4.], [5.,6.]], num.float32)
     581        points_id = id(points)
     582
     583        points_new = ensure_numeric(points, num.float64)
     584        points_new_id = id(points_new)
     585
     586        msg = 'ensure_numeric() should return a copy of the input'
     587        self.failUnless(points_new_id != points_id, msg)
     588
     589        # should never change it's input parameter
     590        msg = "ensure_numeric() changed it's input parameter"
     591        self.failUnless(points_id == id(points), msg)
     592
     593        #####
     594        # Make 'points' a numeric array of float coordinates.
     595        # Should NOT be changed by ensure_numeric(, num.float64).
     596        #####
     597        points = num.array([[1.,2.], [3.,4.], [5.,6.]], num.float)
     598        points_id = id(points)
     599
     600        points_new = ensure_numeric(points, num.float64)
     601        points_new_id = id(points_new)
     602
     603        msg = 'ensure_numeric() should return the original input'
     604        self.failUnless(points_new_id == points_id, msg)
     605        #msg = 'ensure_numeric() should return a copy of the input'
     606        #self.failUnless(points_new_id != points_id, msg)
     607
     608        # should never change it's input parameter
     609        msg = "ensure_numeric() changed it's input parameter"
     610        self.failUnless(points_id == id(points), msg)
     611
     612        #####
     613        # Make 'points' a numeric array of float coordinates.
     614        # Should be changed by ensure_numeric(, num.float96).
     615        #####
     616        points = num.array([[1.,2.], [3.,4.], [5.,6.]], num.float)
     617        points_id = id(points)
     618
     619        points_new = ensure_numeric(points, num.float96)
     620        points_new_id = id(points_new)
     621
     622        msg = 'ensure_numeric() should return a copy of the input'
     623        self.failUnless(points_new_id != points_id, msg)
    553624
    554625        # should never change it's input parameter
Note: See TracChangeset for help on using the changeset viewer.