Debian Codegraph @

compute_kernel_writer/validation/tests/common/KernelWriterInterceptor.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2023 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
25 #ifndef CKW_VALIDATION_TESTS_COMMON_KERNELWRITERINTERCEPTOR_H
26 #define CKW_VALIDATION_TESTS_COMMON_KERNELWRITERINTERCEPTOR_H
27
28 #include <string>
29 #include <utility>
30
31 namespace ckw
32 {
33
34 /** This class provides the ability to capture only the code changed after a point
35 * and compares that part of code with the expected value.
36 *
37 * It is useful for testing purpose when a particular sequence of instructions is interested
38 * while the rest of the initialization code is out of scope.
39 */
40 template <typename T>
41 class KernelWriterInterceptor : public T
42 {
43 public:
44 template <typename... TArgs>
45 KernelWriterInterceptor(const TArgs &&...args)
46 : T(std::forward<TArgs>(args)...)
47 {
48 }
49
50 /** Mark this point in the source code as the start position to capture.
51 * Only source code added after this function is considered when check_add_code is called.
52 */
53 void start_capture_code()
54 {
55 _start_code = this->body_source_code();
56 }
57
58 /** Compare the source code added after start_capture_code is called the the specified expected code. */
59 bool check_added_code(const std::string &expected_added_code)
60 {
61 const auto &end_code = this->body_source_code();
62
63 // Code can only grow over time.
64 if(end_code.length() < _start_code.length())
65 {
66 return false;
67 }
68
69 // New code must be added to the source code without changing the already existed code.
70 if(end_code.substr(0, _start_code.length()) != _start_code)
71 {
72 return false;
73 }
74
75 // The newly added code must match the expected value.
76 if(end_code.substr(_start_code.length(), end_code.length() - _start_code.length()) != expected_added_code)
77 {
78 return false;
79 }
80
81 return true;
82 }
83
84 private:
85 std::string _start_code{};
86 };
87
88 } // namespace ckw
89
90 #endif // CKW_VALIDATION_TESTS_COMMON_KERNELWRITERINTERCEPTOR_H