浏览代码

add float list and unit tests

/bug-failed-api-check
Chris Elion 5 年前
当前提交
3b42f8c4
共有 2 个文件被更改,包括 54 次插入0 次删除
  1. 12
      ml-agents-envs/mlagents_envs/side_channel/side_channel.py
  2. 42
      ml-agents-envs/mlagents_envs/tests/test_side_channel.py

12
ml-agents-envs/mlagents_envs/side_channel/side_channel.py


def write_float32(self, f: float) -> None:
self.buffer += struct.pack("<f", f)
def write_float32_list(self, float_list: List[float]) -> None:
self.write_int32(len(float_list))
for f in float_list:
self.write_float32(f)
def write_string(self, s: str) -> None:
encoded_key = s.encode("ascii")
self.write_int32(len(encoded_key))

val = struct.unpack_from("<f", self.buffer, self.offset)[0]
self.offset += 4
return val
def read_float32_list(self) -> List[float]:
list_len = self.read_int32()
output = []
for _ in range(list_len):
output.append(self.read_float32())
return output
def read_string(self) -> str:
encoded_str_len = self.read_int32()

42
ml-agents-envs/mlagents_envs/tests/test_side_channel.py


messages = receiver.get_and_clear_received_messages()
assert len(messages) == 0
def test_message_int32():
val = 1337
msg_out = OutgoingMessage()
msg_out.write_int32(val)
msg_in = IncomingMessage(msg_out.buffer)
read_val = msg_in.read_int32()
assert val == read_val
def test_message_float32():
val = 42.0
msg_out = OutgoingMessage()
msg_out.write_float32(val)
msg_in = IncomingMessage(msg_out.buffer)
read_val = msg_in.read_float32()
# These won't be exactly equal in general, since python floats are 64-bit.
assert val == read_val
def test_message_string():
val = "mlagents!"
msg_out = OutgoingMessage()
msg_out.write_string(val)
msg_in = IncomingMessage(msg_out.buffer)
read_val = msg_in.read_string()
assert val == read_val
def test_message_float_list():
val = [1.0, 3.0, 9.0]
msg_out = OutgoingMessage()
msg_out.write_float32_list(val)
msg_in = IncomingMessage(msg_out.buffer)
read_val = msg_in.read_float32_list()
# These won't be exactly equal in general, since python floats are 64-bit.
assert val == read_val
正在加载...
取消
保存