contrib/python-zstandard/tests/common.py
changeset 42070 675775c33ab6
parent 37495 b1fb341d8a61
child 43994 de7838053207
equal deleted inserted replaced
42069:668eff08387f 42070:675775c33ab6
    77         setattr(cls, name, new_method)
    77         setattr(cls, name, new_method)
    78 
    78 
    79     return cls
    79     return cls
    80 
    80 
    81 
    81 
    82 class OpCountingBytesIO(io.BytesIO):
    82 class NonClosingBytesIO(io.BytesIO):
       
    83     """BytesIO that saves the underlying buffer on close().
       
    84 
       
    85     This allows us to access written data after close().
       
    86     """
    83     def __init__(self, *args, **kwargs):
    87     def __init__(self, *args, **kwargs):
       
    88         super(NonClosingBytesIO, self).__init__(*args, **kwargs)
       
    89         self._saved_buffer = None
       
    90 
       
    91     def close(self):
       
    92         self._saved_buffer = self.getvalue()
       
    93         return super(NonClosingBytesIO, self).close()
       
    94 
       
    95     def getvalue(self):
       
    96         if self.closed:
       
    97             return self._saved_buffer
       
    98         else:
       
    99             return super(NonClosingBytesIO, self).getvalue()
       
   100 
       
   101 
       
   102 class OpCountingBytesIO(NonClosingBytesIO):
       
   103     def __init__(self, *args, **kwargs):
       
   104         self._flush_count = 0
    84         self._read_count = 0
   105         self._read_count = 0
    85         self._write_count = 0
   106         self._write_count = 0
    86         return super(OpCountingBytesIO, self).__init__(*args, **kwargs)
   107         return super(OpCountingBytesIO, self).__init__(*args, **kwargs)
       
   108 
       
   109     def flush(self):
       
   110         self._flush_count += 1
       
   111         return super(OpCountingBytesIO, self).flush()
    87 
   112 
    88     def read(self, *args):
   113     def read(self, *args):
    89         self._read_count += 1
   114         self._read_count += 1
    90         return super(OpCountingBytesIO, self).read(*args)
   115         return super(OpCountingBytesIO, self).read(*args)
    91 
   116 
   115                     if data:
   140                     if data:
   116                         _source_files.append(data)
   141                         _source_files.append(data)
   117             except OSError:
   142             except OSError:
   118                 pass
   143                 pass
   119 
   144 
       
   145     # Also add some actual random data.
       
   146     _source_files.append(os.urandom(100))
       
   147     _source_files.append(os.urandom(1000))
       
   148     _source_files.append(os.urandom(10000))
       
   149     _source_files.append(os.urandom(100000))
       
   150     _source_files.append(os.urandom(1000000))
       
   151 
   120     return _source_files
   152     return _source_files
   121 
   153 
   122 
   154 
   123 def generate_samples():
   155 def generate_samples():
   124     inputs = [
   156     inputs = [
   138 
   170 
   139     return samples
   171     return samples
   140 
   172 
   141 
   173 
   142 if hypothesis:
   174 if hypothesis:
   143     default_settings = hypothesis.settings()
   175     default_settings = hypothesis.settings(deadline=10000)
   144     hypothesis.settings.register_profile('default', default_settings)
   176     hypothesis.settings.register_profile('default', default_settings)
   145 
   177 
   146     ci_settings = hypothesis.settings(max_examples=2500,
   178     ci_settings = hypothesis.settings(deadline=20000, max_examples=1000)
   147                                       max_iterations=2500)
       
   148     hypothesis.settings.register_profile('ci', ci_settings)
   179     hypothesis.settings.register_profile('ci', ci_settings)
       
   180 
       
   181     expensive_settings = hypothesis.settings(deadline=None, max_examples=10000)
       
   182     hypothesis.settings.register_profile('expensive', expensive_settings)
   149 
   183 
   150     hypothesis.settings.load_profile(
   184     hypothesis.settings.load_profile(
   151         os.environ.get('HYPOTHESIS_PROFILE', 'default'))
   185         os.environ.get('HYPOTHESIS_PROFILE', 'default'))