-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathtest_assert_assume.cpp
96 lines (77 loc) · 3 KB
/
test_assert_assume.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Copyright 2019-RT-RK Computer Based System
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <bm/bm_sim/actions.h>
#include <bm/bm_sim/core/primitives.h>
#include <bm/bm_sim/logger.h>
#include <bm/bm_sim/phv.h>
#include <bm/bm_sim/packet.h>
using namespace bm;
class AssertAssumeTest : public ::testing::TestWithParam<const char *> {
protected:
PHVFactory phv_factory;
ActionFn testActionFn;
ActionFnEntry testActionFnEntry;
std::unique_ptr<PHVSourceIface> phv_source{nullptr};
std::unique_ptr<Packet> pkt{nullptr};
AssertAssumeTest()
: testActionFn("test_primitive", 0, 1),
testActionFnEntry(&testActionFn),
phv_source(PHVSourceIface::make_phv_source()) { }
void SetUp() override {
phv_source->set_phv_factory(0, &phv_factory);
pkt = std::unique_ptr<Packet>(new Packet(
Packet::make_new(phv_source.get())));
}
ArithExpression *build_expression(bool value) {
ArithExpression* condition = new ArithExpression();
condition->push_back_load_bool(value);
condition->push_back_op(ExprOpcode::BOOL_TO_DATA);
condition->build();
return condition;
}
};
TEST_P(AssertAssumeTest, ConditionTrue) {
auto primitive = ActionOpcodesMap::get_instance()->get_primitive(GetParam());
ASSERT_NE(nullptr, primitive);
testActionFn.push_back_primitive(primitive.get());
auto expr = build_expression(true);
std::unique_ptr<ArithExpression> condition(expr);
testActionFn.parameter_push_back_expression(std::move(condition));
testActionFnEntry(pkt.get());
}
using AssertAssumeDeathTest = AssertAssumeTest;
extern bool WITH_VALGRIND; // defined in main.cpp
TEST_P(AssertAssumeDeathTest, ConditionFalse) {
// TODO(antonin): use GTEST_SKIP once we update the version of googletest used
// by bmv2.
if (WITH_VALGRIND) {
SUCCEED();
return;
}
auto primitive = ActionOpcodesMap::get_instance()->get_primitive(GetParam());
ASSERT_NE(nullptr, primitive);
testActionFn.push_back_primitive(primitive.get());
auto expr = build_expression(false);
std::unique_ptr<ArithExpression> condition(expr);
testActionFn.parameter_push_back_expression(std::move(condition));
EXPECT_DEATH(testActionFnEntry(pkt.get()), "");
}
INSTANTIATE_TEST_SUITE_P(AssertAssumeTest,
AssertAssumeTest,
::testing::Values("assert", "assume"));
INSTANTIATE_TEST_SUITE_P(AssertAssumeDeathTest,
AssertAssumeDeathTest,
::testing::Values("assert", "assume"));