xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/FillFixture.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2019 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_TEST_FILL_FIXTURE
25 #define ARM_COMPUTE_TEST_FILL_FIXTURE
26 
27 #include "arm_compute/core/TensorShape.h"
28 #include "arm_compute/core/Types.h"
29 #include "tests/AssetsLibrary.h"
30 #include "tests/Globals.h"
31 #include "tests/IAccessor.h"
32 #include "tests/framework/Asserts.h"
33 #include "tests/framework/Fixture.h"
34 
35 namespace arm_compute
36 {
37 namespace test
38 {
39 namespace validation
40 {
41 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
42 class FillFixture : public framework::Fixture
43 {
44 public:
45     template <typename...>
setup(TensorShape input_shape,DataType data_type)46     void setup(TensorShape input_shape, DataType data_type)
47     {
48         _target = compute_target(input_shape, data_type);
49         _reference = compute_reference(input_shape, data_type);
50     }
51 
52 protected:
compute_target(const TensorShape & input_shape,DataType data_type)53     TensorType compute_target(const TensorShape &input_shape, DataType data_type)
54     {
55         // Create tensors
56         TensorType input = create_tensor<TensorType>(input_shape, data_type);
57 
58         // Allocate tensors
59         input.allocator()->allocate();
60 
61         // Fill tensor with an initial value
62         library->fill_tensor_uniform(AccessorType(input), 0);
63 
64         // Create and configure function
65         FunctionType fill;
66         const T constant_value {1};
67         fill.configure(&input, constant_value);
68 
69         // Compute function with a distinct, second value
70         fill.run();
71 
72         return input;
73     }
74 
compute_reference(const TensorShape & input_shape,DataType data_type)75     SimpleTensor<T> compute_reference(const TensorShape &input_shape, DataType data_type)
76     {
77         // Create reference
78         SimpleTensor<T> input{ input_shape, data_type };
79 
80         // Fill tensor
81         const T constant_value {1};
82         for(int i = 0; i < input.num_elements(); ++i)
83         {
84             input[i] = constant_value;
85         }
86 
87         return input;
88     }
89 
90     TensorType      _target{};
91     SimpleTensor<T> _reference{};
92 };
93 } // namespace validation
94 } // namespace test
95 } // namespace arm_compute
96 #endif /* ARM_COMPUTE_TEST_FILL_FIXTURE */
97