xref: /aosp_15_r20/external/pigweed/pw_i2c/initiator_mock.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2020 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 #include "pw_i2c/initiator_mock.h"
15 
16 #include "pw_assert/check.h"
17 #include "pw_containers/algorithm.h"
18 #include "pw_unit_test/framework.h"
19 
20 namespace pw::i2c {
21 
DoWriteReadFor(Address device_address,ConstByteSpan tx_buffer,ByteSpan rx_buffer,chrono::SystemClock::duration timeout)22 Status MockInitiator::DoWriteReadFor(Address device_address,
23                                      ConstByteSpan tx_buffer,
24                                      ByteSpan rx_buffer,
25                                      chrono::SystemClock::duration timeout) {
26   PW_CHECK_INT_LT(expected_transaction_index_, expected_transactions_.size());
27 
28   EXPECT_EQ(
29       expected_transactions_[expected_transaction_index_].address().GetTenBit(),
30       device_address.GetTenBit());
31 
32   auto expected_timeout =
33       expected_transactions_[expected_transaction_index_].timeout();
34   if (expected_timeout.has_value()) {
35     EXPECT_EQ(expected_timeout.value(), timeout);
36   }
37 
38   ConstByteSpan expected_tx_buffer =
39       expected_transactions_[expected_transaction_index_].write_buffer();
40   EXPECT_TRUE(pw::containers::Equal(expected_tx_buffer, tx_buffer));
41 
42   ConstByteSpan expected_rx_buffer =
43       expected_transactions_[expected_transaction_index_].read_buffer();
44   PW_CHECK_INT_EQ(expected_rx_buffer.size(), rx_buffer.size());
45 
46   std::copy(
47       expected_rx_buffer.begin(), expected_rx_buffer.end(), rx_buffer.begin());
48 
49   // Do not directly return this value as expected_transaction_index_ should be
50   // incremented.
51   const Status expected_return_value =
52       expected_transactions_[expected_transaction_index_].return_value();
53 
54   expected_transaction_index_ += 1;
55 
56   return expected_return_value;
57 }
58 
~MockInitiator()59 MockInitiator::~MockInitiator() { EXPECT_EQ(Finalize(), OkStatus()); }
60 
61 }  // namespace pw::i2c
62