def test_cleanup(self):
        channel_pair = TaborChannelPair(self.instrument,
                                        identifier='asd',
                                        channels=(1, 2))

        self.instrument.paranoia_level = 0
        self.reset_instrument_logs()

        channel_pair._segment_references = np.array([1, 2, 0, 1],
                                                    dtype=np.uint32)
        channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32],
                                                        dtype=np.uint32)
        channel_pair._segment_lengths = 192 + np.array([0, 0, 16, 16],
                                                       dtype=np.uint32)
        channel_pair._segment_hashes = np.array([1, 2, 3, 4], dtype=np.int64)

        channel_pair.cleanup()
        np.testing.assert_equal(channel_pair._segment_references,
                                np.array([1, 2, 0, 1], dtype=np.uint32))
        np.testing.assert_equal(
            channel_pair._segment_capacity,
            192 + np.array([0, 16, 32, 32], dtype=np.uint32))
        np.testing.assert_equal(
            channel_pair._segment_lengths,
            192 + np.array([0, 0, 16, 16], dtype=np.uint32))
        np.testing.assert_equal(channel_pair._segment_hashes,
                                np.array([1, 2, 3, 4], dtype=np.int64))

        channel_pair._segment_references = np.array([1, 2, 0, 1, 0],
                                                    dtype=np.uint32)
        channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32, 32],
                                                        dtype=np.uint32)
        channel_pair._segment_lengths = 192 + np.array([0, 0, 16, 16, 0],
                                                       dtype=np.uint32)
        channel_pair._segment_hashes = np.array([1, 2, 3, 4, 5],
                                                dtype=np.int64)

        channel_pair.cleanup()
        np.testing.assert_equal(channel_pair._segment_references,
                                np.array([1, 2, 0, 1], dtype=np.uint32))
        np.testing.assert_equal(
            channel_pair._segment_capacity,
            192 + np.array([0, 16, 32, 32], dtype=np.uint32))
        np.testing.assert_equal(
            channel_pair._segment_lengths,
            192 + np.array([0, 0, 16, 16], dtype=np.uint32))
        np.testing.assert_equal(channel_pair._segment_hashes,
                                np.array([1, 2, 3, 4], dtype=np.int64))
    def test_upload_segment(self):
        channel_pair = TaborChannelPair(self.instrument,
                                        identifier='asd',
                                        channels=(1, 2))

        self.reset_instrument_logs()

        channel_pair._segment_references = np.array([1, 2, 0, 1],
                                                    dtype=np.uint32)
        channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32],
                                                        dtype=np.uint32)
        channel_pair._segment_lengths = channel_pair._segment_capacity.copy()

        channel_pair._segment_hashes = np.array([1, 2, 3, 4], dtype=np.int64)

        # prevent entering and exiting configuration mode
        channel_pair._configuration_guard_count = 2

        segment = TaborSegment.from_sampled(
            np.ones(192 + 16, dtype=np.uint16),
            np.zeros(192 + 16, dtype=np.uint16), None, None)
        segment_binary = segment.get_as_binary()
        with self.assertRaises(ValueError):
            channel_pair._upload_segment(3, segment)

        with self.assertRaises(ValueError):
            channel_pair._upload_segment(0, segment)

        channel_pair._upload_segment(2, segment)
        np.testing.assert_equal(
            channel_pair._segment_capacity,
            192 + np.array([0, 16, 32, 32], dtype=np.uint32))
        np.testing.assert_equal(
            channel_pair._segment_lengths,
            192 + np.array([0, 16, 16, 32], dtype=np.uint32))
        np.testing.assert_equal(
            channel_pair._segment_hashes,
            np.array([1, 2, hash(segment), 4], dtype=np.int64))

        expected_commands = [
            ':INST:SEL 1', ':INST:SEL 1', ':INST:SEL 1', ':TRAC:DEF 3, 208',
            ':TRAC:SEL 3', ':TRAC:MODE COMB'
        ]
        expected_log = [
            ((),
             dict(cmd_str=cmd,
                  paranoia_level=channel_pair.internal_paranoia_level))
            for cmd in expected_commands
        ]
        self.assertAllCommandLogsEqual(expected_log)

        self.assert_written_segment_data(segment_binary)
    def test_amend_segments_iter(self):
        channel_pair = TaborChannelPair(self.instrument,
                                        identifier='asd',
                                        channels=(1, 2))
        # prevent entering and exiting configuration mode
        channel_pair._configuration_guard_count = 2

        self.instrument.paranoia_level = 0
        self.reset_instrument_logs()

        channel_pair._segment_references = np.array([1, 2, 0, 1],
                                                    dtype=np.uint32)
        channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32],
                                                        dtype=np.uint32)
        channel_pair._segment_lengths = 192 + np.array([0, 0, 16, 16],
                                                       dtype=np.uint32)

        channel_pair._segment_hashes = np.array([1, 2, 3, 4], dtype=np.int64)

        data = np.ones(192, dtype=np.uint16)
        segments = [
            TaborSegment.from_sampled(0 * data, 1 * data, None, None),
            TaborSegment.from_sampled(1 * data, 2 * data, None, None)
        ]

        indices = channel_pair._amend_segments(segments)

        expected_references = np.array([1, 2, 0, 1, 1, 1], dtype=np.uint32)
        expected_capacities = 192 + np.array([0, 16, 32, 32, 0, 0],
                                             dtype=np.uint32)
        expected_lengths = 192 + np.array([0, 0, 16, 16, 0, 0],
                                          dtype=np.uint32)
        expected_hashes = np.array(
            [1, 2, 3, 4, hash(segments[0]),
             hash(segments[1])], dtype=np.int64)

        np.testing.assert_equal(channel_pair._segment_references,
                                expected_references)
        np.testing.assert_equal(channel_pair._segment_capacity,
                                expected_capacities)
        np.testing.assert_equal(channel_pair._segment_lengths,
                                expected_lengths)
        np.testing.assert_equal(channel_pair._segment_hashes, expected_hashes)

        np.testing.assert_equal(indices, np.array([4, 5], dtype=np.int64))

        expected_commands = [
            ':INST:SEL 1', ':TRAC:DEF 5,{}'.format(2 * 192 + 16),
            ':TRAC:SEL 5', ':TRAC:MODE COMB', ':TRAC:DEF 5,192',
            ':TRAC:DEF 6,192'
        ]
        expected_log = [
            ((),
             dict(cmd_str=cmd,
                  paranoia_level=channel_pair.internal_paranoia_level))
            for cmd in expected_commands
        ]
        self.assertAllCommandLogsEqual(expected_log)

        # no segment lengths written
        self.assert_written_segment_lengths()

        expected_bin_blob = make_combined_wave(segments)
        self.assert_written_segment_data(expected_bin_blob)