Skip to content

Commit 7e7d91c

Browse files
authored
Set coverage standard (#3)
* Small code cleanup * Add codecov.yml * Increase report scope * Extra test and minor improvements * Add ASSERT to tests
1 parent 2c21d98 commit 7e7d91c

5 files changed

Lines changed: 114 additions & 12 deletions

File tree

‎codecov.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
target: 96%
6+
threshold: 2%

‎libs/market/book.hpp‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
#pragma once
77

8+
#include "common/utils.hpp"
89
#include "market.hpp"
9-
#include <common/utils.hpp>
1010

1111
#include <utility>
1212
#include <cstddef>
13+
#include <cstdint>
1314
#include <stdexcept>
1415
#include <algorithm>
1516

@@ -189,30 +190,30 @@ namespace market {
189190
for (; i < size_i; ++i) {
190191
freel[i] = i;
191192
}
192-
tail_i = --i;
193+
tail_i = size_i - 1;
193194
side_i[0] = side_i[1] = 0;
194-
ASSERT(side_i[0] + side_i[1] + (size_type)(tail_i + 1) == size_i);
195195
};
196196

197197
void accept() {
198198
ASSERT(freel != nullptr);
199-
size_type i = 0;
200-
for (; i < size_i; ++i) {
199+
for (size_type i = 0; i < size_i; ++i) {
201200
freel[i] = i;
202201
}
203202
// Mark elements in the free list, which are not actually free, with npos
204-
for (i = 0; i < side_i[0] || i < side_i[1]; ++i) {
203+
for (size_type i = 0; i < side_i[0] || i < side_i[1]; ++i) {
205204
if (i < side_i[0]) {
206205
freel[sides[i]] = npos;
207206
}
208207
if (i < side_i[1]) {
209208
freel[sides[capacity + i]] = npos;
210209
}
211210
}
211+
// Note: tail_i will underflow to npos if no space left, by design
212+
tail_i = size_i - (size_type)1 - side_i[0] - side_i[1];
212213
// Shift freel elements marked as taken (i.e. with npos value) to the end of freel
214+
// Note: npos will not be preserved, it has no special meaning outside of accept
213215
auto* const begin = &freel[0];
214216
std::remove_if(begin, begin + size_i, [](size_type n){ return n == npos; } );
215-
ASSERT(side_i[0] + side_i[1] + (size_type)(tail_i + 1) == size_i);
216217
};
217218

218219
public:
@@ -271,7 +272,6 @@ namespace market {
271272

272273
template <side Side>
273274
void sort() {
274-
ASSERT(freel != nullptr);
275275
auto* const begin = &sides[(size_t)Side * capacity];
276276
std::sort(begin, begin + side_i[(size_t)Side], [this](size_type lh, size_type rh){
277277
return book::compare<Side>(levels[lh], levels[rh]);
@@ -297,7 +297,6 @@ namespace market {
297297

298298
template <side Side>
299299
level& at(size_type i) {
300-
ASSERT(freel != nullptr);
301300
ASSERT(i < side_i[(size_t)Side]);
302301
const auto l = sides[(size_t)Side * capacity + i];
303302
return levels[l];

‎tests/book.cpp‎

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
// Distributed under the MIT License. See accompanying file LICENSE
44
// or copy at https://opensource.org/licenses/MIT
55

6-
#include <market/book.hpp>
6+
struct assert_error {};
7+
#define ASSERT(...) do { if((__VA_ARGS__) == 0) throw assert_error{}; } while(0)
8+
9+
#include "market/book.hpp"
710

811
#include <catch2/catch.hpp>
912

@@ -427,6 +430,25 @@ namespace {
427430
: book(levels, sides, freel, i, 0, 0, std::forward<Args>(t)...) {
428431
this->accept();
429432
}
433+
434+
template <market::side Side, size_t Size>
435+
size_type populate(Level const (&input)[Size], size_type offset) {
436+
size_type i = 0;
437+
for (auto const &l : input) {
438+
sides[(size_t)Side * capacity + i] = offset + i;
439+
levels[offset + i++] = l; // Note: must post-increment
440+
}
441+
side_i[(size_t)Side] = i;
442+
return i;
443+
}
444+
445+
using market::book<Level>::reset;
446+
using market::book<Level>::accept;
447+
448+
size_type *&base_freel() { return market::book<Level>::freel; }
449+
size_type &base_tail() { return market::book<Level>::tail_i; }
450+
size_type &base_size_bid() { return market::book<Level>::side_i[0]; }
451+
size_type &base_size_ask() { return market::book<Level>::side_i[1]; }
430452
};
431453
}
432454

@@ -549,6 +571,81 @@ TEST_CASE("AnySizeBook_construction", "[book][capacity][construction][bad_capaci
549571
REQUIRE(ptr->emplace_back<side::bid>() == npos);
550572
REQUIRE(ptr->emplace_back<side::ask>() == npos);
551573
}
574+
575+
SECTION("call accept() on populated book") {
576+
Level bids[2] = {Level{130130, 100}, Level{130140, 10}};
577+
Level offers[3] = {Level{130190, 50}, Level{130180, 40}, Level{130170, 20}};
578+
579+
CHECK_NOTHROW(ptr.reset(new AnySizeBook(10, std::nothrow)));
580+
REQUIRE(ptr->capacity == 10);
581+
auto const offset = ptr->populate<side::bid>(bids, 0);
582+
ptr->accept();
583+
for (std::size_t i = 0; i < 20; ++i) {
584+
CHECK((i >= 18 ? true : ptr->freel[i] == i + 2));
585+
}
586+
CHECK(ptr->size<side::bid>() == 2);
587+
CHECK(ptr->size<side::ask>() == 0);
588+
ptr->populate<side::ask>(offers, offset);
589+
ptr->accept();
590+
for (std::size_t i = 0; i < 20; ++i) {
591+
CHECK((i >= 15 ? true : ptr->freel[i] == i + 5));
592+
}
593+
CHECK(ptr->size<side::bid>() == 2);
594+
CHECK(ptr->size<side::ask>() == 3);
595+
}
596+
597+
SECTION("ASSERT check") {
598+
CHECK_NOTHROW(ptr.reset(new AnySizeBook(10, std::nothrow)));
599+
SECTION("null freel") {
600+
ptr->base_freel() = nullptr;
601+
CHECK_THROWS_AS(ptr->reset(), assert_error);
602+
CHECK_THROWS_AS(ptr->accept(), assert_error);
603+
CHECK_THROWS_AS(ptr->push_back<side::bid>(Level{1, 1}), assert_error);
604+
CHECK_THROWS_AS(ptr->push_back<side::ask>(Level{1, 1}), assert_error);
605+
CHECK_THROWS_AS(ptr->emplace_back<side::bid>(1, 1), assert_error);
606+
CHECK_THROWS_AS(ptr->emplace_back<side::ask>(1, 1), assert_error);
607+
CHECK_THROWS_AS(ptr->remove<side::bid>(0), assert_error);
608+
CHECK_THROWS_AS(ptr->remove<side::ask>(0), assert_error);
609+
}
610+
SECTION("bad bid size") {
611+
ptr->base_size_bid() = 1;
612+
CHECK_THROWS_AS(ptr->push_back<side::bid>(Level{1, 1}), assert_error);
613+
CHECK_THROWS_AS(ptr->push_back<side::ask>(Level{1, 1}), assert_error);
614+
CHECK_THROWS_AS(ptr->emplace_back<side::bid>(1, 1), assert_error);
615+
CHECK_THROWS_AS(ptr->emplace_back<side::ask>(1, 1), assert_error);
616+
CHECK_THROWS_AS(ptr->remove<side::bid>(0), assert_error);
617+
CHECK_THROWS_AS(ptr->remove<side::ask>(0), assert_error);
618+
}
619+
620+
SECTION("bad ask size") {
621+
ptr->base_size_ask() = 1;
622+
CHECK_THROWS_AS(ptr->push_back<side::bid>(Level{1, 1}), assert_error);
623+
CHECK_THROWS_AS(ptr->push_back<side::ask>(Level{1, 1}), assert_error);
624+
CHECK_THROWS_AS(ptr->emplace_back<side::bid>(1, 1), assert_error);
625+
CHECK_THROWS_AS(ptr->emplace_back<side::ask>(1, 1), assert_error);
626+
CHECK_THROWS_AS(ptr->remove<side::bid>(0), assert_error);
627+
CHECK_THROWS_AS(ptr->remove<side::ask>(0), assert_error);
628+
}
629+
630+
SECTION("bad tail") {
631+
ptr->base_tail() = 1;
632+
CHECK_THROWS_AS(ptr->push_back<side::bid>(Level{1, 1}), assert_error);
633+
CHECK_THROWS_AS(ptr->push_back<side::ask>(Level{1, 1}), assert_error);
634+
CHECK_THROWS_AS(ptr->emplace_back<side::bid>(1, 1), assert_error);
635+
CHECK_THROWS_AS(ptr->emplace_back<side::ask>(1, 1), assert_error);
636+
CHECK_THROWS_AS(ptr->remove<side::bid>(0), assert_error);
637+
CHECK_THROWS_AS(ptr->remove<side::ask>(0), assert_error);
638+
}
639+
640+
SECTION("bad index") {
641+
CHECK_THROWS_AS(std::as_const(*ptr).at<side::bid>(0), assert_error);
642+
CHECK_THROWS_AS(std::as_const(*ptr).at<side::ask>(0), assert_error);
643+
CHECK_THROWS_AS(ptr->at<side::bid>(0), assert_error);
644+
CHECK_THROWS_AS(ptr->at<side::ask>(0), assert_error);
645+
CHECK_THROWS_AS(ptr->remove<side::bid>(0), assert_error);
646+
CHECK_THROWS_AS(ptr->remove<side::ask>(0), assert_error);
647+
}
648+
}
552649
}
553650

554651
TEST_CASE("AnySizeBook_binary_search", "[book][binary_search]") {

‎tests/market.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Distributed under the MIT License. See accompanying file LICENSE
44
// or copy at https://opensource.org/licenses/MIT
55

6-
#include <market/market.hpp>
6+
#include "market/market.hpp"
77

88
#include <catch2/catch.hpp>
99

‎tests/utils.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Distributed under the MIT License. See accompanying file LICENSE
44
// or copy at https://opensource.org/licenses/MIT
55

6-
#include <common/utils.hpp>
6+
#include "common/utils.hpp"
77

88
#include <catch2/catch.hpp>
99

0 commit comments

Comments
 (0)