Commit 68721ae8 authored by Christophe Rhodes's avatar Christophe Rhodes
Browse files

updates to 12

parent e85956fc
#+TITLE: Algorithms & Data Structures: Lab 12
#+SUBTITLE: week of 21st January 2019
#+include: ../labsheet.org
* Setup
** Saving your work from last term
By now you should be familiar with the operations needed to save
your work. Make sure you commit your work to version control
often, and always have a backup copy, ideally remotely (for example
in your own account on the department's gitlab installation.)
** Downloading this week's distribution
Once you have successfully saved your changes from last week, you
can get my updates by doing
#+begin_example
git pull
#+end_example
which /should/ automatically merge in new content. After the =git
pull= command, you should have a new directory containing this
week's material (named =12/=) alongside your existing directories.
* Binary search
Implement binary search on sorted arrays, as described in the
lectures. You are provided with skeleton files and test files, as
usual; running =make test= in the =cpp/= or =java/= directory should
provide you with a test failure report, and once you have
successfully implemented binary search, you should be able to rerun
the tests with success.
Integrate an ~OpCounter~ to the class, to count comparisons between
the search key and elements in the array that your code makes.
Expose that functionality with the signature appropriate for your
programming language:
- Java :: ~int count(int array[], int key, int lo, int hi)~
- C++ :: ~int count(int *array, int key, size_t lo, size_t hi)~
Are there (possibly invalid) combinations of arguments to your
implementation of your =search= function which would lead to
erroneous results or infinite loops? Can you write a test case to
expose the bad behaviour? Send a pull request to the lab bundle
with your new test.
** Submission
Submit your work to the [[https://learn.gold.ac.uk/mod/lti/view.php?id=627101][submission area for binary search]]. The
submission system will remain open until *16:00* on *Friday 25th
January*; as usual, you may submit more than once, and your highest
score is retained.
#include "BinarySearch.hpp"
bool BinarySearch::search(int *array, int key, size_t lo, size_t hi) {
return true;
}
#include <cstddef>
class BinarySearch {
public:
bool search(int *, int, size_t, size_t);
};
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include "BinarySearch.hpp"
class BinarySearchSearchTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(BinarySearchSearchTest);
CPPUNIT_TEST(testZeroLengthSearch);
CPPUNIT_TEST(testSmallSearch);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() { };
void tearDown() { };
void testZeroLengthSearch();
void testSmallSearch();
};
void BinarySearchSearchTest::testZeroLengthSearch() {
int x[0];
BinarySearch b;
CPPUNIT_ASSERT_MESSAGE("nothing is in an empty array", !b.search(x, 0, 0, 0));
CPPUNIT_ASSERT_MESSAGE("nothing is in an empty array", !b.search(x, 1, 0, 0));
CPPUNIT_ASSERT_MESSAGE("nothing is in an empty array", !b.search(x, 2, 0, 0));
int y[4] = {0,2,3,4};
CPPUNIT_ASSERT_MESSAGE("nothing is in an empty search", !b.search(y, 0, 0, 0));
CPPUNIT_ASSERT_MESSAGE("nothing is in an empty search", !b.search(y, 1, 1, 1));
CPPUNIT_ASSERT_MESSAGE("nothing is in an empty search", !b.search(y, 2, 2, 2));
CPPUNIT_ASSERT_MESSAGE("nothing is in an empty search", !b.search(y, 4, 3, 3));
}
void BinarySearchSearchTest::testSmallSearch() {
int x[4] = {0,2,4,6};
BinarySearch b;
CPPUNIT_ASSERT_MESSAGE("-2 is not in {0,2,4,6}", !b.search(x, -2, 0, 4));
CPPUNIT_ASSERT_MESSAGE("-1 is not in {0,2,4,6}", !b.search(x, -1, 0, 4));
CPPUNIT_ASSERT_MESSAGE("0 is in {0,2,4,6}", b.search(x, 0, 0, 4));
CPPUNIT_ASSERT_MESSAGE("1 is not in {0,2,4,6}", !b.search(x, 1, 0, 4));
CPPUNIT_ASSERT_MESSAGE("2 is in {0,2,4,6}", b.search(x, 2, 0, 4));
CPPUNIT_ASSERT_MESSAGE("3 is not in {0,2,4,6}", !b.search(x, 3, 0, 4));
CPPUNIT_ASSERT_MESSAGE("4 is in {0,2,4,6}", b.search(x, 4, 0, 4));
CPPUNIT_ASSERT_MESSAGE("5 is not in {0,2,4,6}", !b.search(x, 5, 0, 4));
CPPUNIT_ASSERT_MESSAGE("6 is in {0,2,4,6}", b.search(x, 6, 0, 4));
CPPUNIT_ASSERT_MESSAGE("7 is not in {0,2,4,6}", !b.search(x, 7, 0, 4));
CPPUNIT_ASSERT_MESSAGE("8 is not in {0,2,4,6}", !b.search(x, 8, 0, 4));
CPPUNIT_ASSERT_MESSAGE("4 is not in the first half of {0,2,4,6}", !b.search(x, 4, 0, 2));
CPPUNIT_ASSERT_MESSAGE("2 is not in the second half of {0,2,4,6}", !b.search(x, 2, 2, 4));
}
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(BinarySearchSearchTest, "Search");
CPPUNIT_REGISTRY_ADD_TO_DEFAULT("Search");
include ../../cpp.mk
BinarySearchTest: BinarySearch.o BinarySearchSearchTest.o ../../00/cpp/RunTests.o
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $^ $(LOADLIBES) $(LDLIBS) -o $@
test: BinarySearchTest
./BinarySearchTest
.PHONY: test
public class BinarySearch {
public boolean search(int array[], int key, int lo, int hi) {
return true;
}
}
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
public class BinarySearchSearchTest {
@Test
public void testZeroLengthSearch() {
int[] x = {};
BinarySearch b = new BinarySearch();
assertTrue("nothing is in an empty array", !b.search(x, 0, 0, 0));
assertTrue("nothing is in an empty array", !b.search(x, 1, 0, 0));
assertTrue("nothing is in an empty array", !b.search(x, 2, 0, 0));
int[] y = {0,2,3,4};
assertTrue("nothing is in an empty search", !b.search(y, 0, 0, 0));
assertTrue("nothing is in an empty search", !b.search(y, 1, 1, 1));
assertTrue("nothing is in an empty search", !b.search(y, 2, 2, 2));
assertTrue("nothing is in an empty search", !b.search(y, 4, 3, 3));
};
@Test
public void testSmallSearch() {
int[] x = {0,2,4,6};
BinarySearch b = new BinarySearch();
assertTrue("-2 is not in {0,2,4,6}", !b.search(x, -2, 0, 4));
assertTrue("-1 is not in {0,2,4,6}", !b.search(x, -1, 0, 4));
assertTrue("0 is in {0,2,4,6}", b.search(x, 0, 0, 4));
assertTrue("1 is not in {0,2,4,6}", !b.search(x, 1, 0, 4));
assertTrue("2 is in {0,2,4,6}", b.search(x, 2, 0, 4));
assertTrue("3 is not in {0,2,4,6}", !b.search(x, 3, 0, 4));
assertTrue("4 is in {0,2,4,6}", b.search(x, 4, 0, 4));
assertTrue("5 is not in {0,2,4,6}", !b.search(x, 5, 0, 4));
assertTrue("6 is in {0,2,4,6}", b.search(x, 6, 0, 4));
assertTrue("7 is not in {0,2,4,6}", !b.search(x, 7, 0, 4));
assertTrue("8 is not in {0,2,4,6}", !b.search(x, 8, 0, 4));
assertTrue("4 is not in the first half of {0,2,4,6}", !b.search(x, 4, 0, 2));
assertTrue("2 is not in the second half of {0,2,4,6}", !b.search(x, 2, 2, 4));
};
public static void main(String args[]) {
JUnitCore core = new JUnitCore();
core.run(BinarySearchSearchTest.class);
}
}
include ../../java.mk
TESTCLASSFILES = BinarySearchSearchTest.class
CLASSFILES = BinarySearch.class $(TESTCLASSFILES)
CLASSPATHS += ../../00/java
all: $(CLASSFILES)
clean:
-rm -f $(CLASSFILES) test.xml
test: all
(((($(JAVA) -Xss10m $(CP) $(CLASSPATH) org.junit.runner.JUnitCore $(subst .class,,$(notdir $(TESTCLASSFILES))); echo $$? >&3) | egrep -v \(org.junit\|sun.reflect\|java.lang.reflect\) >&4) 3>&1) | (read xs; exit $$xs)) 4>&1
.PHONY: test all clean
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment