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

Iterator #1

Merged
merged 21 commits into from
Mar 20, 2016
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build/

# Compiled Object files
*.slo
*.lo
Expand Down
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: cpp
compiler:
- gcc-4.8
- clang
install:
- sudo add-apt-repository --yes ppa:ubuntu-toolchain-r/test
- sudo apt-get -qq update
- sudo apt-get -qq install g++-4.8
- if [ "$CXX" = "g++" ]; then
export CXX="g++-4.8";
fi
- $CXX --version
script:
- ./test.sh Iterator
14 changes: 14 additions & 0 deletions Iterator/src/Aggregate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef ITERATOR_SRC_AGGREGATE_H_
#define ITERATOR_SRC_AGGREGATE_H_

#include <memory>
#include "Iterator.h"

template <class T>
class Aggregate {
public:
virtual ~Aggregate() = default;
virtual std::unique_ptr<Iterator<T>> iterator() = 0;
};

#endif // ITERATOR_SRC_AGGREGATE_H_
15 changes: 15 additions & 0 deletions Iterator/src/Book.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef ITERATOR_SRC_BOOK_H_
#define ITERATOR_SRC_BOOK_H_

#include <string>

class Book {
public:
explicit Book(const std::string &name = "") : name(name) {}
std::string getName() const { return name; }

private:
std::string name;
};

#endif // ITERATOR_SRC_BOOK_H_
6 changes: 6 additions & 0 deletions Iterator/src/BookShelf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "BookShelfIterator.h"

std::unique_ptr<Iterator<Book>> BookShelf::iterator() {
return std::unique_ptr<BookShelfIterator>(new BookShelfIterator(*this));
}

27 changes: 27 additions & 0 deletions Iterator/src/BookShelf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef ITERATOR_SRC_BOOKSHELF_H_
#define ITERATOR_SRC_BOOKSHELF_H_

#include <vector>

#include "Aggregate.h"
#include "Book.h"

class BookShelf : public Aggregate<Book> {
public:
explicit BookShelf(int maxsize) : last(0) { books.resize(maxsize); }
~BookShelf() = default;

Book getBookAt(int index) const { return books[index]; }

void appendBook(const Book &book) { books[last++] = book; }

int getLength() { return last; }

std::unique_ptr<Iterator<Book>> iterator();

private:
std::vector<Book> books;
int last;
};

#endif // ITERATOR_SRC_BOOKSHELF_H_
24 changes: 24 additions & 0 deletions Iterator/src/BookShelfIterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef ITERATOR_SRC_BOOKSHELFITERATOR_H_
#define ITERATOR_SRC_BOOKSHELFITERATOR_H_

#include "Iterator.h"
#include "Book.h"
#include "Aggregate.h"
#include "BookShelf.h"

class BookShelfIterator : public Iterator<Book> {
public:
explicit BookShelfIterator(BookShelf bookShelf)
: bookShelf(bookShelf), index(0) {}
~BookShelfIterator() = default;

bool hasNext() { return index < bookShelf.getLength(); }

Book next() { return bookShelf.getBookAt(index++); }

private:
BookShelf bookShelf;
int index;
};

#endif // ITERATOR_SRC_BOOKSHELFITERATOR_H_
4 changes: 4 additions & 0 deletions Iterator/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 2.8)
add_library(Iterator STATIC
BookShelf.cpp
)
12 changes: 12 additions & 0 deletions Iterator/src/Iterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef ITERATOR_SRC_ITERATOR_H_
#define ITERATOR_SRC_ITERATOR_H_

template <class T>
class Iterator {
public:
virtual bool hasNext() = 0;
virtual T next() = 0;
virtual ~Iterator() = default;
};

#endif // ITERATOR_SRC_ITERATOR_H_
37 changes: 37 additions & 0 deletions Iterator/test/BookShelfIteratorTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "gtest/gtest.h"

#include "../src/BookShelfIterator.h"

class BookShelfIteratorTest : public ::testing::Test {
protected:
virtual void SetUp() {
bookShelf.appendBook(Book("Around the World in 80 Days"));
bookShelf.appendBook(Book("Bible"));
bookShelf.appendBook(Book("Cinderella"));
bookShelf.appendBook(Book("Daddy-Long-Legs"));
}
BookShelf bookShelf{4};
};

TEST_F(BookShelfIteratorTest, iterator) {
Book book;
auto it = bookShelf.iterator();

EXPECT_TRUE(it->hasNext());
book = it->next();
EXPECT_EQ(book.getName(), "Around the World in 80 Days");

EXPECT_TRUE(it->hasNext());
book = it->next();
EXPECT_EQ(book.getName(), "Bible");

EXPECT_TRUE(it->hasNext());
book = it->next();
EXPECT_EQ(book.getName(), "Cinderella");

EXPECT_TRUE(it->hasNext());
book = it->next();
EXPECT_EQ(book.getName(), "Daddy-Long-Legs");

EXPECT_FALSE(it->hasNext());
}
19 changes: 19 additions & 0 deletions Iterator/test/BookShelfTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "gtest/gtest.h"

#include "../src/BookShelf.h"

class BookShelfTest : public ::testing::Test {
protected:
virtual void SetUp() {}
BookShelf bookShelf{4};
};

TEST_F(BookShelfTest, append) {
bookShelf.appendBook(Book("Around the World in 80 Days"));
bookShelf.appendBook(Book("Bible"));
bookShelf.appendBook(Book("Cinderella"));
bookShelf.appendBook(Book("Daddy-Long-Legs"));

EXPECT_EQ(4, bookShelf.getLength());
}

9 changes: 9 additions & 0 deletions Iterator/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 2.8)
add_definitions("-Wall -std=c++11")
set(CMAKE_EXE_LINKER_FLAGS "-pthread")
include_directories(../../tools)

add_subdirectory(../src src)
add_subdirectory(../../tools/gtest gtest)
add_executable(IteratorTest BookShelfTest.cpp BookShelfIteratorTest.cpp)
target_link_libraries(IteratorTest Iterator gtest)
5 changes: 5 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

mkdir $1/build && cd $1/build
cmake ../test && make && ./"$1Test"

Loading