From 8ea78af90b3fb7c7e468c8354ec0edcb2c0890f3 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 21 Dec 2017 14:02:49 -0800 Subject: [PATCH] Update build and basic DS test framework. --- autobuild | 2 ++ data/Makefile.am | 2 +- notes/chapter1.txt | 2 ++ src/Makefile.am | 2 +- src/ch01ex06.cc | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/autobuild b/autobuild index 1f94136..d82c3d3 100644 --- a/autobuild +++ b/autobuild @@ -21,6 +21,8 @@ then mkdir -p $BUILDDIR && cd $BUILDDIR autoreconf -i $SRCDIR bash $SRCDIR/configure $CONFOPTS + [ -d "$BUILDDIR/data" ] || mkdir "$BUILDDIR/data" + cp $SRCDIR/data/corpus* "$BUILDDIR/data" else autoreconf -i ./configure $CONFOPTS diff --git a/data/Makefile.am b/data/Makefile.am index fe964cc..dbd6f14 100644 --- a/data/Makefile.am +++ b/data/Makefile.am @@ -8,5 +8,5 @@ ods-cpp/main.cpp: ods-cpp.tgz ODS_SAMPLE_CODE_URL := http://opendatastructures.org/ods-cpp.tgz ods-cpp.tgz: - if command -v curl 2>&1 > /dev/null ; then curl -L -O $(ODS_SAMPLE_CODE_URL) \ + if command -v curl 2>&1 > /dev/null ; then curl -L -O $(ODS_SAMPLE_CODE_URL) ; \ elif command -v curl 2>&1 > /dev/null ; then wget $(ODS_SAMPLE_CODE_URL) ; fi diff --git a/notes/chapter1.txt b/notes/chapter1.txt index de987a5..e041813 100644 --- a/notes/chapter1.txt +++ b/notes/chapter1.txt @@ -148,6 +148,8 @@ Run times come in three flavours: interfaces. These do not have to be efficient. They can be used later to test the correctness and performance of more efficient implementations. + In progress: src/ch01ex06.cc, src/ods/simplist.h, src/ods/uset.h. + 7. Work to improve the performance of your implementations from the previous question using any tricks you can think of. Experiment and think about how you could improve the performance of add(i,x) and diff --git a/src/Makefile.am b/src/Makefile.am index b9b101b..9778194 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,6 @@ AM_CPPFLAGS = -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align AM_CPPFLAGS += -Wwrite-strings -Wmissing-declarations -Wno-long-long -Werror -AM_CPPFLAGS += -Wunused-variable -std=c++14 -D_XOPEN_SOURCE -O0 -g -I. +AM_CPPFLAGS += -Wunused-variable -std=c++17 -D_XOPEN_SOURCE -O0 -g -I. AM_CPPFLAGS += -fno-elide-constructors -Weffc++ bin_PROGRAMS := ch01ex01 ch01ex03 ch01ex04 ch01ex05 ch01ex06 diff --git a/src/ch01ex06.cc b/src/ch01ex06.cc index 7b23901..d186043 100644 --- a/src/ch01ex06.cc +++ b/src/ch01ex06.cc @@ -1,5 +1,7 @@ #include +#include #include +#include using namespace std; @@ -12,6 +14,54 @@ check_simplist(void) sl.add(1, 2); sl.add(2, 3); assert(sl.size() == 3); + + sl.add(0, 4); + sl.add(1, 5); + sl.add(2, 6); + assert(sl.size() == 6); + + int expected[6] = {4, 5, 6, 1, 2, 3}; + for (size_t i = 0; i < 6; i++) { + assert(sl.get(i) == expected[i]); + } + + bool caught = false; + try { + sl.add(8, 8); + } catch (std::invalid_argument) { + caught = true; + } + assert(caught); + + assert(sl.get(2) == 6); + sl.remove(2); + assert(sl.get(2) == 1); + assert(sl.size() == 5); + sl.set(2, 6); + assert(sl.get(2) == 6); + + // expected = {1, 2, 3, 4, 5, 6}; + for (size_t i = 0; i < 6; i++) { + expected[i] = i+1; + } + + for (size_t i = 0; i < 5; i++) { + sl.set(i, i+1); + } + sl.add(5, 6); + + for (size_t i = 0; i < 6; i++) { + assert(sl.get(i) == expected[i]); + } +} + + +static void +check_simpuset(void) +{ + ods::SimpUSet us; + + assert(us.add(1)); } @@ -19,4 +69,6 @@ int main(void) { check_simplist(); + check_simpuset(); + cout << "OK" << endl; }