def test_smallest_int_sctype(self):
     # Smallest int sctype with full recaster
     params = sctype_attributes()
     RF = Recaster()
     test_triples = [(np.uint8, 0, 255),
                   (np.int8, -128, 0),
                   (np.uint16, 0, params[np.uint16]['max']),
                   (np.int16, params[np.int16]['min'], 0),
                   (np.uint32, 0, params[np.uint32]['max']),
                   (np.int32, params[np.int32]['min'], 0),
                   (np.uint64, 0, params[np.uint64]['max']),
                   (np.int64, params[np.int64]['min'], 0)]
     for T, mn, mx in test_triples:
         rt = RF.smallest_int_sctype(mx, mn)
         assert np.dtype(rt) == np.dtype(T), \
                'Expected %s, got %s type' % (T, rt)
     # Smallest int sctype with restricted recaster
     mmax = params[np.int32]['max']
     mmin = params[np.int32]['min']
     RR = Recaster([np.int32])
     for kind in ('int', 'uint'):
         for T in np.sctypes[kind]:
             mx = params[T]['max']
             mn = params[T]['min']
             rt = RR.smallest_int_sctype(mx, mn)
             if mx <= mmax and mn >= mmin:
                 assert rt == np.int32, \
                        'Expected int32 type, got %s' % rt
             else:
                 assert rt is None, \
                        'Expected None, got %s for %s' % (T, rt)
     # Test preferred int flag
     mx = 1000
     mn = 0
     rt = RF.smallest_int_sctype(mx, mn)
     assert rt == np.int16, 'Expected int16, got %s' % rt
     rt = RF.smallest_int_sctype(mx, mn, 'i')
     assert rt == np.int16, 'Expected int16, got %s' % rt
     rt = RF.smallest_int_sctype(mx, mn, prefer='u')
     assert rt == np.uint16, 'Expected uint16, got %s' % rt
Esempio n. 2
0
 def test_smallest_int_sctype(self):
     # Smallest int sctype with full recaster
     params = sctype_attributes()
     RF = Recaster()
     test_triples = [(np.uint8, 0, 255), (np.int8, -128, 0),
                     (np.uint16, 0, params[np.uint16]['max']),
                     (np.int16, params[np.int16]['min'], 0),
                     (np.uint32, 0, params[np.uint32]['max']),
                     (np.int32, params[np.int32]['min'], 0),
                     (np.uint64, 0, params[np.uint64]['max']),
                     (np.int64, params[np.int64]['min'], 0)]
     for T, mn, mx in test_triples:
         rt = RF.smallest_int_sctype(mx, mn)
         assert np.dtype(rt) == np.dtype(T), \
                'Expected %s, got %s type' % (T, rt)
     # Smallest int sctype with restricted recaster
     mmax = params[np.int32]['max']
     mmin = params[np.int32]['min']
     RR = Recaster([np.int32])
     for kind in ('int', 'uint'):
         for T in np.sctypes[kind]:
             mx = params[T]['max']
             mn = params[T]['min']
             rt = RR.smallest_int_sctype(mx, mn)
             if mx <= mmax and mn >= mmin:
                 assert rt == np.int32, \
                        'Expected int32 type, got %s' % rt
             else:
                 assert rt is None, \
                        'Expected None, got %s for %s' % (T, rt)
     # Test preferred int flag
     mx = 1000
     mn = 0
     rt = RF.smallest_int_sctype(mx, mn)
     assert rt == np.int16, 'Expected int16, got %s' % rt
     rt = RF.smallest_int_sctype(mx, mn, 'i')
     assert rt == np.int16, 'Expected int16, got %s' % rt
     rt = RF.smallest_int_sctype(mx, mn, prefer='u')
     assert rt == np.uint16, 'Expected uint16, got %s' % rt