Skip to content

Commit 5b92002

Browse files
author
thk123
committed
Adding utilities for checking types in unit tests
1 parent 429c13f commit 5b92002

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

unit/testing-utils/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ SRC = \
22
c_to_expr.cpp \
33
load_java_class.cpp \
44
require_expr.cpp \
5+
require_type.cpp \
56
# Empty last line (please keep above list sorted!)
67

78
INCLUDES = -I .. -I . -I ../../src

unit/testing-utils/require_type.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test utilities
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
9+
#include "require_type.h"
10+
11+
#include <testing-utils/catch.hpp>
12+
#include <util/base_type.h>
13+
14+
/// Checks a type is a pointer type optionally with a specific subtype
15+
/// \param type: The type to check
16+
/// \param subtype: An optional subtype. If provided, checks the subtype of the
17+
/// pointer is this.
18+
/// \return A cast to pointer_typet version of type
19+
pointer_typet require_type::require_pointer(
20+
const typet &type,
21+
const optionalt<typet> &subtype)
22+
{
23+
REQUIRE(type.id() == ID_pointer);
24+
const pointer_typet &pointer = to_pointer_type(type);
25+
26+
if(subtype)
27+
{
28+
// TODO: use base_type_eq
29+
REQUIRE(pointer.subtype() == subtype.value());
30+
}
31+
return pointer;
32+
}
33+
34+
/// Checks a struct like type has a component with a specific name
35+
/// \param struct_type: The structure that should have the component
36+
/// \param component_name: The name of the component
37+
/// \return The component with the specified name
38+
struct_union_typet::componentt require_type::require_component(
39+
const struct_typet &struct_type,
40+
const irep_idt &component_name)
41+
{
42+
const auto &componet = std::find_if(
43+
struct_type.components().begin(),
44+
struct_type.components().end(),
45+
[&component_name](const struct_union_typet::componentt &component) {
46+
return component.get_name() == component_name;
47+
});
48+
49+
REQUIRE(componet != struct_type.components().end());
50+
return *componet;
51+
}

unit/testing-utils/require_type.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test utilities
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Helper functions for requiring specific types
11+
/// If the type is of the wrong type, throw a CATCH exception
12+
/// Also checks associated properties and returns a casted version of the
13+
/// expression.
14+
15+
#ifndef CPROVER_TESTING_UTILS_REQUIRE_TYPE_H
16+
#define CPROVER_TESTING_UTILS_REQUIRE_TYPE_H
17+
18+
#include <util/optional.h>
19+
#include <util/std_types.h>
20+
21+
// NOLINTNEXTLINE(readability/namespace)
22+
namespace require_type
23+
{
24+
pointer_typet
25+
require_pointer(const typet &type, const optionalt<typet> &subtype);
26+
27+
struct_typet::componentt require_component(
28+
const struct_typet &struct_type,
29+
const irep_idt &component_name);
30+
}
31+
32+
#endif

0 commit comments

Comments
 (0)