mirror of
https://github.com/genodelabs/genode.git
synced 2024-12-20 22:23:16 +00:00
test-spark: add object-size assertions
This commit is contained in:
parent
2a71c8fa82
commit
30505c7977
@ -2,6 +2,11 @@ pragma Ada_2012;
|
||||
|
||||
package body Machinery is
|
||||
|
||||
function Number_Of_Bits (Machinery : Machinery_Type) return Number_Of_Bits_Type is
|
||||
begin
|
||||
return Machinery'Size;
|
||||
end Number_Of_Bits;
|
||||
|
||||
procedure Initialize (Machinery : out Machinery_Type) is
|
||||
begin
|
||||
Machinery := ( Temperature => 25 );
|
||||
|
@ -2,10 +2,17 @@ package Machinery is
|
||||
|
||||
pragma Pure;
|
||||
|
||||
type Number_Of_Bits_Type is mod 2**32 with Size => 32;
|
||||
|
||||
type Temperature_Type is mod 2**32 with Size => 32;
|
||||
|
||||
type Machinery_Type is private;
|
||||
|
||||
function Number_of_bits (Machinery : Machinery_Type) return Number_Of_Bits_Type
|
||||
with Export,
|
||||
Convention => C,
|
||||
External_Name => "_ZN5Spark14number_of_bitsERKNS_9MachineryE";
|
||||
|
||||
procedure Initialize (Machinery : out Machinery_Type)
|
||||
with Export,
|
||||
Convention => C,
|
||||
|
@ -20,6 +20,13 @@ namespace Spark {
|
||||
template <Genode::size_t BYTES>
|
||||
struct Object
|
||||
{
|
||||
/**
|
||||
* Exception type
|
||||
*/
|
||||
struct Object_size_mismatch { };
|
||||
|
||||
static constexpr Genode::size_t bytes() { return BYTES; }
|
||||
|
||||
long _space[(BYTES + sizeof(long) - 1)/sizeof(long)] { };
|
||||
};
|
||||
|
||||
@ -31,6 +38,15 @@ namespace Spark {
|
||||
|
||||
Genode::uint32_t temperature() const;
|
||||
};
|
||||
|
||||
Genode::size_t number_of_bits(Machinery const &);
|
||||
|
||||
template <typename T>
|
||||
static inline void assert_valid_object_size()
|
||||
{
|
||||
if (number_of_bits(*(T *)nullptr) > T::bytes()*8)
|
||||
throw typename T::Object_size_mismatch();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -38,6 +54,8 @@ static inline void test_spark_object_construction()
|
||||
{
|
||||
using namespace Genode;
|
||||
|
||||
Spark::assert_valid_object_size<Spark::Machinery>();
|
||||
|
||||
Spark::Machinery machinery { };
|
||||
|
||||
auto check = [&] (char const *msg, Genode::uint32_t expected)
|
||||
|
Loading…
Reference in New Issue
Block a user