def array_dtypes( subtype_strategy=scalar_dtypes(), # type: st.SearchStrategy[np.dtype] min_size=1, # type: int max_size=5, # type: int allow_subarrays=False, # type: bool ): # type: (...) -> st.SearchStrategy[np.dtype] """Return a strategy for generating array (compound) dtypes, with members drawn from the given subtype strategy.""" order_check("size", 0, min_size, max_size) # Field names must be native strings and the empty string is weird; see #1963. if PY2: field_names = st.binary(min_size=1) else: field_names = st.text(min_size=1) elements = st.tuples(field_names, subtype_strategy) if allow_subarrays: elements |= st.tuples( field_names, subtype_strategy, array_shapes(max_dims=2, max_side=2) ) return st.lists( elements=elements, min_size=min_size, max_size=max_size, unique_by=lambda d: d[0], )
def combaination(draw): from wifi.config import Config packets = draw(integers(min_value=0, max_value=64)) rate = draw(sampled_from([6, 9, 12, 18, 24, 36, 48, 54])) conf = Config.from_data_rate(rate) lim = packets * conf.coded_bits_per_ofdm_symbol data = draw(binary(min_size=lim, max_size=lim)) data = bitstr.from_bytes(data) return data, conf.coded_bits_per_ofdm_symbol, conf.coded_bits_per_carrier_symbol
def from_dtype(dtype): # type: (np.dtype) -> st.SearchStrategy[Any] """Creates a strategy which can generate any value of the given dtype.""" check_type(np.dtype, dtype, "dtype") # Compound datatypes, eg 'f4,f4,f4' if dtype.names is not None: # mapping np.void.type over a strategy is nonsense, so return now. return st.tuples(*[from_dtype(dtype.fields[name][0]) for name in dtype.names]) # Subarray datatypes, eg '(2, 3)i4' if dtype.subdtype is not None: subtype, shape = dtype.subdtype return arrays(subtype, shape) # Scalar datatypes if dtype.kind == u"b": result = st.booleans() # type: SearchStrategy[Any] elif dtype.kind == u"f": if dtype.itemsize == 2: result = st.floats(width=16) elif dtype.itemsize == 4: result = st.floats(width=32) else: result = st.floats() elif dtype.kind == u"c": if dtype.itemsize == 8: float32 = st.floats(width=32) result = st.builds(complex, float32, float32) else: result = st.complex_numbers() elif dtype.kind in (u"S", u"a"): # Numpy strings are null-terminated; only allow round-trippable values. # `itemsize == 0` means 'fixed length determined at array creation' result = st.binary(max_size=dtype.itemsize or None).filter( lambda b: b[-1:] != b"\0" ) elif dtype.kind == u"u": result = st.integers(min_value=0, max_value=2 ** (8 * dtype.itemsize) - 1) elif dtype.kind == u"i": overflow = 2 ** (8 * dtype.itemsize - 1) result = st.integers(min_value=-overflow, max_value=overflow - 1) elif dtype.kind == u"U": # Encoded in UTF-32 (four bytes/codepoint) and null-terminated result = st.text(max_size=(dtype.itemsize or 0) // 4 or None).filter( lambda b: b[-1:] != u"\0" ) elif dtype.kind in (u"m", u"M"): if "[" in dtype.str: res = st.just(dtype.str.split("[")[-1][:-1]) else: res = st.sampled_from(TIME_RESOLUTIONS) result = st.builds(dtype.type, st.integers(-(2 ** 63), 2 ** 63 - 1), res) else: raise InvalidArgument(u"No strategy inference for {}".format(dtype)) return result.map(dtype.type)
def array_dtypes( subtype_strategy=scalar_dtypes(), # type: st.SearchStrategy[np.dtype] min_size=1, # type: int max_size=5, # type: int allow_subarrays=False, # type: bool ): # type: (...) -> st.SearchStrategy[np.dtype] """Return a strategy for generating array (compound) dtypes, with members drawn from the given subtype strategy.""" order_check("size", 0, min_size, max_size) native_strings = st.text() # type: SearchStrategy[Any] if text_type is not str: # pragma: no cover native_strings = st.binary() elements = st.tuples(native_strings, subtype_strategy) if allow_subarrays: elements |= st.tuples(native_strings, subtype_strategy, array_shapes(max_dims=2, max_side=2)) return st.lists( elements=elements, min_size=min_size, max_size=max_size, unique_by=lambda d: d[0], )
assume(bitstr.is_divisible(data, by=4)) data = [bit for i, bit in enumerate(data) if (i % 4) != 3] elif coding_rate == '3/4': # throw out each 3. and 4. bit groups of 6 bits assume(bitstr.is_divisible(data, by=6)) data = [bit for i, bit in enumerate(data) if (i % 6) != 3 and (i % 6) != 4] return bitstr.merge(data) def undo(data: bits, coding_rate='1/2') -> bits: # un-puncturing process i.e. add 'X' bits, which are basically just ignored by the conv decoder if coding_rate == '3/4': data = [d[:3] + '??' + d[3] for d in bitstr.split(data, 4)] elif coding_rate == '2/3': data = [d + '?' for d in bitstr.split(data, 3)] return bitstr.merge(data) @given(binary(), sampled_from(['1/2', '2/3', '3/4'])) def test_hypothesis(data, coding_rate): data = bits(data) # cant test equality because 'do' throws away data do1 = do(data, coding_rate) undo1 = undo(do1, coding_rate) assert len(undo1) == len(data) do2 = do(undo1, coding_rate) assert do1 == do2
def is_divisible(x: bits, by: int) -> bool: return len(x) != 0 and len(x) % by == 0 @given(integers()) def test_int(i): assert to_int(from_int(i, 32)) == i @given(integers()) def test_hex(i): i = hex(i) assert to_hex(from_hex(i)) == i @given(binary()) def test_bytes(i): assert to_bytes(from_bytes(i)) == i def test_fill(): assert from_int(0, 3) == '000' def test_split_merge(): b = from_hex('0x12') s = split(b, 4) assert s == ['0001', '0010'] m = merge(s) assert m == b
def random_packet(draw): elements = draw(integers(min_value=0, max_value=(2**12) - 1)) data = draw(binary(min_size=elements, max_size=elements)) data = bitstr.from_bytes(data) rate = draw(sampled_from([6, 9, 12, 18, 24, 36, 48, 54])) return data, rate
from hypothesis._strategies import binary, sampled_from from wifi import bits, bitstr import numpy as np def do(data: bits, data_bits_per_ofdm_symbol: int) -> Tuple[bits, int]: service = '0' * 16 tail = '0' * 6 data = service + data + tail n_symbols = int(np.ceil(len(data) / data_bits_per_ofdm_symbol)) n_data = n_symbols * data_bits_per_ofdm_symbol n_pad = int(n_data - len(data)) pad = '0' * n_pad data = data + pad return data, n_pad def undo(data: bits, length_bytes: int) -> bits: return data[16:16 + length_bytes * 8] @given(binary(), sampled_from([48, 96, 192, 288])) def test_hypothesis(data, data_bits_per_ofdm_symbol): data = bitstr.from_bytes(data) done_data, n_pad = do(data, data_bits_per_ofdm_symbol) assert undo(done_data, len(data) // 8) == data
# test reverse rev = undo(symbols, bits_per_symbol=4) assert rev == input def test_i144(): # Table I-9—SIGNAL field bits after interleaving input = bits('100101001101000000010100100000110010010010010100') # Table I-10—Frequency domain representation of SIGNAL field expected = [(1 + 0j), (-1 + 0j), (-1 + 0j), (1 + 0j), (-1 + 0j), (1 + 0j), (-1 + 0j), (-1 + 0j), (1 + 0j), (1 + 0j), (-1 + 0j), (1 + 0j), (-1 + 0j), (-1 + 0j), (-1 + 0j), (-1 + 0j), (-1 + 0j), (-1 + 0j), (-1 + 0j), (1 + 0j), (-1 + 0j), (1 + 0j), (-1 + 0j), (-1 + 0j), (1 + 0j), (-1 + 0j), (-1 + 0j), (-1 + 0j), (-1 + 0j), (-1 + 0j), (1 + 0j), (1 + 0j), (-1 + 0j), (-1 + 0j), (1 + 0j), (-1 + 0j), (-1 + 0j), (1 + 0j), (-1 + 0j), (-1 + 0j), (1 + 0j), (-1 + 0j), (-1 + 0j), (1 + 0j), (-1 + 0j), (1 + 0j), (-1 + 0j), (-1 + 0j)] symbols = do(input, bits_per_symbol=1) np.testing.assert_equal(expected, np.round(symbols, 3)) # test reverse rev = undo(symbols, bits_per_symbol=1) assert rev == input @given(binary(), sampled_from([1, 2, 4, 6])) def test_hypothesis(data, bits_per_symbol): data = bitstr.from_bytes(data) assert undo(do(data, bits_per_symbol), bits_per_symbol) == data