Skip to content

Commit dc30321

Browse files
authored
[orc-rt] Add unit test utility: MakeAllocAction. (#162229)
MakeAllocAction can be used to construct AllocActions with less boilerplate than the previous option (spsSerialize + AllocAction constructor call). This will be used to simplify upcoming unit tests that use AllocActions. The existing AllocActionsTest is updated to use the new utility.
1 parent e05d80e commit dc30321

File tree

2 files changed

+63
-21
lines changed

2 files changed

+63
-21
lines changed

orc-rt/unittests/AllocActionTest.cpp

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "orc-rt/ExecutorAddress.h"
1515
#include "orc-rt/SPSAllocAction.h"
1616

17-
#include "SimplePackedSerializationTestUtils.h"
17+
#include "AllocActionTestUtils.h"
1818
#include "gtest/gtest.h"
1919

2020
using namespace orc_rt;
@@ -77,11 +77,14 @@ TEST(AllocActionTest, RunFinalizationActionsComplete) {
7777

7878
std::vector<AllocActionPair> InitialActions;
7979

80-
auto MakeArgBuffer = [&]() { return makeExecutorAddrBuffer(&Val); };
81-
InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()},
82-
{decrement_sps_allocaction, MakeArgBuffer()}});
83-
InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()},
84-
{decrement_sps_allocaction, MakeArgBuffer()}});
80+
auto MakeAAOnVal = [&](AllocActionFn Fn) {
81+
return *MakeAllocAction<SPSExecutorAddr>::from(Fn,
82+
ExecutorAddr::fromPtr(&Val));
83+
};
84+
InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
85+
MakeAAOnVal(decrement_sps_allocaction)});
86+
InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
87+
MakeAAOnVal(decrement_sps_allocaction)});
8588

8689
auto DeallocActions = cantFail(runFinalizeActions(std::move(InitialActions)));
8790

@@ -102,11 +105,14 @@ TEST(AllocActionTest, RunFinalizeActionsFail) {
102105

103106
std::vector<AllocActionPair> InitialActions;
104107

105-
auto MakeArgBuffer = [&]() { return makeExecutorAddrBuffer(&Val); };
106-
InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()},
107-
{decrement_sps_allocaction, MakeArgBuffer()}});
108-
InitialActions.push_back({{fail_sps_allocaction, MakeArgBuffer()},
109-
{decrement_sps_allocaction, MakeArgBuffer()}});
108+
auto MakeAAOnVal = [&](AllocActionFn Fn) {
109+
return *MakeAllocAction<SPSExecutorAddr>::from(Fn,
110+
ExecutorAddr::fromPtr(&Val));
111+
};
112+
InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
113+
MakeAAOnVal(decrement_sps_allocaction)});
114+
InitialActions.push_back({*MakeAllocAction<>::from(fail_sps_allocaction),
115+
MakeAAOnVal(decrement_sps_allocaction)});
110116

111117
auto DeallocActions = runFinalizeActions(std::move(InitialActions));
112118

@@ -126,11 +132,14 @@ TEST(AllocActionTest, RunFinalizeActionsNullFinalize) {
126132

127133
std::vector<AllocActionPair> InitialActions;
128134

129-
auto MakeArgBuffer = [&]() { return makeExecutorAddrBuffer(&Val); };
130-
InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()},
131-
{decrement_sps_allocaction, MakeArgBuffer()}});
132-
InitialActions.push_back({{nullptr, WrapperFunctionBuffer()},
133-
{decrement_sps_allocaction, MakeArgBuffer()}});
135+
auto MakeAAOnVal = [&](AllocActionFn Fn) {
136+
return *MakeAllocAction<SPSExecutorAddr>::from(Fn,
137+
ExecutorAddr::fromPtr(&Val));
138+
};
139+
InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
140+
MakeAAOnVal(decrement_sps_allocaction)});
141+
InitialActions.push_back({*MakeAllocAction<>::from(nullptr),
142+
MakeAAOnVal(decrement_sps_allocaction)});
134143

135144
auto DeallocActions = cantFail(runFinalizeActions(std::move(InitialActions)));
136145

@@ -148,11 +157,14 @@ TEST(AllocActionTest, RunFinalizeActionsNullDealloc) {
148157

149158
std::vector<AllocActionPair> InitialActions;
150159

151-
auto MakeArgBuffer = [&]() { return makeExecutorAddrBuffer(&Val); };
152-
InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()},
153-
{decrement_sps_allocaction, MakeArgBuffer()}});
154-
InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()},
155-
{nullptr, WrapperFunctionBuffer()}});
160+
auto MakeAAOnVal = [&](AllocActionFn Fn) {
161+
return *MakeAllocAction<SPSExecutorAddr>::from(Fn,
162+
ExecutorAddr::fromPtr(&Val));
163+
};
164+
InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
165+
MakeAAOnVal(decrement_sps_allocaction)});
166+
InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
167+
*MakeAllocAction<>::from(nullptr)});
156168

157169
auto DeallocActions = cantFail(runFinalizeActions(std::move(InitialActions)));
158170

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===- AllocActionTestUtils.h ---------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef ORC_RT_UNITTEST_ALLOCACTIONTESTUTILS_H
10+
#define ORC_RT_UNITTEST_ALLOCACTIONTESTUTILS_H
11+
12+
#include "SimplePackedSerializationTestUtils.h"
13+
#include "orc-rt/AllocAction.h"
14+
15+
#include <optional>
16+
17+
template <typename... SPSArgTs> struct MakeAllocAction {
18+
template <typename... ArgTs>
19+
static std::optional<orc_rt::AllocAction> from(orc_rt::AllocActionFn Fn,
20+
ArgTs &&...Args) {
21+
using SPS = orc_rt::SPSArgList<SPSArgTs...>;
22+
auto B = orc_rt::WrapperFunctionBuffer::allocate(SPS::size(Args...));
23+
orc_rt::SPSOutputBuffer OB(B.data(), B.size());
24+
if (!SPS::serialize(OB, Args...))
25+
return std::nullopt;
26+
return orc_rt::AllocAction(Fn, std::move(B));
27+
}
28+
};
29+
30+
#endif // ORC_RT_UNITTEST_ALLOCACTIONTESTUTILS_H

0 commit comments

Comments
 (0)