def test_dict_creation() -> None: d1 = {String("t1"): 1, String("t2"): 2} dict1 = Dict(d1) assert type(getattr(dict1, "id", None)) is UID d2 = dict({"t1": 1, "t2": 2}) dict2 = Dict(d2) dict2._id = UID() assert type(getattr(dict2, "id", None)) is UID d3 = UserDict({"t1": 1, "t2": 2}) dict3 = Dict(**d3) assert type(getattr(dict3, "id", None)) is UID assert dict1.keys() == dict2.keys() assert dict1.keys() == dict3.keys() # ValuesView uses object.__eq__ # https://stackoverflow.com/questions/34312674/why-are-the-values-of-an-ordereddict-not-equal assert dict1.values() != dict2.values() assert dict1.values() != dict3.values() assert dict1.items() == dict2.items() assert dict1.items() == dict3.items() it = list(iter(dict2.values())) assert len(it) == 2 assert type(it) is list
def send_get_string_local(data: str, duet: sy.VirtualMachine) -> None: syft_string = String(data) ptr = syft_string.send(duet, pointable=True) remote_data = ptr.get() assert data == remote_data
def test_string_serde() -> None: syft_string = String("Hello OpenMined") serialized = syft_string._object2proto() assert isinstance(serialized, String_PB) deserialized = String._proto2object(proto=serialized) assert isinstance(deserialized, String) assert deserialized.id == syft_string.id
def test_string_send() -> None: alice = syft.VirtualMachine(name="alice") alice_client = alice.get_client() syft_string = String("Hello OpenMined!") ptr = syft_string.send(alice_client) # Check pointer type assert ptr.__class__.__name__ == "StringPointer" # Check that we can get back the object res = ptr.get() assert res == syft_string
def test_list_send(client: sy.VirtualMachineClient) -> None: syft_list = Dict({ String("t1"): String("test"), String("t2"): String("test") }) ptr = syft_list.send(client) # Check pointer type assert ptr.__class__.__name__ == "DictPointer" # Check that we can get back the object res = ptr.get() for res_el, original_el in zip(res, syft_list): assert res_el == original_el
def test_dict_creation() -> None: d1 = {String("t1"): 1, String("t2"): 2} dict1 = Dict(d1) assert type(getattr(dict1, "id", None)) is UID d2 = dict({"t1": 1, "t2": 2}) dict2 = Dict(d2) dict2._id = UID() assert type(getattr(dict2, "id", None)) is UID d3 = UserDict({"t1": 1, "t2": 2}) dict3 = Dict(**d3) assert type(getattr(dict3, "id", None)) is UID assert dict1.keys() == dict2.keys() assert dict1.keys() == dict3.keys()
def test_dictitems_contains_use_after_free(self): class X: def __eq__(self, other): d.clear() return NotImplemented d = Dict({0: String("test")}) # TODO: we should be able to support set (0, X()) in d.items()
def test_slice_types() -> None: py_string = "Python" py_list = ["P", "y", "t", "h", "o", "n"] py_tuple = ("P", "y", "t", "h", "o", "n") sy_string = String(py_string) sy_tuple = Tuple(py_tuple) sy_list = List(py_list) py_slice1 = slice(1) sy_slice1 = Slice(1) assert py_slice1.start == sy_slice1.start assert py_slice1.stop == sy_slice1.stop assert py_slice1.step == sy_slice1.step assert py_slice1 == sy_slice1 py_slice2 = slice(1, 2) sy_slice2 = Slice(1, 2) assert py_slice2 == sy_slice2 assert py_slice2.start == sy_slice2.start assert py_slice2.stop == sy_slice2.stop assert py_slice2.step == sy_slice2.step py_slice3 = slice(1, 2, -1) sy_slice3 = Slice(1, 2, -1) assert py_slice3 == sy_slice3 assert py_slice3.start == sy_slice3.start assert py_slice3.stop == sy_slice3.stop assert py_slice3.step == sy_slice3.step assert sy_string[sy_slice1] == py_string[py_slice1] assert sy_string[sy_slice2] == py_string[py_slice2] assert sy_string[sy_slice3] == py_string[py_slice3] assert sy_tuple[sy_slice1] == py_tuple[py_slice1] assert sy_tuple[sy_slice2] == py_tuple[py_slice2] assert sy_tuple[sy_slice3] == py_tuple[py_slice3] assert sy_list[sy_slice1] == py_list[py_slice1] assert sy_list[sy_slice2] == py_list[py_slice2] assert sy_list[sy_slice3] == py_list[py_slice3] assert sy_list[py_slice3] == py_list[py_slice3]
def test_sane_len(): # this test just tests our assumptions about __len__ # this will start failing if __len__ changes assertions for badval in [String("illegal"), Int(-1), Int(1 << 32)]: class A: def __len__(self): return badval try: Bool(A()) except (Exception) as e_bool: try: len(A()) except (Exception) as e_len: assert str(e_bool) == str(e_len)
def test_id_concat_syft_type() -> None: val = String(" 2020") result = STRING_SYFT + val assert result.id assert result.id != STRING_SYFT.id assert val.id != result.id assert result == STRING_PYTHON + val result = val + STRING_SYFT assert result.id assert result.id != STRING_SYFT.id assert val.id != result.id assert val + STRING_PYTHON == result
def send_get_string_multiprocess(data: String, port: int) -> None: run_endpoints(do_send, ds_get, String(data), port)
def test_string(): assert String("xyz").endswith("z") == SyTrue assert String("xyz").endswith("x") == SyFalse assert String("xyz0123").isalnum() == SyTrue assert String("@#$%").isalnum() == SyFalse assert String("xyz").isalpha() == SyTrue assert String("@#$%").isalpha() == SyFalse assert String("0123").isdigit() == SyTrue assert String("xyz").isdigit() == SyFalse assert String("xyz").islower() == SyTrue assert String("XYZ").islower() == SyFalse assert String("0123").isdecimal() == SyTrue assert String("xyz").isdecimal() == SyFalse assert String("0123").isnumeric() == SyTrue assert String("xyz").isnumeric() == SyFalse assert String(" ").isspace() == SyTrue assert String("\xa0").isspace() == SyTrue assert String("\u3000").isspace() == SyTrue assert String("XYZ").isspace() == SyFalse assert String("X").istitle() == SyTrue assert String("x").istitle() == SyFalse assert String("XYZ").isupper() == SyTrue assert String("xyz").isupper() == SyFalse assert String("xyz").startswith("x") == SyTrue assert String("xyz").startswith("z") == SyFalse
def test_repr() -> None: assert repr(SyFalse) == String("False") assert repr(SyTrue) == String("True") assert eval(repr(SyFalse)) == SyFalse assert eval(repr(SyTrue)) == SyTrue
def test_format() -> None: assert String("%d") % SyFalse == "0" assert String("%d") % SyTrue == "1" assert String("%x") % SyFalse == "0" assert String("%x") % SyTrue == "1"
def string_serde(data: str) -> None: syft_string = String(data) serialized = syft_string._object2proto() String._proto2object(proto=serialized)
def test_str() -> None: assert str(SyFalse) == String("False") assert str(SyTrue) == String("True")
# syft absolute from syft.lib.python.string import String STRING_PYTHON = "Hello OpenMined" STRING_SYFT = String(STRING_PYTHON) def test_id_concat_python_type() -> None: val = " 2020" result = STRING_SYFT + val assert result.id assert result.id != STRING_SYFT.id assert result == STRING_PYTHON + val result = val + STRING_SYFT assert result.id assert result.id != STRING_SYFT.id assert val + STRING_PYTHON == result def test_id_concat_syft_type() -> None: val = String(" 2020") result = STRING_SYFT + val assert result.id assert result.id != STRING_SYFT.id assert val.id != result.id