Skip to content

Commit

Permalink
some testing
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Aug 27, 2024
1 parent aa24682 commit 5a6e1a9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
2 changes: 0 additions & 2 deletions include/flatmemory/details/types/bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,6 @@ void Builder<Bitset<Block, Tag>>::resize_to_fit(const Other& other)
{
m_blocks.resize(other.get_blocks().size(), m_default_bit_value ? BitsetUtils::block_ones<Block> : BitsetUtils::block_zeroes<Block>);
}

assert(m_blocks.size() > 0);
}

template<IsUnsignedIntegral Block, typename Tag>
Expand Down
19 changes: 9 additions & 10 deletions include/flatmemory/details/types/tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,7 @@ size_t Builder<Tuple<Ts...>>::finish_iterative_impl(std::index_sequence<Is...>,
if constexpr (is_trivial)
{
/* Write the data inline. */
auto& value = std::get<Is>(m_data);
out.write(pos + Layout<Tuple<Ts...>>::data_positions[Is], value);
out.write(pos + Layout<Tuple<Ts...>>::data_positions[Is], std::get<Is>(m_data));
}
else
{
Expand Down Expand Up @@ -459,14 +458,6 @@ auto View<Tuple<Ts...>>::get()
}
}

template<IsTriviallyCopyableOrNonTrivialType... Ts>
template<std::size_t I>
void View<Tuple<Ts...>>::mutate(View<Tuple<Ts...>>::element_type<I> value)
requires(IsTriviallyCopyable<View<Tuple<Ts...>>::element_type<I>>)
{
return write_value<View<Tuple<Ts...>>::element_type<I>>(m_buf + Layout<Tuple<Ts...>>::data_positions[I], value);
}

template<IsTriviallyCopyableOrNonTrivialType... Ts>
template<std::size_t I>
auto View<Tuple<Ts...>>::get() const
Expand All @@ -485,6 +476,14 @@ auto View<Tuple<Ts...>>::get() const
}
}

template<IsTriviallyCopyableOrNonTrivialType... Ts>
template<std::size_t I>
void View<Tuple<Ts...>>::mutate(View<Tuple<Ts...>>::element_type<I> value)
requires(IsTriviallyCopyable<View<Tuple<Ts...>>::element_type<I>>)
{
return write_value<View<Tuple<Ts...>>::element_type<I>>(m_buf + Layout<Tuple<Ts...>>::data_positions[I], value);
}

template<IsTriviallyCopyableOrNonTrivialType... Ts>
uint8_t* View<Tuple<Ts...>>::buffer()
{
Expand Down
68 changes: 60 additions & 8 deletions tests/unit/types/tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,19 +290,71 @@ struct StructTest
uint64_t y;
};

static_assert(sizeof(StructTest) == 16);

TEST(FlatmemoryTests, TypesTupleStructTest)
{
EXPECT_EQ((IsTriviallyCopyable<View<Tuple<StructTest>>>), true);
EXPECT_EQ((IsTriviallyCopyable<Tuple<StructTest>>), false);
EXPECT_EQ((IsTriviallyCopyable<View<Tuple<Vector<uint32_t>, StructTest>>>), true);
EXPECT_EQ((IsTriviallyCopyable<Tuple<Vector<uint32_t>, StructTest>>), false);

auto builder = Builder<Tuple<StructTest>>();
builder.get<0>().x = 5;
builder.get<0>().y = 10;
auto builder = Builder<Tuple<Vector<uint32_t>, StructTest>>();
builder.get<0>().resize(1);
builder.get<1>().x = 5;
builder.get<1>().y = 10;
builder.finish();
EXPECT_EQ(builder.buffer().size(), 36);

auto view = View<Tuple<Vector<uint32_t>, StructTest>>(builder.buffer().data());
EXPECT_EQ(view.get<0>().size(), 1);
EXPECT_EQ(view.get<0>().at(0), 0);
EXPECT_EQ(view.get<1>().x, 5);
EXPECT_EQ(view.get<1>().y, 10);
EXPECT_EQ(view.buffer_size(), 36);
}

/*
struct FlatSimpleEffect
{
bool is_negated;
size_t atom_id;
}; // __attribute__((packed)); // Pack the struct to avoid padding
static_assert(sizeof(FlatSimpleEffect) == 16);
TEST(FlatmemoryTests, TypesTupleConditionalEffectTest)
{
uint8_t buffer[16] = { 00, 00, 00, 00, 00, 00, 00, 00, 04, 00, 00, 00, 00, 00, 00, 00 };
auto effect = read_value<FlatSimpleEffect>(buffer);
EXPECT_FALSE(effect.is_negated);
EXPECT_EQ(effect.atom_id, 4);
using FlatIndexListLayout = flatmemory::Vector<uint32_t>;
using FlatConditionalEffectLayout = flatmemory::Tuple<FlatIndexListLayout, // Positive static atom indices
FlatIndexListLayout, // Negative static atom indices
FlatIndexListLayout, // Positive fluent atom indices
FlatIndexListLayout, // Negative fluent atom indices
FlatIndexListLayout, // Positive derived atom indices
FlatIndexListLayout, // Negative derived atom indices
FlatSimpleEffect>; // simple add or delete effect
Layout<FlatConditionalEffectLayout>().print();
auto builder = Builder<FlatConditionalEffectLayout>();
builder.get<0>().push_back(4);
builder.get<0>().push_back(10);
builder.get<3>().push_back(1);
builder.get<6>().is_negated = false;
builder.get<6>().atom_id = 4;
builder.finish();
auto view = View<FlatConditionalEffectLayout>(builder.buffer().data());
print(builder.buffer().data(), builder.buffer().size());
auto view = View<Tuple<StructTest>>(builder.buffer().data());
EXPECT_EQ(view.get<0>().x, 5);
EXPECT_EQ(view.get<0>().y, 10);
EXPECT_FALSE(view.get<6>().is_negated);
EXPECT_EQ(view.get<6>().atom_id, 4);
}
*/

}

0 comments on commit 5a6e1a9

Please sign in to comment.