Skip to content

Commit

Permalink
use cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
Raekye committed Jul 16, 2019
1 parent 9a704d8 commit 4456091
Show file tree
Hide file tree
Showing 24 changed files with 188 additions and 67 deletions.
6 changes: 6 additions & 0 deletions midori/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required (VERSION 3.11)

enable_testing()

add_subdirectory(src)
add_subdirectory(tests)
42 changes: 0 additions & 42 deletions midori/Makefile

This file was deleted.

10 changes: 10 additions & 0 deletions midori/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
project(hmmm VERSION 0.0.1 DESCRIPTION "hmmm")

add_subdirectory(midori)

set(CMAKE_CXX_STANDARD 11)

add_executable(hmmm
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
target_link_libraries(hmmm midori)
12 changes: 6 additions & 6 deletions midori/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <sstream>
#include <fstream>
#include "helper.h"
#include "regex.h"
#include "lexer.h"
#include "parser.h"
#include "generator.h"
#include "interval_tree.h"
#include "midori/helper.h"
#include "midori/regex.h"
#include "midori/lexer.h"
#include "midori/parser.h"
#include "midori/generator.h"
#include "midori/interval_tree.h"

int test_parser() {
std::unique_ptr<Parser> p = RegexParserGenerator::make();
Expand Down
13 changes: 13 additions & 0 deletions midori/src/midori/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
project(midori VERSION 0.0.1 DESCRIPTION "hmmm")

set(CMAKE_CXX_STANDARD 11)

set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/finite_automata.cpp
${CMAKE_CURRENT_SOURCE_DIR}/generator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/lexer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/parser.cpp
${CMAKE_CURRENT_SOURCE_DIR}/regex.cpp
)

add_library(midori SHARED ${SOURCES})
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ template <typename K, typename V> class IntervalTree {

void insert(Interval, V);
std::unique_ptr<SearchList> pop(Interval);
std::unique_ptr<SearchList> find(Interval);
std::unique_ptr<SearchList> all();

void invariants();
Expand All @@ -38,6 +39,7 @@ template <typename K, typename V> class IntervalTree {
static std::unique_ptr<Node> insert(std::unique_ptr<Node>, Interval, V);
static std::unique_ptr<Node> pop_min(std::unique_ptr<Node>, std::unique_ptr<Node>*);
static std::unique_ptr<Node> pop(std::unique_ptr<Node>, Interval, std::unique_ptr<Node>*);
static void find(Node*, Interval, SearchList*);
static void all(Node*, SearchList*);

Int balance_factor();
Expand All @@ -47,7 +49,8 @@ template <typename K, typename V> class IntervalTree {
bool overlaps(Interval);
bool overlaps_recursive(Interval);

static void invariants(Node*);
static void _invariants(Node*);
void invariants();
};

std::unique_ptr<Node> root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ template <typename K, typename V> std::unique_ptr<typename IntervalTree<K, V>::S
return results;
}

template <typename K, typename V> std::unique_ptr<typename IntervalTree<K, V>::SearchList> IntervalTree<K, V>::find(Interval key) {
std::unique_ptr<SearchList> results(new SearchList);
Node::find(this->root.get(), key, results.get());
return results;
}

template <typename K, typename V> std::unique_ptr<typename IntervalTree<K, V>::SearchList> IntervalTree<K, V>::all() {
std::unique_ptr<SearchList> results(new SearchList);
if (this->root != nullptr) {
Node::all(this->root.get(), results.get());
}
Node::all(this->root.get(), results.get());
return results;
}

template <typename K, typename V> void IntervalTree<K, V>::invariants() {
Node::invariants(this->root.get());
Node::_invariants(this->root.get());
}

#pragma mark - IntervalTree::Node
Expand Down Expand Up @@ -159,6 +163,20 @@ template <typename K, typename V> std::unique_ptr<typename IntervalTree<K, V>::N
return self;
}

template <typename K, typename V> void IntervalTree<K, V>::Node::find(Node* self, Interval key, SearchList* results) {
if (self == nullptr) {
return;
}
if (!self->overlaps_recursive(key)) {
return;
}
Node::find(self->left.get(), key, results);
if (self->overlaps(key)) {
results->push_back(SearchResult(self->key, self->value));
}
Node::find(self->right.get(), key, results);
}

template <typename K, typename V> void IntervalTree<K, V>::Node::all(Node* self, SearchList* results) {
if (self == nullptr) {
return;
Expand Down Expand Up @@ -210,28 +228,32 @@ template <typename K, typename V> bool IntervalTree<K, V>::Node::overlaps_recurs

#pragma mark - IntervalTree::Node - debug

template <typename K, typename V> void IntervalTree<K, V>::Node::invariants(Node* self) {
template <typename K, typename V> void IntervalTree<K, V>::Node::_invariants(Node* self) {
if (self == nullptr) {
return;
}
Node::invariants(self->left.get());
Node::invariants(self->right.get());
Int x = self->balance_factor();
Node::_invariants(self->left.get());
Node::_invariants(self->right.get());
self->invariants();
}

template <typename K, typename V> void IntervalTree<K, V>::Node::invariants() {
Int x = this->balance_factor();
assert(x * x <= 1);
assert(self->height == std::max(Node::_height(self->left.get()), Node::_height(self->right.get())) + 1);
if (self->left == nullptr) {
assert(self->lower == self->key.first);
if (self->right == nullptr) {
assert(self->upper == self->key.second);
assert(this->height == std::max(Node::_height(this->left.get()), Node::_height(this->right.get())) + 1);
if (this->left == nullptr) {
assert(this->lower == this->key.first);
if (this->right == nullptr) {
assert(this->upper == this->key.second);
} else {
assert(self->upper == std::max(self->key.second, self->right->upper));
assert(this->upper == std::max(this->key.second, this->right->upper));
}
} else {
assert(self->lower == self->left->lower);
if (self->right == nullptr) {
assert(self->upper == std::max(self->key.second, self->left->upper));
assert(this->lower == this->left->lower);
if (this->right == nullptr) {
assert(this->upper == std::max(this->key.second, this->left->upper));
} else {
assert(self->upper == std::max({ self->key.second, self->left->upper, self->right->upper }));
assert(this->upper == std::max({ this->key.second, this->left->upper, this->right->upper }));
}
}
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
45 changes: 45 additions & 0 deletions midori/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(
${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL
)

# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()


# Now simply link against gtest or gtest_main as needed. Eg
add_executable(hmmmtests driver.cpp)
target_link_libraries(hmmmtests gtest_main)

include_directories(AFTER "${CMAKE_SOURCE_DIR}/src")
target_link_libraries(hmmmtests midori)

add_test(NAME hmmmtests COMMAND hmmmtests)
15 changes: 15 additions & 0 deletions midori/tests/CMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 2.8.2)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
49 changes: 49 additions & 0 deletions midori/tests/driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "gtest/gtest.h"
#include "midori/interval_tree.h"

class IntervalTreeTest : public ::testing::Test {
public:
typedef IntervalTree<Int, Int> IT;
};

TEST_F(IntervalTreeTest, IsBalanced) {
IT a;
for (Int i = 0; i < 1000; i++) {
a.insert(IT::Interval(i, i + 16), i);
}
for (Int i = 0; i < 1000; i++) {
Int j = 1000 - i;
a.insert(IT::Interval(j, j + 16), j);
}
a.invariants();
}

TEST_F(IntervalTreeTest, Find) {
IT a;
for (Int i = 0; i < 1000; i++) {
a.insert(IT::Interval(i, i + 2), i);
}
ASSERT_EQ(a.find(IT::Interval(700, 716))->size(), 19);
ASSERT_EQ(a.all()->size(), 1000);
}

TEST_F(IntervalTreeTest, Pop) {
IT a;
for (Int i = 0; i < 1000; i++) {
a.insert(IT::Interval(i, i + 16), i);
}
ASSERT_EQ(a.pop(IT::Interval(700, 702))->size(), 19);
ASSERT_EQ(a.all()->size(), 1000 - 19);

IT b;
for (Int i = 0; i < 1000; i++) {
b.insert(IT::Interval(i, i), i);
}
ASSERT_EQ(b.pop(IT::Interval(700, 716))->size(), 17);
ASSERT_EQ(b.all()->size(), 1000 - 17);
}

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 4456091

Please sign in to comment.