xref: /aosp_15_r20/external/pigweed/pw_spi/initiator_mock.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_spi/initiator_mock.h"
16 
17 #include "pw_assert/check.h"
18 #include "pw_containers/algorithm.h"
19 #include "pw_unit_test/framework.h"
20 
21 namespace pw::spi {
22 
DoWriteRead(ConstByteSpan tx_buffer,ByteSpan rx_buffer)23 Status MockInitiator::DoWriteRead(ConstByteSpan tx_buffer, ByteSpan rx_buffer) {
24   PW_CHECK_INT_LT(expected_transaction_index_, expected_transactions_.size());
25 
26   ConstByteSpan expected_tx_buffer =
27       expected_transactions_[expected_transaction_index_].write_buffer();
28   EXPECT_TRUE(pw::containers::Equal(expected_tx_buffer, tx_buffer));
29 
30   ConstByteSpan expected_rx_buffer =
31       expected_transactions_[expected_transaction_index_].read_buffer();
32   PW_CHECK_INT_EQ(expected_rx_buffer.size(), rx_buffer.size());
33 
34   std::copy(
35       expected_rx_buffer.begin(), expected_rx_buffer.end(), rx_buffer.begin());
36 
37   // Do not directly return this value as expected_transaction_index_ should be
38   // incremented.
39   const Status expected_return_value =
40       expected_transactions_[expected_transaction_index_].return_value();
41 
42   expected_transaction_index_ += 1;
43 
44   return expected_return_value;
45 }
46 
~MockInitiator()47 MockInitiator::~MockInitiator() { EXPECT_EQ(Finalize(), OkStatus()); }
48 
49 }  // namespace pw::spi
50