# Path: tensorboardX/record_writer.py
# class RecordWriter(object):
#     def __init__(self, path):
#         self._name_to_tf_name = {}
#         self._tf_names = set()
#         self.path = path
#         self._writer = None
#         self._writer = open_file(path)
# 
#     def write(self, data):
#         w = self._writer.write
#         header = struct.pack('Q', len(data))
#         w(header)
#         w(struct.pack('I', masked_crc32c(header)))
#         w(data)
#         w(struct.pack('I', masked_crc32c(data)))
# 
#     def flush(self):
#         self._writer.flush()
# 
#     def close(self):
#         self._writer.close()


import six
import os
import unittest
    import tempfile
from tensorboardX.record_writer import RecordWriter
from tensorboard.compat.tensorflow_stub.pywrap_tensorflow import PyRecordReader_New
from __future__ import print_function



class RecordWriterTest(unittest.TestCase):
  def get_temp_dir(self):
    return tempfile.mkdtemp()

  def test_expect_bytes_written(self):
    filename = os.path.join(self.get_temp_dir(), "expect_bytes_written")
    byte_len = 64
    w = RecordWriter(filename)
    bytes_to_write = b"x" * byte_len
    w.write(bytes_to_write)
    w.close()
    with open(filename, 'rb') as f:
      self.assertEqual(len(f.read()), (8 + 4 + byte_len + 4))  # uint64+uint32+data+uint32

  def test_empty_record(self):
    filename = os.path.join(self.get_temp_dir(), "empty_record")
    w = RecordWriter(filename)
    bytes_to_write = b""
    w.write(bytes_to_write)
    w.close()
    r = PyRecordReader_New(filename)
    r.GetNext()
    self.assertEqual(r.record(), bytes_to_write)

  def test_record_writer_roundtrip(self):
    filename = os.path.join(self.get_temp_dir(), "record_writer_roundtrip")

# Path: tensorboardX/record_writer.py
# class RecordWriter(object):
#     def __init__(self, path):
#         self._name_to_tf_name = {}
#         self._tf_names = set()
#         self.path = path
#         self._writer = None
#         self._writer = open_file(path)
# 
#     def write(self, data):
#         w = self._writer.write
#         header = struct.pack('Q', len(data))
#         w(header)
#         w(struct.pack('I', masked_crc32c(header)))
#         w(data)
#         w(struct.pack('I', masked_crc32c(data)))
# 
#     def flush(self):
#         self._writer.flush()
# 
#     def close(self):
#         self._writer.close()


# Path: tests/record_writer_test.py
import six
import os
import unittest
    import tempfile
from tensorboardX.record_writer import RecordWriter
from tensorboard.compat.tensorflow_stub.pywrap_tensorflow import PyRecordReader_New

from __future__ import print_function



class RecordWriterTest(unittest.TestCase):
  def get_temp_dir(self):
    return tempfile.mkdtemp()

  def test_expect_bytes_written(self):
    filename = os.path.join(self.get_temp_dir(), "expect_bytes_written")
    byte_len = 64
    w = RecordWriter(filename)
    bytes_to_write = b"x" * byte_len
    w.write(bytes_to_write)
    w.close()
    with open(filename, 'rb') as f:
      self.assertEqual(len(f.read()), (8 + 4 + byte_len + 4))  # uint64+uint32+data+uint32

  def test_empty_record(self):
    filename = os.path.join(self.get_temp_dir(), "empty_record")
    w = RecordWriter(filename)
    bytes_to_write = b""
    w.write(bytes_to_write)
    w.close()
    r = PyRecordReader_New(filename)
    r.GetNext()
    self.assertEqual(r.record(), bytes_to_write)

  def test_record_writer_roundtrip(self):
    filename = os.path.join(self.get_temp_dir(), "record_writer_roundtrip")

    w = RecordWriter(filename)