Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constexpr_vector class #415

Merged
merged 2 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/fizzy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_library(fizzy::fizzy ALIAS fizzy)
target_sources(
fizzy PRIVATE
bytes.hpp
constexpr_vector.hpp
execute.cpp
execute.hpp
instructions.cpp
Expand Down
35 changes: 35 additions & 0 deletions lib/fizzy/constexpr_vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Fizzy: A fast WebAssembly interpreter
// Copyright 2019-2020 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <utility>

namespace fizzy
{
/// Constexpr vector is used to represent compile time constant value lists.
/// It allows to have a collection (constexpr array) of such lists, where list lengths vary across
/// the collection, but each length is still known at compile time (and doesn't exceed Capacity
/// limit).
template <typename T, std::size_t Capacity>
class constexpr_vector
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
private:
const T m_array[Capacity];
const std::size_t m_size;

public:
template <typename... U>
constexpr constexpr_vector(U&&... u) noexcept
: m_array{std::forward<U>(u)...}, m_size(sizeof...(U))
{}

constexpr const T* data() const noexcept { return m_array; }
constexpr std::size_t size() const noexcept { return m_size; }

constexpr const T* begin() const noexcept { return m_array; }
constexpr const T* end() const noexcept { return m_array + m_size; }
};

} // namespace fizzy
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ target_link_libraries(fizzy-unittests PRIVATE fizzy::fizzy fizzy::test-utils GTe
target_sources(
fizzy-unittests PRIVATE
api_test.cpp
constexpr_vector_test.cpp
end_to_end_test.cpp
execute_call_test.cpp
execute_control_test.cpp
Expand Down
61 changes: 61 additions & 0 deletions test/unittests/constexpr_vector_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Fizzy: A fast WebAssembly interpreter
// Copyright 2019-2020 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0

#include "constexpr_vector.hpp"
#include "span.hpp"
#include <gmock/gmock.h>
#include <vector>

using namespace fizzy;
using namespace testing;

static_assert(constexpr_vector<int, 5>{1, 2, 3}.size() == 3);
static_assert(*constexpr_vector<int, 5>{1, 2, 3}.data() == 1);

TEST(constexpr_vector, size)
{
constexpr constexpr_vector<int, 5> v1 = {};
EXPECT_EQ(v1.size(), 0);

constexpr constexpr_vector<int, 5> v2 = {1, 2, 3};
EXPECT_EQ(v2.size(), 3);
EXPECT_EQ(*v2.data(), 1);

constexpr constexpr_vector<int, 5> v3 = {1, 2, 3, 4, 5};
EXPECT_EQ(v3.size(), 5);
EXPECT_EQ(*v3.data(), 1);
}

TEST(constexpr_vector, iterator)
{
constexpr constexpr_vector<int, 5> v = {1, 2, 3};
int expected = 1;
for (const auto i : v)
{
EXPECT_EQ(i, expected);
++expected;
}
EXPECT_EQ(expected, 4);
}

TEST(constexpr_vector, span)
{
constexpr constexpr_vector<int, 5> v = {1, 2, 3};
int expected = 1;
for (const auto i : span<const int>(v))
{
EXPECT_EQ(i, expected);
++expected;
}
EXPECT_EQ(expected, 4);
}

TEST(constexpr_vector, array)
{
constexpr constexpr_vector<int, 5> arr[] = {{1, 2, 3}, {4, 5, 6, 7, 8}, {9}, {}};
EXPECT_THAT(std::vector<int>(arr[0].begin(), arr[0].end()), ElementsAre(1, 2, 3));
EXPECT_THAT(std::vector<int>(arr[1].begin(), arr[1].end()), ElementsAre(4, 5, 6, 7, 8));
EXPECT_THAT(std::vector<int>(arr[2].begin(), arr[2].end()), ElementsAre(9));
EXPECT_THAT(std::vector<int>(arr[3].begin(), arr[3].end()), IsEmpty());
}