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_upload(self):
        segments = np.array([1, 2, 3, 4, 5])
        segment_lengths = np.array([0, 16, 0, 16, 0], dtype=np.uint16).tolist()

        segment_references = np.array([1, 1, 2, 0, 1], dtype=np.uint32)

        w2s = np.array([-1, -1, 1, 2, -1], dtype=np.int64)
        ta = np.array([True, False, False, False, True])
        ti = np.array([-1, 3, -1, -1, -1])

        channels = (1, None)
        markers = (None, None)
        voltage_transformations = (lambda x: x, lambda x: x)
        sample_rate = TimeType.from_fraction(1, 1)

        with mock.patch('qupulse.hardware.awgs.tabor.TaborProgram',
                        specs=TaborProgram) as DummyTaborProgram:
            tabor_program = DummyTaborProgram.return_value
            tabor_program.get_sampled_segments.return_value = (segments,
                                                               segment_lengths)

            program = Loop(waveform=DummyWaveform(duration=192))

            channel_pair = TaborChannelPair(self.instrument,
                                            identifier='asd',
                                            channels=(1, 2))
            channel_pair._segment_references = segment_references

            def dummy_find_place(segments_, segement_lengths_):
                self.assertIs(segments_, segments)
                self.assertIs(segment_lengths, segement_lengths_)
                return w2s, ta, ti

            def dummy_upload_segment(segment_index, segment):
                self.assertEqual(segment_index, 3)
                self.assertEqual(segment, 2)

            def dummy_amend_segments(segments_):
                np.testing.assert_equal(segments_, np.array([1, 5]))
                return np.array([5, 6], dtype=np.int64)

            self.instrument.amplitude = mock.Mock(return_value=1.)
            self.instrument.sample_rate = mock.Mock(return_value=10**9)

            channel_pair._find_place_for_segments_in_memory = dummy_find_place
            channel_pair._upload_segment = dummy_upload_segment
            channel_pair._amend_segments = dummy_amend_segments

            channel_pair.upload('test', program, channels, markers,
                                voltage_transformations)

            DummyTaborProgram.assert_called_once_with(
                program,
                channels=tuple(channels),
                markers=markers,
                device_properties=channel_pair.device.dev_properties,
                sample_rate=sample_rate,
                amplitudes=(.5, .5),
                offsets=(0., 0.),
                voltage_transformations=voltage_transformations)

            self.assertEqual(self.instrument.amplitude.call_args_list,
                             [mock.call(1), mock.call(2)])
            self.instrument.sample_rate.assert_called_once_with(1)

            # the other references are increased in amend and upload segment method
            np.testing.assert_equal(channel_pair._segment_references,
                                    np.array([1, 2, 3, 0, 1]))

            self.assertEqual(len(channel_pair._known_programs), 1)
            np.testing.assert_equal(
                channel_pair._known_programs['test'].waveform_to_segment,
                np.array([5, 3, 1, 2, 6], dtype=np.int64))