xref: /aosp_15_r20/external/federated-compute/fcp/aggregation/core/tensor_data_test.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "fcp/aggregation/core/tensor_data.h"
18 
19 #include <cstdint>
20 #include <memory>
21 
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "fcp/testing/testing.h"
25 
26 namespace fcp {
27 namespace aggregation {
28 namespace {
29 
30 using testing::Return;
31 
32 class MockTensorData : public TensorData {
33  public:
34   MockTensorData(size_t data_pointer_offset, size_t size);
35 
36   MOCK_METHOD(const void*, data, (), (const override));
37   MOCK_METHOD(size_t, byte_size, (), (const override));
38 };
39 
MockTensorData(size_t data_pointer_offset,size_t size)40 MockTensorData::MockTensorData(size_t data_pointer_offset, size_t size) {
41   EXPECT_CALL(*this, byte_size()).WillRepeatedly(Return(size));
42   EXPECT_CALL(*this, data())
43       .WillRepeatedly(Return(reinterpret_cast<void*>(data_pointer_offset)));
44 }
45 
TEST(TensorDataTest,CheckValid_ZeroByteSize)46 TEST(TensorDataTest, CheckValid_ZeroByteSize) {
47   MockTensorData tensor_data(0, 0);
48   EXPECT_THAT(tensor_data.CheckValid(1), IsCode(FAILED_PRECONDITION));
49 }
50 
TEST(TensorDataTest,CheckValid_ByteSizeNotAligned)51 TEST(TensorDataTest, CheckValid_ByteSizeNotAligned) {
52   MockTensorData tensor_data(0, 33);
53   EXPECT_THAT(tensor_data.CheckValid(4), IsCode(FAILED_PRECONDITION));
54 }
55 
TEST(TensorDataTest,CheckValid_AddressNotAligned)56 TEST(TensorDataTest, CheckValid_AddressNotAligned) {
57   MockTensorData tensor_data(3, 100);
58   EXPECT_THAT(tensor_data.CheckValid(4), IsCode(FAILED_PRECONDITION));
59 }
60 
TEST(TensorDataTest,CheckValid_Success)61 TEST(TensorDataTest, CheckValid_Success) {
62   MockTensorData tensor_data(0, 96);
63   EXPECT_THAT(tensor_data.CheckValid(1), IsOk());
64   EXPECT_THAT(tensor_data.CheckValid(2), IsOk());
65   EXPECT_THAT(tensor_data.CheckValid(4), IsOk());
66   EXPECT_THAT(tensor_data.CheckValid(8), IsOk());
67 }
68 
69 }  // namespace
70 }  // namespace aggregation
71 }  // namespace fcp
72