Skip to content

Fix static vector bug #144

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

Merged
merged 4 commits into from
Aug 27, 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
22 changes: 19 additions & 3 deletions .github/workflows/skyr-url-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ jobs:
chmod +x llvm.sh
sudo ./llvm.sh 10

- name: Install vcpkg (Linux/MacOS)
id: vcpkg_linux_macos
if: startsWith(matrix.config.os, 'ubuntu') || startsWith(matrix.config.os, 'macos')
- name: Install vcpkg (Linux)
id: vcpkg_linux
if: startsWith(matrix.config.os, 'ubuntu')
shell: bash
run: |
mkdir -p ${GITHUB_WORKSPACE}/vcpkg
Expand All @@ -285,6 +285,22 @@ jobs:
./bootstrap-vcpkg.sh
./vcpkg install tl-expected range-v3 catch2 nlohmann-json fmt

- name: Install vcpkg (MacOS)
id: vcpkg_macos
if: startsWith(matrix.config.os, 'macos')
shell: bash
run: |
brew install pkg-config
mkdir -p ${GITHUB_WORKSPACE}/vcpkg
cd ${GITHUB_WORKSPACE}/vcpkg
git init
git remote add origin https://github.com/microsoft/vcpkg.git
git fetch origin master
git checkout -b master origin/master
export
./bootstrap-vcpkg.sh
./vcpkg install tl-expected range-v3 catch2 nlohmann-json fmt

- name: Install vcpkg (Windows)
id: vcpkg_windows
if: startsWith(matrix.config.os, 'windows')
Expand Down
16 changes: 12 additions & 4 deletions include/skyr/v1/containers/static_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class static_vector {
/// Constructor
constexpr static_vector() = default;

~static_vector() {
clear();
}

/// Gets the first const element in the vector
/// \return a const T &
/// \pre `size() > 0`
Expand Down Expand Up @@ -83,7 +87,7 @@ class static_vector {
///
/// \param value
/// \return
/// \pre `size() < `capacity()`
/// \pre `size() < capacity()`
/// \post `size() > 0 && size() <= capacity()`
constexpr auto push_back(const_reference value) noexcept -> reference {
impl_[size_++] = value;
Expand All @@ -94,19 +98,20 @@ class static_vector {
/// \tparam Args
/// \param args
/// \return
/// \pre `size() < `capacity()`
/// \pre `size() < capacity()`
/// \post `size() > 0 && size() <= capacity()`
template <class... Args>
constexpr auto emplace_back(Args &&... args)
noexcept(std::is_trivially_move_assignable_v<T>) -> reference {
impl_[size_++] = value_type{args...};
impl_[size_++] = value_type{std::forward<Args>(args)...};
return impl_[size_ - 1];
}

///
/// \pre `size() > 0`
constexpr void pop_back() noexcept {
--size_;
impl_[size_].~T();
}

///
Expand Down Expand Up @@ -140,8 +145,11 @@ class static_vector {
}

///
/// \post size() == 0
constexpr void clear() noexcept {
size_ = 0;
while (size_) {
pop_back();
}
}

///
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function(skyr_create_test file_name output_dir test_name)
set(test_name ${test} PARENT_SCOPE)
endfunction()

add_subdirectory(containers)
add_subdirectory(unicode)
add_subdirectory(domain)
add_subdirectory(percent_encoding)
Expand Down
10 changes: 10 additions & 0 deletions tests/containers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) Glyn Matthews 2020.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

foreach (
file_name
static_vector_tests.cpp)
skyr_create_test(${file_name} ${PROJECT_BINARY_DIR}/tests/container test_name)
endforeach ()
48 changes: 48 additions & 0 deletions tests/containers/static_vector_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2020 Glyn Matthews.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <memory>
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <skyr/v1/containers/static_vector.hpp>


struct test_destructor_call {
bool *destructed = nullptr;

explicit test_destructor_call(bool *destructed)
: destructed(destructed) {
*destructed = false;
}

test_destructor_call(const test_destructor_call &) = delete;
test_destructor_call &operator=(const test_destructor_call &) = delete;
test_destructor_call(test_destructor_call &&) = delete;
test_destructor_call &operator=(test_destructor_call &&) = delete;

~test_destructor_call() {
*destructed = true;
}
};

TEST_CASE("pop back calls destructor", "[containers]") {
auto destructed = false;

auto vector = skyr::static_vector<std::shared_ptr<test_destructor_call>, 8>{};
vector.emplace_back(std::make_shared<test_destructor_call>(&destructed));
CHECK_FALSE(destructed);
vector.pop_back();
CHECK(destructed);
}

TEST_CASE("clear calls destructor", "[containers]") {
auto destructed = false;

auto vector = skyr::static_vector<std::shared_ptr<test_destructor_call>, 8>{};
vector.emplace_back(std::make_shared<test_destructor_call>(&destructed));
CHECK_FALSE(destructed);
vector.clear();
CHECK(destructed);
}