setup project for assignment 3

This commit is contained in:
2021-01-08 16:01:54 +01:00
parent 51b54f68fb
commit 2f9f32e50e
13 changed files with 268 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.16)
project(assignment_3)
find_package( Threads )
set(CMAKE_CXX_STANDARD 20)
include_directories(${PROJECT_SOURCE_DIR}) # Allows absolute paths: #include <util/bar/bar.h>
enable_testing()
add_subdirectory(lib)
add_subdirectory(test) # enable_testing() used there
add_executable(assignment_3 main.cpp)
target_link_libraries(assignment_3 sets)
target_link_libraries(assignment_3 ${CMAKE_THREAD_LIBS_INIT} )

1
Assignment_3/clean.sh Executable file
View File

@@ -0,0 +1 @@
rm -R build

View File

@@ -0,0 +1 @@
add_library(sets STATIC Node.h Node.cpp Set.h Set.cpp )

43
Assignment_3/lib/Node.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include <mutex>
#include <lib/Node.h>
Node::Node(int element){
data = element;
dummy = false;
deleted = false;
}
Node* Node::Dummy(){
Node* n = new Node(0);
n->dummy = true;
return n;
}
bool Node::is_smaller_than(Node* n) {
return this->is_smaller_than(n->data);
}
bool Node::is_smaller_than(int n) {
// Everything is smaller than a dummy -> false
return this->data < n && !this->dummy;
}
bool Node::is_equal(int n) {
// Dummies are not equal to any number -> false
return this->data == n && !this->dummy;
}
void Node::lock() {
this->mut.lock();
}
void Node::unlock() {
this->mut.unlock();
}
bool Node::is_dummy() {
return dummy;
}
int Node::get_data(){
return data;
}

26
Assignment_3/lib/Node.h Normal file
View File

@@ -0,0 +1,26 @@
#pragma once
#include <mutex>
class Node {
protected:
int data;
bool dummy;
std::mutex mut;
public:
Node* next;
bool deleted;
Node(int element);
static Node* Dummy();
// Dummy();
void lock();
void unlock();
bool is_smaller_than(int n);
bool is_smaller_than(Node* n);
bool is_equal(int n);
bool is_dummy();
int get_data();
};

26
Assignment_3/lib/Set.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "Set.h"
#include "Node.h"
#include <iostream>
#include<sstream>
Set::Set(std::string _name){
name = _name;
first = Node::Dummy();
first->next = Node::Dummy();
}
bool Set::add(int element) { return false; }
bool Set::rmv(int element) { return false; }
bool Set::ctn(int element) { return false; }
std::string Set::get_name() { return name; }
std::string Set::print_set(){
Node* c = this->first->next;
std::stringstream ss;
while (!c->is_dummy()) {
ss << c->get_data() << " ";
c = c->next;
}
return ss.str();
}

16
Assignment_3/lib/Set.h Normal file
View File

@@ -0,0 +1,16 @@
#pragma once
#include "Node.h"
class Set {
protected:
std::string name;
Set(std::string _name);
Node* first;
public:
bool add(int element);
bool rmv(int element);
bool ctn(int element);
std::string get_name();
std::string print_set();
};

86
Assignment_3/main.cpp Normal file
View File

@@ -0,0 +1,86 @@
#include <chrono>
#include <iostream>
#include <stdlib.h>
#include <lib/Set.cpp>
using namespace std::chrono;
#define DEBUG false
#if DEBUG
#define DEBUG_MSG(str) do { std::cout << str << std::endl; } while( false )
#else
#define DEBUG_MSG(str) do { } while ( false )
#endif
#define OP_COUNT 100000
enum methodname {add, rmv, ctn, noop};
typedef struct _operation{
methodname method;
int input;
} operation;
void do_operation(operation* op, Set* set) {
switch (op->method) {
case methodname::ctn:
set->ctn(op->input);
break;
case methodname::add:
set->add(op->input);
break;
case methodname::rmv:
set->rmv(op->input);
break;
default:
break;
}
};
//void generate_operations_uniform(std::vector<operation> *operations, int minVal, int maxVal) {
// int range_size = maxVal - minVal;
// for (auto op = operations->begin(); op != operations->end(); op++) {
// op->input = (rand() % range_size) + minVal;
// switch (rand() % 3) {
// case 0:
// op->method = methodname::add;
// break;
// case 1:
// op->method = methodname::rmv;
// break;
// default:
// op->method = methodname::ctn;
// break;
// }
// }
//}
//void generate_operations(std::vector<operation> *operations, int minVal, int maxVal, int i) {
// int range_size = maxVal - minVal;
// for (auto op = operations->begin(); op != operations->end(); op++) {
// op->input = (rand() % range_size) + minVal;
// if (rand() % 100 < i) {
// op->method = methodname::ctn;
// } else if (rand() % 10 < 9) {
// op->method = methodname::add;
// } else {
// op->method = methodname::rmv;
// }
// }
// DEBUG_MSG("Generated operations with i = "<< i);
//}
//void run_worker(std::vector<operation>* operations, Set* set) {
// DEBUG_MSG("Run worker");
// long op_counter = 0;
// for (operation op : operations) {
/* code */
// do_operation(&op, set);
// }
//}
int main(){
return 0;
}

4
Assignment_3/run.sh Executable file
View File

@@ -0,0 +1,4 @@
mkdir -p build && cd build
cmake ..
cmake --build .
./assignment_3

2
Assignment_3/run_no_compile.sh Executable file
View File

@@ -0,0 +1,2 @@
cd build
./assignment_3

4
Assignment_3/test.sh Executable file
View File

@@ -0,0 +1,4 @@
mkdir -p build && cd build
cmake ..
cmake --build .
ctest .

View File

@@ -0,0 +1,12 @@
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.10.0
)
FetchContent_MakeAvailable(googletest)
add_executable(node_unit_test Node_test.cpp)
target_link_libraries(node_unit_test PUBLIC gtest gtest_main)
target_link_libraries(node_unit_test PUBLIC sets)
add_test(NodeTest node_unit_test)

View File

@@ -0,0 +1,32 @@
#include <gtest/gtest.h>
#include <lib/Node.h>
TEST(NodeTest, DummyNode_smaller_int) {
Node * n = Node::Dummy();
ASSERT_FALSE(n->is_smaller_than(std::numeric_limits<int>::max()));
ASSERT_FALSE(n->is_smaller_than(std::numeric_limits<int>::min()));
}
TEST(NodeTest, DummyNode_smaller_node) {
Node * n = Node::Dummy();
Node * max = new Node(std::numeric_limits<int>::max());
Node * min = new Node(std::numeric_limits<int>::min());
ASSERT_FALSE(n->is_smaller_than(max));
ASSERT_FALSE(n->is_smaller_than(min));
}
TEST(NodeTest, LockingUnlockingNode) {
Node * n = new Node(1);
n->lock();
n->unlock();
}
TEST(NodeTest, ChainingNode) {
Node* first = Node::Dummy();
Node* last = Node::Dummy();
first->next = last;
ASSERT_TRUE(first->next == last);
}